Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Client Compression (unpack gzip static xml data)

    Hello gwt experts :)

    I have many static data XML files, which i use in SmartGWT app, but the file size is quite big, and it's static, and never change.

    Is there are any solution to keep this XML file on server hard drive in gzip, for example, but unpack it on client side?? I want to load compressed XML to SmartGWT data-source on client :)

    Any ideas?

    #2
    With Power and above, the CompressionFilter does this automatically if you just place the .xml files somewhere where the filter is active.

    Comment


      #3
      Originally posted by Isomorphic
      With Power and above, the CompressionFilter does this automatically if you just place the .xml files somewhere where the filter is active.
      Automatic compression is very bad idea. High traffic kills your CPU.
      I found the right solution for static data. (js,css,xml,etc)
      http://blog.jcoglan.com/2007/05/02/compress-javascript-and-css-without-touching-your-application-code/

      Comment


        #4
        The CompressionFilter will use pre-compressed versions if available. It is a superset of the solution you've found, which, as the author notes, has issues with various browsers - not all of them respond correctly to compressed responses for all content types and a variety of workarounds are required to correctly deliver compressed responses to all browsers.

        Comment


          #5
          Originally posted by Isomorphic
          The CompressionFilter will use pre-compressed versions if available. It is a superset of the solution you've found, which, as the author notes, has issues with various browsers - not all of them respond correctly to compressed responses for all content types and a variety of workarounds are required to correctly deliver compressed responses to all browsers.
          Standard Tomcat CompressionFilter doesn't support it automatically. If you have this servlet please share :)

          Comment


            #6
            The CompressionFilter that has these features is part of Power edition and above, and the calss is com.isomorphic.servlet.CompressionFilter. It has JavaDoc in the SDK. See also the FileDownload servlet.

            Comment


              #7
              Finaly I did it.
              Filter for static files like .js .css .xml, compressed by GZIP.

              PreCompressFilter.java
              Code:
               
              package compressionFilters;
              
              import javax.servlet.*;
              import javax.servlet.http.HttpServletRequest;
              import javax.servlet.http.HttpServletResponse;
              import java.io.IOException;
              import java.net.URL;
              import java.util.Enumeration;
              import java.util.regex.Pattern;
              
              
              
              public class PreCompressFilter implements Filter {
              
                  /**
                   * The filter configuration object we are associated with.  If this value
                   * is null, this filter instance is not currently configured.
                   */
                  private FilterConfig config = null;
              
                  /**
                   * Debug level for this filter
                   */
                  private int debug = 0;
              
                  /**
                   * Place this filter into service.
                   *
                   * @param filterConfig The filter configuration object
                   */
              
                  protected static Pattern COMMA_SEP = Pattern.compile(" *, *");
              
              
                  protected String[] compressedExts;
              
                  public void init(FilterConfig filterConfig) {
              
                      config = filterConfig;
                      if (filterConfig != null) {
                          String value = filterConfig.getInitParameter("debug");
                          if (value != null) {
                              debug = Integer.parseInt(value);
                          } else {
                              debug = 0;
                          }
                      }
                      String value = null;
                      try {
                          value = filterConfig.getInitParameter("compressedExts");
                          if (value != null && value.length() != 0) {
                              compressedExts = COMMA_SEP.split(value);
                          }
                      } catch (Exception e) {
                          System.out.println("DefaultServlet.init: couldn't read  compressedExts from " + value);
                      }
                  }
              
                  /**
                   * Take this filter out of service.
                   */
                  public void destroy() {
              
                      this.config = null;
              
                  }
              
                    public void doFilter(ServletRequest request, ServletResponse response,
                                       FilterChain chain) throws IOException, ServletException {
              
                      if (debug > 0) {
                          System.out.println("@doFilter");
                      }
              
                      boolean supportCompression = false;
                      if (request instanceof HttpServletRequest) {
                          if (debug > 1) {
                              System.out.println("requestURI = " + ((HttpServletRequest) request).getRequestURI());
                          }
                          // Are we allowed to compress ?
                          String s = (String) ((HttpServletRequest) request).getParameter("gzip");
                          if ("false".equals(s)) {
                              if (debug > 0) {
                                  System.out.println("got parameter gzip=false --> don't compress, just chain filter");
                              }
                              chain.doFilter(request, response);
                              return;
                          }
              
                          Enumeration e =
                                  ((HttpServletRequest) request).getHeaders("Accept-Encoding");
                          while (e.hasMoreElements()) {
                              String name = (String) e.nextElement();
                              if (name.indexOf("gzip") != -1) {
                                  if (debug > 0) {
                                      System.out.println("supports compression");
                                  }
                                  supportCompression = true;
                              } else {
                                  if (debug > 0) {
                                      System.out.println("no support for compresion");
                                  }
                              }
                          }
                      }
              
                      if (!supportCompression) {
                          if (debug > 0) {
                              System.out.println("doFilter gets called wo compression");
                          }
                          chain.doFilter(request, response);
                          return;
                      } else {
                          // Serve the requested resource, including the data content
                          try {
                              if (compressedExts != null && checkCompressedExts(((HttpServletRequest) request).getRequestURI()) != -1) {
              
                                  String gzPath = getRelativePath((HttpServletRequest) request) + "gz";
              
                                  URL gzEntry = config.getServletContext().getResource(gzPath);
                                  
                                  if (gzEntry != null) {
                                      ((HttpServletResponse) response).setHeader("Content-Encoding", "gzip");
                                       request.getRequestDispatcher(gzPath).forward(request, response);
                                  } else {
                                      request.setAttribute("org.apache.tomcat.sendfile.support", Boolean.FALSE);
                                  }
                              }
              
                          } catch (IOException ex) {
                              // we probably have this check somewhere else too.
                              if (ex.getMessage() != null && ex.getMessage().indexOf("Broken  pipe") >= 0) {
                                  // ignore it.
                              }
                              throw ex;
                          }
              
                          if (response instanceof HttpServletResponse) {
                              chain.doFilter(request, response);
              
                              return;
                          }
                      }
                  }
              
                  /**
                   * Set filter config
                   * This function is equivalent to init. Required by Weblogic 6.1
                   *
                   * @param filterConfig The filter configuration object
                   */
                  public void setFilterConfig(FilterConfig filterConfig) {
                      init(filterConfig);
                  }
              
                  /**
                   * Return filter config
                   * Required by Weblogic 6.1
                   */
                  public FilterConfig getFilterConfig() {
                      return config;
                  }
              
                  protected int checkCompressedExts(String requestUri) {
              
                      for (int i = 0; i < compressedExts.length; i++) {
                          if (requestUri.endsWith(compressedExts[i])) return i;
                      }
                      return -1;
                  }
              
                  protected String getRelativePath(HttpServletRequest request) {
                      String result;
                      if (request.getAttribute("javax.servlet.include.request_uri") != null) {
                          result = (String) request.getAttribute("javax.servlet.include.path_info");
                          if (result == null)
                              result = (String) request.getAttribute("javax.servlet.include.servlet_path");
                          if (result == null || result.equals(""))
                              result = "/";
                          return result;
                      }
                      result = request.getPathInfo();
                      if (result == null)
                          result = request.getServletPath();
                      if (result == null || result.equals(""))
                          result = "/";
                      return result;
                  }
              
              }
              Web app
              web.xml
              Code:
               <filter>
                      <filter-name>Compression Filter</filter-name>
                      <filter-class>compressionFilters.PreCompressFilter</filter-class>
                      <init-param>
                        <param-name>debug</param-name>
                        <param-value>5</param-value>
                      </init-param>
                      <init-param>
                         <param-name>compressedExts</param-name>
                         <param-value>.css,.js,.xml</param-value>
                      </init-param>
                  </filter>
              
                  <filter-mapping>
                    <filter-name>Compression Filter</filter-name>
                    <url-pattern>/*</url-pattern>
                  </filter-mapping>
              
                  <mime-mapping>
                      <extension>cssgz</extension>
                      <mime-type>text/css</mime-type>
                  </mime-mapping>
              
                  <mime-mapping>
                      <extension>xmlgz</extension>
                      <mime-type>text/xml</mime-type>
                  </mime-mapping>
              
                  <mime-mapping>
                      <extension>jsgz</extension>
                      <mime-type>text/javascript</mime-type>
                  </mime-mapping>
              and script to gzip static files recursively. Run it in webapp root.
              Code:
              #!/bin/bash
              
              for fname in `find . -name "*.js"`
              do
                 fbname=${fname%.js}
                 gzip -c $fname  > $fbname.jsgz
                 echo $x $fname 
              done
              
              for fname in `find . -name "*.css"`
              do
                 fbname=${fname%.css}
                 gzip -c $fname  > $fbname.cssgz
                 echo $x $fname.
              done
              for fname in `find . -name "*.xml"`
              do
                 fbname=${fname%.xml}
                  gzip -c $fname  > $fbname.xmlgz
                  echo $x $fname.
              done

              Comment

              Working...
              X