Rahul Borkar
Rahul Borkar

Reputation: 2762

Creating Java filter for image requests rewritings

I have created one filter which is working fine for all requests which my application gets, for this I have written new class ResourceRequestWrapper which extends HttpServletRequestWrapper to modify requests.

For example, when request comes for /ui/tx/images/abc.png I have overridden getServletPath(), getRequestURL() and getRequestURI() methods of HttpServletRequestWrapper to modify text of incoming request to /txeditor/images/abc.png.

Now, I have created virtual directory in JBoss/Tomcat by modifying server.xml and adding following,

<Context path="/txeditor" docBase="C:\resources\web_resources\txeditor.war" unpackWAR="false">               
</Context>  

I have also defined WEB-INF in C:\resources\web_resources\txeditor.war and created deployment descriptor as follows,

<?xml version="1.0"?>

<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
   <description>Web descriptor for the html adaptor</description> 
</web-app>

Everything above works fine, every request gets modified, but when my page gets opened I am not able to see any images and I think when we are modifying request from filter to access virtual directory defined in server.xml, its not possible?

Can anybody have other idea?

Upvotes: 1

Views: 980

Answers (1)

BalusC
BalusC

Reputation: 1108782

This is indeed not going to work. You want to redirect the request instead.

String oldUrl = request.getRequestURI();
String newUrl = oldUrl.replaceFirst("/ui/tx/", "/txteditor/");
response.sendRedirect(newUrl);

If you want a permanent (301) redirect, do the following instead of the last line:

response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", newUrl);

Upvotes: 2

Related Questions