Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Adding Gzip Compression in CXF APIs and Interceptors

DZone's Guide to

Adding Gzip Compression in CXF APIs and Interceptors

· Integration Zone
Free Resource

Migrating from On-Prem to Cloud Middleware? Here’s what Aberdeen Group says leading companies should be considering. Brought to you in partnershp with Liaison Technologies

Nowadays it has become mandatory to Gzipping the APIs response due to huge amount of data we are sending in response. It saves network bandwidth and delivery time and off course space over the internet.

While using CXF; it provides an option to use the Gzip Compression in no of ways.

  1. Blueprint
  2. Annotation

Blueprint:

<bean id="gZipInterceptor" class="org.apache.cxf.transport.common.gzip.GZIPOutInterceptor" />
<jaxrs:server id="rsServer" address="/gZip">
<jaxrs:outInterceptors>
<ref component-id="gZipInterceptor" />
</jaxrs:outInterceptors>
</jaxrs:server>

Annotation:

First you need to register the GZIPOutInterceptor in out interceptors list. For that you need to hook into CXF initialization classes.

public class InterceptorManager extends AbstractFeature {

private static final Logger LOGGER = Logger.getLogger( "simcore" );
private static final Interceptor< Message > GZIP = new GZIPOutInterceptor();
//private static final Interceptor< Message > GZIP = new GZIPOutInterceptor(512);

/* (non-Javadoc)
 * @see org.apache.cxf.feature.AbstractFeature#initializeProvider(org.apache.cxf.interceptor.InterceptorProvider, org.apache.cxf.Bus)
 */
@Override
protected void initializeProvider( InterceptorProvider provider, Bus bus ) {
/**
 * Adding Gzip interceptor to all outbound requests/responses
 */
LOGGER.debug( " ##############  Adding Gzip as OUT Interceptor ##############" );

provider.getOutInterceptors().add( GZIP );

}
}

GZIPOutInterceptor comes with an option to set the Threshold value as no of Bytes. If response size will be below this threshold value then it will not be compressed. It is extremely useful when we will be sending empty lists and status messages/codes only. Because compressing those small responses will be overhead at server side.

But there is another factor which is no of users requesting the response. So set this value by thinking over all the cases in mind.

@GZIP

Now we can use this annotation on any of our web-services controller to implement compression on all the APIs provided in that class.

@WebService
@Consumes ( { MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } )
@Produces ( MediaType.APPLICATION_JSON )
@GZIP
public interface WebServicesController {

        @GET
@Path ( "/myGzipData" )
@Produces ( { MediaType.APPLICATION_JSON } )
Response getZipData( );

}

Moreover we can set different parameters in Gzip annotation.

@GZIP ( force = true, threshold = 512 )

Is iPaaS solving the right problems? Not knowing the fundamental difference between iPaaS and iPaaS+ could cost you down the road. Brought to you in partnership with Liaison Technologies.

Topics:
java ,tips and tricks

Published at DZone with permission of Shan Arshad, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}