3
3
import java .util .regex .Matcher ;
4
4
import java .util .regex .Pattern ;
5
5
6
- /**
7
- * Represents a HTTP Cache-Control response header and parses it from string.
8
- *
9
- * <p>Note: This class ignores <tt>1#field-name</tt> parameter for
10
- * <tt>private</tt> and <tt>no-cache</tt> directive and cache extensions.</p>
11
- *
12
- * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9">HTTP/1.1 section 14.9</a>
13
- */
14
6
public class CacheControl {
15
7
16
8
17
- // copied from org.apache.abdera.protocol.util.CacheControlUtil
9
+ // see org.apache.abdera.protocol.util.CacheControlUtil
18
10
private static final Pattern PATTERN
19
11
= Pattern .compile ("\\ s*([\\ w\\ -]+)\\ s*(=)?\\ s*(\\ -?\\ d+|\\ \" ([^\" \\ \\ ]*(\\ \\ .[^\" \\ \\ ]*)*)+\\ \" )?\\ s*" );
20
12
21
- /**
22
- * Corresponds to the <tt>max-age</tt> cache control directive.
23
- * The default value is <tt>-1</tt>, i.e. not specified.
24
- *
25
- * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.3">HTTP/1.1 section 14.9.3</a>
26
- */
27
13
private int maxAge = -1 ;
28
14
29
- /**
30
- * Corresponds to the <tt>s-maxage</tt> cache control directive.
31
- * The default value is <tt>-1</tt>, i.e. not specified.
32
- *
33
- * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.3">HTTP/1.1 section 14.9.3</a>
34
- */
35
- private int sMaxAge = -1 ;
36
-
37
- /**
38
- * Whether the <tt>must-revalidate</tt> directive is specified.
39
- * The default value is <tt>false</tt>.
40
- *
41
- * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4">HTTP/1.1 section 14.9.4</a>
42
- */
43
15
private boolean isMustRevalidate = false ;
44
16
45
- /**
46
- * Whether the <tt>no-cache</tt> directive is specified.
47
- * The default value is <tt>false</tt>.
48
- *
49
- * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1">HTTP/1.1 section 14.9.1</a>
50
- */
51
17
private boolean isNoCache = false ;
52
18
53
- /**
54
- * Whether the <tt>no-store</tt> directive is specified.
55
- * The default value is <tt>false</tt>.
56
- *
57
- * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.2">HTTP/1.1 section 14.9.2</a>
58
- */
59
19
private boolean isNoStore = false ;
60
20
61
- /**
62
- * Whether the <tt>no-transform</tt> directive is specified.
63
- * The default value is <tt>false</tt>.
64
- *
65
- * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.5">HTTP/1.1 section 14.9.5</a>
66
- */
67
- private boolean isNoTransform = false ;
68
-
69
- /**
70
- * Whether the <tt>private</tt> directive is specified.
71
- * The default value is <tt>false</tt>.
72
- *
73
- * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1">HTTP/1.1 section 14.9.1</a>
74
- */
75
- private boolean isPrivate = false ;
76
-
77
- /**
78
- * Whether the <tt>public</tt> directive is specified.
79
- * The default value is <tt>false</tt>.
80
- *
81
- * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1">HTTP/1.1 section 14.9.1</a>
82
- */
83
- private boolean isPublic = false ;
84
-
85
- /**
86
- * Whether the <tt>proxy-revalidate</tt> directive is specified.
87
- * The default value is <tt>false</tt>.
88
- *
89
- * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4">HTTP/1.1 section 14.9.4</a>
90
- */
91
- private boolean isProxyRevalidate = false ;
92
-
93
-
94
- /**
95
- * Creates a new instance of CacheControl by parsing the supplied string.
96
- *
97
- * @param value A value the Cache-Control header.
98
- */
21
+
99
22
public static CacheControl valueOf (String value ) {
100
23
CacheControl cc = new CacheControl ();
101
24
@@ -105,48 +28,19 @@ public static CacheControl valueOf(String value) {
105
28
switch (matcher .group (1 ).toLowerCase ()) {
106
29
case "max-age" :
107
30
cc .setMaxAge (Integer .parseInt (matcher .group (3 ))); break ;
108
- case "s-maxage" :
109
- cc .setSMaxAge (Integer .parseInt (matcher .group (3 ))); break ;
110
31
case "must-revalidate" :
111
32
cc .setMustRevalidate (true ); break ;
112
33
case "no-cache" :
113
34
cc .setNoCache (true ); break ;
114
35
case "no-store" :
115
36
cc .setNoStore (true ); break ;
116
- case "no-transform" :
117
- cc .setNoTransform (true ); break ;
118
- case "private" :
119
- cc .setPrivate (true ); break ;
120
- case "public" :
121
- cc .setPublic (true ); break ;
122
- case "proxy-revalidate" :
123
- cc .setProxyRevalidate (true ); break ;
124
37
default : //ignore
125
38
}
126
39
}
127
40
}
128
41
return cc ;
129
42
}
130
43
131
- /**
132
- * Returns <tt>max-age</tt>, or <tt>s-maxage</tt> according to whether
133
- * considering a shared cache, or a private cache. If shared cache and the
134
- * <tt>s-maxage</tt> is negative (i.e. not set), then returns
135
- * <tt>max-age</tt> instead.
136
- *
137
- * @param sharedCache <tt>true</tt> for a shared cache,
138
- * or <tt>false</tt> for a private cache
139
- * @return A {@link #maxAge}, or {@link #sMaxAge} according to the given
140
- * sharedCache argument.
141
- */
142
- public int getMaxAge (boolean sharedCache ) {
143
- if (sharedCache ) {
144
- return sMaxAge >= 0 ? sMaxAge : maxAge ;
145
- } else {
146
- return maxAge ;
147
- }
148
- }
149
-
150
44
public void setMaxAge (int maxAge ) {
151
45
this .maxAge = maxAge ;
152
46
}
@@ -155,14 +49,6 @@ public int getMaxAge() {
155
49
return maxAge ;
156
50
}
157
51
158
- public int getSMaxAge () {
159
- return sMaxAge ;
160
- }
161
-
162
- public void setSMaxAge (int sMaxAge ) {
163
- this .sMaxAge = sMaxAge ;
164
- }
165
-
166
52
public boolean isMustRevalidate () {
167
53
return isMustRevalidate ;
168
54
}
@@ -187,50 +73,13 @@ public void setNoStore(boolean noStore) {
187
73
isNoStore = noStore ;
188
74
}
189
75
190
- public boolean isNoTransform () {
191
- return isNoTransform ;
192
- }
193
-
194
- public void setNoTransform (boolean noTransform ) {
195
- isNoTransform = noTransform ;
196
- }
197
-
198
- public boolean isPrivate () {
199
- return isPrivate ;
200
- }
201
-
202
- public void setPrivate (boolean aPrivate ) {
203
- isPrivate = aPrivate ;
204
- }
205
-
206
- public boolean isPublic () {
207
- return isPublic ;
208
- }
209
-
210
- public void setPublic (boolean aPublic ) {
211
- isPublic = aPublic ;
212
- }
213
-
214
- public boolean isProxyRevalidate () {
215
- return isProxyRevalidate ;
216
- }
217
-
218
- public void setProxyRevalidate (boolean proxyRevalidate ) {
219
- isProxyRevalidate = proxyRevalidate ;
220
- }
221
-
222
76
@ Override
223
77
public String toString () {
224
78
return "CacheControl{" +
225
79
"maxAge=" + maxAge +
226
- ", sMaxAge=" + sMaxAge +
227
80
", isMustRevalidate=" + isMustRevalidate +
228
81
", isNoCache=" + isNoCache +
229
82
", isNoStore=" + isNoStore +
230
- ", isNoTransform=" + isNoTransform +
231
- ", isPrivate=" + isPrivate +
232
- ", isPublic=" + isPublic +
233
- ", isProxyRevalidate=" + isProxyRevalidate +
234
83
'}' ;
235
84
}
236
85
}
0 commit comments