Gzip jackson exported model | Community
Skip to main content
Level 4
October 25, 2022
Solved

Gzip jackson exported model

  • October 25, 2022
  • 1 reply
  • 1615 views

I recently converted a set of json generating servlets to use the jackson sling model exporter. Previously, I used gzip encoding and wrote the http entity with a gzip writer after I had constructed the json objects myself--not a very fun or concise process.

 

I am thrilled with the clarity and brevity of the jackson exporter version's code, but the tradeoff is that the file sizes have increased greatly (sometimes by a factor of 10) since its no longer compressed. (The particular sticking point is with a list of 900 of our university's programs, which is 9.7MB as raw json, even if I cut the individual json objects down to only display the necessary information for my front-end code, it's still very large ~6MB, so I really need to compress.)

 

Is there an easy way to tell my servlet to export as gzip? Neither the sling docs or the jackson docs point to any simple solution such as exporter options or any other annotation.

 

I am currently trying to write my own gzip json exporter, based on JacksonExporter, but I can't really wrap my head around where the gzip compression would actually happen when using exporters, or even where the response body is written for that matter (so far, I just have the JacksonExporter copied and pasted in my package).

 

Thank you

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by BenSt10

I added a servlet filter and used a wrapper to wrap the response as detailed here: https://jenkov.com/tutorials/java-servlets/gzip-servlet-filter.html

 

For my sling servler filter options, I used the same resource types and extensions as defined in my model/exporter annotations

1 reply

TarunKumar
Community Advisor
Community Advisor
October 25, 2022

Hi @benst10 ,

Can you try using something like below in your custom exporter:
A custom compressor.

public class Compressor{
public static byte[] compress(byte[] content){
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try{
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
gzipOutputStream.write(content);
gzipOutputStream.close();
} catch(IOException e){
throw new RuntimeException(e);
}
return byteArrayOutputStream.toByteArray();
}

BenSt10Author
Level 4
October 25, 2022

This looks easy enough (it's not far from the gzipping code that I had in my original servlets), but I dont know where I would place this code. I don't see a place in the exporter to actually write out the response body.

BenSt10AuthorAccepted solution
Level 4
October 25, 2022

I added a servlet filter and used a wrapper to wrap the response as detailed here: https://jenkov.com/tutorials/java-servlets/gzip-servlet-filter.html

 

For my sling servler filter options, I used the same resource types and extensions as defined in my model/exporter annotations