Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Gzip jackson exported model

Avatar

Level 5

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

1 Accepted Solution

Avatar

Correct answer by
Level 5

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

View solution in original post

3 Replies

Avatar

Community Advisor

Hi @B_Stockwell ,

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();
}

Avatar

Level 5

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.

Avatar

Correct answer by
Level 5

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