views:

4088

answers:

3

However, I want to compress my responses with GZIP wheren possible. I tried using the Compression filter code available for free download in the headfirst site. It works great for html, images, css and javascript.

I post the filter next. It checks if GZIP is an accepted encoding and it adds gzip as Content-Encoding. See: wrappedResp.setHeader("Content-Encoding", "gzip");

public class CompressionFilter implements Filter {

  private ServletContext ctx;
  private FilterConfig cfg;

    /**
     * The init method saves the config object and a quick reference to the
     * servlet context object (for logging purposes).
     */
  public void init(FilterConfig cfg) 
     throws ServletException {
    this.cfg = cfg;
    ctx = cfg.getServletContext();
    //ctx.log(cfg.getFilterName() + " initialized.");
  }

        /**
         * The heart of this filter wraps the response object with a Decorator
         * that wraps the output stream with a compression I/O stream.
         * Compression of the output stream is only performed if and only if
         * the client includes an Accept-Encoding header (specifically, for gzip).
         */
      public void doFilter(ServletRequest req,
                ServletResponse resp,
                FilterChain fc)
         throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        // Dose the client accept GZIP compression?
        String valid_encodings = request.getHeader("Accept-Encoding");
        if ( (valid_encodings != null) && (valid_encodings.indexOf("gzip") > -1) ) {

          // Then wrap the response object with a compression wrapper
          // We'll look at this class in a minute.
          CompressionResponseWrapper wrappedResp = new CompressionResponseWrapper(response);

          // Declare that the response content is being GZIP encoded.
          wrappedResp.setHeader("Content-Encoding", "gzip");

          // Chain to the next component (thus processing the request)
          fc.doFilter(request, wrappedResp);

          // A GZIP compression stream must be "finished" which also
          // flushes the GZIP stream buffer which sends all of its
          // data to the original response stream.
          GZIPOutputStream gzos = wrappedResp.getGZIPOutputStream();
          gzos.finish();
          // The container handles the rest of the work.

          //ctx.log(cfg.getFilterName() + ": finished the request.");

        } else {
          fc.doFilter(request, response);
          //ctx.log(cfg.getFilterName() + ": no encoding performed.");
        }   
      }

      public void destroy() {
          // nulling out my instance variables
          cfg = null;
          ctx = null;
      }
    }

I was using the next code to send JSON responses in Struts web application.

public ActionForward get(ActionMapping mapping,
    ActionForm     form,
    HttpServletRequest request,
    HttpServletResponse response) {
       JSONObject json = // Do some logic here
       RequestUtils.populateWithJSON(response, json);
       return null;      
}

public static void populateWithJSON(HttpServletResponse response,JSONObject json) {
    if(json!=null) {
        response.setContentType("text/x-json;charset=UTF-8");  
        response.setHeader("Cache-Control", "no-cache");
        try {
             response.getWriter().write(json.toString());
        } catch (IOException e) {
            throw new ApplicationException("IOException in populateWithJSON", e);
        }    
    }
 }

It works fine without compression but if I compress JSON responses, I can not see my JSON objects anymore. I handle JSON Ajax calls with JQuery with code snippets as follows:

    $.post(url,parameters, function(json) {
 // Do some DOM manipulation with the data contained in the JSON Object
}, "json");

If I see the response with Firebug it is empty.

Should I refractor my compression filter to skip compression in JSON responses? or there is a workaround to this?

For me, it looks like JQuery does not recognize the response as JSON because I am adding the Gzip compression.

+3  A: 

you have to add one more header "content-encoding: gzip" if you are compressing it.

Akash Kava
I did already. I edit my post. Thanks for the answer anyway.
Sergio del Amo
A: 

Have you tried with an explicit java-based client to ensure it's a problem with jQuery or browser? If java client fails, something is wrong with server response.

But I am guessing that whereas browser can deal with uncompression with direct requests, this is perhaps not applied to Ajax calls.

It's an interesting question, I hope we'll get a more definitive answer. :)

StaxMan
+2  A: 

If I see the response with Firebug it is empty.

There's your clue - it's not a JQuery problem, it's server-side. (I'm afraid I can't help you with that, other than to suggest you stop looking at the client-side)

There's no problem gzipping ajax responses - if you can't see the response in Firebug, then JQuery can't see it either.

Will Dean