All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tosan.http.server.starter.wrapper.CustomHttpServletRequestWrapper Maven / Gradle / Ivy

package com.tosan.http.server.starter.wrapper;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;

/**
 * @author M.khoshnevisan
 * @since 4/21/2021
 */
public class CustomHttpServletRequestWrapper extends HttpServletRequestWrapper {
    private ByteArrayOutputStream cachedBytes;

    public CustomHttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    @Override
    public CachedServletInputStream 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 {
        cachedBytes = new ByteArrayOutputStream();
        copy(super.getInputStream(), cachedBytes);
    }

    private void copy(InputStream source, OutputStream target) throws IOException {
        byte[] buf = new byte[8192];
        int length;
        while ((length = source.read(buf)) != -1) {
            target.write(buf, 0, length);
        }
    }

    public class CachedServletInputStream extends ServletInputStream {
        private ByteArrayInputStream input;
        byte[] byteArray = cachedBytes.toByteArray();

        public CachedServletInputStream() {
            input = new ByteArrayInputStream(byteArray);
        }

        public byte[] getInputByteArray() {
            return byteArray;
        }

        @Override
        public int read() {
            return input.read();
        }

        @Override
        public boolean isFinished() {
            return false;
        }

        @Override
        public boolean isReady() {
            return false;
        }

        @Override
        public void setReadListener(ReadListener listener) {
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy