Created
June 26, 2014 01:38
-
-
Save pentaho-nbaker/1829d575e69ac5873656 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.pentaho.platform.web.http.request; | |
| import org.apache.commons.io.IOUtils; | |
| import javax.servlet.ServletInputStream; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletRequestWrapper; | |
| import java.io.BufferedReader; | |
| import java.io.ByteArrayInputStream; | |
| import java.io.ByteArrayOutputStream; | |
| import java.io.IOException; | |
| import java.io.InputStreamReader; | |
| /** | |
| * @author Rowell Belen | |
| * | |
| * see http://stackoverflow.com/questions/10210645/http-servlet-request-lose-params-from-post-body-after-read-it-once | |
| */ | |
| public class MultiReadHttpServletRequest extends HttpServletRequestWrapper { | |
| private byte[] cachedBytes; | |
| public MultiReadHttpServletRequest( HttpServletRequest request ) { | |
| super( request ); | |
| } | |
| @Override | |
| public ServletInputStream getInputStream() throws IOException { | |
| if ( cachedBytes == null ) { | |
| cacheInputStream(); | |
| } | |
| return new CachedServletInputStream(); | |
| } | |
| @Override | |
| public BufferedReader getReader() throws IOException { | |
| return new BufferedReader( new InputStreamReader( getInputStream() ) ); | |
| } | |
| private void cacheInputStream() throws IOException { | |
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
| IOUtils.copy( super.getInputStream(), out ); | |
| cachedBytes = out.toByteArray(); | |
| } | |
| /* An inputstream which reads the cached request body */ | |
| public class CachedServletInputStream extends ServletInputStream { | |
| private ByteArrayInputStream input; | |
| public CachedServletInputStream() { | |
| /* create a new input stream from the cached request body */ | |
| input = new ByteArrayInputStream( cachedBytes ); | |
| } | |
| @Override | |
| public int read() throws IOException { | |
| return input.read(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment