From 79ca9e56cbe1108334a5d2f4209570a118b2a645 Mon Sep 17 00:00:00 2001 From: Scott Murphy Heiberg Date: Wed, 20 Nov 2024 21:29:28 -0800 Subject: [PATCH] move multipartConfig to auto configuration --- .../ControllersAutoConfiguration.java | 31 ++++++++++++++++--- .../ControllersGrailsPlugin.groovy | 8 +---- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersAutoConfiguration.java b/grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersAutoConfiguration.java index 641e4aad8f..8e6519111c 100644 --- a/grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersAutoConfiguration.java +++ b/grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersAutoConfiguration.java @@ -3,6 +3,7 @@ import grails.config.Settings; import jakarta.servlet.DispatcherType; import jakarta.servlet.Filter; +import jakarta.servlet.MultipartConfigElement; import org.grails.web.config.http.GrailsFilters; import org.grails.web.filters.HiddenHttpMethodFilter; import org.grails.web.servlet.mvc.GrailsWebRequestFilter; @@ -33,13 +34,25 @@ public class ControllersAutoConfiguration { private boolean filtersForceEncoding; @Value("${" + Settings.RESOURCES_CACHE_PERIOD + ":0}") - int resourcesCachePeriod; + private int resourcesCachePeriod; @Value("${" + Settings.RESOURCES_ENABLED + ":true}") - boolean resourcesEnabled; + private boolean resourcesEnabled; - @Value("${" + Settings.RESOURCES_PATTERN + ":"+Settings.DEFAULT_RESOURCE_PATTERN+"}") - String resourcesPattern; + @Value("${" + Settings.RESOURCES_PATTERN + ":" + Settings.DEFAULT_RESOURCE_PATTERN + "}") + private String resourcesPattern; + + @Value("${" + Settings.CONTROLLERS_UPLOAD_LOCATION + ":#{null}}") + private String uploadTmpDir; + + @Value("${" + Settings.CONTROLLERS_UPLOAD_MAX_FILE_SIZE + ":128000}") + private long maxFileSize; + + @Value("${" + Settings.CONTROLLERS_UPLOAD_MAX_REQUEST_SIZE + ":128000}") + private long maxRequestSize; + + @Value("${" + Settings.CONTROLLERS_UPLOAD_FILE_SIZE_THRESHOLD + ":0}") + private int fileSizeThreshold; @Bean @ConditionalOnMissingBean(CharacterEncodingFilter.class) @@ -80,7 +93,15 @@ public FilterRegistrationBean grailsWebRequestFilter(ApplicationContext } @Bean - GrailsWebMvcConfigurer webMvcConfig() { + public MultipartConfigElement multipartConfigElement() { + if (uploadTmpDir == null) { + uploadTmpDir = System.getProperty("java.io.tmpdir"); + } + return new MultipartConfigElement(uploadTmpDir, maxFileSize, maxRequestSize, fileSizeThreshold); + } + + @Bean + public GrailsWebMvcConfigurer webMvcConfig() { return new GrailsWebMvcConfigurer(resourcesCachePeriod, resourcesEnabled, resourcesPattern); } diff --git a/grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersGrailsPlugin.groovy b/grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersGrailsPlugin.groovy index ac96cb64a4..e73928df14 100644 --- a/grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersGrailsPlugin.groovy +++ b/grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersGrailsPlugin.groovy @@ -57,10 +57,6 @@ class ControllersGrailsPlugin extends Plugin { def config = application.config boolean useJsessionId = config.getProperty(Settings.GRAILS_VIEWS_ENABLE_JSESSIONID, Boolean, false) - String uploadTmpDir = config.getProperty(Settings.CONTROLLERS_UPLOAD_LOCATION, System.getProperty("java.io.tmpdir")) - long maxFileSize = config.getProperty(Settings.CONTROLLERS_UPLOAD_MAX_FILE_SIZE, Long, 128000L) - long maxRequestSize = config.getProperty(Settings.CONTROLLERS_UPLOAD_MAX_REQUEST_SIZE, Long, 128000L) - 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) @@ -76,8 +72,6 @@ class ControllersGrailsPlugin extends Plugin { "${CompositeViewResolver.BEAN_NAME}"(CompositeViewResolver) - multipartConfigElement(MultipartConfigElement, uploadTmpDir, maxFileSize, maxRequestSize, fileSizeThreashold) - def handlerInterceptors = springConfig.containsBean("localeChangeInterceptor") ? [ref("localeChangeInterceptor")] : [] def interceptorsClosure = { interceptors = handlerInterceptors @@ -91,7 +85,7 @@ class ControllersGrailsPlugin extends Plugin { dispatcherServletRegistration(DispatcherServletRegistrationBean, ref("dispatcherServlet"), grailsServletPath) { loadOnStartup = 2 asyncSupported = true - multipartConfig = multipartConfigElement + multipartConfig = ref('multipartConfigElement') } for (controller in application.getArtefacts(ControllerArtefactHandler.TYPE)) {