3

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.

drake
  • 174
  • 1
  • 9

1 Answers1

2

Based on your example, it sounds to me like you are solving the wrong issue. The real issue here is seems to be that you are using relative links (e.g. css/login.css) to static content. When the browser picks up a relative link, it uses the current url to determine the file's path.

However, if you use Grails' resource tag, grails will generate a URL that is anchored to the root of the URL. For example, your css/login.css link would become /myAppName/css/login.css.

Usage is like so:

<link type="text/css" href="${resource(dir: 'css', file: 'login.css')}" />

This guarantees that, no matter the controller, action, or other ID information, the link to the CSS file will always be correct.


As for why your UrlMapping isn't working, it's because it isn't dynamically processed. It's processed during application startup, and so the request object doesn't even exist at that time.

If you absolutely need a URL rewriting system, and using a controller to process the resources is too complex for this purpose, I recommend putting your servlet engine (i.e. Tomcat) behind Apache and just using mod_rewrite. Properly designed, however, this really shouldn't be necessary.


Edit

Just for complete information, to get dynamic control of the mapping is to use the syntax specified in Dynamic Controller and Action Names. However, this is somewhat limited, and you still can't use it to perform a redirect.

OverZealous
  • 39,252
  • 15
  • 98
  • 100
  • Thank you - this is great advice, especially the insight that the request object doesn't exist when UrlMappings is processed! As for whether I'm trying to solve the wrong problem, I'm not sure - I am basically trying to "fool" a CDN to refresh its cache by passing it different (versioned) URIs for the same resource, but I wanted the grails application (sitting behind the CDN) to translate, for example: `/pub/cdn/[any number]/css/login.css` to actually pull from `/css/login.css` regardless of the content of [any number]. – drake Jan 30 '12 at 19:44
  • For that, I would probably go the `mod_rewrite` route, if that's possible. The only other suggestion is to try explicit URI remapping, but I don't know it's capabilities regarding wildcards: `"/hello"(uri: "/hello.dispatch")`. It's from the [first section on UrlMappings](http://grails.org/doc/2.0.x/guide/theWebLayer.html#mappingToControllersAndActions). – OverZealous Jan 30 '12 at 19:59