JAX-RS and gzip

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

JAX-RS and gzip

Borislav Iordanov
Hi guys,

Any idea how to compress large JSON content using the JAX-RS implementation in Restlet?

I have a MessageBodyWriter that writes Json objects to the output stream. It is annotated with:

@javax.ws.rs.Produces("application/json")

and it works fine. But I'd like to be able to compress the content with gzip which is automatically recognized by browsers given the appropriate HTTP response headers. To achieve that I tried the following:

    public void writeTo(Json t, Class<?> type, Type genericType,
                        Annotation[] annotations, MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders,
                        OutputStream entityStream) throws IOException,
            WebApplicationException
    {
    httpHeaders.put("Content-Type", Collections.singletonList((Object)"application/json"));
    httpHeaders.put("Content-Encoding", Collections.singletonList((Object)"gzip"));
    java.util.zip.GZIPOutputStream gzip = new java.util.zip.GZIPOutputStream(entityStream);
        gzip.write(t.toString().getBytes());
    }


But I'm getting an exception: "The changing of the http headers is not supported by this runtime environment." What would be the suggested way?

Thanks much!
Boris

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2949229
Reply | Threaded
Open this post in threaded view
|

RE: JAX-RS and gzip

Jerome Louvel-2
Hi Boris,

You should try to enable and configure the "encoderService" property on your
JaxRsApplication (subclass of Application). See
org.restlet.service.EncoderService class as well.

Best regards,
Jerome
--
http://www.restlet.com
http://twitter.com/#!/jlouvel


-----Message d'origine-----
De : Borislav Iordanov [mailto:[hidden email]]
Envoyé : mardi 17 avril 2012 21:45
À : [hidden email]
Objet : JAX-RS and gzip

Hi guys,

Any idea how to compress large JSON content using the JAX-RS implementation
in Restlet?

I have a MessageBodyWriter that writes Json objects to the output stream. It
is annotated with:

@javax.ws.rs.Produces("application/json")

and it works fine. But I'd like to be able to compress the content with gzip
which is automatically recognized by browsers given the appropriate HTTP
response headers. To achieve that I tried the following:

    public void writeTo(Json t, Class<?> type, Type genericType,
                        Annotation[] annotations, MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders,
                        OutputStream entityStream) throws IOException,
            WebApplicationException
    {
    httpHeaders.put("Content-Type",
Collections.singletonList((Object)"application/json"));
    httpHeaders.put("Content-Encoding",
Collections.singletonList((Object)"gzip"));
    java.util.zip.GZIPOutputStream gzip = new
java.util.zip.GZIPOutputStream(entityStream);
        gzip.write(t.toString().getBytes());
    }


But I'm getting an exception: "The changing of the http headers is not
supported by this runtime environment." What would be the suggested way?

Thanks much!
Boris

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=29492
29

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2949449
Reply | Threaded
Open this post in threaded view
|

RE: JAX-RS and gzip

Borislav Iordanov
Thanks, Jerome! That seems to have worked. For the benefit of future readers, the code looks like:


final JaxRsApplication app = new JaxRsApplication(server.getContext().createChildContext());
EncoderService encoderService = new EncoderService();
encoderService.setEnabled(true);
app.setEncoderService(encoderService);

It all seems mysterious because of lack of documentation, but what this encoder service does is look up if a certain media type (application/json in my case) can be encoded and then selects the "best" encoding according to some internal logic. That best encoding ends up being gzip.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2949935
som
Reply | Threaded
Open this post in threaded view
|

RE: JAX-RS and gzip

som
This post has NOT been accepted by the mailing list yet.
Hi

where I will write this 'writeTo()' method tell me