Query about wrapper class
Hi All,
I am trying to understand the use of wrapper class. I am using filter example and here is a sample code
Filter Class
Code:
public class XssFilter implements Filter {
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(new XssRequestWrapper((HttpServletRequest)request),
response);
}
}
Wrapper Class
Code:
public final class XssRequestWrapper extends HttpServletRequestWrapper {
public String[] getParameterValues(String paramString) {
//do something
}
public String getParameter(String paramString) {
//do something
}
public String getHeader(String paramString) {
//do something
}
}
If we have mapping for '/*' to call this filter then how it works. Will it call all three get methods in wrapper class or we have to expilicty call one we have to use?
Thanks
Re: Query about wrapper class
From your code above we don't know whether it call one, two or three methods you defined above. Because we can only see that you create a new instance of XssRequestWrapper which extends HttpServletRequestWrapper. Unless we know what both of this class do then we cannot tell anything. But because you might have to code there you can see what is happening there. Take a look starting from the XssRequestWrapper constructor.
Re: Query about wrapper class
When I debug the program what I saw was getHeader() and getParameter() methods are automatically called without being called explicitly in a code.
So I am trying to understand why this happen. I don't have any thing extra in XssRequestWrapper constructor rather than a single line to call super(request).
Can anyone elaborate why this happening?
Re: Query about wrapper class
I don't have any idea because I don't know what is in that classes. What part of library if these classes? If it is an open source library then you'd better check to see the source code.
Re: Query about wrapper class
No this is similar to this example
Re: Query about wrapper class
Quote:
Originally Posted by
anjibman
When I debug the program what I saw was getHeader() and getParameter() methods are automatically called without being called explicitly in a code.
So I am trying to understand why this happen. I don't have any thing extra in XssRequestWrapper constructor rather than a single line to call super(request).
Can anyone elaborate why this happening?
Basically, the Filter that you create will be executed before your application read the http headers or the http parameters. As you can see when you call the doChain() method in the filter you wrap the servlet request with the XssRequestWrapper. This mean that to actual HttpServletRequest read by your application is wrapped in XssRequestWrapper. So the call to HttpServletRequest.getParameter() in your application will actually call the XssRequestWrapper.getParameter().