Pack200 content negotiation

Since Java J2SE 5.0 the compression formats gzip and Pack200 have been available to Java deployment of applications and applets. This page shows how Apache mod_rewrite rules can be utilized for gzip and pack200 content negotiation.

I you are unfamiliar with Pack200 a nice illustrated introduction is available on the Pack200 and Compression page.

To utilize these formats some form of content negotiation must be provided by the serverside. This could be a servlet as developed by the Deployment project. If a servlet isn't feasible, Keith Lea and Chris Nokleberg suggest Apache's mod_negotiation, but unfortunately my webhotel doesn't provide this module.

It does provide mod_rewrite, and the below .htaccess file with mod_rewrite rules, is an alternative to the previous mentioned gzip and Pack200 content negotiaiton schemes.

AddType application/x-java-archive .jar
AddType application/x-java-jnlp-file .jnlp

<Files *.jar.pack.gz>
  AddEncoding pack200-gzip .jar
  RemoveEncoding .gz
</Files>

RewriteEngine on
RewriteCond %{HTTP:Accept-Encoding} pack200-gzip
RewriteCond %{REQUEST_FILENAME}.pack.gz -f
RewriteRule ^(.*\.jar)$ $1.pack.gz [NC,L]

RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*\.jar)$ $1.gz [NC,L]

Download: htaccess.txt

To install, just upload the above .htaccess file to the directory where your jar and compressed jar archives are hosted, and you're all set to go, e.g.

$ ls -a
.htaccess
app.jnlp
app.jar
app.jar.gz
app.jar.pack.gz

Here is a utility script that can help compress jar archives:

#!/bin/sh

#
# pack/compress a jar archive with pack200 and gzip
#

# get option
if [ "$1" = "--strip-debug" ]; then
   STRIP_DEBUG="$1"
   shift
fi

# usage message
if [ "$#" -ne 1 ]; then
   echo "usage: `basename \"$0\"` [--strip-debug] <jarfile>"
   exit 1
fi

# normalize and optionally strip debug symbols from archive
pack200 --repack $STRIP_DEBUG "$1"

# sign archive
#jarsigner -storepass mysecret -keypass mysecret "$1" mykey

# pack it
gzip -9 -c "$1" > "$1.gz"
pack200 --effort=9 "$1.pack.gz" "$1"

Download: packjar.sh