We have a legacy Spring based REST Application. We are planning to migrate this application to Quarkus.
As part of the application we have a Servlet Filter (BasicInputValidationFilter)
that reads the input payload.
We pass the request to a servlet request wrapper (ServletRequestWrapper)
that reads the data from request input stream and stores it. (We have to store it since the data from input stream can be read only once.) So later the REST Endpoint needs to read it again, so we use the implementation of HttpServletRequestWrapper (ServletRequestWrapper)
Later we pass the same ServletRequestWrapper
(instead of original Servlet Request) to next filter in the chain
.
public class BasicInputValidationFilter implements javax.servlet.Filter {
@Override
public void doFilter(ServletRequest inRequest, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
ServletRequest request = null;
if (inRequest.getContentType()!=null && (!inRequest.getContentType().toLowerCase(Locale.ENGLISH).startsWith("multipart/")
&& !inRequest.getContentType().toLowerCase(Locale.ENGLISH).contains("octet-stream"))) {
HttpServletRequest httpServletRequest = (HttpServletRequest) inRequest;
request = new ServletRequestWrapper(httpServletRequest);
String dataPayload = ((ServletRequestWrapper)request).getDataPayload();
//Some Input Validation Logic to validate dataPayload.
} else {
request = inRequest;
}
filterChain.doFilter(request, response);
}
}
This is the class ServletRequestWrapper that read the input stream data from request in the constructor and stores it. Laters whenever someone calls getInputStream() again it returns the same stored data again.
public class ServletRequestWrapper extends javax.servlet.http.HttpServletRequestWrapper {
private final String dataPayload;
public ServletRequestWrapper(HttpServletRequest request) throws RequestWrapperException, IOException {
super(request);
InputStream inputStream = request.getInputStream();
if (inputStream == null) {
dataPayload = null;
return;
}
dataPayload = IOUtils.toString(inputStream);
}
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(getDataPayload().getBytes());
return new ServletInputStream() {
//Code to return ByteArrayInputStream as ServletInputStream
};
}
public String getDataPayload() {
return dataPayload;
}
}
I am aware of ContainerRequestFilter in Quarkus
that can replace javax.servlet.Filter
but how can I replace the request that is received within the filter, with my custom request.