gzip Java/Axis

gzip is a core component of the Java platform, and many web servers have the ability to compress content independent of the files or applications it serves up. Customers can use gzip in conjunction with an Axis SOAP implementation. Following are instructions on processing the gzipped response using Java / Axis. To implement Java code for sending a gzipped request and processing a gzipped response, click here.

Modifying HTTPSender

In the HTTPSender.java file, modify the writeToSocket() method and readFromSocketmethod(). The writeToSocket() changes are to let the server know that the client can accept gzipped response. The readFromSocket() changes are required to read from a gzipped stream.

Accepting gzip Response

  1. Modify the client to indicate to the server that it can handle compression.

  2. Set the header to Accept-Encoding. This HTTP header stores a string value and is often a list of values. See gzip Sample HTTP Header. Shown is the gzip value.

  3. To add a header, navigate in the HTTPSender code to view the Headers. There is a framework for adding headers, but right now, Accept-Encoding is not a header the developers deal with.

  4. Hard-code the encoding. How you integrate or set the HTTP header is entirely up to you.

Note: There are many ways to tackle this problem. You can insert the code snippet in gzip Java/Axis Sample for Adding gzip Support 1.

gzip Sample HTTP Header

Following is a sample HTTP POST from Internet Explorer to GWS. The following code can go into the HTTP Header.
Note
: For the Host connection, ensure you have the correct DNS for your service level or region.

POST /gws/XmlSelect / HTTP/1.1

Accept: */*

Accept-Language: en-us

Accept-Encoding: gzip

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)

Host: americas.webservices.travelport.com 80

Connection: Keep-Alive  

gzip Java/Axis Sample for Adding gzip Support 1

Following is a sample code snippet from HTTPSender.java (writeToSocket).

For un-annotated code, see the gzip Java/Axis Sample 1 topic.

Process user defined headers for  information.

Hashtable userHeaderTable =

(Hashtable) msgContext.getProperty(HTTPConstants.REQUEST_HEADERS);

 

 

Adding Accept-Encoding header to the hashtable.

if(userHeaderTable == null){userHeaderTable =

new Hashtable();} userHeaderTable.put("Accept-Encoding", "gzip");

Finalizing the code

  1. Having modified the HTTP request to include the Accept-Encoding header with a value of gzip, modify the method used to read that content.

  2. Check the HTTP header content-encoding to see if the server responded with gzip:

  1. Modify the input stream just before the body of the HTTP message is decoded, but after the headers have been parsed.

  1. From that point, Axis does not know if it is reading from a compressed stream; nor should it. You now have an Axis client that requests and handles gzip encoding transparently to the underlying code.

gzip Java/Axis Sample for Adding gzip Support 2

Code fragment from HTTPSender.java (readFromSocket)

For un-annotated code, see the gzip Java/Axis Sample 2 topic.

 

 

 

String zip = (String)headers.get("content-encoding");

 

InputStream is = null;

 

 

Check the content encoding. If it is gzip then...

if(zip.indexOf("gzip") != -1){

...wrapper the input stream as a GZIPInputStream

  GZIPInputStream zipIn = new GZIPInputStream(inp);

 

  is = zipIn;

 

} else {

 

        is = inp;

 

}      

 

 

end gzip code

 

outMsg =

new Message( new SocketInputStream(is, sock), false, contentType, contentLocation);