in server/src/main/java/org/apache/asyncweb/server/filter/GZipFilter.java [54:84]
public void handleResponse(NextFilter next, HttpServiceContext context)
throws Exception {
String ae = context.getRequest().getHeader("accept-encoding");
if (ae != null
&& ae.indexOf("gzip") != -1
&& context.getCommittedResponse() instanceof MutableHttpResponse) {
LOG.debug("Compressing content");
// compress
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzos = new GZIPOutputStream(baos);
gzos.write(context.getCommittedResponse().getContent().array());
gzos.close();
baos.close();
// recreate an IoBuffer
byte[] bytes = baos.toByteArray();
IoBuffer gzipedResponse = IoBuffer.allocate(bytes.length);
gzipedResponse.put(bytes);
gzipedResponse.flip();
LOG.debug("Old content size {}",context.getCommittedResponse().getContent().remaining());
LOG.debug("Compressed content size {}",gzipedResponse.remaining());
// change the response content and content type
MutableHttpResponse mutableResponse = (MutableHttpResponse) context.getCommittedResponse();
mutableResponse.setHeader("Content-Encoding", "gzip");
mutableResponse.setContent(gzipedResponse);
mutableResponse.normalize(context.getRequest());
}
next.invoke();
}