You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by paul <pa...@roadrunner.uk.com> on 2001/10/08 12:26:39 UTC

Using filters For XML translation

I would like to use the Filters of tomcat to implement XML translation.

I create a class that implements the Filter interface and then
after calling

        chain.dofilter(request, response)

inside the doFilter(request,response,chain) method

the response will contain my XML.

As the XML is in an OutputStream obtained by response.getOutputStream how do
I read it into my XML parser or translator?

Do I use piped io or is there some other way of doing this that I am
missing?

Thanks for any help







Bug? Changing tags & jsp recompilation

Posted by Jim Cheesman <jc...@msl.es>.
Hi all,

I've been forced to change the structure of the data that I'm using 
from  String[] to Object[] - which forces a futher change in the tag I'm 
using to iterate over this data. (The Object[] are contained in an ArrayList.)

The result of this change on the tag is that the TEI has had to change from 
"String[]" to "Object[]" in the getVariableInfo method (see below):

public class HTMLGridTagDataTEI extends TagExtraInfo {
   /**
    *  Gets the VariableInfo
    *
    *@param  data  the TagData
    *@return       The VariableInfo
    */
   public VariableInfo[] getVariableInfo(TagData data) {
     return new VariableInfo[]{
       new VariableInfo(data.getAttributeString("dataName"),
       "Object[]",
       true,
       VariableInfo.NESTED)
       };
   }


All the classes compiled correctly, but... It didn't work. Catalina gave me 
a ClassCastException when I tried to load a jsp that used this tag. After 
searching/correcting the jsp, it still didn't work. It was only when I 
deleted the contents of %CATALINA_HOME%/work that it would do so.

Given that jsp reloading has worked correctly up to this point - Is this a 
bug? Is this part of the specification?




Jim

--

                           *   Jim Cheesman   *
             Trabajo: 
jchees@msl.es - (34)(91) 724 9200 x 2360
    I have this nagging fear that 
everyone is out to make me paranoid.



Re: Using filters For XML translation

Posted by paul <pa...@roadrunner.uk.com>.
thanks for the answer Graig

I will search for the tech tip

meanwhile  i copied the code below for my filter class .. is this O.K.

but... I keep getting an illegalState Exception about the getOutputStream()
see my other posting



import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.ByteArrayOutputStream;


public class FilterTest implements Filter {


  public FilterTest() {
  }

  public void init(FilterConfig config) throws ServletException {

    System.out.println("..............................INITING filters");
  }

  public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
              throws IOException, ServletException {

    System.out.println("..............................doing filters");
    out = response.getOutputStream();
    ResponseWrapper myResponse = new
ResponseWrapper((HttpServletResponse)response);
    chain.doFilter(request,response);
    out.close();

  }

  public void destroy(){
  }

  class ResponseWrapper extends HttpServletResponseWrapper {

    private ByteArrayOutputStream output;
    private int contentLength;
    private String contentType;
    public ResponseWrapper(HttpServletResponse response) {
      super(response);
      output = new ByteArrayOutputStream();
    }
    public byte[] getData() {
      return output.toByteArray();
    }
    public ServletOutputStream getOutputStream(){
      return new FilterServletOutputStream(output);
    }
    public PrintWriter getWrite() {
      return new PrintWriter(getOutputStream(),true);
    }
    public void setContentLength(int length) {
      this.contentLength = length;
      super.setContentLength(length);
    }
    public int getContentLength(){
      return contentLength;
    }
    public void setcontentType(String type){
      this.contentType = type;
      super.setContentType(type);
    }
    public String getContentType(){
      return contentType;
    }

  }//end of class ResponseWrapper

  class FilterServletOutputStream extends ServletOutputStream {

    private DataOutputStream stream;
    public FilterServletOutputStream() {
    }
    public FilterServletOutputStream(OutputStream output) {
      stream = new DataOutputStream(output);
    }
    public void write(int b) throws java.io.IOException {
      stream.write(b);
    }
    public void write(byte[] b) throws java.io.IOException {
      stream.write(b);
    }
    public void write(byte[] b, int off, int len) throws java.io.IOException
{
      stream.write(b, off, len);
    }
  }//end of class FilterServletOutputStream






----- Original Message -----
From: "Craig R. McClanahan" <cr...@apache.org>
To: <to...@jakarta.apache.org>
Sent: Monday, October 08, 2001 4:57 PM
Subject: Re: Using filters For XML translation


>
>
> On Mon, 8 Oct 2001, paul wrote:
>
> > Date: Mon, 8 Oct 2001 11:26:39 +0100
> > From: paul <pa...@roadrunner.uk.com>
> > Reply-To: tomcat-user@jakarta.apache.org
> > To: tomcat-user@jakarta.apache.org
> > Subject: Using filters For XML translation
> >
> > I would like to use the Filters of tomcat to implement XML translation.
> >
> > I create a class that implements the Filter interface and then
> > after calling
> >
> >         chain.dofilter(request, response)
> >
> > inside the doFilter(request,response,chain) method
> >
> > the response will contain my XML.
> >
> > As the XML is in an OutputStream obtained by response.getOutputStream
how do
> > I read it into my XML parser or translator?
> >
> > Do I use piped io or is there some other way of doing this that I am
> > missing?
> >
>
> There was a "Tech Tip" on the Java Developer Connection recently
> (http://developer.java.sun.com) that implemented precisely this concept -
> doing an XSLT transformation in the filter on the response generated by
> the servlet.
>
> The trick is that you have to wrap the response that the filter passes on
> so that you can capture the output.  What happens is that, even though the
> servlet "thinks" it is writing to the client, it's really getting buffered
> inside your filter's wrapper so that you can post-process it when the
> servlet completes.
>
> > Thanks for any help
> >
> >
>
> Craig McClanahan
>


Re: Using filters For XML translation

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Mon, 8 Oct 2001, paul wrote:

> Date: Mon, 8 Oct 2001 11:26:39 +0100
> From: paul <pa...@roadrunner.uk.com>
> Reply-To: tomcat-user@jakarta.apache.org
> To: tomcat-user@jakarta.apache.org
> Subject: Using filters For XML translation
>
> I would like to use the Filters of tomcat to implement XML translation.
>
> I create a class that implements the Filter interface and then
> after calling
>
>         chain.dofilter(request, response)
>
> inside the doFilter(request,response,chain) method
>
> the response will contain my XML.
>
> As the XML is in an OutputStream obtained by response.getOutputStream how do
> I read it into my XML parser or translator?
>
> Do I use piped io or is there some other way of doing this that I am
> missing?
>

There was a "Tech Tip" on the Java Developer Connection recently
(http://developer.java.sun.com) that implemented precisely this concept -
doing an XSLT transformation in the filter on the response generated by
the servlet.

The trick is that you have to wrap the response that the filter passes on
so that you can capture the output.  What happens is that, even though the
servlet "thinks" it is writing to the client, it's really getting buffered
inside your filter's wrapper so that you can post-process it when the
servlet completes.

> Thanks for any help
>
>

Craig McClanahan