DPSConfigActionsGetServlet.java
9.69 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.dps.client.authorization.AuthorizationDao
* com.adobe.dps.client.authorization.Project
* com.adobe.dps.client.producer.utils.AccessToken
* com.adobe.dps.client.utils.Config
* com.day.cq.i18n.I18n
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* org.apache.commons.lang3.StringUtils
* 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.adapter.AdapterManager
* 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.JSONArray
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.dps.impl.servlets;
import com.adobe.cq.mobile.dps.DPSException;
import com.adobe.cq.mobile.dps.impl.DPSConnection;
import com.adobe.cq.mobile.dps.impl.service.AdobeDPSClient;
import com.adobe.cq.mobile.dps.impl.service.DPSConnectionAdapterFactory;
import com.adobe.cq.mobile.dps.impl.utils.DPSUtil;
import com.adobe.dps.client.authorization.AuthorizationDao;
import com.adobe.dps.client.authorization.Project;
import com.adobe.dps.client.producer.utils.AccessToken;
import com.adobe.dps.client.utils.Config;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
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.adapter.AdapterManager;
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.JSONArray;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Property(name="service.description", value={"Experience Manager Mobile Config Action GET Servlet"})
@Component(metatype=0)
@SlingServlet(methods={"GET"}, resourceTypes={"cq:Page"}, selectors={"getDPSProjects", "validateConnect"}, extensions={"json"}, generateComponent=0)
public class DPSConfigActionsGetServlet
extends SlingAllMethodsServlet {
public static final String ACTION_GET_PROJECTS = "getDPSProjects";
public static final String ACTION_GET_CONNECTION = "validateConnect";
private static final Logger LOGGER = LoggerFactory.getLogger(DPSConfigActionsGetServlet.class);
private static final String KEY_ERROR = "error";
private static final String KEY_STATUS = "status";
private static final String KEY_DURATION = "duration";
private static final String STATUS_VALID = "valid";
private static final String STATUS_INVALID = "invalid";
@Reference
AdapterManager adapterManager;
@Reference
DPSConnectionAdapterFactory dpsConnectionAdapterFactory;
@Reference
private AdobeDPSClient adobePublishClient;
private I18n i18n;
private static final String PARAM_DEVICE_ID = "deviceId";
private static final String PARAM_DEVICE_TOKEN = "deviceToken";
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
String[] selectors = request.getRequestPathInfo().getSelectors();
String action = selectors[0];
this.i18n = new I18n((HttpServletRequest)request);
try {
JSONObject jsonResponse;
Resource resource = request.getResource();
ConfigurationManager configurationManager = (ConfigurationManager)resource.getResourceResolver().adaptTo(ConfigurationManager.class);
Page page = (Page)resource.adaptTo(Page.class);
jsonResponse = new JSONObject();
if (page != null) {
try {
if ("getDPSProjects".equals(action)) {
Configuration dpsConfiguration = DPSUtil.getDPSConfiguration(configurationManager, page.getPath());
DPSConnection dpsConnection = this.dpsConnectionAdapterFactory.getAdapter((Object)dpsConfiguration, DPSConnection.class);
jsonResponse = this.getProjects(dpsConnection, page, configurationManager);
} else if ("validateConnect".equals(action)) {
long start = System.currentTimeMillis();
String deviceId = request.getParameter("deviceId");
if (StringUtils.isBlank((CharSequence)deviceId)) {
throw new ServletException(this.i18n.get("Missing mandatory request parameter {0}", "parameter name", new Object[]{"deviceId"}));
}
String deviceToken = request.getParameter("deviceToken");
if (StringUtils.isBlank((CharSequence)deviceToken)) {
throw new ServletException(this.i18n.get("Missing mandatory request parameter {0}", "parameter name", new Object[]{"deviceToken"}));
}
AdobeDPSClient.clearCache(deviceId + deviceToken);
Config config = this.adobePublishClient.getConfig(deviceId, deviceToken);
AccessToken accessToken = this.adobePublishClient.getAccessToken(config);
AuthorizationDao dao = new AuthorizationDao(config);
List projects = dao.getAuthorizedProjects(accessToken);
jsonResponse.put("status", (Object)(projects == null ? "invalid" : "valid"));
jsonResponse.put("duration", System.currentTimeMillis() - start);
}
}
catch (Exception ex) {
LOGGER.error("Experience Manager Mobile Action failed: " + action, (Throwable)ex);
jsonResponse.put("error", (Object)this.i18n.get(ex.getMessage()));
}
} else {
jsonResponse.put("error", (Object)this.i18n.get("Not a valid resource {0}", "path to resource", new Object[]{resource.getPath()}));
}
response.getWriter().print(jsonResponse.toString());
}
catch (Exception ex) {
LOGGER.error("Experience Manager Mobile Action failed: " + action, (Throwable)ex);
throw new ServletException((Throwable)ex);
}
}
private JSONObject getProjects(DPSConnection dpsConnection, Page page, ConfigurationManager configurationManager) throws Exception {
Configuration dpsConfiguration = DPSUtil.getDPSConfiguration(configurationManager, page.getPath());
if (dpsConfiguration == null) {
throw new DPSException(this.i18n.get("DPS CloudService configuration not found on: {0}", "path to page", new Object[]{page.getPath()}));
}
JSONObject projectsJSON = new JSONObject();
try {
JSONArray dpsProjects = new JSONArray();
AccessToken accessToken = dpsConnection.getAccessToken();
AuthorizationDao authorizationDao = dpsConnection.getAuthorizationAccess();
for (Project project : authorizationDao.getAuthorizedProjects(accessToken)) {
JSONObject projectJSON = new JSONObject();
projectJSON.put("projectId", (Object)project.projectGuid);
projectJSON.put("name", (Object)project.projectName);
projectJSON.put("title", (Object)project.projectTitle);
dpsProjects.put((Object)projectJSON);
}
projectsJSON.put("projects", (Object)dpsProjects);
}
catch (Exception ex) {
LOGGER.error("Failed to find projects", (Throwable)ex);
throw ex;
}
return projectsJSON;
}
protected void bindAdapterManager(AdapterManager adapterManager) {
this.adapterManager = adapterManager;
}
protected void unbindAdapterManager(AdapterManager adapterManager) {
if (this.adapterManager == adapterManager) {
this.adapterManager = null;
}
}
protected void bindDpsConnectionAdapterFactory(DPSConnectionAdapterFactory dPSConnectionAdapterFactory) {
this.dpsConnectionAdapterFactory = dPSConnectionAdapterFactory;
}
protected void unbindDpsConnectionAdapterFactory(DPSConnectionAdapterFactory dPSConnectionAdapterFactory) {
if (this.dpsConnectionAdapterFactory == dPSConnectionAdapterFactory) {
this.dpsConnectionAdapterFactory = null;
}
}
protected void bindAdobePublishClient(AdobeDPSClient adobeDPSClient) {
this.adobePublishClient = adobeDPSClient;
}
protected void unbindAdobePublishClient(AdobeDPSClient adobeDPSClient) {
if (this.adobePublishClient == adobeDPSClient) {
this.adobePublishClient = null;
}
}
}