ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Two Servlet Filters Every Web Application Should Have
Subject:   Geting Binary Data
Date:   2004-05-17 09:08:44
From:   gselvara
Response to: Geting Binary Data

Well I found a way to get over this. It seems that in my application the same jsp gets called twice or thrice this means the output is compressed over and over giving me funny characters that looks like binary data (with help from nagesh@bea.com and his http://ftpna2.bea.com/pub/downloads/Gzipfilter_war.zip) thanks nagesh. Here the code fix,


package com.jspbook;


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class GZIPFilter implements Filter {
private static final String WL_GZIP_FILTER = "com.jspbook.GZIPFilter";

public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
if(!(res instanceof HttpServletResponse) || !(req instanceof HttpServletRequest)){
chain.doFilter(req, res);
return;
}

if (req instanceof HttpServletRequest) {
HttpServletResponse httpservletresponse = (HttpServletResponse)res;
HttpServletRequest httpservletrequest = (HttpServletRequest)req;
if(httpservletrequest.getAttribute(WL_GZIP_FILTER) != null){
chain.doFilter(req, res);
return;
}
httpservletrequest.setAttribute(WL_GZIP_FILTER, "true");
boolean flag = false;

String ae = httpservletrequest.getHeader("accept-encoding");
if (ae != null && ae.indexOf("gzip") != -1) {
GZIPResponseWrapper wrappedResponse =
new GZIPResponseWrapper(httpservletresponse);
chain.doFilter(req, wrappedResponse);
wrappedResponse.finishResponse();
return;
}else{
chain.doFilter(req, res);
return;
}
}else{
chain.doFilter(req, res);
return;
}
}
public void init(FilterConfig filterConfig) {
}
public void destroy() {
}
}