0 Replies Latest reply on Nov 9, 2015 11:48 AM by tari_manga

    Best practice to enable gzip compression for Webservices on WildFly?

    tari_manga

      Ciao, I'm looking for the best practice in order to enable gzip compression for Webservices on WildFly, meaning the request will income as gzip, and the response need to be in gzip.

       

      I checked and the http request for the SOAP Webservice is incoming with the right header parameters:

      POST /mywebservice/mywebservice HTTP/1.1[\r][\n]
      Content-Encoding: gzip[\r][\n]
      Accept-Encoding: gzip,deflate[\r][\n]
      (...)
      

      and the request payload is a correct and valid gzip.

       

      I tried by enabling undertow gzip filter by modifying the standalone.xml of the WildFly as follows:

      <subsystem xmlns="urn:jboss:domain:undertow:1.2">
        <buffer-cache name="default"/>
        <server name="default-server">
        <http-listener name="default" socket-binding="http"/>
        <host name="default-host" alias="localhost">
        (...)
        <filter-ref name="gzipFilter" predicate="exists['%{o,Content-Type}'] and regex[pattern='(?:application/javascript|text/css|text/html|text/xml|application/json)(;.*)?', value=%{o,Content-Type}, full-match=true]"/>
        </host>
        </server>
      (...)
        <filters>
        (...)
        <gzip name="gzipFilter"/>
        </filters>
      </subsystem>
      

       

      But it does not solve the issue, at least it does not solve for the incoming gzip request (e.g.: same error with or without this filter, once I fire the request to the server, I get the error of unable to process the incoming stream).

       

      So I've solved by implementing manually a javax.servlet.Filter, which I wire up in the Java EE application by means of the web.xml for example:

        <filter>
          <filter-name>gzipfilter</filter-name>
          <filter-class>com.acme.GZIPFilter</filter-class>
        </filter>
        <filter-mapping>
          <filter-name>gzipfilter</filter-name>
          <url-pattern>/*</url-pattern>
        </filter-mapping>
      

       

      And the code (only meaningful excerpt for brevity):

      public class GZIPFilter implements Filter {
          @Override
          public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
              String contentEncoding;
              String acceptEncoding;
              HttpServletRequest httpReq;
              RequestWrapper reqWrapper = null;
              ResponseWrapper resWrapper = null;
              
              if (request instanceof HttpServletRequest) {
                  httpReq = (HttpServletRequest) request; 
                  
                  if ((contentEncoding = httpReq.getHeader("Content-Encoding")) != null && contentEncoding.contains("gzip") ) {
                      LOG.debug("Content is coming as gzip");
                      reqWrapper = new RequestWrapper((HttpServletRequest) request);
                  }
                  
                  if ((acceptEncoding = httpReq.getHeader("Accept-Encoding")) != null && acceptEncoding.contains("gzip") ) {
                      LOG.debug("Response should be produced as gzip");
                      HttpServletResponse httpServletResponse = (HttpServletResponse) response;
                      httpServletResponse.setHeader("Content-Encoding", "gzip");
                      resWrapper = new ResponseWrapper(httpServletResponse);
                  }
              }
              
              try {
                      chain.doFilter(reqWrapper!=null?reqWrapper:request, resWrapper!=null?resWrapper:response);
              } finally {
                      // close streams.
              }
          }
      

       

      I'm wondering if implementing manually the javax.servlet.Filter is the best practice and the thing to do?

      I would like to underline once again I've proceeded manually given that trying to wire the undertow gzip filter does not solve the issue at least for the incoming request being gzip-ed.


      Thanks,

      Ciao