AppCacheManagerServlet.java
9.5 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.mobile.ui.AppCacheDataSource
* com.day.cq.contentsync.config.Config
* javax.jcr.Node
* javax.jcr.Session
* javax.servlet.ServletException
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.sling.SlingServlet
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.appcache.impl;
import com.adobe.cq.mobile.appcache.impl.AppCacheException;
import com.adobe.cq.mobile.appcache.impl.AppCacheManager;
import com.adobe.cq.mobile.appcache.impl.AppCacheManagerAdapterFactory;
import com.adobe.cq.mobile.appcache.impl.CacheReporter;
import com.adobe.cq.mobile.appcache.impl.ConfigUtil;
import com.adobe.cq.mobile.platform.impl.utils.MobileUtil;
import com.adobe.cq.mobile.ui.AppCacheDataSource;
import com.day.cq.contentsync.config.Config;
import java.io.IOException;
import java.io.PrintWriter;
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.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Property(name="service.description", value={"App Cache Manager Servlet"})
@Component(metatype=0)
@SlingServlet(methods={"GET", "POST"}, resourceTypes={"sling/servlet/default"}, selectors={"buildZips", "activateZips", "clear", "update", "fill", "hasUpdate", "updateDetails", "contentListing", "newupdate"}, extensions={"json"}, generateComponent=0)
public class AppCacheManagerServlet
extends SlingAllMethodsServlet {
private static final long serialVersionUID = -2535015041406286180L;
private static final Logger LOGGER = LoggerFactory.getLogger(AppCacheManagerServlet.class);
static final String ACTION_BUILDZIPS = "buildZips";
static final String ACTION_ACTIVATEZIPS = "activateZips";
static final String ACTION_CLEAR = "clear";
static final String ACTION_FILL = "fill";
static final String ACTION_UPDATE = "update";
static final String ACTION_NEWUPDATE = "newupdate";
static final String ACTION_UPDATES = "hasUpdate";
static final String ACTION_GET_UPDATE_DETAILS = "updateDetails";
static final String ACTION_GET_CONTENT_LISTING = "contentListing";
static final String RESPONSE_TEXT_NOOP = "Update not required";
private static final String PARAM_IF_MODIFIED_SINCE = "ifModifiedSince";
@Reference
private AppCacheManagerAdapterFactory appCacheManagerAdapterFactory;
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
block15 : {
try {
ResourceResolver resourceResolver = request.getResourceResolver();
Resource resource = request.getResource();
Config config = (Config)resource.adaptTo(Config.class);
Session session = (Session)resourceResolver.adaptTo(Session.class);
String action = request.getRequestPathInfo().getSelectorString();
AppCacheManager appCacheManager = this.appCacheManagerAdapterFactory.getAdapter((Object)session, AppCacheManager.class);
if (action.equals("clear")) {
appCacheManager.clearCache(config);
break block15;
}
if (action.equals("newupdate")) {
String desc;
String title = request.getParameter("title");
boolean updateWasRequired = appCacheManager.updateCache(config, title, desc = request.getParameter("desc"));
if (updateWasRequired) {
appCacheManager.buildZips(config);
} else {
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("status", (Object)"Update not required");
this.sendJSONResponse(response, jsonResponse);
}
break block15;
}
if (action.equals("fill")) {
appCacheManager.fillCache(config);
break block15;
}
if (action.equals("update")) {
String title = request.getParameter("title");
String desc = request.getParameter("desc");
appCacheManager.updateCache(config, title, desc);
break block15;
}
if (action.equals("buildZips")) {
appCacheManager.buildZips(config);
break block15;
}
if (action.equals("hasUpdate")) {
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);
boolean hasUpdates = appCacheManager.hasUpdates(config, new Date(ifModifiedSince));
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("updates", hasUpdates);
this.sendJSONResponse(response, jsonResponse);
break block15;
}
if (action.equals("updateDetails")) {
JSONObject jsonResponse = this.getJSON(resource);
this.sendJSONResponse(response, jsonResponse);
break block15;
}
if (action.equals("contentListing")) {
Long ifModifiedSince = null;
if (request.getParameter("ifModifiedSince") != null && request.getParameter("ifModifiedSince").length() > 0) {
ifModifiedSince = new Long(request.getParameter("ifModifiedSince"));
}
LOGGER.debug("ifModifiedSince: " + ifModifiedSince);
String cachePath = ConfigUtil.configToCachePath(config, session);
Node cacheFolder = session.getNode(cachePath);
CacheReporter listingUtil = new CacheReporter(cacheFolder);
JSONObject jsonResponse = listingUtil.getFileListingJSON(ifModifiedSince);
this.sendJSONResponse(response, jsonResponse);
break block15;
}
if (action.equals("activateZips")) {
String title = request.getParameter("title");
String desc = request.getParameter("desc");
appCacheManager.transferZips(config, title, desc);
session.save();
break block15;
}
throw new AppCacheException("Unknown request: " + action);
}
catch (Exception ex) {
LOGGER.error("Unexpected error during processing of action on : " + (Object)request.getResource(), (Throwable)ex);
throw new ServletException((Throwable)ex);
}
}
}
private void sendJSONResponse(SlingHttpServletResponse response, JSONObject jsonObject) throws IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("application/json");
response.getWriter().print(jsonObject.toString());
response.flushBuffer();
}
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
private JSONObject getJSON(Resource resource) throws Exception {
AppCacheDataSource appCacheDS = (AppCacheDataSource)resource.adaptTo(AppCacheDataSource.class);
if (appCacheDS != null) {
return appCacheDS.getCacheUpdates();
}
return null;
}
private String getDateString(long ts) {
String s = "";
if (ts != new Date(0).getTime()) {
s = MobileUtil.getDateAsString(new Date(ts));
}
return s;
}
protected void bindAppCacheManagerAdapterFactory(AppCacheManagerAdapterFactory appCacheManagerAdapterFactory) {
this.appCacheManagerAdapterFactory = appCacheManagerAdapterFactory;
}
protected void unbindAppCacheManagerAdapterFactory(AppCacheManagerAdapterFactory appCacheManagerAdapterFactory) {
if (this.appCacheManagerAdapterFactory == appCacheManagerAdapterFactory) {
this.appCacheManagerAdapterFactory = null;
}
}
}