Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable GZIP for Jetty 9

Tags:

java

jetty

I'm trying to enabled gzip compression on Jetty 9. I do not want to configure it in my web.xml, so based on the Jetty documentation I've configured a override-web.xml. I'm not using Jetty in an embedded mode, but as a container.

In my {jetty.home}/webapps folder, I've my war file - cc.war. I also have defined a cc.xml as follows

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<!-- ==================================================================
Configure and deploy the test web application in $(jetty.home)/webapps/test

Note. If this file did not exist or used a context path other that /test
then the default configuration of jetty.xml would discover the test
webapplication with a WebAppDeployer.  By specifying a context in this
directory, additional configuration may be specified and hot deployments
detected.
===================================================================== -->

<Configure class="org.eclipse.jetty.webapp.WebAppContext">

  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <!-- Required minimal context configuration :                        -->
  <!--  + contextPath                                                  -->
  <!--  + war OR resourceBase                                          -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <Set name="contextPath">/</Set>
  <Set name="war"><Property name="jetty.webapps"/>/cc.war</Set>

  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <!-- Optional context configuration                                  -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!--   <Set name="extractWAR">true</Set>
  <Set name="copyWebDir">false</Set>
 -->
    <!--<Set name="defaultsDescriptor"><Property name="jetty.home" default="."/>/etc/webdefault.xml</Set>-->
  <Set name="overrideDescriptor"><Property name="jetty.webapps" default="."/>/cc.d/override-web.xml</Set>
</Configure>

In the folder cc.d, I've override-web.xml as follows:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">

    <filter>
        <filter-name>GzipFilter</filter-name>
        <filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class>
        <init-param>
            <param-name>methods</param-name>
            <param-value>GET,POST</param-value>
        </init-param>
        <init-param>
            <param-name>mimeTypes</param-name>
            <param-value>text/html,text/xml,text/plain,text/css,text/javascript,text/json,application/x-javascript,application/javascript,application/json,application/xml,application/xml+xhtml,image/svg+xml</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>GzipFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> 
</web-app>

Jetty seems to load this fine, but gzip compression is not applied on any of the responses. I say Jetty loaded this fine because earlier when I tried to put the override-web.xml in the webapps folder, Jetty was complaining.

I've gone through the various questions here on SO and but none of them seem to have an answer for this. Any help is appreciated.

like image 956
IceMan Avatar asked Sep 23 '14 18:09

IceMan


2 Answers

I think you should be able to configure common filters in webdefault.xml. Try registering the Gzip filter there.

like image 85
kaqqao Avatar answered Nov 20 '22 05:11

kaqqao


One way to keep GZIP configuration out of your application while running in a Jetty container (non-embedded) is to add the filter to your context XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="contextPath"><!-- set context path --></Set>
    <Set name="war"><!-- set war path --></Set>

    <Call name="addFilter">
        <Arg>org.eclipse.jetty.servlets.GzipFilter</Arg>
        <Arg>/*</Arg>
        <Arg>
            <Call name="of" class="java.util.EnumSet">
                <Arg><Get name="REQUEST" class="javax.servlet.DispatcherType" /></Arg>
            </Call>
        </Arg>
        <Call name="setInitParameter">
            <Arg>mimetypes</Arg>
            <Arg>text/html,text/xml,text/plain,text/css,text/javascript,text/json,application/x-javascript,application/javascript,application/json,application/xml,application/xml+xhtml,image/svg+xml</Arg>
        </Call>
        <Call name="setInitParameter">
            <Arg>methods</Arg>
            <Arg>GET,POST</Arg>
        </Call>
    </Call>
</Configure>
like image 22
Ben Hutchison Avatar answered Nov 20 '22 05:11

Ben Hutchison