RequestHelper.java
3.37 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
83
84
85
86
87
88
89
90
91
92
93
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.AccessDeniedException
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.servlet.http.HttpServletRequest
* javax.servlet.http.HttpServletResponse
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.commons;
import java.util.Calendar;
import javax.jcr.AccessDeniedException;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RequestHelper {
private static final Logger log = LoggerFactory.getLogger(RequestHelper.class);
public static boolean handleIfModifiedSince(HttpServletRequest req, HttpServletResponse resp, ValueMap properties) {
Calendar lastMod = (Calendar)properties.get("jcr:lastModified", Calendar.class);
if (lastMod == null) {
lastMod = (Calendar)properties.get("cq:lastModified", Calendar.class);
}
if (lastMod != null) {
long ims;
long modTime = lastMod.getTimeInMillis() / 1000;
if (modTime <= (ims = req.getDateHeader("If-Modified-Since") / 1000)) {
resp.setStatus(304);
return true;
}
resp.setDateHeader("Last-Modified", lastMod.getTimeInMillis());
}
return false;
}
public static boolean handleIfModifiedSince(HttpServletRequest req, HttpServletResponse resp, Node node) {
Calendar lastMod = null;
try {
Node scan = node;
while (scan.getDepth() > 0 && lastMod == null) {
if (scan.hasProperty("jcr:lastModified")) {
lastMod = scan.getProperty("jcr:lastModified").getDate();
continue;
}
if (scan.hasProperty("cq:lastModified")) {
lastMod = scan.getProperty("cq:lastModified").getDate();
continue;
}
scan = scan.getParent();
}
}
catch (AccessDeniedException e) {
log.debug("Error while searching for last modified property: " + (Object)e);
}
catch (RepositoryException e) {
log.warn("Error while searching for last modified property: " + (Object)e);
}
try {
if (lastMod == null && node.hasProperty("jcr:content/jcr:lastModified")) {
lastMod = node.getProperty("jcr:content/jcr:lastModified").getDate();
}
}
catch (AccessDeniedException e) {
log.debug("Error while searching for last modified property: " + (Object)e);
}
catch (RepositoryException e) {
log.warn("Error while searching for last modified property: " + (Object)e);
}
if (lastMod != null) {
long ims;
long modTime = lastMod.getTimeInMillis() / 1000;
if (modTime <= (ims = req.getDateHeader("If-Modified-Since") / 1000)) {
resp.setStatus(304);
return true;
}
resp.setDateHeader("Last-Modified", lastMod.getTimeInMillis());
}
return false;
}
}