Created
October 9, 2014 15:47
-
-
Save erichsend/5475b05065139ce9908d to your computer and use it in GitHub Desktop.
input stream copy
This file contains 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
/** | |
* Class which is used to wrap a request in order that the wrapped request's input stream can be | |
* read once and later be read again in a pseudo fashion by virtue of keeping the original payload | |
* as a string which is actually what is returned by subsequent calls to getInputStream(). | |
*/ | |
public class AuthenticationRequestWrapper | |
extends HttpServletRequestWrapper { | |
private static Logger log = Logger.getLogger(AuthenticationRequestWrapper.class.getName()); | |
private final String xmlPayload; | |
public AuthenticationRequestWrapper (HttpServletRequest request) | |
throws AuthenticationException { | |
super(request); | |
// read the original payload into the xmlPayload variable | |
StringBuilder stringBuilder = new StringBuilder(); | |
BufferedReader bufferedReader = null; | |
try { | |
// read the payload into the StringBuilder | |
InputStream inputStream = request.getInputStream(); | |
if (inputStream != null) { | |
bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); | |
char[] charBuffer = new char[128]; | |
int bytesRead = -1; | |
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { | |
stringBuilder.append(charBuffer, 0, bytesRead); | |
} | |
} else { | |
// make an empty string since there is no payload | |
stringBuilder.append(""); | |
} | |
} catch (IOException ex) { | |
log.error("Error reading the request payload", ex); | |
throw new AuthenticationException("Error reading the request payload", ex); | |
} finally { | |
if (bufferedReader != null) { | |
try { | |
bufferedReader.close(); | |
} catch (IOException iox) { | |
// ignore | |
} | |
} | |
} | |
xmlPayload = stringBuilder.toString(); | |
} | |
/** | |
* Override of the getInputStream() method which returns an InputStream that reads from the | |
* stored XML payload string instead of from the request's actual InputStream. | |
*/ | |
@Override | |
public ServletInputStream getInputStream () | |
throws IOException { | |
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xmlPayload.getBytes()); | |
ServletInputStream inputStream = new ServletInputStream() { | |
public int read () | |
throws IOException { | |
return byteArrayInputStream.read(); | |
} | |
}; | |
return inputStream; | |
} | |
} | |
public void doFilter (ServletRequest servletRequest, | |
ServletResponse servletResponse, | |
FilterChain filterChain) | |
throws IOException, ServletException { | |
// perform request filtering | |
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; | |
AuthenticationRequestWrapper authenticationRequestWrapper; | |
try { | |
authenticationRequestWrapper = new AuthenticationRequestWrapper(httpServletRequest); | |
} catch (AuthenticationException ex) { | |
log.warn("Unable to wrap the request", ex); | |
throw new ServletException("Unable to wrap the request", ex); | |
} | |
try { | |
requestAuthenticator.authenticateRequest(authenticationRequestWrapper); | |
} catch (Exception ex) { | |
log.warn("Invalid request", ex); | |
throw new ServletException("Invalid request", ex); | |
} | |
// continue the filter chain | |
filterChain.doFilter(authenticationRequestWrapper, servletResponse); | |
// perform response filtering | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment