I have the below code using which I am trying to fetch and print the request stream's entities using ContainerRequestContext. However, its breaking the API call after I read the input stream. I am able to fetch other details from "ContainerRequestContext" using getters such as responseContext.getURiInfo() etc. Please note I dont want to use "aroundReadFrom" of "ReaderInterceptor" and Lombok anywhere. Any help is appreciated. Thanks !!
UPDATE: The request payload is "x-www-form-urlencoded" and how do I set the request payload back to "x-www-form-urlencoded" after reading it ? It might be the reason why the request fails after I read the payload. I might not be setting the "setEntityStream()" correctly.
public String getRequestEntityStream(ContainerRequestContext requestContext) throws IOException {
try (InputStream in = requestContext.getEntityStream()) {
ByteArrayOutputStream out = new ByteArrayOutputStream(64 * 1024);
IOUtils.copy(in, out);
byte[] entity = out.toByteArray();
//restore input
requestContext.setEntityStream(new ByteArrayInputStream(entity));
String str=new String(entity);
return str;
}
}
I am using the above as below to append to StringBuilder :
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
StringBuilder sbRequest = new StringBuilder();
sbRequest.append(" - Request Entity Stream : ").append(getRequestEntityStream(requestContext));
}