/
CacheControlProvider.java
219 lines (193 loc) · 7.76 KB
/
CacheControlProvider.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.message.internal;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.ws.rs.core.CacheControl;
import javax.inject.Singleton;
import org.glassfish.jersey.internal.LocalizationMessages;
import org.glassfish.jersey.spi.HeaderDelegateProvider;
import static org.glassfish.jersey.message.internal.Utils.throwIllegalArgumentExceptionIfNull;
/**
* {@code Cache-Control} {@link HeaderDelegateProvider header delegate provider}.
*
* @author Paul Sandoz
* @author Marek Potociar
* @author hubick@java.net
*/
@Singleton
public final class CacheControlProvider implements HeaderDelegateProvider<CacheControl> {
private static final Pattern WHITESPACE = Pattern.compile("\\s");
private static final Pattern COMMA_SEPARATED_LIST = Pattern.compile("[\\s]*,[\\s]*");
@Override
public boolean supports(Class<?> type) {
return type == CacheControl.class;
}
@Override
public String toString(CacheControl header) {
throwIllegalArgumentExceptionIfNull(header, LocalizationMessages.CACHE_CONTROL_IS_NULL());
StringBuilder b = new StringBuilder();
if (header.isPrivate()) {
appendQuotedWithSeparator(b, "private", buildListValue(header.getPrivateFields()));
}
if (header.isNoCache()) {
appendQuotedWithSeparator(b, "no-cache", buildListValue(header.getNoCacheFields()));
}
if (header.isNoStore()) {
appendWithSeparator(b, "no-store");
}
if (header.isNoTransform()) {
appendWithSeparator(b, "no-transform");
}
if (header.isMustRevalidate()) {
appendWithSeparator(b, "must-revalidate");
}
if (header.isProxyRevalidate()) {
appendWithSeparator(b, "proxy-revalidate");
}
if (header.getMaxAge() != -1) {
appendWithSeparator(b, "max-age", header.getMaxAge());
}
if (header.getSMaxAge() != -1) {
appendWithSeparator(b, "s-maxage", header.getSMaxAge());
}
for (Map.Entry<String, String> e : header.getCacheExtension().entrySet()) {
appendWithSeparator(b, e.getKey(), quoteIfWhitespace(e.getValue()));
}
return b.toString();
}
private void readFieldNames(List<String> fieldNames, HttpHeaderReader reader)
throws ParseException {
if (!reader.hasNextSeparator('=', false)) {
return;
}
reader.nextSeparator('=');
fieldNames.addAll(Arrays.asList(COMMA_SEPARATED_LIST.split(reader.nextQuotedString())));
}
private int readIntValue(HttpHeaderReader reader, String directiveName)
throws ParseException {
reader.nextSeparator('=');
int index = reader.getIndex();
try {
return Integer.parseInt(reader.nextToken().toString());
} catch (NumberFormatException nfe) {
ParseException pe = new ParseException(
"Error parsing integer value for " + directiveName + " directive", index);
pe.initCause(nfe);
throw pe;
}
}
private void readDirective(CacheControl cacheControl,
HttpHeaderReader reader) throws ParseException {
final String directiveName = reader.nextToken().toString().toLowerCase(Locale.ROOT);
if ("private".equals(directiveName)) {
cacheControl.setPrivate(true);
readFieldNames(cacheControl.getPrivateFields(), reader);
} else if ("public".equals(directiveName)) {
// CacheControl API doesn't support 'public' for some reason.
cacheControl.getCacheExtension().put(directiveName, null);
} else if ("no-cache".equals(directiveName)) {
cacheControl.setNoCache(true);
readFieldNames(cacheControl.getNoCacheFields(), reader);
} else if ("no-store".equals(directiveName)) {
cacheControl.setNoStore(true);
} else if ("no-transform".equals(directiveName)) {
cacheControl.setNoTransform(true);
} else if ("must-revalidate".equals(directiveName)) {
cacheControl.setMustRevalidate(true);
} else if ("proxy-revalidate".equals(directiveName)) {
cacheControl.setProxyRevalidate(true);
} else if ("max-age".equals(directiveName)) {
cacheControl.setMaxAge(readIntValue(reader, directiveName));
} else if ("s-maxage".equals(directiveName)) {
cacheControl.setSMaxAge(readIntValue(reader, directiveName));
} else {
String value = null;
if (reader.hasNextSeparator('=', false)) {
reader.nextSeparator('=');
value = reader.nextTokenOrQuotedString().toString();
}
cacheControl.getCacheExtension().put(directiveName, value);
}
}
@Override
public CacheControl fromString(String header) {
throwIllegalArgumentExceptionIfNull(header, LocalizationMessages.CACHE_CONTROL_IS_NULL());
try {
HttpHeaderReader reader = HttpHeaderReader.newInstance(header);
CacheControl cacheControl = new CacheControl();
cacheControl.setNoTransform(false); // defaults to true
while (reader.hasNext()) {
readDirective(cacheControl, reader);
if (reader.hasNextSeparator(',', true)) {
reader.nextSeparator(',');
}
}
return cacheControl;
} catch (ParseException pe) {
throw new IllegalArgumentException(
"Error parsing cache control '" + header + "'", pe);
}
}
private void appendWithSeparator(StringBuilder b, String field) {
if (b.length() > 0) {
b.append(", ");
}
b.append(field);
}
private void appendQuotedWithSeparator(StringBuilder b, String field, String value) {
appendWithSeparator(b, field);
if (value != null && !value.isEmpty()) {
b.append("=\"");
b.append(value);
b.append("\"");
}
}
private void appendWithSeparator(StringBuilder b, String field, String value) {
appendWithSeparator(b, field);
if (value != null && !value.isEmpty()) {
b.append("=");
b.append(value);
}
}
private void appendWithSeparator(StringBuilder b, String field, int value) {
appendWithSeparator(b, field);
b.append("=");
b.append(value);
}
private String buildListValue(List<String> values) {
StringBuilder b = new StringBuilder();
for (String value : values) {
appendWithSeparator(b, value);
}
return b.toString();
}
private String quoteIfWhitespace(String value) {
if (value == null) {
return null;
}
Matcher m = WHITESPACE.matcher(value);
if (m.find()) {
return "\"" + value + "\"";
}
return value;
}
}