Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable gzip compression in CXF client

I'm trying to make my client use gzip. I have the GZip Feature enabled in the server. It seems that the client it's not sending the correct header:

POST /api/v1/data HTTP/1.1
Content-Type: text/xml; charset=UTF-8
Accept: */*
SOAPAction: ""
User-Agent: Apache CXF 2.6.2
Cache-Control: no-cache
Pragma: no-cache
Host: localhost:8001
Connection: keep-alive
Content-Length: 539

Here's the code where I create the client:

 private static final QName SERVICE_NAME = new QName(
            "http://xxx/", "IData");
    private static final QName PORT_NAME = new QName(
            "http://xxx/", "IDataPort");
    IData port;

    public void initPort() {
        Service service = Service.create(SERVICE_NAME);
        // Endpoint Address
        String endpointAddress = ClientUtil.getUrl()
                + "data";

        // Add a port to the Service
        service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING,
                endpointAddress);

        port = service.getPort(IData.class);
    }

The IData interface implements has the GZip Annotation:

@WebService
@GZIP
public interface IData ....
like image 591
Andres Olarte Avatar asked Sep 21 '12 02:09

Andres Olarte


People also ask

How do I enable gzip on my server?

Gzip on Windows Servers (IIS Manager)Open up IIS Manager. Click on the site you want to enable compression for. Click on Compression (under IIS) Now Enable static compression and you are done!


2 Answers

Solution:

After a revision, this is what you need:

Client client = ClientProxy.getClient(port);
client.getInInterceptors().add(new GZIPInInterceptor());
client.getOutInterceptors().add(new GZIPOutInterceptor());

After that it worked.

like image 140
Andres Olarte Avatar answered Oct 16 '22 09:10

Andres Olarte


As I understand from http://fusesource.com/docs/esb/4.4/cxf_jaxws/JavaFirst-AnnotateCxf-Compress.html

"GZIP is a negotiated enhancement. That is, an initial request from a client will not be gzipped, but an Accept header will be added and, if the server supports GZIP compression, the response will be gzipped and any subsequent requests will be also."

Check if the web service accepts Gzip, and check only requests after the first request.

like image 40
JackDev Avatar answered Oct 16 '22 08:10

JackDev