PGBuildActionsServlet.java
10.6 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
199
200
201
202
203
204
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* javax.jcr.RepositoryException
* 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.resource.ValueMap
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.phonegap.impl.build.servlets;
import com.adobe.cq.mobile.phonegap.impl.PGBuildException;
import com.adobe.cq.mobile.phonegap.impl.PGException;
import com.adobe.cq.mobile.phonegap.impl.build.PGBErrorCodes;
import com.adobe.cq.mobile.phonegap.impl.build.PGBuildManager;
import com.adobe.cq.mobile.phonegap.impl.build.metadata.AccountInfo;
import com.adobe.cq.mobile.phonegap.impl.build.metadata.AppInfo;
import com.adobe.cq.mobile.phonegap.impl.build.service.PGBuildManagerAdapterFactory;
import com.day.cq.wcm.api.Page;
import java.io.IOException;
import java.io.PrintWriter;
import javax.jcr.RepositoryException;
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.resource.ValueMap;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Property(name="service.description", value={"PGBuild Actions Servlet"})
@Component(metatype=0)
@SlingServlet(methods={"GET"}, resourceTypes={"cq:Page"}, selectors={"pgbuild.getApp", "pgbuild.getAccountProfile"}, extensions={"json"}, generateComponent=0)
public class PGBuildActionsServlet
extends SlingAllMethodsServlet {
public static final String ACTION_PREFIX = "pgbuild.";
public static final String ACTION_GET_APP = "pgbuild.getApp";
public static final String ACTION_GET_ACCOUNT_PROFILE = "pgbuild.getAccountProfile";
private static final Logger LOGGER = LoggerFactory.getLogger(PGBuildActionsServlet.class);
private static final String PARAM_USER = "user";
private static final String PARAM_PASSWORD = "password";
private static final String KEY_ERROR = "error";
private static final String KEY_ERROR_CODE = "errorCode";
private static final String KEY_ERROR_DETAILS = "errorDetails";
private static final String KEY_RESPONSE = "response";
private static final String KEY_STATUS = "status";
private static final String STATUS_OK = "ok";
private static final String PN_PHONEGAP_BUILD_CONFIG = "phonegapConfig";
private static final String PN_PHONEGAP_BUILD_ID = "phonegap-buildId";
@Reference
PGBuildManagerAdapterFactory phonegapBuildManagerAdapterFactory;
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
String[] selectors = request.getRequestPathInfo().getSelectors();
String action = selectors[0] + "." + selectors[1];
try {
JSONObject jsonResponse;
ResourceResolver resolver = request.getResourceResolver();
Session session = (Session)resolver.adaptTo(Session.class);
Resource resource = request.getResource();
Page page = (Page)resource.adaptTo(Page.class);
PGBuildManager pgBuildManager = this.phonegapBuildManagerAdapterFactory.getAdapter((Object)session, PGBuildManager.class);
jsonResponse = new JSONObject();
if (page != null) {
try {
if (action.equals("pgbuild.getApp")) {
jsonResponse.put("response", (Object)PGBuildActionsServlet.getApp(pgBuildManager, page));
} else if (action.equals("pgbuild.getAccountProfile")) {
String user = request.getParameter("user");
String password = request.getParameter("password");
if (user == null || password == null || user.isEmpty() || password.isEmpty()) {
jsonResponse.put("response", (Object)PGBuildActionsServlet.getAccountProfile(pgBuildManager, page, session));
} else {
jsonResponse.put("response", (Object)PGBuildActionsServlet.getAccountProfile(pgBuildManager, user, password));
}
} else {
throw new Exception("Unknown request");
}
jsonResponse.put("status", (Object)"ok");
}
catch (PGException ex) {
LOGGER.debug("Failed request", (Throwable)ex);
jsonResponse.put("error", (Object)ex.getMessage());
PGBuildException pgBuildException = this.getPGBuildException(ex);
if (pgBuildException != null) {
jsonResponse.put("errorCode", (Object)pgBuildException.getErrorCode());
jsonResponse.put("errorDetails", (Object)pgBuildException.getErrorDetails());
}
}
catch (Exception ex) {
LOGGER.error("Failed request", (Throwable)ex);
jsonResponse.put("error", (Object)ex.getMessage());
}
} else {
jsonResponse.put("error", (Object)("Not a valid PGBuild enabled page" + resource.getPath()));
}
response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonResponse.toString());
}
catch (Exception ex) {
LOGGER.error("PBBuild Action failed: " + action, (Throwable)ex);
throw new ServletException((Throwable)ex);
}
}
private PGBuildException getPGBuildException(PGException pgException) {
if (pgException instanceof PGBuildException) {
return (PGBuildException)pgException;
}
Throwable causeException = pgException.getCause();
if (causeException != null && causeException instanceof PGException) {
return this.getPGBuildException((PGException)causeException);
}
return null;
}
private static JSONObject getAccountProfile(PGBuildManager pgBuildManager, Page page, Session session) throws PGException, JSONException {
PGBuildActionsServlet.validatePageProperty(page, "phonegapConfig", PGBErrorCodes.PGBERROR_CODE_PGBUILD_CONFIG_MISSING, PGBErrorCodes.PGBERROR_CODE_PGBUILD_CONFIG_UNLINKED, true, session);
AccountInfo accinfo = pgBuildManager.getAccountProfile(page);
if (accinfo != null) {
return accinfo.getJSON();
}
throw new PGException("Failed to get account profile");
}
private static JSONObject getAccountProfile(PGBuildManager pgBuildManager, String userId, String password) throws PGException, JSONException {
AccountInfo accInfo = pgBuildManager.getAccountProfile(userId, password);
if (accInfo != null) {
return accInfo.getJSON();
}
throw new PGException("Failed to get account profile");
}
private static JSONObject getApp(PGBuildManager pgBuildManager, Page page) throws PGException, JSONException {
PGBuildActionsServlet.validatePageProperty(page, "phonegap-buildId", PGBErrorCodes.PGBERROR_CODE_BUILD_ID_MISSING);
AppInfo appinfo = pgBuildManager.getApp(page);
if (appinfo != null) {
return appinfo.getJSON();
}
throw new PGException("Failed to get app for " + page.getPath());
}
private static void validatePageProperty(Page page, String propertyName, Integer missingErrorCode) throws PGBuildException {
PGBuildActionsServlet.validatePageProperty(page, propertyName, missingErrorCode, -1, false, null);
}
private static void validatePageProperty(Page page, String propertyName, Integer missingErrorCode, Integer unlinkedErrorCode, boolean isJcrPath, Session session) throws PGBuildException {
Resource contentRes = page.getContentResource();
if (contentRes == null) {
throw new PGBuildException("Property " + propertyName + " not found on page " + page.getPath(), missingErrorCode, "Property " + propertyName + " not found on page " + page.getPath());
}
ValueMap valueMap = (ValueMap)contentRes.adaptTo(ValueMap.class);
if (!valueMap.containsKey((Object)propertyName)) {
throw new PGBuildException("Property " + propertyName + " not found on page " + page.getPath(), missingErrorCode, "Property " + propertyName + " not found on page " + page.getPath());
}
if (isJcrPath) {
String cloudCfg = (String)valueMap.get(propertyName, String.class);
try {
if (!session.nodeExists(cloudCfg)) {
throw new PGBuildException("Property " + propertyName + " targets non-existent entity on " + page.getPath(), unlinkedErrorCode, "Property " + propertyName + " targets non-existent entity on " + page.getPath());
}
}
catch (RepositoryException repEx) {
throw new PGBuildException("Property " + propertyName + " targets non-existent entity on " + page.getPath(), unlinkedErrorCode, "Property " + propertyName + " targets non-existent entity on " + page.getPath());
}
}
}
protected void bindPhonegapBuildManagerAdapterFactory(PGBuildManagerAdapterFactory pGBuildManagerAdapterFactory) {
this.phonegapBuildManagerAdapterFactory = pGBuildManagerAdapterFactory;
}
protected void unbindPhonegapBuildManagerAdapterFactory(PGBuildManagerAdapterFactory pGBuildManagerAdapterFactory) {
if (this.phonegapBuildManagerAdapterFactory == pGBuildManagerAdapterFactory) {
this.phonegapBuildManagerAdapterFactory = null;
}
}
}