1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.cache.filter;
35
36 import info.magnolia.module.cache.util.GZipUtil;
37 import org.apache.commons.collections.MultiMap;
38
39 import javax.servlet.FilterChain;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42 import java.io.IOException;
43 import java.util.Collection;
44
45
46
47
48
49
50
51
52 public class InMemoryCachedEntry extends ContentCachedEntry {
53
54 private final byte[] gzippedContent;
55 private final byte[] plainContent;
56
57 public InMemoryCachedEntry(byte[] out, String contentType, String characterEncoding, int statusCode, MultiMap headers, long modificationDate, String originalUrl, int timeToLiveInSeconds) throws IOException {
58 super(contentType, characterEncoding, statusCode, headers, modificationDate, originalUrl, timeToLiveInSeconds);
59
60 if (GZipUtil.isGZipped(out)) {
61 this.gzippedContent = out;
62 boolean isGzippedResponse = headers.containsKey("Content-Encoding") && ((Collection)headers.get("Content-Encoding")).contains("gzip");
63 if(isGzippedResponse){
64 this.plainContent = GZipUtil.ungzip(out);
65 }
66
67 else{
68 this.plainContent = null;
69 }
70 } else {
71 this.gzippedContent = GZipUtil.gzip(out);
72 this.plainContent = out;
73 }
74 }
75
76 @Override
77 protected boolean canServeGzipContent() {
78 return true;
79 }
80
81 @Override
82 protected void writeContent(HttpServletRequest request, HttpServletResponse response, FilterChain chain, boolean acceptsGzipEncoding) throws IOException {
83 final byte[] body;
84 if (acceptsGzipEncoding || getPlainContent() == null) {
85 body = getGzippedContent();
86 } else {
87 body = getPlainContent();
88 }
89
90 response.setContentLength(body.length);
91 response.getOutputStream().write(body);
92 }
93
94
95 public byte[] getPlainContent() {
96 return plainContent;
97 }
98
99 public byte[] getGzippedContent() {
100 return gzippedContent;
101 }
102
103 }