0

I am using a jBoss EAP 6.1 sevrer in which I want to Enable the GZIP Compression for my HTML, CSS and javaScript files.

I am entirely new to this compression technology, can anyone suggest me a tutorial or can directly give me a step wise process to enable a compression on Jboss EAP 6.1 server.

Apart from these I have the following doubts:

1) Do I need to compress the files on my workspace or if my jboss server will be compression enabled it will take care of compression , I just need to deploy the war.

2) When server respond my request , Do i need to explicitly convert or decode or decompress the files to use them or to interpret them?

Lukasz Stelmach
  • 5,281
  • 4
  • 25
  • 29
vaibhav
  • 762
  • 2
  • 12
  • 34

2 Answers2

3

GZIP compression for web resources is optional, so you can't compress all of them and then hope that every web client is able to handle it. That's why it's usually enabled at runtime when the client (might be a web browser) says "gzip is OK for me" with the Accept-Encoding: gzip, deflate header. See https://en.wikipedia.org/wiki/HTTP_compression

On the server side, the magic is handles by a HTTP Filter which intercepts the request, notes the header, then sends the request on to the rest of the app, intercepts the response and compresses accordingly.

JBoss has some built-in support: Enabling gzip compression for Jboss

If you want to do it yourself, you need to write a Filter and configure it in your web.xml.

Java2s has an implementation:

  public void doFilter(ServletRequest req, ServletResponse res,
      FilterChain chain) throws IOException, ServletException {
    if (req instanceof HttpServletRequest) {
      HttpServletRequest request = (HttpServletRequest) req;
      HttpServletResponse response = (HttpServletResponse) res;
      String ae = request.getHeader("accept-encoding");
      if (ae != null && ae.indexOf("gzip") != -1) {        
        GZIPResponseWrapper wrappedResponse = new GZIPResponseWrapper(response);
        chain.doFilter(req, wrappedResponse);
        wrappedResponse.finishResponse();
        return;
      }
      chain.doFilter(req, res);
    }
  }

or you can use a performance optimization library like WebUtilities to enable compression as described here https://github.com/rpatil26/webutilities/wiki/Enable-Compression

See also:

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Hello @Aaron Digulla I have implemented this code on my jBoss EAP 6.1 and It was working fine when I was working on my local server. But when I moved to actual server environment the changes are not getting reflected. My server is jBoss Server EAP 6.1 and is working on Linux OS. please refer to my new question . http://stackoverflow.com/questions/30588454/gzip-compression-is-not-woking-on-jboss-server-when-depolyed-on-linux-system – vaibhav Jun 02 '15 at 07:27
1

In Jboss EAP 7.0 this worked for me:

edit: Standalone.xml

<subsystem xmlns="urn:jboss:domain:undertow:1.2">   <!-- SEARCH FOR THIS: urn:jboss:domain:undertow -->
  <buffer-cache name="default"/>  
  <server name="default-server">  
  <http-listener name="default" socket-binding="http"/>  
  <host name="default-host" alias="localhost">  
  (...)

  <!-- ADD THIS FOR GZIP COMPRESSION -->
  <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]"/>  
  <!-- /GZIP COMPRESSION -->

  </host>  
  </server>  
(...)  
  <filters>  
  (...)  

  <!-- ADD THIS FOR GZIP COMPRESSION -->
  <gzip name="gzipFilter"/>  
  <!-- /GZIP COMPRESSION -->

  </filters>  
</subsystem>

Restart the server

Roger Vega
  • 53
  • 6
  • Worth noting that in later versions of JBoss square brackets cause a warning: `Predicate not min-content-size[2048] uses old style square braces to define predicates, which will be removed in a future release. predicate[value] should be changed to predicate(value)`. I'm seeing good results with this alternative predicate, which catches many types of textual content type: `predicate="exists('%{o,Content-Type}') and regex(pattern='(?:text|javascript|json|html|multipart|xml)',value=%{o,Content-Type},full-match=false)"` – Steve Chambers Nov 11 '20 at 12:08