Java Reference
In-Depth Information
Content-Type: text/html; charset=ISO-8859-1
Cache-control: max-age=604800
Expires: Sun, 28 Apr 2013 15:12:46 GMT
Last-modified: Sat, 20 Apr 2013 09:55:04 GMT
ETag: "67099097696afcf1b67e"
Example 7-6
is a simple Java class for parsing and querying Cache-control headers.
Example 7-6. How to inspect a Cache-control header
import
java.util.Date
;
import
java.util.Locale
;
public
class
CacheControl
{
private
Date
maxAge
=
null
;
private
Date
sMaxAge
=
null
;
private
boolean
mustRevalidate
=
false
;
private
boolean
noCache
=
false
;
private
boolean
noStore
=
false
;
private
boolean
proxyRevalidate
=
false
;
private
boolean
publicCache
=
false
;
private
boolean
privateCache
=
false
;
public
CacheControl
(
String
s
)
{
if
(
s
==
null
||
!
s
.
contains
(
":"
))
{
return
;
// default policy
}
String
value
=
s
.
split
(
":"
)[
1
].
trim
();
String
[]
components
=
value
.
split
(
","
);
Date
now
=
new
Date
();
for
(
String
component
:
components
)
{
try
{
component
=
component
.
trim
().
toLowerCase
(
Locale
.
US
);
if
(
component
.
startsWith
(
"max-age="
))
{
int
secondsInTheFuture
=
Integer
.
parseInt
(
component
.
substring
(
8
));
maxAge
=
new
Date
(
now
.
getTime
()
+
1000
*
secondsInTheFuture
);
}
else
if
(
component
.
startsWith
(
"s-maxage="
))
{
int
secondsInTheFuture
=
Integer
.
parseInt
(
component
.
substring
(
8
));
sMaxAge
=
new
Date
(
now
.
getTime
()
+
1000
*
secondsInTheFuture
);
}
else
if
(
component
.
equals
(
"must-revalidate"
))
{
mustRevalidate
=
true
;
}
else
if
(
component
.
equals
(
"proxy-revalidate"
))
{
proxyRevalidate
=
true
;
}
else
if
(
component
.
equals
(
"no-cache"
))
{
noCache
=
true
;
}
else
if
(
component
.
equals
(
"public"
))
{
publicCache
=
true
;
}
else
if
(
component
.
equals
(
"private"
))
{
privateCache
=
true
;