CreateMobileAppOperation.java
14.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.ui.components.HtmlResponse
* com.adobe.granite.xss.XSSAPI
* com.day.cq.commons.jcr.JcrUtil
* com.day.cq.i18n.I18n
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.WCMException
* com.day.text.Text
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.PathNotFoundException
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.nodetype.NodeType
* javax.servlet.http.HttpServletRequest
* org.apache.commons.lang3.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.request.RequestParameterMap
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.servlets.post.Modification
* org.apache.sling.servlets.post.ModificationType
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.platform.impl.operations;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.adobe.cq.mobile.platform.impl.operations.MobileAbstractOperation;
import com.adobe.cq.mobile.platform.impl.operations.MobileOperationException;
import com.adobe.cq.mobile.platform.impl.utils.MobileAppJCRUtil;
import com.adobe.granite.ui.components.HtmlResponse;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import com.day.text.Text;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
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.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.request.RequestParameterMap;
import org.apache.sling.api.resource.ModifiableValueMap;
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.servlets.post.Modification;
import org.apache.sling.servlets.post.ModificationType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0, label="Create Mobile App Operation")
@Service
@Properties(value={@Property(name="sling.post.operation", value={"mobileapps:createMobileApp"})})
public class CreateMobileAppOperation
extends MobileAbstractOperation {
private static final Logger LOGGER = LoggerFactory.getLogger(CreateMobileAppOperation.class);
public static final String OPERATION_NAME = "createMobileApp";
protected static final String PARAM_APP_ID = "appId";
protected static final String PARAM_TEMPLATE = "template";
protected static final String PARAM_PARENT_PATH = "parentPath";
protected static final String PARAM_PAGE_NAME = "pageName";
protected static final String PARAM_MERGE_SHELL = "mergeGroupAndShell";
protected static final String PARAM_SUBFOLDERS = "subfolders";
protected static final String PARAM_PAGE_DESC = "jcr:description";
protected static final String PARAM_PAGE_TITLE = "pageTitle";
protected static final String PARAM_ARCHIVE_PATH = "archivePath";
protected static final String PARAM_DASHBOARD_CONFIG = "pge-dashboard-config";
protected static final String PARAM_ICON_FILEREF = "widget/icon/fileReference";
protected static final String PN_APP_TYPE = "pge-type";
private static final String CQ_APPS_ADMIN_CREATEAPP_EDIT = "cq-apps-admin-createapp-edit";
@Override
protected void perform(SlingHttpServletRequest request, HtmlResponse response, List<Modification> modifications) {
I18n i18n = new I18n((HttpServletRequest)request);
try {
this.createPage(request, response, modifications, i18n);
}
catch (MobileOperationException e) {
String title = i18n.get("Error");
this.generateError(response, e.getMessage(), title);
}
catch (Exception ex) {
String message = i18n.get("The server has problem processing your request");
String title = i18n.get("Error");
if (ex.getCause() instanceof PathNotFoundException) {
message = i18n.get("The provided path does not exist") + ":<br/>" + ex.getCause().getMessage();
}
this.generateError(response, message, title);
}
}
protected Page createPage(SlingHttpServletRequest request, HtmlResponse response, List<Modification> modifications, I18n i18n) throws WCMException, MobileOperationException, UnsupportedEncodingException {
boolean mergeShellWithGroup;
ValueMap vm;
String parentPath = request.getParameter("parentPath");
String pageName = request.getParameter("pageName");
String appId = request.getParameter("appId");
String templatePath = request.getParameter("template");
String pageTitle = request.getParameter("pageTitle");
String appDescription = request.getParameter("jcr:description");
String appIcon = request.getParameter("widget/icon/fileReference");
String appDashboard = request.getParameter("pge-dashboard-config");
String mergeShell = request.getParameter("mergeGroupAndShell");
String[] subFolders = request.getParameterValues("subfolders");
if (StringUtils.isBlank((CharSequence)parentPath)) {
String message = i18n.get("Missing mandatory parameter {0}", null, new Object[]{"parentPath"});
throw new MobileOperationException(message);
}
if ("/content/phonegap".equalsIgnoreCase(parentPath)) {
parentPath = "/content/mobileapps";
}
if (StringUtils.isEmpty((CharSequence)pageName)) {
pageName = (String)request.getAttribute("pageName");
}
if (StringUtils.isBlank((CharSequence)pageTitle) && StringUtils.isBlank((CharSequence)pageName)) {
String message = i18n.get("Missing mandatory parameter {0}", null, new Object[]{"pageTitle"});
throw new MobileOperationException(message);
}
if (StringUtils.isBlank((CharSequence)templatePath)) {
String message = i18n.get("Missing mandatory parameter {0}", null, new Object[]{"template"});
throw new MobileOperationException(message);
}
String appType = null;
Resource templateRes = request.getResourceResolver().resolve(templatePath);
if (templateRes != null && (vm = templateRes.getValueMap()) != null) {
appType = (String)vm.get("pge-type", String.class);
}
PageManager pageManager = (PageManager)request.getResourceResolver().adaptTo(PageManager.class);
if (StringUtils.isNotEmpty((CharSequence)pageName)) {
pageName = JcrUtil.createValidName((String)pageName);
}
Page groupPage = pageManager.create(parentPath, pageName, templatePath, pageTitle, true);
Node groupNode = (Node)groupPage.adaptTo(Node.class);
try {
groupNode.addMixin("cq:MobileApp");
}
catch (RepositoryException e) {
this.log.error("Unable to add cq:MobileApp mixing to the node " + groupPage.getPath(), (Throwable)e);
throw new MobileOperationException(e.getMessage());
}
ArrayList<String> types = new ArrayList<String>();
types.add(MobileResourceType.GROUP.getType());
Page shellPage = null;
ModifiableValueMap groupPropertiesVM = (ModifiableValueMap)groupPage.getContentResource().adaptTo(ModifiableValueMap.class);
boolean bl = mergeShellWithGroup = StringUtils.isNotEmpty((CharSequence)mergeShell) && mergeShell.equalsIgnoreCase("true");
if (!mergeShellWithGroup) {
NodeIterator nodeIterator;
String archivePath = request.getParameter("archivePath");
if (StringUtils.isBlank((CharSequence)archivePath) && StringUtils.isBlank((CharSequence)appId)) {
String message = i18n.get("Missing mandatory parameter {0}", null, new Object[]{"appId"});
throw new MobileOperationException(message);
}
Resource templateResource = request.getResourceResolver().getResource(templatePath);
Node templateNode = (Node)templateResource.adaptTo(Node.class);
try {
nodeIterator = templateNode.getNodes();
}
catch (RepositoryException e) {
String message = i18n.get("Error retrieving nodes.");
throw new MobileOperationException(message);
}
while (nodeIterator.hasNext()) {
Node node = nodeIterator.nextNode();
try {
if (!"cq:Page".equals(node.getPrimaryNodeType().toString())) continue;
Node pageNode = JcrUtil.copy((Node)node, (Node)((Node)groupPage.adaptTo(Node.class)), (String)null);
Resource pageResource = request.getResourceResolver().getResource(pageNode.getPath());
MobileResource mobileResource = (MobileResource)pageResource.adaptTo(MobileResource.class);
if (!mobileResource.isA(MobileResourceType.INSTANCE.getType())) continue;
MobileAppJCRUtil.setMobileAppProperty(pageResource.getChild("jcr:content"), "widget/id", new String[]{appId});
MobileAppJCRUtil.setMobileAppProperty(pageResource.getChild("jcr:content"), "widget/name/text", new String[]{pageTitle});
MobileAppJCRUtil.setMobileAppProperty(pageResource.getChild("jcr:content"), "widget/description/text", new String[]{appDescription});
if (appIcon != null && !appIcon.isEmpty()) {
MobileAppJCRUtil.setMobileAppProperty(pageResource.getChild("jcr:content"), "widget/icon/fileReference", new String[]{appIcon});
}
shellPage = (Page)pageResource.adaptTo(Page.class);
continue;
}
catch (RepositoryException ex) {
String message = i18n.get("Error iterating nodes.");
throw new MobileOperationException(message);
}
}
} else {
shellPage = groupPage;
ModifiableValueMap shellPropertiesVM = groupPropertiesVM;
this.addAdditionalInformation(shellPropertiesVM, request);
if (StringUtils.isNotEmpty((CharSequence)appType)) {
types.add(appType);
types.add(MobileResourceType.INSTANCE.getType());
} else {
types.add(MobileResourceType.INSTANCE.getType());
}
shellPropertiesVM.put((Object)"pge-type", (Object)types.toArray(new String[types.size()]));
if (StringUtils.isNotEmpty((CharSequence)appDescription)) {
shellPropertiesVM.put((Object)"jcr:description", (Object)appDescription);
}
if (StringUtils.isNotBlank((CharSequence)appDashboard)) {
shellPropertiesVM.put((Object)"pge-dashboard-config", (Object)appDashboard);
}
}
Session session = (Session)request.getResourceResolver().adaptTo(Session.class);
String title = i18n.get("App created");
String message = i18n.get("Your App has been created.");
if (subFolders != null && subFolders.length > 0) {
for (String nextFolder : subFolders) {
String[] parts = nextFolder.split(";");
if (parts.length != 2) continue;
try {
JcrUtil.createPath((String)(groupPage.getPath() + "/" + parts[1]), (String)parts[0], (Session)session);
continue;
}
catch (RepositoryException repEx) {
message = i18n.get("Unable to create {}", groupPage.getPath() + "/" + parts[1]);
}
}
}
String openUrl = request.getContextPath() + "/libs/mobileapps/admin/content/dashboard.html" + shellPage.getPath();
response.addLink("cq-apps-admin-createapp-edit", this.xssAPI.getValidHref(openUrl), i18n.get("Manage App"));
try {
session.save();
}
catch (Exception ex) {
message = i18n.get("Error saving extra properties on {0}", "Path to Page", new Object[]{shellPage.getPath()});
throw new MobileOperationException(message);
}
modifications.add(new Modification(ModificationType.CREATE, groupPage.getPath(), groupPage.getPath()));
String redirectTo = this.getRedirect(request);
if (StringUtils.isBlank((CharSequence)redirectTo)) {
redirectTo = "/aem/apps.html" + Text.escapePath((String)parentPath);
}
this.generateResponse(response, 201, message, title, redirectTo, i18n.get("Done"));
return groupPage;
}
private void addAdditionalInformation(ModifiableValueMap mvp, SlingHttpServletRequest request) {
if (mvp == null) {
return;
}
for (Map.Entry parameter : request.getRequestParameterMap().entrySet()) {
if (!((String)parameter.getKey()).startsWith("app/")) continue;
String realKey = ((String)parameter.getKey()).substring(4);
for (int i = 0; i < ((RequestParameter[])parameter.getValue()).length; ++i) {
try {
String value = ((RequestParameter[])parameter.getValue())[i].getString("utf-8");
if (StringUtils.isEmpty((CharSequence)value)) continue;
mvp.put((Object)realKey, (Object)value);
continue;
}
catch (UnsupportedEncodingException encodingEx) {
LOGGER.warn("Error with encoding utf-8 string: " + realKey, (Throwable)encodingEx);
}
}
}
}
}