Java tutorial
/* * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you 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 org.wso2.carbon.identity.captcha.util; 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.IOException; import java.io.InputStream; import java.io.InputStreamReader; /** * Captcha Response Wrapper. */ public class CaptchaHttpServletRequestWrapper extends HttpServletRequestWrapper { private byte[] body; public CaptchaHttpServletRequestWrapper(HttpServletRequest httpServletRequest) throws IOException { super(httpServletRequest); // Read the request body and save it as a byte array InputStream is = super.getInputStream(); body = IOUtils.toByteArray(is); } @Override public ServletInputStream getInputStream() throws IOException { return new ServletInputStreamImpl(new ByteArrayInputStream(body)); } @Override public BufferedReader getReader() throws IOException { String enc = getCharacterEncoding(); if (enc == null) enc = "UTF-8"; return new BufferedReader(new InputStreamReader(getInputStream(), enc)); } private class ServletInputStreamImpl extends ServletInputStream { private InputStream is; public ServletInputStreamImpl(InputStream is) { this.is = is; } public int read() throws IOException { return is.read(); } public boolean markSupported() { return false; } public synchronized void mark(int i) { throw new RuntimeException(new IOException("mark/reset not supported")); } public synchronized void reset() throws IOException { throw new IOException("mark/reset not supported"); } } }