org.zodiac.autoconfigure.web.remote.WebRemoteApiServletAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.web.remote;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.zodiac.core.web.remote.RemoteApiResponseFilter;
import org.zodiac.core.web.remote.model.DefaultRemoteApiResultTransformer;
import org.zodiac.core.web.remote.model.RemoteApiResultTransformer;
import org.zodiac.core.web.remote.servlet.RemoteApiFilterInterceptor;
import org.zodiac.core.web.remote.servlet.RemoteApiHandlerMapping;
@SpringBootConfiguration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(value = com.fasterxml.jackson.databind.ObjectMapper.class)
public class WebRemoteApiServletAutoConfiguration implements WebMvcConfigurer, WebMvcRegistrations {
private com.fasterxml.jackson.databind.ObjectMapper objectMapper;
private RemoteApiFilterInterceptor remoteApiFilterInterceptor;
public WebRemoteApiServletAutoConfiguration(ObjectProvider objectMapperProvider,
ObjectProvider remoteApiFilterInterceptorProvider) {
this.objectMapper = objectMapperProvider.getIfAvailable();
this.remoteApiFilterInterceptor = remoteApiFilterInterceptorProvider.getIfAvailable();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
if (null != remoteApiFilterInterceptor) {
registry.addInterceptor(remoteApiFilterInterceptor).addPathPatterns("/**").excludePathPatterns("/*.html",
"/**/*.html",
"/**/*.ico", "/**/*.jpg", "/**/*.jpeg", "/**/*.png", "/**/*.gif", "/**/*.bmp", "/**/*.svg", "/**/*.css",
"/**/*.js", "/**/*.map", "/**/*.swf", "/**/*.mov", "/**/*.mp4", "/**/*.mp3", "/**/*.avi", "/**/*.jpg");
}
}
@Override
public RemoteApiHandlerMapping getRequestMappingHandlerMapping() {
return new RemoteApiHandlerMapping();
}
@Bean
@Order(value = Ordered.LOWEST_PRECEDENCE)
@ConditionalOnMissingBean(value = RemoteApiResultTransformer.class)
protected RemoteApiResultTransformer remoteApiResultTransformer() {
return new DefaultRemoteApiResultTransformer();
}
@Bean
@ConditionalOnMissingBean(value = RemoteApiResponseFilter.class)
protected RemoteApiResponseFilter apiResponseFilter() {
return new RemoteApiResponseFilter.Default();
}
}