Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

XSL Transform Filter With ResponseWrapper setContentLength Problem

843841Sep 18 2005 — edited Sep 21 2005
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( ? ) 
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 19 2005
Added on Sep 18 2005
3 comments
116 views