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.hc.client5.http.impl.cache;
28
29 import java.util.Iterator;
30 import java.util.Map;
31
32 import org.apache.hc.client5.http.cache.HeaderConstants;
33 import org.apache.hc.client5.http.cache.HttpCacheEntry;
34 import org.apache.hc.core5.function.Factory;
35 import org.apache.hc.core5.http.Header;
36 import org.apache.hc.core5.http.HeaderElement;
37 import org.apache.hc.core5.http.HttpRequest;
38 import org.apache.hc.core5.http.message.MessageSupport;
39
40 class ConditionalRequestBuilder<T extends HttpRequest> {
41
42 private final Factory<T, T> messageCopier;
43
44 ConditionalRequestBuilder(final Factory<T, T> messageCopier) {
45 this.messageCopier = messageCopier;
46 }
47
48
49
50
51
52
53
54
55
56
57
58 public T buildConditionalRequest(final T request, final HttpCacheEntry cacheEntry) {
59 final T newRequest = messageCopier.create(request);
60
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 final Iterator<HeaderElement> it = MessageSupport.iterate(cacheEntry, HeaderConstants.CACHE_CONTROL);
71 while (it.hasNext()) {
72 final HeaderElement elt = it.next();
73 if (HeaderConstants.CACHE_CONTROL_MUST_REVALIDATE.equalsIgnoreCase(elt.getName())
74 || HeaderConstants.CACHE_CONTROL_PROXY_REVALIDATE.equalsIgnoreCase(elt.getName())) {
75 mustRevalidate = true;
76 break;
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 T buildConditionalRequestFromVariants(final T request, final Map<String, Variant> variants) {
98 final T newRequest = messageCopier.create(request);
99
100
101 final StringBuilder etags = new StringBuilder();
102 boolean first = true;
103 for(final String etag : variants.keySet()) {
104 if (!first) {
105 etags.append(",");
106 }
107 first = false;
108 etags.append(etag);
109 }
110
111 newRequest.setHeader(HeaderConstants.IF_NONE_MATCH, etags.toString());
112 return newRequest;
113 }
114
115
116
117
118
119
120
121
122
123
124
125 public T buildUnconditionalRequest(final T request) {
126 final T newRequest = messageCopier.create(request);
127 newRequest.addHeader(HeaderConstants.CACHE_CONTROL,HeaderConstants.CACHE_CONTROL_NO_CACHE);
128 newRequest.addHeader(HeaderConstants.PRAGMA,HeaderConstants.CACHE_CONTROL_NO_CACHE);
129 newRequest.removeHeaders(HeaderConstants.IF_RANGE);
130 newRequest.removeHeaders(HeaderConstants.IF_MATCH);
131 newRequest.removeHeaders(HeaderConstants.IF_NONE_MATCH);
132 newRequest.removeHeaders(HeaderConstants.IF_UNMODIFIED_SINCE);
133 newRequest.removeHeaders(HeaderConstants.IF_MODIFIED_SINCE);
134 return newRequest;
135 }
136
137 }