I have a servlet response filter. My response body is not modified for successful requests. Exceptions are successful wrapped and response body is modified. My code is mere copy/paste from the book O'Reilly Learning Java 4th edition code found at https://learning.oreilly.com/library/view/learning-java-4th/9781449372477/ch15s04.html#:-:text=Filtering%20the%20Servlet%20Response
Here is the code from the book with minor modifications for my requirement-
public class LinkResponseFilter implements Filter {
FilterConfig filterConfig;
public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
WrappedResponse wrappedResponse = new WrappedResponse((HttpServletResponse) res);
chain.doFilter(req, wrappedResponse);
wrappedResponse.close();
}
public void destroy() {
}
static class WrappedResponse extends HttpServletResponseWrapper {
boolean linkText;
PrintWriter client;
WrappedResponse(HttpServletResponse res) {
super(res);
}
public void setContentType(String mime) {
super.setContentType(mime);
if (mime.startsWith("application/fhir+json")) { //BOOK CODE CHECKS FOR html
linkText = true;
}
}
public PrintWriter getWriter() throws IOException {
if (client == null) {
if (linkText) {
client = new LinkWriter(super.getWriter(), new ByteArrayOutputStream());
} else {
client = super.getWriter();
}
}
return client;
}
void close() {
if (client != null) {
client.close();
}
}
}
static class LinkWriter extends PrintWriter {
ByteArrayOutputStream buffer;
Writer client;
LinkWriter(Writer client, ByteArrayOutputStream buffer) {
super(buffer);
this.buffer = buffer;
this.client = client;
}
public void close() {
try {
flush();
client.write(linkText(buffer.toString()));
client.close();
} catch (IOException e) {
setError();
}
}
String linkText(String text) {
text = text.replaceAll( "resourceType", "None");//BOOK HAS DIFFERENT LOGIC
return text;
}
}
}
From the book itself it clearly mentioned
which means when we set the
linkText = truefor the contentType"application/fhir+json"then thegetWriter()method will return the same object which means the WrappedResponse will not happen to the successful responses which has the contentType"application/fhir+json".As I asked previously have you gone through the debug mode for successful and failure .
linkTextis the culprit in your case . Do the debug and you will came to a conclusion