ccleve
ccleve

Reputation: 15779

Doing a URL rewrite in a ServletFilter using Jetty

I need to do a URL rewrite in a ServletFilter so that "foo.domain.com" gets rewritten to "foo.domain.com/foo". I'm using Jetty, which has a handy way of modifying requests: just cast the request to a Jetty Request object and you get a bunch of setters which allow you to modify it. Here's my code (which doesn't work):

String subdom = Util.getSubDomain(req);
org.eclipse.jetty.server.Request jettyReq = (Request) req;
String oldUri = jettyReq.getRequestURI();
String newUri = "/" + subdom + oldUri;
jettyReq.setRequestURI(newUri);

My purpose is to serve files out of the /foo directory, which lives at /webapps/root/foo.

I'm guessing that I also need to call things like setContextPath(), setPathInfo(), setURI(), setServletPath(), and who knows what else.

What's the magic combination that will make it look like the original request was for /foo?

Edit: to clarify, the reason I say the code doesn't work is that files are still being served out of /webapps/root, not /webapps/root/foo.

Upvotes: 0

Views: 1656

Answers (2)

ccleve
ccleve

Reputation: 15779

Answering my own question: I was missing

jettyReq.setServletPath(newUri); 

Add that and everything works.

Upvotes: 1

jesse mcconnell
jesse mcconnell

Reputation: 7182

Just use the rewrite handler, we have support for what you're trying to do:

http://wiki.eclipse.org/Jetty/Feature/Rewrite_Handler

Upvotes: 1

Related Questions