tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: Rewriting entire request /body in Servlet Filter not working as expected
Date Mon, 05 Oct 2015 03:46:31 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Michael,

On 10/4/15 2:03 PM, Michael Greco wrote:
> First time post here.

Welcome!

> Using : Tomcat 8.0.26 JDK1.8.0 update 51 Apache MyFaces 2.2.8. 
> Maven build of webapp war file Chrome  45.0.2454.101 m 64 bit 
> Windows 7 64 bit

Thanks.

> Trying to rewrite the entire request body in a filter using a http
> request wrapper.  Not entirely sure if this is the right approach
> or even possible but researching similar examples it seems this
> approach should work. Created a simple test case with a one page
> test app, expecting the index.xhtml page's text value of  "Welcome"
> to be replaced with the text "NEW TEXT HERE" found in the filter
> code, but I only get "Welcome" back when the page is rendered in
> the browser.

I'm curious about your use-case. Why would you want to re-write the
user's request?

> I even tried to re-read the entire request in the
> getParametersMap() from examples I found on the web, but this
> didn't seen to do the job either.  It seems that the request body
> is somehow cached somewhere / somehow and not changable?

Once Tomcat has consumed the request body -- like for a POST where
some component has called any "getParameter" method on the request --
then Tomcat will return the previously-parsed data. If you don't
re-write the request before then, you'll have no effect, unless the
servlet itself reads the request... which will probably work.

> index page code (index.xhtml) : 
> ----------------------------------------------------------------------
- -----------
>
> 
<!DOCTYPE html>
> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" 
> xmlns:jsf="http://xmlns.jcp.org/jsf" 
> xmlns:h="http://xmlns.jcp.org/jsf/html"> <head jsf:id="head"> <meta
> http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
> <title>Test Webapp</title> </head> <body jsf:id="body"> 
> <div><h1>Welcome</h1></div> </body> </html>

I'm pretty sure the above is going to display "Welcome" no matter what
your Filter does (as long as it doesn't crash or return without
calling down the chain). What were you expecting?

> public void doFilter(ServletRequest request, ServletResponse
> response, FilterChain chain) throws IOException, ServletException
> { String newReqBody = new String("<div><h1>NEW TEXT
> HERE</h1></div>");
> 
> HttpServletRequest req = (HttpServletRequest)request; 
> RewriteBodyRequestWrapper rewriteBodyRequestWrapper = new 
> RewriteBodyRequestWrapper(req, newReqBody);
> 
> chain.doFilter(rewriteBodyRequestWrapper, response); }

[snip]

> class RewriteBodyRequestWrapper extends HttpServletRequestWrapper
> {
> 
> private byte[] buffer;
> 
> public RewriteBodyRequestWrapper(HttpServletRequest req, String
> reqBody) throws IOException { super(req); 
> System.out.println("entering RewriteBodyRequestWrapper()
> constructor"); this.buffer = reqBody.getBytes(); }

You might want to make sure the encoding is right on that getBytes()
call. It's got nothing to do with whatever problem you're having, but
good to think about for the future.

> @Override public ServletInputStream getInputStream() { 
> System.out.println("entering
> RewriteBodyRequestWrapper().getInputStream()"); try { 
> ByteArrayInputStream bais = new ByteArrayInputStream(buffer); 
> BufferedServletInputStream bsis = new
> BufferedServletInputStream(bais); return bsis; } catch (Exception
> ex) { ex.printStackTrace(); throw new RuntimeException(ex); } }
> 
> @Override public BufferedReader getReader() { 
> System.out.println("entering
> RewriteBodyRequestWrapper().getReader()"); InputStreamReader isr =
> new InputStreamReader(getInputStream()); return new
> BufferedReader(isr); }

I think that's about the best you can do. Just make sure that the
Filter is the first possible Filter to run (i.e. is mapped topmost in
web.xml).

> web.xml code: 
> ----------------------------------------------------------------------
- ----------------------------
>
> 
<?xml version="1.0" encoding="ISO-8859-1"?>
> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi=" 
> http://www.w3.org/2001/XMLSchema-instance" 
> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
> http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
> metadata-complete="true" version="3.0">
> 
> <display-name>Test Webapp Req Rewrite</display-name>
> 
> <!-- Change to "Production" when you are ready to deploy --> 
> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name>

> <param-value>Development</param-value> </context-param>
> 
> <!-- JSF mapping --> <servlet> 
> <servlet-name>FacesServlet</servlet-name> 
> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
> <load-on-startup>1</load-on-startup> </servlet>
> 
> <!-- Map these files with JSF --> <servlet-mapping> 
> <servlet-name>FacesServlet</servlet-name> 
> <url-pattern>/faces/*</url-pattern> </servlet-mapping>
> 
> <!-- Session timeout --> <session-config> 
> <session-timeout>30</session-timeout> </session-config>
> 
> <!-- Welcome page --> <welcome-file-list> 
> <welcome-file>faces/index.xhtml</welcome-file> 
> </welcome-file-list>
> 
> <!-- Filters --> <filter> 
> <filter-name>RewriteBodyTestFilter</filter-name> 
> <filter-class>com.testwebapp.reqrewrite.filter.RewriteBodyTestFilter
>
> 
</filter-class>
> </filter>
> 
> <!-- Filter mappings / chain ordering --> <filter-mapping> 
> <filter-name>RewriteBodyTestFilter</filter-name> 
> <servlet-name>FacesServlet</servlet-name> 
> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher>

> </filter-mapping>
> 
> </web-app>

So... what were you expecting here and what did you get?

- -chris
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org

iQIcBAEBCAAGBQJWEfKXAAoJEBzwKT+lPKRYzXQP/0ol5Yuf13WdaPdaqsILcGd5
iP9TLGWhMe/neCosMuPYQ/BLxXym/wMcCuBxxD8U4FO1aX/JRfWB8zIe9JmSnECH
wKutjkKlwOrQS2AtO1EFryYO1yYEO3W29JcK7AJwTPt1y6dQ+daoEK6VVbuRJe3a
cJTe0ZoH220fkgyn6th/IzwrMjbO0WoattcJ9N4pTjoImou9Rj4O84jtHV6PX81w
IcpKfRhuVNDd3aUl551L5tUaPP3UsQAAhmwtDlx2ujme/ThTiURuzg3ATw4buAy1
ugG9rJrfc8dMiP6zfD2gYz1CQVXMa7q1mEfuuCJEXvYf/sW/h6t0mqi5SA7fHr2d
2r3o9AiSdhvMF6JZYQChxLdJvazOA4+25v+JhFR9PsxeBcVKoJsTeIYKmvDm7BGA
8Vlvig2z6O3T0fV1pmdNlLYgJowej0B/8UZY1sVet29etCEoqQagzkaaovn+nqK0
OojNEYwms3tBZNTO2BGFrpQPwpbg6LKoaZJdEDtDthbUu6S2JYNWZE5z936ANmDf
cwvQ7zpNQ/kv0+ySzcErLZF0iJRjdd2AlP2CszygvzaQAky3bQTV7S7TCdfjT1Pa
wsFyHx7ruuGofhlnVlG8FSBgs39fyhSEjIViufPzpxWWTbLftjW5Esevyndk/LyA
Wr8OKqnHT9m7b9lx9An9
=AFHk
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message