Skip to content

Commit acd6772

Browse files
authored
Merge pull request #13866 from codeconsole/7.0.x-controller-webmvc
Continue ControllersAutoConfiguration Migration
2 parents 7c71d9f + 4634e09 commit acd6772

File tree

2 files changed

+112
-80
lines changed

2 files changed

+112
-80
lines changed

grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersAutoConfiguration.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
package org.grails.plugins.web.controllers;
22

33
import grails.config.Settings;
4+
import grails.core.GrailsApplication;
45
import jakarta.servlet.DispatcherType;
56
import jakarta.servlet.Filter;
7+
import jakarta.servlet.MultipartConfigElement;
68
import org.grails.web.config.http.GrailsFilters;
79
import org.grails.web.filters.HiddenHttpMethodFilter;
10+
import org.grails.web.servlet.mvc.GrailsDispatcherServlet;
811
import org.grails.web.servlet.mvc.GrailsWebRequestFilter;
912
import org.springframework.beans.factory.annotation.Value;
1013
import org.springframework.boot.autoconfigure.AutoConfiguration;
1114
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
1215
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
16+
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;
1317
import org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration;
1418
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
1519
import org.springframework.boot.web.servlet.FilterRegistrationBean;
1620
import org.springframework.boot.web.servlet.filter.OrderedCharacterEncodingFilter;
1721
import org.springframework.context.ApplicationContext;
1822
import org.springframework.context.annotation.Bean;
23+
import org.springframework.util.ClassUtils;
1924
import org.springframework.web.filter.CharacterEncodingFilter;
25+
import org.springframework.web.servlet.DispatcherServlet;
26+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
27+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
2028

2129
import java.util.EnumSet;
2230

@@ -30,6 +38,30 @@ public class ControllersAutoConfiguration {
3038
@Value("${" + Settings.FILTER_FORCE_ENCODING + ":false}")
3139
private boolean filtersForceEncoding;
3240

41+
@Value("${" + Settings.RESOURCES_CACHE_PERIOD + ":0}")
42+
private int resourcesCachePeriod;
43+
44+
@Value("${" + Settings.RESOURCES_ENABLED + ":true}")
45+
private boolean resourcesEnabled;
46+
47+
@Value("${" + Settings.RESOURCES_PATTERN + ":" + Settings.DEFAULT_RESOURCE_PATTERN + "}")
48+
private String resourcesPattern;
49+
50+
@Value("${" + Settings.CONTROLLERS_UPLOAD_LOCATION + ":#{null}}")
51+
private String uploadTmpDir;
52+
53+
@Value("${" + Settings.CONTROLLERS_UPLOAD_MAX_FILE_SIZE + ":128000}")
54+
private long maxFileSize;
55+
56+
@Value("${" + Settings.CONTROLLERS_UPLOAD_MAX_REQUEST_SIZE + ":128000}")
57+
private long maxRequestSize;
58+
59+
@Value("${" + Settings.CONTROLLERS_UPLOAD_FILE_SIZE_THRESHOLD + ":0}")
60+
private int fileSizeThreshold;
61+
62+
@Value("${" + Settings.WEB_SERVLET_PATH + ":#{null}}")
63+
String grailsServletPath;
64+
3365
@Bean
3466
@ConditionalOnMissingBean(CharacterEncodingFilter.class)
3567
public CharacterEncodingFilter characterEncodingFilter() {
@@ -67,4 +99,84 @@ public FilterRegistrationBean<Filter> grailsWebRequestFilter(ApplicationContext
6799
registrationBean.setOrder(GrailsFilters.GRAILS_WEB_REQUEST_FILTER.getOrder());
68100
return registrationBean;
69101
}
102+
103+
@Bean
104+
public MultipartConfigElement multipartConfigElement() {
105+
if (uploadTmpDir == null) {
106+
uploadTmpDir = System.getProperty("java.io.tmpdir");
107+
}
108+
return new MultipartConfigElement(uploadTmpDir, maxFileSize, maxRequestSize, fileSizeThreshold);
109+
}
110+
111+
@Bean
112+
public DispatcherServlet dispatcherServlet() {
113+
return new GrailsDispatcherServlet();
114+
}
115+
116+
@Bean
117+
public DispatcherServletRegistrationBean dispatcherServletRegistration(GrailsApplication application, DispatcherServlet dispatcherServlet, MultipartConfigElement multipartConfigElement) {
118+
if (grailsServletPath == null) {
119+
boolean isTomcat = ClassUtils.isPresent("org.apache.catalina.startup.Tomcat", application.getClassLoader());
120+
grailsServletPath = isTomcat ? Settings.DEFAULT_TOMCAT_SERVLET_PATH : Settings.DEFAULT_WEB_SERVLET_PATH;
121+
}
122+
DispatcherServletRegistrationBean dispatcherServletRegistration = new DispatcherServletRegistrationBean(dispatcherServlet, grailsServletPath);
123+
dispatcherServletRegistration.setLoadOnStartup(2);
124+
dispatcherServletRegistration.setAsyncSupported(true);
125+
dispatcherServletRegistration.setMultipartConfig(multipartConfigElement);
126+
return dispatcherServletRegistration;
127+
}
128+
129+
@Bean
130+
public GrailsWebMvcConfigurer webMvcConfig() {
131+
return new GrailsWebMvcConfigurer(resourcesCachePeriod, resourcesEnabled, resourcesPattern);
132+
}
133+
134+
static class GrailsWebMvcConfigurer implements WebMvcConfigurer {
135+
136+
private static final String[] SERVLET_RESOURCE_LOCATIONS = new String[] { "/" };
137+
138+
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[] {
139+
"classpath:/META-INF/resources/", "classpath:/resources/",
140+
"classpath:/static/", "classpath:/public/"
141+
};
142+
143+
private static final String[] RESOURCE_LOCATIONS;
144+
145+
static {
146+
RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length
147+
+ SERVLET_RESOURCE_LOCATIONS.length];
148+
System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0,
149+
SERVLET_RESOURCE_LOCATIONS.length);
150+
System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS,
151+
SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length);
152+
}
153+
154+
boolean addMappings;
155+
Integer cachePeriod;
156+
String resourcesPattern;
157+
158+
GrailsWebMvcConfigurer(Integer cachePeriod, Boolean addMappings, String resourcesPattern) {
159+
this.addMappings = addMappings;
160+
this.cachePeriod = cachePeriod;
161+
this.resourcesPattern = resourcesPattern;
162+
}
163+
164+
@Override
165+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
166+
if (!addMappings) {
167+
return;
168+
}
169+
170+
if (!registry.hasMappingForPattern("/webjars/**")) {
171+
registry.addResourceHandler("/webjars/**")
172+
.addResourceLocations("classpath:/META-INF/resources/webjars/")
173+
.setCachePeriod(cachePeriod);
174+
}
175+
if (!registry.hasMappingForPattern(resourcesPattern)) {
176+
registry.addResourceHandler(resourcesPattern)
177+
.addResourceLocations(RESOURCE_LOCATIONS)
178+
.setCachePeriod(cachePeriod);
179+
}
180+
}
181+
}
70182
}

grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersGrailsPlugin.groovy

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,16 @@ import grails.config.Settings
1919
import grails.core.GrailsControllerClass
2020
import grails.plugins.Plugin
2121
import grails.util.GrailsUtil
22-
import groovy.transform.CompileStatic
2322
import groovy.util.logging.Slf4j
2423
import org.grails.core.artefact.ControllerArtefactHandler
2524
import org.grails.plugins.web.servlet.context.BootStrapClassRunner
2625
import org.grails.web.errors.GrailsExceptionResolver
27-
import org.grails.web.servlet.mvc.GrailsDispatcherServlet
2826
import org.grails.web.servlet.mvc.TokenResponseActionResultTransformer
2927
import org.grails.web.servlet.view.CompositeViewResolver
3028
import org.springframework.beans.factory.support.AbstractBeanDefinition
31-
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean
3229
import org.springframework.context.ApplicationContext
33-
import org.springframework.util.ClassUtils
34-
import org.springframework.web.multipart.support.StandardServletMultipartResolver
35-
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
36-
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
3730
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
3831
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
39-
import jakarta.servlet.MultipartConfigElement
4032

4133
/**
4234
* Handles the configuration of controllers for Grails.
@@ -61,15 +53,6 @@ class ControllersGrailsPlugin extends Plugin {
6153
def config = application.config
6254

6355
boolean useJsessionId = config.getProperty(Settings.GRAILS_VIEWS_ENABLE_JSESSIONID, Boolean, false)
64-
String uploadTmpDir = config.getProperty(Settings.CONTROLLERS_UPLOAD_LOCATION, System.getProperty("java.io.tmpdir"))
65-
long maxFileSize = config.getProperty(Settings.CONTROLLERS_UPLOAD_MAX_FILE_SIZE, Long, 128000L)
66-
long maxRequestSize = config.getProperty(Settings.CONTROLLERS_UPLOAD_MAX_REQUEST_SIZE, Long, 128000L)
67-
int fileSizeThreashold = config.getProperty(Settings.CONTROLLERS_UPLOAD_FILE_SIZE_THRESHOLD, Integer, 0)
68-
boolean isTomcat = ClassUtils.isPresent("org.apache.catalina.startup.Tomcat", application.classLoader)
69-
String grailsServletPath = config.getProperty(Settings.WEB_SERVLET_PATH, isTomcat ? Settings.DEFAULT_TOMCAT_SERVLET_PATH : Settings.DEFAULT_WEB_SERVLET_PATH)
70-
int resourcesCachePeriod = config.getProperty(Settings.RESOURCES_CACHE_PERIOD, Integer, 0)
71-
boolean resourcesEnabled = config.getProperty(Settings.RESOURCES_ENABLED, Boolean, true)
72-
String resourcesPattern = config.getProperty(Settings.RESOURCES_PATTERN, String, Settings.DEFAULT_RESOURCE_PATTERN)
7356

7457
if (!Boolean.parseBoolean(System.getProperty(Settings.SETTING_SKIP_BOOTSTRAP))) {
7558
bootStrapClassRunner(BootStrapClassRunner)
@@ -83,8 +66,6 @@ class ControllersGrailsPlugin extends Plugin {
8366

8467
"${CompositeViewResolver.BEAN_NAME}"(CompositeViewResolver)
8568

86-
multipartConfigElement(MultipartConfigElement, uploadTmpDir, maxFileSize, maxRequestSize, fileSizeThreashold)
87-
8869
def handlerInterceptors = springConfig.containsBean("localeChangeInterceptor") ? [ref("localeChangeInterceptor")] : []
8970
def interceptorsClosure = {
9071
interceptors = handlerInterceptors
@@ -93,17 +74,6 @@ class ControllersGrailsPlugin extends Plugin {
9374
annotationHandlerMapping(RequestMappingHandlerMapping, interceptorsClosure)
9475
annotationHandlerAdapter(RequestMappingHandlerAdapter)
9576

96-
// add Grails webmvc config
97-
webMvcConfig(GrailsWebMvcConfigurer, resourcesCachePeriod, resourcesEnabled, resourcesPattern)
98-
99-
// add the dispatcher servlet
100-
dispatcherServlet(GrailsDispatcherServlet)
101-
dispatcherServletRegistration(DispatcherServletRegistrationBean, ref("dispatcherServlet"), grailsServletPath) {
102-
loadOnStartup = 2
103-
asyncSupported = true
104-
multipartConfig = multipartConfigElement
105-
}
106-
10777
for (controller in application.getArtefacts(ControllerArtefactHandler.TYPE)) {
10878
log.debug "Configuring controller $controller.fullName"
10979
if (controller.available) {
@@ -128,55 +98,6 @@ class ControllersGrailsPlugin extends Plugin {
12898
}
12999
} }
130100

131-
132-
@CompileStatic
133-
static class GrailsWebMvcConfigurer implements WebMvcConfigurer {
134-
135-
private static final String[] SERVLET_RESOURCE_LOCATIONS = [ "/" ]
136-
137-
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = [
138-
"classpath:/META-INF/resources/", "classpath:/resources/",
139-
"classpath:/static/", "classpath:/public/" ]
140-
141-
private static final String[] RESOURCE_LOCATIONS
142-
static {
143-
RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length
144-
+ SERVLET_RESOURCE_LOCATIONS.length]
145-
System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0,
146-
SERVLET_RESOURCE_LOCATIONS.length)
147-
System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS,
148-
SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length);
149-
}
150-
151-
boolean addMappings = true
152-
Integer cachePeriod
153-
String resourcesPattern
154-
155-
GrailsWebMvcConfigurer(Integer cachePeriod, boolean addMappings = true, String resourcesPattern = '/static/**') {
156-
this.addMappings = addMappings
157-
this.cachePeriod = cachePeriod
158-
this.resourcesPattern = resourcesPattern
159-
}
160-
161-
@Override
162-
public void addResourceHandlers(ResourceHandlerRegistry registry) {
163-
if (!addMappings) {
164-
return
165-
}
166-
167-
if (!registry.hasMappingForPattern("/webjars/**")) {
168-
registry.addResourceHandler("/webjars/**")
169-
.addResourceLocations("classpath:/META-INF/resources/webjars/")
170-
.setCachePeriod(cachePeriod)
171-
}
172-
if (!registry.hasMappingForPattern(resourcesPattern)) {
173-
registry.addResourceHandler(resourcesPattern)
174-
.addResourceLocations(RESOURCE_LOCATIONS)
175-
.setCachePeriod(cachePeriod)
176-
}
177-
}
178-
}
179-
180101
@Override
181102
void onChange( Map<String, Object> event) {
182103
if (!(event.source instanceof Class)) {
@@ -205,5 +126,4 @@ class ControllersGrailsPlugin extends Plugin {
205126
}
206127
}
207128
}
208-
209129
}

0 commit comments

Comments
 (0)