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 package org.apache.http.impl.client.cache;
28
29 import java.util.Map;
30
31 import org.apache.http.Header;
32 import org.apache.http.HeaderElement;
33 import org.apache.http.ProtocolException;
34 import org.apache.http.annotation.Contract;
35 import org.apache.http.annotation.ThreadingBehavior;
36 import org.apache.http.client.cache.HeaderConstants;
37 import org.apache.http.client.cache.HttpCacheEntry;
38 import org.apache.http.client.methods.HttpRequestWrapper;
39
40
41
42
43 @Contract(threading = ThreadingBehavior.IMMUTABLE)
44 class ConditionalRequestBuilder {
45
46
47
48
49
50
51
52
53
54
55
56
57 public HttpRequestWrapper buildConditionalRequest(final HttpRequestWrapper request, final HttpCacheEntry cacheEntry)
58 throws ProtocolException {
59 final HttpRequestWrapper newRequest = HttpRequestWrapper.wrap(request.getOriginal());
60 newRequest.setHeaders(request.getAllHeaders());
61 final Header eTag = cacheEntry.getFirstHeader(HeaderConstants.ETAG);
62 if (eTag != null) {
63 newRequest.setHeader(HeaderConstants.IF_NONE_MATCH, eTag.getValue());
64 }
65 final Header lastModified = cacheEntry.getFirstHeader(HeaderConstants.LAST_MODIFIED);
66 if (lastModified != null) {
67 newRequest.setHeader(HeaderConstants.IF_MODIFIED_SINCE, lastModified.getValue());
68 }
69 boolean mustRevalidate = false;
70 for(final Header h : cacheEntry.getHeaders(HeaderConstants.CACHE_CONTROL)) {
71 for(final HeaderElement elt : h.getElements()) {
72 if (HeaderConstants.CACHE_CONTROL_MUST_REVALIDATE.equalsIgnoreCase(elt.getName())
73 || HeaderConstants.CACHE_CONTROL_PROXY_REVALIDATE.equalsIgnoreCase(elt.getName())) {
74 mustRevalidate = true;
75 break;
76 }
77 }
78 }
79 if (mustRevalidate) {
80 newRequest.addHeader(HeaderConstants.CACHE_CONTROL, HeaderConstants.CACHE_CONTROL_MAX_AGE + "=0");
81 }
82 return newRequest;
83
84 }
85
86
87
88
89
90
91
92
93
94
95
96
97 public HttpRequestWrapper buildConditionalRequestFromVariants(final HttpRequestWrapper request,
98 final Map<String, Variant> variants) {
99 final HttpRequestWrapper newRequest = HttpRequestWrapper.wrap(request.getOriginal());
100 newRequest.setHeaders(request.getAllHeaders());
101
102
103 final StringBuilder etags = new StringBuilder();
104 boolean first = true;
105 for(final String etag : variants.keySet()) {
106 if (!first) {
107 etags.append(",");
108 }
109 first = false;
110 etags.append(etag);
111 }
112
113 newRequest.setHeader(HeaderConstants.IF_NONE_MATCH, etags.toString());
114 return newRequest;
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128 public HttpRequestWrapper buildUnconditionalRequest(final HttpRequestWrapper request, final HttpCacheEntry entry) {
129 final HttpRequestWrapper newRequest = HttpRequestWrapper.wrap(request.getOriginal());
130 newRequest.setHeaders(request.getAllHeaders());
131 newRequest.addHeader(HeaderConstants.CACHE_CONTROL,HeaderConstants.CACHE_CONTROL_NO_CACHE);
132 newRequest.addHeader(HeaderConstants.PRAGMA,HeaderConstants.CACHE_CONTROL_NO_CACHE);
133 newRequest.removeHeaders(HeaderConstants.IF_RANGE);
134 newRequest.removeHeaders(HeaderConstants.IF_MATCH);
135 newRequest.removeHeaders(HeaderConstants.IF_NONE_MATCH);
136 newRequest.removeHeaders(HeaderConstants.IF_UNMODIFIED_SINCE);
137 newRequest.removeHeaders(HeaderConstants.IF_MODIFIED_SINCE);
138 return newRequest;
139 }
140
141 }