-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCacheControl.java
More file actions
82 lines (68 loc) · 2.08 KB
/
Copy pathCacheControl.java
File metadata and controls
82 lines (68 loc) · 2.08 KB
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
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;
}
} catch (RuntimeException ex) {
continue;
}
}
}
public Date getMaxAge() {
return maxAge;
}
public Date getSharedMaxAge() {
return sMaxAge;
}
public boolean mustRevalidate() {
return mustRevalidate;
}
public boolean proxyRevalidate() {
return proxyRevalidate;
}
public boolean noStore() {
return noStore;
}
public boolean noCache() {
return noCache;
}
public boolean publicCache() {
return publicCache;
}
public boolean privateCache() {
return privateCache;
}
}