Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

getRequestURI() in extended HttpServletRequestWrapper isn't called

843842Jun 12 2010
Hello,

i wrote a simple Filter that shall replace parts in a called URL.
Therefore i've created a HttpServletRequest-Wrapper which overrides the getRequestURI() method, and give that wrapper as argument to chain.doFilter(...) method.

When i call a URL, that has a path to a file which doesn't exist in Web Applikation (Context), the getRequestURI() method in the wrapper-object is called.

When i call a URL, that has a path to a file which exists in Web Applikation (Context), the getRequestURI() method in the wrapper-object won't be called.

What's wrong here ? Why the Servlet-Container ignores calling of getRequestURI()-method in wrapper-object for files that do exist in Web Applikation (Context) ?

Web Container used: Tomcat 6.0.18

URL: http://localhost:8080/ServletRequestFilterExample/testdds does work -> getRequestURI() method in wrapper-object is called
URL: http://localhost:8080/ServletRequestFilterExample/test.hml doesn't work -> getRequestURI() method in wrapper-object isn't called
http://localhost:8080/ServletRequestFilterExample/test.jpg doesn't work -> getRequestURI() method in wrapper-object isn't called

Web Application Structure (Dynamic Web Project in Eclipse):

WebContent
|
|---META-INF
|--- MANIFEST.MF
|---WEB-INF
|----- lib
|----- web.xml
|
|---- test.html
|---- test.jpg

test.html and test.jpg are put in ROOT-Directory of Web Application.


web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ServletRequestFilterExample</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
	<filter-name>UrlRewriteFilter</filter-name>
	<filter-class>urlRewriterFilter.MyUrlRewriteFilter</filter-class>  
  </filter>

  <filter-mapping>
	<filter-name>UrlRewriteFilter</filter-name>
	<url-pattern>/*</url-pattern>  
  </filter-mapping>

</web-app>
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class MyUrlRewriteFilter implements Filter {

	private FilterConfig fc;
	
	public void init(FilterConfig filterConfig) throws ServletException {
		this.fc = filterConfig;
	}
	
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest req = (HttpServletRequest) request;
		UrlRewriteWrapper urlRwW = new UrlRewriteWrapper(req, req.getRequestURI());

                chain.doFilter(urlRwW, response);
	}

	
	public void destroy() {	}
	
}
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class UrlRewriteWrapper extends HttpServletRequestWrapper {

private String requestUri;

public UrlRewriteWrapper(HttpServletRequest request, String uri) {
super(request);
this.requestUri = uri;
}

@Override
public String getMethod() {
HttpServletRequest request = (HttpServletRequest) getRequest();
return request.getMethod();

}

@Override
public String getRequestURI() {

// get the request object and cast it
HttpServletRequest request = (HttpServletRequest) getRequest();
System.out.println("before replace .jpg: " + this.requestUri);
if(requestUri.endsWith(".jpg")) {

requestUri = requestUri.replaceAll(".jpg", ".pdf");
System.out.println("inside replace .jpg" + requestUri);
return requestUri;

}else{
// otherwise return the original request uri / fall through to wrapped request object
return request.getRequestURI();
}
}

@Override
public String getHeader(String name) {
HttpServletRequest request = (HttpServletRequest) getRequest();
return request.getHeader(name);
}


}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 10 2010
Added on Jun 12 2010
0 comments
645 views