-
Notifications
You must be signed in to change notification settings - Fork 951
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13845 from codeconsole/7.0.x-url-mappings-autocon…
…figuration AutoConfiguration for Url Mappings and Controllers Plugins
- Loading branch information
Showing
8 changed files
with
149 additions
and
71 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
...lers/src/main/groovy/org/grails/plugins/web/controllers/ControllersAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.grails.plugins.web.controllers; | ||
|
||
import grails.config.Settings; | ||
import jakarta.servlet.DispatcherType; | ||
import jakarta.servlet.Filter; | ||
import org.grails.spring.config.http.GrailsFilters; | ||
import org.grails.web.filters.HiddenHttpMethodFilter; | ||
import org.grails.web.servlet.mvc.GrailsWebRequestFilter; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; | ||
import org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; | ||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.boot.web.servlet.filter.OrderedCharacterEncodingFilter; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.web.filter.CharacterEncodingFilter; | ||
|
||
import java.util.EnumSet; | ||
|
||
@AutoConfiguration(before = { HttpEncodingAutoConfiguration.class, WebMvcAutoConfiguration.class }) | ||
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) | ||
public class ControllersAutoConfiguration { | ||
|
||
@Value("${" + Settings.FILTER_ENCODING + ":utf-8}") | ||
private String filtersEncoding; | ||
|
||
@Value("${" + Settings.FILTER_FORCE_ENCODING + ":false}") | ||
private boolean filtersForceEncoding; | ||
|
||
@Bean | ||
@ConditionalOnMissingBean(CharacterEncodingFilter.class) | ||
public CharacterEncodingFilter characterEncodingFilter() { | ||
FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>(); | ||
OrderedCharacterEncodingFilter characterEncodingFilter = new OrderedCharacterEncodingFilter(); | ||
characterEncodingFilter.setEncoding(filtersEncoding); | ||
characterEncodingFilter.setForceEncoding(filtersForceEncoding); | ||
characterEncodingFilter.setOrder(GrailsFilters.CHARACTER_ENCODING_FILTER.getOrder()); | ||
return characterEncodingFilter; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class) | ||
public FilterRegistrationBean<Filter> hiddenHttpMethodFilter() { | ||
FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>(); | ||
registrationBean.setFilter(new HiddenHttpMethodFilter()); | ||
registrationBean.addUrlPatterns(Settings.DEFAULT_WEB_SERVLET_PATH); | ||
registrationBean.setOrder(GrailsFilters.HIDDEN_HTTP_METHOD_FILTER.getOrder()); | ||
return registrationBean; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean(GrailsWebRequestFilter.class) | ||
public FilterRegistrationBean<Filter> grailsWebRequestFilter() { | ||
FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>(); | ||
registrationBean.setFilter(new GrailsWebRequestFilter()); | ||
registrationBean.setDispatcherTypes(EnumSet.of( | ||
DispatcherType.FORWARD, | ||
DispatcherType.INCLUDE, | ||
DispatcherType.REQUEST) | ||
); | ||
registrationBean.addUrlPatterns(Settings.DEFAULT_WEB_SERVLET_PATH); | ||
registrationBean.setOrder(GrailsFilters.GRAILS_WEB_REQUEST_FILTER.getOrder()); | ||
return registrationBean; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.grails.plugins.web.controllers.ControllersAutoConfiguration |
74 changes: 74 additions & 0 deletions
74
...mappings/src/main/groovy/org/grails/plugins/web/mapping/UrlMappingsAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.grails.plugins.web.mapping; | ||
|
||
import grails.config.Settings; | ||
import grails.util.Environment; | ||
import grails.web.CamelCaseUrlConverter; | ||
import grails.web.HyphenatedUrlConverter; | ||
import grails.web.UrlConverter; | ||
import grails.web.mapping.LinkGenerator; | ||
import grails.web.mapping.UrlMappings; | ||
import grails.web.mapping.cors.GrailsCorsConfiguration; | ||
import grails.web.mapping.cors.GrailsCorsFilter; | ||
import org.grails.web.mapping.CachingLinkGenerator; | ||
import org.grails.web.mapping.DefaultLinkGenerator; | ||
import org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter; | ||
import org.grails.web.mapping.servlet.UrlMappingsErrorPageCustomizer; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.*; | ||
|
||
@AutoConfiguration | ||
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) | ||
@EnableConfigurationProperties({ GrailsCorsConfiguration.class }) | ||
public class UrlMappingsAutoConfiguration { | ||
@Value("${" + Settings.WEB_LINK_GENERATOR_USE_CACHE + ":#{null}}") | ||
private Boolean cacheUrls; | ||
|
||
@Value("${" + Settings.SERVER_URL + ":#{null}}") | ||
private String serverURL; | ||
|
||
@Bean(UrlConverter.BEAN_NAME) | ||
@ConditionalOnMissingBean(name = UrlConverter.BEAN_NAME) | ||
@ConditionalOnProperty(name = Settings.WEB_URL_CONVERTER, havingValue = "camelCase", matchIfMissing = true) | ||
public UrlConverter camelCaseUrlConverter() { | ||
return new CamelCaseUrlConverter(); | ||
} | ||
|
||
@Bean(UrlConverter.BEAN_NAME) | ||
@ConditionalOnMissingBean(name = UrlConverter.BEAN_NAME) | ||
@ConditionalOnProperty(name = Settings.WEB_URL_CONVERTER, havingValue = "hyphenated") | ||
public UrlConverter hyphenatedUrlConverter() { | ||
return new HyphenatedUrlConverter(); | ||
} | ||
|
||
@Bean | ||
public LinkGenerator grailsLinkGenerator() { | ||
if (cacheUrls == null) { | ||
cacheUrls = !Environment.isDevelopmentMode() && !Environment.getCurrent().isReloadEnabled(); | ||
} | ||
return cacheUrls? new CachingLinkGenerator(serverURL) : new DefaultLinkGenerator(serverURL); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnProperty(name = Settings.SETTING_CORS_FILTER, havingValue = "true", matchIfMissing = true) | ||
public GrailsCorsFilter grailsCorsFilter(GrailsCorsConfiguration grailsCorsConfiguration) { | ||
return new GrailsCorsFilter(grailsCorsConfiguration); | ||
} | ||
|
||
@Bean | ||
public UrlMappingsErrorPageCustomizer urlMappingsErrorPageCustomizer(ObjectProvider<UrlMappings> urlMappingsProvider) { | ||
UrlMappingsErrorPageCustomizer errorPageCustomizer = new UrlMappingsErrorPageCustomizer(); | ||
errorPageCustomizer.setUrlMappings(urlMappingsProvider.getIfAvailable()); | ||
return errorPageCustomizer; | ||
} | ||
|
||
@Bean | ||
public UrlMappingsInfoHandlerAdapter urlMappingsInfoHandlerAdapter() { | ||
return new UrlMappingsInfoHandlerAdapter(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.grails.plugins.web.mapping.UrlMappingsAutoConfiguration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters