Java tutorial
/* * Copyright 2016 Intuit * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.intuit.autumn.web; import javax.servlet.ReadListener; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import java.io.ByteArrayInputStream; import java.io.IOException; import static org.apache.commons.io.IOUtils.toByteArray; /** * Wrapper object around HttpServletRequest to allow multiple readings of InputStream */ public class InputStreamHttpServletRequestWrapper extends HttpServletRequestWrapper { private final byte[] body; /** * Constructs a request object wrapping the given request. * * @param request inbound request * @throws IllegalArgumentException if the request is null */ public InputStreamHttpServletRequestWrapper(HttpServletRequest request) throws IOException { super(request); body = toByteArray(request.getInputStream()); } /** * Return wrapped intput stream * * @return invalid response stream * @throws IOException unintended exception */ @Override public ServletInputStream getInputStream() throws IOException { final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body); return new ServletInputStream() { @Override public boolean isFinished() { return byteArrayInputStream.available() == 0; } @Override public boolean isReady() { return true; } @Override public void setReadListener(ReadListener readListener) { throw new UnsupportedOperationException("Not implemented"); } @Override public int read() throws IOException { return byteArrayInputStream.read(); } }; } }