tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jacob Kjome <h...@visi.com>
Subject GZIP filter problem....
Date Tue, 07 Jan 2003 01:22:15 GMT

I'm trying to use a GZIP servlet filter under Tomcat-4.1.18.  I am basing this filter on
an existing example at Orion (
http://www.orionserver.com/tutorials/filters/5.html )

It GZIPs fine and, in my debugging, I can decompress the data back to
what it was originally (more on that below).  The problem
is, all I get when I send the data out to the browser is
non-decompressed garbled data.  That is, the browser doesn't seem to understand that it is
gzip'ed stream and doesn't decode it.  Here is the relevant code.  Can anyone see what the
problem might be?  Note that in the no compression case where I simply call
dofilter(res, wrapper.getData()) it works just fine, so it isn't a
problem with the filter in general....


...
...
...
httpResponse.setHeader("Vary", "Accept-Encoding");
OutputStream out = response.getOutputStream();
httpResponse.setHeader("Content-Encoding", "gzip");
ByteArrayOutputStream compressed = new ByteArrayOutputStream();
GZIPOutputStream gzout = new GZIPOutputStream(compressed);
gzout.write(wrapper.getData());
gzout.finish();
gzout.close();

if (logger.isDebugEnabled()) {
    logger.debug("compressed data...");
    logger.debug(compressed);

    ByteArrayInputStream bais = new ByteArrayInputStream(compressed.toByteArray());
    GZIPInputStream gzin = new GZIPInputStream(bais);
    byte[] buffer = new byte[1024];
    int n, i = 0, m = buffer.length;
    while ((n = gzin.read (buffer, i, m - i)) >= 0) {
        i += n;
        if (i >= m) {
            byte[] newBuffer = new byte[m *= 2];
            System.arraycopy (buffer, 0, newBuffer, 0, i);
            buffer = newBuffer;
        }
    }
    byte[] result = new byte[i];
    System.arraycopy (buffer, 0, result, 0, i);
    ByteArrayOutputStream decompressed = new ByteArrayOutputStream();
    DataOutputStream daos = new DataOutputStream(decompressed);
    daos.write(result);
    daos.flush();
    daos.close();
    logger.debug("decompressed data...");
    logger.debug(decompressed);
}

out.write(compressed.toByteArray());
response.setContentLength(compressed.size());
if (logger.isDebugEnabled()) logger.debug("Wrote filter compressed data: "+compressed.size()+"
bytes");
out.flush();
response.flushBuffer();
out.close();
...
...
...


Here is an example of the debugging output so you can see that I can
compress and decompress the data without issue...

compressed data...
‹       ³±¯ÈÍQ(K-*ÎÌϳU2Ô3P²·ã²qÉÏMÌÌ+¶ƒ2òsSm•’ósó*•ôÑÄRórRAÂú0?
ºÒ‘šX   
decompressed data...
<?xml version="1.0"?>
<Root><MyElement name="someName"/><MyElement name="someOtherName"/></Root>

Note that the contentType was set by the servlet as "text/xml" and was
not reset by the filter.

Any ideas?  It wouldn't be a bug in Tomcat, would it?  I just don't
see anything wrong with the code???


Thanks,

Jake
  

-- 
Best regards,
 Jacob                          mailto:hoju@visi.com


--
To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-user-help@jakarta.apache.org>


Mime
View raw message