I am trying to read the FreemarkerView rendering result:
View view = viewResolver.resolveViewName(viewName, locale);
view.render(model, request, mockResponse);
To read the result, I created mockResponseone that encapsulates the HttpServletResponse:
public class HttpServletResponseEx extends HttpServletResponseWrapper {
ServletOutputStream outputStream;
public HttpServletResponseEx(HttpServletResponse response) throws IOException {
super(response);
outputStream = new ServletOutputStreamEx();
}
@Override
public ServletOutputStream getOutputStream() {
return outputStream;
}
@Override
public PrintWriter getWriter() throws IOException {
return new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"));
}
}
And also my ServletOutputStream, which builds a String using StringBuilder:
public class ServletOutputStreamEx extends ServletOutputStream {
StringBuilder stringBuilder;
public ServletOutputStreamEx() {
this.stringBuilder = new StringBuilder();
}
@Override
public void write(int b) throws IOException {
}
@Override
public void write(byte b[], int off, int len) throws IOException {
stringBuilder.append(new String(b, "UTF-8"));
}
@Override
public String toString() {
return stringBuilder.toString();
}
}
With these, I can easily read the answer using the method ServletOutputStreamEx.toString.
My problem is that the write method is not called in the correct order, and in the end the final line is mixed, not in the correct order . This is probably caused by concurrency in Freemarker, but I have no idea how to fix it.
source
share