com.chutneytesting.WebConfiguration Maven / Gradle / Ivy
The newest version!
/*
* SPDX-FileCopyrightText: 2017-2024 Enedis
*
* SPDX-License-Identifier: Apache-2.0
*
*/
package com.chutneytesting;
import com.chutneytesting.security.AuditHandler;
import com.chutneytesting.tools.MyMixInForIgnoreType;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.File;
import java.nio.file.Paths;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Bean
@Primary
public ObjectMapper webObjectMapper() {
return new ObjectMapper()
.addMixIn(Resource.class, MyMixInForIgnoreType.class) // TODO - Is this still useful ?
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
.findAndRegisterModules();
}
@Bean
WebServerFactoryCustomizer embeddedServletContainerCustomizer() {
// We need to explicitly change the assets path
// because even when in DEV asset are always generated by webpack in target/www
return this::setLocationForStaticAssets;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AuditHandler());
}
private void setLocationForStaticAssets(AbstractServletWebServerFactory container) {
File root = new File(resolvePathPrefix() + "ui/dist/chutney/"); // TODO use Path instead ?
if (root.exists() && root.isDirectory()) {
container.setDocumentRoot(root);
}
}
/**
* Resolve path prefix to static resources.
*/
private String resolvePathPrefix() {
String fullExecutablePath = this.getClass().getResource("").getPath();
String rootPath = Paths.get(".").toUri().normalize().getPath();
String extractedPath = fullExecutablePath.replace(rootPath, "");
int extractionEndIndex = extractedPath.indexOf("target/");
if (extractionEndIndex < 0) {
return "";
} else if (extractionEndIndex == 0) {
return "../";
}
return rootPath;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy