77 lines
2.0 KiB
Java
77 lines
2.0 KiB
Java
package net.gepafin.tendermanagement.config;
|
|
|
|
import jakarta.servlet.ReadListener;
|
|
import jakarta.servlet.ServletInputStream;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import jakarta.servlet.http.HttpServletRequestWrapper;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
|
|
public class CachedBodyHttpServletRequest extends HttpServletRequestWrapper {
|
|
|
|
private final byte[] cachedBody;
|
|
|
|
public CachedBodyHttpServletRequest(HttpServletRequest request) throws IOException {
|
|
|
|
super(request);
|
|
InputStream requestInputStream = request.getInputStream();
|
|
this.cachedBody = requestInputStream.readAllBytes();
|
|
}
|
|
|
|
@Override
|
|
public ServletInputStream getInputStream() {
|
|
|
|
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(cachedBody);
|
|
return new ServletInputStreamWrapper(byteArrayInputStream);
|
|
}
|
|
|
|
@Override
|
|
public BufferedReader getReader() throws IOException {
|
|
|
|
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(cachedBody);
|
|
return new BufferedReader(new InputStreamReader(byteArrayInputStream));
|
|
}
|
|
|
|
public String getCachedBodyAsString() {
|
|
|
|
return new String(cachedBody);
|
|
}
|
|
|
|
private static class ServletInputStreamWrapper extends ServletInputStream {
|
|
|
|
private final ByteArrayInputStream byteArrayInputStream;
|
|
|
|
public ServletInputStreamWrapper(ByteArrayInputStream byteArrayInputStream) {
|
|
|
|
this.byteArrayInputStream = byteArrayInputStream;
|
|
}
|
|
|
|
@Override
|
|
public int read() throws IOException {
|
|
|
|
return byteArrayInputStream.read();
|
|
}
|
|
|
|
@Override
|
|
public boolean isFinished() {
|
|
|
|
return byteArrayInputStream.available() == 0;
|
|
}
|
|
|
|
@Override
|
|
public boolean isReady() {
|
|
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public void setReadListener(ReadListener readListener) {
|
|
|
|
}
|
|
}
|
|
}
|