Created
March 7, 2021 16:26
-
-
Save njofce/03a2b2f5fb2b4f56b10dc4251c9aa128 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.filters; | |
import java.io.BufferedInputStream; | |
import java.io.BufferedReader; | |
import java.io.ByteArrayInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.StringReader; | |
import java.nio.charset.Charset; | |
import javax.servlet.ReadListener; | |
import javax.servlet.ServletInputStream; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletRequestWrapper; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class CustomHttpRequestWrapper extends HttpServletRequestWrapper { | |
private static final Logger logger = LoggerFactory.getLogger(CustomHttpRequestWrapper.class); | |
private final String bodyInStringFormat; | |
public CustomHttpRequestWrapper(HttpServletRequest request) throws IOException { | |
super(request); | |
bodyInStringFormat = readInputStreamInStringFormat(request.getInputStream(), Charset.forName(request.getCharacterEncoding())); | |
logger.info("Body: {}", bodyInStringFormat); | |
} | |
private String readInputStreamInStringFormat(InputStream stream, Charset charset) throws IOException { | |
final int MAX_BODY_SIZE = 1024; | |
final StringBuilder bodyStringBuilder = new StringBuilder(); | |
if (!stream.markSupported()) { | |
stream = new BufferedInputStream(stream); | |
} | |
stream.mark(MAX_BODY_SIZE + 1); | |
final byte[] entity = new byte[MAX_BODY_SIZE + 1]; | |
final int bytesRead = stream.read(entity); | |
if (bytesRead != -1) { | |
bodyStringBuilder.append(new String(entity, 0, Math.min(bytesRead, MAX_BODY_SIZE), charset)); | |
if (bytesRead > MAX_BODY_SIZE) { | |
bodyStringBuilder.append("..."); | |
} | |
} | |
stream.reset(); | |
return bodyStringBuilder.toString(); | |
} | |
@Override | |
public BufferedReader getReader() throws IOException { | |
return new BufferedReader(new InputStreamReader(getInputStream())); | |
} | |
@Override | |
public ServletInputStream getInputStream () throws IOException { | |
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bodyInStringFormat.getBytes()); | |
return new ServletInputStream() { | |
private boolean finished = false; | |
@Override | |
public boolean isFinished() { | |
return finished; | |
} | |
@Override | |
public int available() throws IOException { | |
return byteArrayInputStream.available(); | |
} | |
@Override | |
public void close() throws IOException { | |
super.close(); | |
byteArrayInputStream.close(); | |
} | |
@Override | |
public boolean isReady() { | |
return true; | |
} | |
@Override | |
public void setReadListener(ReadListener readListener) { | |
throw new UnsupportedOperationException(); | |
} | |
public int read () throws IOException { | |
int data = byteArrayInputStream.read(); | |
if (data == -1) { | |
finished = true; | |
} | |
return data; | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment