I would like to basically do a URL rewrite in UrlMappings.groovy, for example:
"/pub/cdn/$version/**"(uri: request.forwardURI.replaceFirst("/pub/cdn/[0-9]*", ""))
So that a request for:
/pub/cdn/1327516405188/css/login.css
for example, would get translated to:
/css/login.css
The idea is to take the original URI and strip out some of the path, and pass along the new URI.
However, using the above code, I get the error:
> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'grailsUrlMappingsHolder': Cannot resolve
> reference to bean 'urlMappingsTargetSource' while setting bean
> property 'targetSource'; nested exception is
> ...
> groovy.lang.MissingPropertyException: No such property: forwardURI for
> class: java.lang.String
which seems to imply that the request object is a String. Is there a way to obtain the original URI in UrlMappings.groovy?
The "/old/path"(uri: "/new/path") method does work as long as the new path is hard coded, but I can't figure out how to access the original requested path as a variable inside the UrlMappings class.
P.S. I tried having UrlMappings call a controller:
"/pub/cdn/$version/**" {
controller = "image"
action = "cdnRedirect"
}
and in the Controller:
def cdnRedirect = {
def newUri = request.forwardURI?.toString().replaceFirst("/pub/cdn/[0-9]*", "")
return redirect(uri: newUri)
}
While the controller closure gets called correctly, and the URI gets translated correctly, the redirect does not seem to do anything at all.