Hello,
I'm stuck here.
I have a Filter: XslTransformFilter that catches the output from what it is filtering
and then performs an xsl transformation on that output.
I catch the output with these 2 classes:
class ResponseCatcher extends HttpServletResponseWrapper {
private CharArrayWriter output;
public ResponseCatcher(HttpServletResponse response) {
super(response);
output=new CharArrayWriter();
}
public String getOutput() {
try{ output.write("getOutput called");} catch(Exception e) {}
this.output.flush();
return this.output.toString();
}
public PrintWriter getWriter() {
try {
output.write("getWriter called"); } catch(Exception e) {}
return new PrintWriter(output);
}
public ServletOutputStream getOutputStream() {
try{ output.write("getOutputStream called+");} catch(Exception e) {}
return new ServletOutputStreamWrapper(output);
}
public void flushBuffer() throws IOException {
}
}
class ServletOutputStreamWrapper extends ServletOutputStream {
private CharArrayWriter writer;
public ServletOutputStreamWrapper(CharArrayWriter writer) {
this.writer=writer;
}
public void write(int c) {
writer.write(c);
}
public void flush() throws IOException {
writer.flush();
}
}
The code in the doFilter and transform methods:
/**
* Will start the transformation chain.
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
ResponseCatcher responseWrapper=new ResponseCatcher((HttpServletResponse)response);
chain.doFilter(request, responseWrapper);
transform(responseWrapper.getOutput(), response);
}
/**
* This method performs the transformation and sends the output to the outputstream
*/
public void transform(String xmlString, ServletResponse response) throws ServletException, IOException {
System.out.println("XslTransformFilter "+xslFile+" transform called");
// response.setContentLength( ? )
OutputStream output=response.getOutputStream();
StreamResult streamResult=new StreamResult(output);
try {
TransformerFactory tFactory=TransformerFactory.newInstance();
Source xmlSource=new StreamSource(new StringReader(xmlString));
Source xslSource=new StreamSource(xslFile);
Transformer transformer = tFactory.newTransformer(xslSource);
transformer.transform(xmlSource, streamResult);
response.flushBuffer();
} catch(TransformerException te) {
System.err.println(te);
}
}
The problem is that i do'nt know what the content length of the output in the transformation wil be.
How can i get that content length???
// response.setContentLength( ? )