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.cms.util.RequestHeaderUtil; |
37 |
|
import info.magnolia.module.cache.util.GZipUtil; |
38 |
|
|
39 |
|
import java.io.IOException; |
40 |
|
import java.io.ObjectInputStream; |
41 |
|
import java.io.ObjectOutputStream; |
42 |
|
import java.io.Serializable; |
43 |
|
import java.util.ArrayList; |
44 |
|
import java.util.Collection; |
45 |
|
import java.util.HashMap; |
46 |
|
import java.util.Iterator; |
47 |
|
import java.util.Map; |
48 |
|
import java.util.Map.Entry; |
49 |
|
|
50 |
|
import javax.servlet.FilterChain; |
51 |
|
import javax.servlet.ServletException; |
52 |
|
import javax.servlet.http.HttpServletRequest; |
53 |
|
import javax.servlet.http.HttpServletResponse; |
54 |
|
|
55 |
|
import org.apache.commons.collections4.MultiMap; |
56 |
|
import org.apache.commons.collections4.map.MultiValueMap; |
57 |
|
import org.apache.commons.lang3.builder.ToStringBuilder; |
58 |
|
import org.apache.commons.lang3.builder.ToStringStyle; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
@author |
66 |
|
@version |
67 |
|
|
|
|
| 63.5% |
Uncovered Elements: 31 (85) |
Complexity: 21 |
Complexity Density: 0.4 |
|
68 |
|
public abstract class ContentCachedEntry implements CachedEntry, Serializable { |
69 |
|
|
70 |
|
private static final ToStringStyle BYTE_ARRAY_SIZE_STYLE = new ToStringStyle() { |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
71 |
0 |
@Override... |
72 |
|
protected void appendDetail(StringBuffer buffer, String fieldName, |
73 |
|
byte[] array) { |
74 |
0 |
super.appendDetail(buffer, fieldName, array.length + " bytes"); |
75 |
|
} |
76 |
|
}; |
77 |
|
|
78 |
|
private final String contentType; |
79 |
|
private final String characterEncoding; |
80 |
|
private final int statusCode; |
81 |
|
private transient MultiMap headers; |
82 |
|
private Map serializableHeadersBackingList; |
83 |
|
private final long lastModificationTime; |
84 |
|
private String originalUrl; |
85 |
|
private int timeToLiveInSeconds = -1; |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
@param |
90 |
|
@param |
91 |
|
@param |
92 |
|
@param |
93 |
|
@param |
94 |
|
@param |
95 |
|
@throws |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
97 |
17 |
public ContentCachedEntry(String contentType, String characterEncoding, int statusCode, MultiMap headers, long modificationDate, String originalUrl, int timeToLiveInSeconds) throws IOException {... |
98 |
17 |
this.contentType = contentType; |
99 |
17 |
this.characterEncoding = characterEncoding; |
100 |
17 |
this.statusCode = statusCode; |
101 |
17 |
this.headers = headers; |
102 |
17 |
this.lastModificationTime = modificationDate; |
103 |
17 |
this.originalUrl = originalUrl; |
104 |
17 |
this.timeToLiveInSeconds = timeToLiveInSeconds; |
105 |
|
} |
106 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
107 |
|
@Override... |
108 |
|
public String getOriginalURL() { |
109 |
|
return this.originalUrl; |
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
119 |
|
public String getContentType() {... |
120 |
|
return contentType; |
121 |
|
} |
122 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
123 |
|
public String getCharacterEncoding() {... |
124 |
|
return characterEncoding; |
125 |
|
} |
126 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
127 |
|
public int getStatusCode() {... |
128 |
|
return statusCode; |
129 |
|
} |
130 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
131 |
|
public MultiMap getHeaders() {... |
132 |
|
return headers; |
133 |
|
} |
134 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
135 |
|
@Override... |
136 |
|
public long getLastModificationTime() { |
137 |
|
return lastModificationTime; |
138 |
|
} |
139 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
140 |
|
@Override... |
141 |
|
public int getTimeToLiveInSeconds() { |
142 |
|
return timeToLiveInSeconds; |
143 |
|
} |
144 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
145 |
0 |
@Override... |
146 |
|
public String toString() { |
147 |
0 |
return ToStringBuilder.reflectionToString(this, BYTE_ARRAY_SIZE_STYLE); |
148 |
|
} |
149 |
|
|
150 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
151 |
0 |
private void writeObject(ObjectOutputStream out) throws IOException {... |
152 |
0 |
serializableHeadersBackingList = new HashMap(); |
153 |
0 |
Iterator iter = headers.entrySet().iterator(); |
154 |
0 |
while (iter.hasNext()) { |
155 |
0 |
Map.Entry entry = (Entry) iter.next(); |
156 |
0 |
serializableHeadersBackingList.put(entry.getKey(), new ArrayList((Collection)entry.getValue())); |
157 |
|
} |
158 |
0 |
out.defaultWriteObject(); |
159 |
|
} |
160 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
161 |
0 |
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {... |
162 |
0 |
in.defaultReadObject(); |
163 |
0 |
headers = new MultiValueMap(); |
164 |
0 |
Iterator iter = serializableHeadersBackingList.entrySet().iterator(); |
165 |
0 |
while (iter.hasNext()) { |
166 |
0 |
Map.Entry entry = (Entry) iter.next(); |
167 |
0 |
Collection c = (Collection) entry.getValue(); |
168 |
0 |
for (Iterator ic = c.iterator(); ic.hasNext();) { |
169 |
0 |
headers.put(entry.getKey(), ic.next()); |
170 |
|
} |
171 |
|
} |
172 |
0 |
serializableHeadersBackingList = null; |
173 |
|
} |
174 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
175 |
7 |
@Override... |
176 |
|
public void replay(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { |
177 |
7 |
response.setStatus(getStatusCode()); |
178 |
|
|
179 |
7 |
boolean acceptsGzipEncoding = isAcceptsGzip(request) && canServeGzipContent(); |
180 |
7 |
addHeaders(acceptsGzipEncoding, response); |
181 |
|
|
182 |
|
|
183 |
7 |
response.setContentType(getContentType()); |
184 |
7 |
response.setCharacterEncoding(getCharacterEncoding()); |
185 |
|
|
186 |
7 |
writeContent(request, response, chain, acceptsGzipEncoding); |
187 |
|
} |
188 |
|
|
189 |
|
protected abstract void writeContent(HttpServletRequest request, HttpServletResponse response, FilterChain chain, boolean acceptsGzipEncoding) throws IOException, ServletException; |
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
|
|
| 90% |
Uncovered Elements: 4 (40) |
Complexity: 11 |
Complexity Density: 0.5 |
|
194 |
7 |
protected void addHeaders(final boolean acceptsGzipEncoding, final HttpServletResponse response) {... |
195 |
7 |
final MultiMap headers = getHeaders(); |
196 |
|
|
197 |
7 |
final Iterator it = headers.keySet().iterator(); |
198 |
15 |
while (it.hasNext()) { |
199 |
8 |
final String header = (String) it.next(); |
200 |
8 |
if (!acceptsGzipEncoding) { |
201 |
|
|
202 |
4 |
if ("Content-Encoding".equals(header) || "Vary".equals(header)) { |
203 |
2 |
continue; |
204 |
|
} |
205 |
|
} |
206 |
6 |
final Collection values = (Collection) headers.get(header); |
207 |
6 |
final Iterator valIt = values.iterator(); |
208 |
13 |
while (valIt.hasNext()) { |
209 |
7 |
final Object value = valIt.next(); |
210 |
|
|
211 |
7 |
if (value instanceof Long) { |
212 |
2 |
response.setDateHeader(header, ((Long) value).longValue()); |
213 |
5 |
} else if (value instanceof Integer) { |
214 |
0 |
response.setIntHeader(header, ((Integer) value).intValue()); |
215 |
5 |
} else if (value instanceof String) { |
216 |
5 |
response.setHeader(header, (String) value); |
217 |
|
} else { |
218 |
0 |
throw new IllegalStateException("Unrecognized type for header [" + header + "], value is: " + value); |
219 |
|
} |
220 |
|
} |
221 |
|
} |
222 |
|
|
223 |
7 |
if(acceptsGzipEncoding){ |
224 |
|
|
225 |
5 |
if (!response.containsHeader("Content-Encoding")) { |
226 |
1 |
RequestHeaderUtil.addAndVerifyHeader(response, "Content-Encoding", "gzip"); |
227 |
1 |
RequestHeaderUtil.addAndVerifyHeader(response, "Vary", "Accept-Encoding"); |
228 |
|
} |
229 |
|
} |
230 |
|
} |
231 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
232 |
7 |
protected boolean isAcceptsGzip(HttpServletRequest request){... |
233 |
7 |
return GZipUtil.isAcceptsGzip(request); |
234 |
|
} |
235 |
|
|
236 |
|
abstract protected boolean canServeGzipContent(); |
237 |
|
|
238 |
|
} |