Skip to content

Commit 1d8c858

Browse files
sergiomichelsGraeme Rocher
authored andcommitted
Master (#9969)
* Fix for #9905 Fix for using the http method in the interceptor, instead of the UrlMapping that can be generic. * Removing unused imports.
1 parent 8ab5c4c commit 1d8c858

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

grails-plugin-interceptors/src/main/groovy/grails/artefact/Interceptor.groovy

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package grails.artefact
1818
import grails.artefact.controller.support.RequestForwarder
1919
import grails.artefact.controller.support.ResponseRedirector
2020
import grails.artefact.controller.support.ResponseRenderer
21-
import grails.core.GrailsApplication
2221
import grails.interceptors.Matcher
2322
import grails.util.GrailsNameUtils
2423
import grails.web.api.ServletAttributes
@@ -35,12 +34,8 @@ import org.grails.web.servlet.mvc.exceptions.ControllerExecutionException
3534
import org.grails.web.servlet.view.CompositeViewResolver
3635
import org.grails.web.util.GrailsApplicationAttributes
3736
import org.grails.web.util.WebUtils
38-
import org.springframework.beans.factory.annotation.Autowired
3937
import org.springframework.core.Ordered
4038
import org.springframework.web.servlet.ModelAndView
41-
import org.springframework.web.servlet.ViewResolver
42-
43-
import javax.annotation.PostConstruct
4439
import javax.servlet.http.HttpServletRequest
4540
import javax.servlet.http.HttpServletResponse
4641
import java.util.concurrent.ConcurrentLinkedQueue
@@ -95,10 +90,11 @@ trait Interceptor implements ResponseRenderer, ResponseRedirector, RequestForwar
9590
def uri = req.requestURI
9691

9792
def matchedInfo = request.getAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST)
93+
9894
UrlMappingInfo grailsMappingInfo = (UrlMappingInfo)matchedInfo
9995

10096
for(Matcher matcher in allMatchers) {
101-
if(matcher.doesMatch(uri, grailsMappingInfo)) {
97+
if(matcher.doesMatch(uri, grailsMappingInfo, req.method)) {
10298
request.setAttribute(interceptorMatchKey, Boolean.TRUE)
10399
return true
104100
}

grails-plugin-interceptors/src/main/groovy/grails/interceptors/Matcher.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ interface Matcher {
3939
*/
4040
boolean doesMatch(String uri, UrlMappingInfo info)
4141

42+
/**
43+
* Perform the matches using the http method of the request instead of the UrlMappingInfo
44+
* @param uri
45+
* @param info
46+
* @param method
47+
* @return
48+
*/
49+
boolean doesMatch(String uri, UrlMappingInfo info, String method)
50+
4251
/**
4352
* Defines the match for the given arguments
4453
*

grails-plugin-interceptors/src/main/groovy/org/grails/plugins/web/interceptors/UrlMappingMatcher.groovy

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ class UrlMappingMatcher implements Matcher {
5757
}
5858

5959
boolean doesMatch(String uri, UrlMappingInfo info) {
60+
return doesMatch(uri, info, null)
61+
}
62+
63+
boolean doesMatch(String uri, UrlMappingInfo info, String method) {
6064
boolean hasUriPatterns = !uriPatterns.isEmpty()
6165

6266
boolean isExcluded = this.isExcluded(uri, info)
@@ -75,7 +79,7 @@ class UrlMappingMatcher implements Matcher {
7579
Boolean matched = CACHED_MATCHES.get(infoCode)
7680
if (matched != null) return matched
7781

78-
if (doesMatchInternal(info)) {
82+
if (doesMatchInternal(info, method)) {
7983
if (Environment.current == Environment.PRODUCTION) {
8084
CACHED_MATCHES.put(infoCode, Boolean.TRUE)
8185
}
@@ -100,12 +104,12 @@ class UrlMappingMatcher implements Matcher {
100104
false
101105
}
102106

103-
protected boolean doesMatchInternal(UrlMappingInfo info) {
107+
protected boolean doesMatchInternal(UrlMappingInfo info, String method) {
104108
(info != null &&
105109
((info.controllerName ?: '') ==~ controllerRegex) &&
106110
((info.actionName ?: '') ==~ actionRegex) &&
107111
((info.namespace ?: '') ==~ namespaceRegex) &&
108-
((info.httpMethod ?: '') ==~ methodRegex))
112+
((method ?: info.httpMethod ?: '') ==~ methodRegex))
109113
}
110114

111115
@Override

grails-plugin-interceptors/src/test/groovy/grails/artefact/InterceptorSpec.groovy

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import org.grails.web.mapping.DefaultUrlMappingInfo
2121
import org.grails.web.mapping.ForwardUrlMappingInfo
2222
import org.grails.web.mapping.mvc.UrlMappingsHandlerMapping
2323
import org.grails.web.servlet.mvc.GrailsWebRequest
24+
import org.springframework.mock.web.MockHttpServletRequest
25+
import org.springframework.mock.web.MockHttpServletResponse
26+
import org.springframework.mock.web.MockServletContext
2427
import org.springframework.web.context.request.RequestContextHolder
2528
import spock.lang.Specification
2629

@@ -156,6 +159,25 @@ class InterceptorSpec extends Specification {
156159
then:"We don't match"
157160
i.doesMatch()
158161
}
162+
163+
void "Test match with http method"() {
164+
given:"A test interceptor"
165+
def i = new TestMethodInterceptor()
166+
def webRequest = GrailsWebMockUtil.bindMockWebRequest(new MockServletContext(), new MockHttpServletRequest(httpMethod, ""), new MockHttpServletResponse())
167+
def request = webRequest.request
168+
169+
when:"The http method of the current request is ${httpMethod}"
170+
request.setAttribute(UrlMappingsHandlerMapping.MATCHED_REQUEST, new ForwardUrlMappingInfo(controllerName: "test", action: "save"))
171+
172+
then: "We match: ${shouldMatch}"
173+
i.doesMatch() == shouldMatch
174+
175+
where:
176+
httpMethod | shouldMatch
177+
'POST' | true
178+
'GET' | false
179+
}
180+
159181
void clearMatch(i, HttpServletRequest request) {
160182
request.removeAttribute(i.getClass().name + InterceptorArtefactHandler.MATCH_SUFFIX)
161183
}
@@ -197,4 +219,10 @@ class Test4Interceptor implements Interceptor {
197219
matchAll()
198220
.excludes(controller:"foo", action:"bar")
199221
}
200-
}
222+
}
223+
224+
class TestMethodInterceptor implements Interceptor {
225+
TestMethodInterceptor() {
226+
match(method: 'POST')
227+
}
228+
}

0 commit comments

Comments
 (0)