Skip to content

Instantly share code, notes, and snippets.

@joemccall86
Created November 16, 2016 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joemccall86/b434aa52cdf6802f9ccc72b672976fff to your computer and use it in GitHub Desktop.
Save joemccall86/b434aa52cdf6802f9ccc72b672976fff to your computer and use it in GitHub Desktop.
Extended class for working around #10319
import grails.artefact.Interceptor
import groovy.transform.CompileStatic
import javax.servlet.http.HttpServletRequest
/**
* Workaround for https://github.com/grails/grails-core/issues/10319
*
* Allows an interceptor to behave properly when server.contextPath is set
*/
@CompileStatic
trait ContextPathAwareInterceptor extends Interceptor {
@Override
boolean doesMatch(HttpServletRequest request) {
super.doesMatch(new ContextPathRemovingRequestWrapper(originalRequest: request))
}
/**
* Wrapper that intercepts the request URI and removes the context path
*/
static class ContextPathRemovingRequestWrapper implements HttpServletRequest {
@Delegate HttpServletRequest originalRequest
@Override
String getRequestURI() {
originalRequest.requestURI - contextPath
}
}
}
@orubel
Copy link

orubel commented Nov 28, 2016

Graeme doesn't like me to contribute and has blocked me so I thought I would add here...

Very easy fix if you do something like this...

String entryPoint = "/api/v${Metadata.current.getProperty(Metadata.APPLICATION_VERSION, String.class)}"

ContextPathAwareInterceptor(){
	match(uri:"/${entryPoint}/**")
}

... or something like that. I have had to work this into alot of what I do and this works great and provides the fastest response.

You just have to separate out as a variable first, then match it.

In this case. if my APPLICATION_VERSION was '0.1', it would match on 'api/v0.1'

@joemccall86
Copy link
Author

That doesn't seem to address the case where the server.contextPath is set in config. The value of entryPoint will still be checked against a string that still contains the contextPath. Am I missing something?

@orubel
Copy link

orubel commented Dec 1, 2016

No I think I was mistaken. So by subtracting the contextPath, are you trying to match on:

scheme + "://" + serverName + ((includePort) ? (":" + serverPort) : "")

??? Because the forwardUri == contextPath effectively. The above would give you a blank string (as far as I know).

What are you try for as an outcome?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment