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.cache.CacheConstants; |
37 |
|
|
38 |
|
import java.util.ArrayList; |
39 |
|
import java.util.Date; |
40 |
|
import java.util.List; |
41 |
|
|
42 |
|
import org.apache.http.HeaderElement; |
43 |
|
import org.apache.http.client.utils.DateUtils; |
44 |
|
import org.apache.http.message.BasicHeaderValueParser; |
45 |
|
import org.slf4j.Logger; |
46 |
|
import org.slf4j.LoggerFactory; |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
|
|
| 91% |
Uncovered Elements: 11 (122) |
Complexity: 35 |
Complexity Density: 0.5 |
|
66 |
|
public class ResponseExpirationCalculator { |
67 |
|
|
|
|
| 52.9% |
Uncovered Elements: 8 (17) |
Complexity: 7 |
Complexity Density: 1 |
|
68 |
|
private static class HeaderEntry { |
69 |
|
|
70 |
|
private final String name; |
71 |
|
private final Object value; |
72 |
|
private HeaderElement[] elements; |
73 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
74 |
25 |
public HeaderEntry(String name, Object value) {... |
75 |
25 |
this.name = name; |
76 |
25 |
this.value = value; |
77 |
|
} |
78 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
79 |
|
public String getName() {... |
80 |
|
return name; |
81 |
|
} |
82 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
83 |
|
public Object getValue() {... |
84 |
|
return value; |
85 |
|
} |
86 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
87 |
37 |
public HeaderElement[] getElements() {... |
88 |
37 |
if (elements == null) { |
89 |
13 |
elements = BasicHeaderValueParser.parseElements(value.toString(), BasicHeaderValueParser.INSTANCE); |
90 |
|
} |
91 |
37 |
return elements; |
92 |
|
} |
93 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 3 |
Complexity Density: 3 |
|
94 |
0 |
public String toExternalFormat() {... |
95 |
0 |
return (name != null ? name : "") + ": " + (value != null ? value : ""); |
96 |
|
} |
97 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
98 |
0 |
@Override... |
99 |
|
public String toString() { |
100 |
0 |
return toExternalFormat(); |
101 |
|
} |
102 |
|
} |
103 |
|
|
104 |
|
private static final Logger logger = LoggerFactory.getLogger(ResponseExpirationCalculator.class); |
105 |
|
|
106 |
|
private final List<HeaderEntry> headers = new ArrayList<HeaderEntry>(); |
107 |
|
|
|
|
| 95% |
Uncovered Elements: 1 (20) |
Complexity: 5 |
Complexity Density: 0.42 |
|
108 |
34 |
public boolean addHeader(String name, Object value) {... |
109 |
|
|
110 |
34 |
if (CacheConstants.HEADER_EXPIRES.equals(name)) { |
111 |
8 |
this.headers.add(new HeaderEntry(name, value)); |
112 |
8 |
return true; |
113 |
|
} |
114 |
|
|
115 |
26 |
if (CacheConstants.HEADER_CACHE_CONTROL.equals(name)) { |
116 |
15 |
this.headers.add(new HeaderEntry(name, value)); |
117 |
15 |
return true; |
118 |
|
} |
119 |
|
|
120 |
11 |
if (CacheConstants.HEADER_PRAGMA.equals(name)) { |
121 |
2 |
HeaderEntry headerEntry = new HeaderEntry(name, value); |
122 |
2 |
if (isHeaderWithElement(headerEntry, CacheConstants.HEADER_PRAGMA, CacheConstants.HEADER_VALUE_NO_CACHE)) { |
123 |
2 |
this.headers.add(headerEntry); |
124 |
2 |
return true; |
125 |
|
} |
126 |
|
} |
127 |
|
|
128 |
9 |
return false; |
129 |
|
} |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
|
|
| 88.5% |
Uncovered Elements: 10 (87) |
Complexity: 26 |
Complexity Density: 0.51 |
|
135 |
14 |
public int getMaxAgeInSeconds() {... |
136 |
|
|
137 |
14 |
for (HeaderEntry header : headers) { |
138 |
|
|
139 |
|
|
140 |
19 |
if (isHeaderWithElement(header, CacheConstants.HEADER_PRAGMA, CacheConstants.HEADER_VALUE_NO_CACHE)) { |
141 |
1 |
return 0; |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
18 |
if (isHeaderWithElement(header, CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_NO_CACHE)) { |
146 |
1 |
return 0; |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
17 |
if (isHeaderWithElement(header, CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_PRIVATE)) { |
151 |
1 |
return 0; |
152 |
|
} |
153 |
|
} |
154 |
|
|
155 |
11 |
int maxAge = -1; |
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
11 |
for (HeaderEntry header : headers) { |
162 |
16 |
HeaderElement element = getHeaderElement(header, CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_S_MAXAGE); |
163 |
16 |
if (element != null) { |
164 |
5 |
try { |
165 |
5 |
int n = Integer.parseInt(element.getValue()); |
166 |
5 |
if (maxAge == -1 || n < maxAge) { |
167 |
4 |
maxAge = n; |
168 |
|
} |
169 |
|
} catch (NumberFormatException e) { |
170 |
0 |
logger.warn("Ignoring unparseable " + CacheConstants.HEADER_CACHE_CONTROL + " header [" + header.toExternalFormat() + "]"); |
171 |
|
} |
172 |
|
} |
173 |
|
} |
174 |
|
|
175 |
11 |
if (maxAge != -1) { |
176 |
4 |
return maxAge; |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
7 |
for (HeaderEntry header : headers) { |
183 |
10 |
HeaderElement element = getHeaderElement(header, CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_MAX_AGE); |
184 |
10 |
if (element != null) { |
185 |
4 |
try { |
186 |
4 |
int n = Integer.parseInt(element.getValue()); |
187 |
4 |
if (maxAge == -1 || n < maxAge) { |
188 |
3 |
maxAge = n; |
189 |
|
} |
190 |
|
} catch (NumberFormatException e) { |
191 |
0 |
logger.warn("Ignoring unparseable " + CacheConstants.HEADER_CACHE_CONTROL + " header [" + header.toExternalFormat() + "]"); |
192 |
|
} |
193 |
|
} |
194 |
|
} |
195 |
|
|
196 |
7 |
if (maxAge != -1) { |
197 |
3 |
return maxAge; |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
|
|
202 |
4 |
for (HeaderEntry header : headers) { |
203 |
5 |
if (CacheConstants.HEADER_EXPIRES.equals(header.getName())) { |
204 |
|
|
205 |
5 |
Object value = header.getValue(); |
206 |
|
|
207 |
5 |
if (value instanceof Integer) { |
208 |
|
|
209 |
1 |
int n = (int) ((Integer) value - (System.currentTimeMillis() / 1000L)); |
210 |
1 |
if (maxAge == -1 || n < maxAge) { |
211 |
1 |
maxAge = n; |
212 |
|
} |
213 |
|
} |
214 |
5 |
if (value instanceof Long) { |
215 |
|
|
216 |
3 |
int n = (int) (((Long) value - System.currentTimeMillis()) / 1000L); |
217 |
3 |
if (maxAge == -1 || n < maxAge) { |
218 |
2 |
maxAge = n; |
219 |
|
} |
220 |
|
} |
221 |
5 |
if (value instanceof String) { |
222 |
1 |
String s = (String) value; |
223 |
|
|
224 |
|
|
225 |
1 |
if ("0".equals(s)) { |
226 |
0 |
return 0; |
227 |
|
} |
228 |
|
|
229 |
|
|
230 |
1 |
Date expires = DateUtils.parseDate(s); |
231 |
1 |
if (expires == null) { |
232 |
0 |
logger.warn("Ignoring unparsable {} header [{}]", CacheConstants.HEADER_EXPIRES, header.toExternalFormat()); |
233 |
0 |
continue; |
234 |
|
} |
235 |
1 |
int n = (int) (expires.getTime() - System.currentTimeMillis()); |
236 |
1 |
if (maxAge == -1 || n < maxAge) { |
237 |
1 |
maxAge = n; |
238 |
|
} |
239 |
|
} |
240 |
|
} |
241 |
|
} |
242 |
|
|
243 |
4 |
return maxAge; |
244 |
|
} |
245 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
246 |
56 |
private boolean isHeaderWithElement(HeaderEntry header, String headerName, String elementName) {... |
247 |
56 |
return getHeaderElement(header, headerName, elementName) != null; |
248 |
|
} |
249 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
250 |
82 |
private HeaderElement getHeaderElement(HeaderEntry header, String headerName, String elementName) {... |
251 |
82 |
if (headerName.equals(header.getName())) { |
252 |
37 |
HeaderElement[] elements = header.getElements(); |
253 |
37 |
for (HeaderElement element : elements) { |
254 |
43 |
if (element.getName().equals(elementName)) { |
255 |
14 |
return element; |
256 |
|
} |
257 |
|
} |
258 |
|
} |
259 |
68 |
return null; |
260 |
|
} |
261 |
|
} |