Skip to content

Commit

Permalink
Move WebMvcConfigurer to auto configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
codeconsole committed Nov 21, 2024
1 parent aabbe7e commit 5ec8291
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.EnumSet;

Expand All @@ -30,6 +32,15 @@ public class ControllersAutoConfiguration {
@Value("${" + Settings.FILTER_FORCE_ENCODING + ":false}")
private boolean filtersForceEncoding;

@Value("${" + Settings.RESOURCES_CACHE_PERIOD + ":0}")
int resourcesCachePeriod;

@Value("${" + Settings.RESOURCES_ENABLED + ":true}")
boolean resourcesEnabled;

@Value("${" + Settings.RESOURCES_PATTERN + ":"+Settings.DEFAULT_RESOURCE_PATTERN+"}")
String resourcesPattern;

@Bean
@ConditionalOnMissingBean(CharacterEncodingFilter.class)
public CharacterEncodingFilter characterEncodingFilter() {
Expand Down Expand Up @@ -67,4 +78,58 @@ public FilterRegistrationBean<Filter> grailsWebRequestFilter(ApplicationContext
registrationBean.setOrder(GrailsFilters.GRAILS_WEB_REQUEST_FILTER.getOrder());
return registrationBean;
}

@Bean
GrailsWebMvcConfigurer webMvcConfig() {
return new GrailsWebMvcConfigurer(resourcesCachePeriod, resourcesEnabled, resourcesPattern);
}

static class GrailsWebMvcConfigurer implements WebMvcConfigurer {

private static final String[] SERVLET_RESOURCE_LOCATIONS = new String[] { "/" };

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[] {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/"
};

private static final String[] RESOURCE_LOCATIONS;

static {
RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length
+ SERVLET_RESOURCE_LOCATIONS.length];
System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0,
SERVLET_RESOURCE_LOCATIONS.length);
System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS,
SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length);
}

boolean addMappings;
Integer cachePeriod;
String resourcesPattern;

GrailsWebMvcConfigurer(Integer cachePeriod, Boolean addMappings, String resourcesPattern) {
this.addMappings = addMappings;
this.cachePeriod = cachePeriod;
this.resourcesPattern = resourcesPattern;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!addMappings) {
return;
}

if (!registry.hasMappingForPattern("/webjars/**")) {
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(cachePeriod);
}
if (!registry.hasMappingForPattern(resourcesPattern)) {
registry.addResourceHandler(resourcesPattern)
.addResourceLocations(RESOURCE_LOCATIONS)
.setCachePeriod(cachePeriod);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import grails.config.Settings
import grails.core.GrailsControllerClass
import grails.plugins.Plugin
import grails.util.GrailsUtil
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import org.grails.core.artefact.ControllerArtefactHandler
import org.grails.plugins.web.servlet.context.BootStrapClassRunner
Expand All @@ -31,9 +30,6 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean
import org.springframework.context.ApplicationContext
import org.springframework.util.ClassUtils
import org.springframework.web.multipart.support.StandardServletMultipartResolver
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
import jakarta.servlet.MultipartConfigElement
Expand Down Expand Up @@ -67,9 +63,6 @@ class ControllersGrailsPlugin extends Plugin {
int fileSizeThreashold = config.getProperty(Settings.CONTROLLERS_UPLOAD_FILE_SIZE_THRESHOLD, Integer, 0)
boolean isTomcat = ClassUtils.isPresent("org.apache.catalina.startup.Tomcat", application.classLoader)
String grailsServletPath = config.getProperty(Settings.WEB_SERVLET_PATH, isTomcat ? Settings.DEFAULT_TOMCAT_SERVLET_PATH : Settings.DEFAULT_WEB_SERVLET_PATH)
int resourcesCachePeriod = config.getProperty(Settings.RESOURCES_CACHE_PERIOD, Integer, 0)
boolean resourcesEnabled = config.getProperty(Settings.RESOURCES_ENABLED, Boolean, true)
String resourcesPattern = config.getProperty(Settings.RESOURCES_PATTERN, String, Settings.DEFAULT_RESOURCE_PATTERN)

if (!Boolean.parseBoolean(System.getProperty(Settings.SETTING_SKIP_BOOTSTRAP))) {
bootStrapClassRunner(BootStrapClassRunner)
Expand All @@ -93,9 +86,6 @@ class ControllersGrailsPlugin extends Plugin {
annotationHandlerMapping(RequestMappingHandlerMapping, interceptorsClosure)
annotationHandlerAdapter(RequestMappingHandlerAdapter)

// add Grails webmvc config
webMvcConfig(GrailsWebMvcConfigurer, resourcesCachePeriod, resourcesEnabled, resourcesPattern)

// add the dispatcher servlet
dispatcherServlet(GrailsDispatcherServlet)
dispatcherServletRegistration(DispatcherServletRegistrationBean, ref("dispatcherServlet"), grailsServletPath) {
Expand Down Expand Up @@ -128,55 +118,6 @@ class ControllersGrailsPlugin extends Plugin {
}
} }


@CompileStatic
static class GrailsWebMvcConfigurer implements WebMvcConfigurer {

private static final String[] SERVLET_RESOURCE_LOCATIONS = [ "/" ]

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = [
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" ]

private static final String[] RESOURCE_LOCATIONS
static {
RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length
+ SERVLET_RESOURCE_LOCATIONS.length]
System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0,
SERVLET_RESOURCE_LOCATIONS.length)
System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS,
SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length);
}

boolean addMappings = true
Integer cachePeriod
String resourcesPattern

GrailsWebMvcConfigurer(Integer cachePeriod, boolean addMappings = true, String resourcesPattern = '/static/**') {
this.addMappings = addMappings
this.cachePeriod = cachePeriod
this.resourcesPattern = resourcesPattern
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!addMappings) {
return
}

if (!registry.hasMappingForPattern("/webjars/**")) {
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(cachePeriod)
}
if (!registry.hasMappingForPattern(resourcesPattern)) {
registry.addResourceHandler(resourcesPattern)
.addResourceLocations(RESOURCE_LOCATIONS)
.setCachePeriod(cachePeriod)
}
}
}

@Override
void onChange( Map<String, Object> event) {
if (!(event.source instanceof Class)) {
Expand Down Expand Up @@ -205,5 +146,4 @@ class ControllersGrailsPlugin extends Plugin {
}
}
}

}

0 comments on commit 5ec8291

Please sign in to comment.