Skip to content

Commit

Permalink
move multipartConfig to auto configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
codeconsole committed Nov 21, 2024
1 parent 5ec8291 commit 79ca9e5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -80,7 +93,15 @@ public FilterRegistrationBean<Filter> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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)) {
Expand Down

0 comments on commit 79ca9e5

Please sign in to comment.