Implementing request message body interceptors

You can use the javax.ws.rs.ext.ReaderInterceptor interface to intercept and manipulate the incoming message body.

The following example illustrates how you can implement ReaderInterceptor to intercept the incoming message in order to unzip the zipped body content:

//Other imports are omitted for brevity 
import java.util.zip.GZIPInputStream; 
import javax.ws.rs.WebApplicationException; 
import javax.ws.rs.ext.Provider; 
import javax.ws.rs.ext.ReaderInterceptor; 
import javax.ws.rs.ext.ReaderInterceptorContext; 
 
@Provider 
public class JAXRSReaderInterceptor implements ReaderInterceptor { 
 
    @Override 
    public Object aroundReadFrom(ReaderInterceptorContext context) 
        throws IOException, WebApplicationException { 
        List<String> header = context.getHeaders() 
            .get("Content-Encoding"); 
        // decompress gzip stream only 
        if (header != null && header.contains("gzip")) { 
 
            InputStream originalInputStream =  
            context.getInputStream(); 
            context.setInputStream(new  
            GZIPInputStream(originalInputStream)); 
        } 
        return context.proceed(); 
 
    } 
} 

You follow the same interface contract (ReaderInterceptor) for building the read interceptors for client-side use as well. You can register the ReaderInterceptor interface to the JAX-RS client by calling the register() method on the javax.ws.rs.client.Client object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.70.247