parent
5363d298b4
commit
f8fa60ed36
@ -1,21 +0,0 @@
|
||||
package au.com.royalpay.payment.manage;
|
||||
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
public class CacheRequestFilter extends OncePerRequestFilter {
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
||||
if (request instanceof CachedBodyServletRequest) {
|
||||
filterChain.doFilter(request, response);
|
||||
} else {
|
||||
CachedBodyServletRequest wrappedRequest = new CachedBodyServletRequest(request);
|
||||
filterChain.doFilter(wrappedRequest, response);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
package au.com.royalpay.payment.manage;
|
||||
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import javax.servlet.ReadListener;
|
||||
import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletRequestWrapper;
|
||||
import java.io.*;
|
||||
|
||||
public class CachedBodyServletRequest extends HttpServletRequestWrapper {
|
||||
private final byte[] content;
|
||||
|
||||
public CachedBodyServletRequest(HttpServletRequest request) throws IOException {
|
||||
super(request);
|
||||
try (InputStream in = request.getInputStream()) {
|
||||
content = StreamUtils.copyToByteArray(in);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServletInputStream getInputStream() throws IOException {
|
||||
return new CachedBodyServletInputStream(content);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BufferedReader getReader() throws IOException {
|
||||
ByteArrayInputStream boi = new ByteArrayInputStream(content);
|
||||
return new BufferedReader(new InputStreamReader(boi));
|
||||
}
|
||||
|
||||
public static class CachedBodyServletInputStream extends ServletInputStream {
|
||||
private final InputStream delegate;
|
||||
|
||||
public CachedBodyServletInputStream(byte[] content) {
|
||||
this.delegate = new ByteArrayInputStream(content);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFinished() {
|
||||
try {
|
||||
return delegate.available() == 0;
|
||||
} catch (IOException e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReady() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReadListener(ReadListener readListener) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
return delegate.read();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue