UpdatesServlet.java
6.78 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
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.TidyJSONWriter
* com.day.cq.contentsync.config.Config
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.Session
* javax.servlet.ServletException
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.osgi.service.cm.ConfigurationAdmin
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.appcache.impl;
import com.adobe.cq.mobile.appcache.impl.AppCacheManager;
import com.adobe.cq.mobile.appcache.impl.AppCacheManagerAdapterFactory;
import com.adobe.cq.mobile.appcache.impl.AppCacheUtil;
import com.day.cq.commons.TidyJSONWriter;
import com.day.cq.contentsync.config.Config;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Date;
import javax.jcr.Node;
import javax.jcr.Session;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.cm.ConfigurationAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0, label="CQ Content Sync Check for Updates Servlet")
@Service
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"contentsync/config"}, propertyPrivate=1), @Property(name="sling.servlet.methods", value={"GET"}, propertyPrivate=1), @Property(name="sling.servlet.selectors", value={"pge-updates"}, propertyPrivate=1), @Property(name="sling.servlet.extensions", value={"json"}, propertyPrivate=1)})
public class UpdatesServlet
extends SlingAllMethodsServlet {
private static final long serialVersionUID = -2909081134019367825L;
@Reference
private AppCacheManagerAdapterFactory appCacheManagerAdapterFactory;
@Reference
private ResourceResolverFactory resolverFactory;
@Reference
private ConfigurationAdmin configurationAdmin = null;
private static final Logger LOGGER = LoggerFactory.getLogger(UpdatesServlet.class);
private static final String PARAM_IF_MODIFIED_SINCE = "ifModifiedSince";
private static final String PARAM_RETURN_REDIRECT_PATH = "returnRedirectZipPath";
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
try {
Resource resource = request.getResource();
Config config = (Config)resource.adaptTo(Config.class);
Session session = (Session)request.getResourceResolver().adaptTo(Session.class);
long ifModifiedSince = request.getDateHeader("If-Modified-Since");
if (ifModifiedSince == -1 && request.getParameter("ifModifiedSince") != null && request.getParameter("ifModifiedSince").length() > 0) {
ifModifiedSince = new Long(request.getParameter("ifModifiedSince"));
}
LOGGER.debug("ifModifiedSince: " + ifModifiedSince);
String authorizable = AppCacheUtil.getAuthorizable(this.configurationAdmin, config);
AppCacheUtil.setupCacheACLs(this.resolverFactory, config, session, false, true, true, authorizable);
AppCacheManager appCacheManager = this.appCacheManagerAdapterFactory.getAdapter((Object)session, AppCacheManager.class);
boolean hasUpdates = appCacheManager.hasUpdates(config, new Date(ifModifiedSince));
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
TidyJSONWriter out = new TidyJSONWriter((Writer)response.getWriter());
out.object();
out.key("updates").value(hasUpdates);
if (hasUpdates) {
try {
String zipRedirectPath = AppCacheUtil.getZipPath(appCacheManager, config, new Date(ifModifiedSince), session);
if (session.nodeExists(zipRedirectPath)) {
Node zipNode = session.getNode(zipRedirectPath);
long size = zipNode.getProperty("jcr:content/jcr:data").getLength();
out.key("size").value(size);
String includeZipPathParam = request.getParameter("returnRedirectZipPath");
boolean includeZipPath = Boolean.parseBoolean(includeZipPathParam);
if (includeZipPath) {
out.key("zipPath").value((Object)zipRedirectPath);
}
}
}
catch (Exception ex) {
LOGGER.warn("Unable to report update size for " + config.getPath());
}
}
out.endObject();
}
catch (Exception e) {
throw new ServletException((Throwable)e);
}
}
protected void bindAppCacheManagerAdapterFactory(AppCacheManagerAdapterFactory appCacheManagerAdapterFactory) {
this.appCacheManagerAdapterFactory = appCacheManagerAdapterFactory;
}
protected void unbindAppCacheManagerAdapterFactory(AppCacheManagerAdapterFactory appCacheManagerAdapterFactory) {
if (this.appCacheManagerAdapterFactory == appCacheManagerAdapterFactory) {
this.appCacheManagerAdapterFactory = null;
}
}
protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resolverFactory = resourceResolverFactory;
}
protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resolverFactory == resourceResolverFactory) {
this.resolverFactory = null;
}
}
protected void bindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
this.configurationAdmin = configurationAdmin;
}
protected void unbindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
if (this.configurationAdmin == configurationAdmin) {
this.configurationAdmin = null;
}
}
}