MobileAppsPublishSettingsOperation.java
9.92 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.mobile.platform.MobileResource
* com.adobe.dps.client.authorization.AuthorizationDao
* com.adobe.dps.client.authorization.Project
* com.adobe.dps.client.producer.utils.AccessToken
* com.adobe.granite.ui.components.HtmlResponse
* com.day.cq.commons.inherit.InheritanceValueMap
* com.day.cq.i18n.I18n
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* javax.jcr.RepositoryException
* javax.jcr.Session
* 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.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.resource.ModifiableValueMap
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.servlets.post.Modification
*/
package com.adobe.cq.mobile.dps.impl.operations;
import com.adobe.cq.mobile.dps.DPSException;
import com.adobe.cq.mobile.dps.impl.DPSConnection;
import com.adobe.cq.mobile.dps.impl.operations.MobilePublishAbstractOperation;
import com.adobe.cq.mobile.dps.impl.service.DPSConnectionAdapterFactory;
import com.adobe.cq.mobile.platform.MobileResource;
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.granite.ui.components.HtmlResponse;
import com.day.cq.commons.inherit.InheritanceValueMap;
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.util.List;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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.Reference;
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.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.servlets.post.Modification;
@Component(metatype=0, label="Experience Manager Mobile Publish Updater")
@Service
@Properties(value={@Property(name="sling.post.operation", value={"dpsapps:appPublish"}), @Property(name="sling.servlet.methods", value={"POST"})})
public class MobileAppsPublishSettingsOperation
extends MobilePublishAbstractOperation {
public static final String PARAM_APP_INSTANCE = "appInstance";
public static final String PN_PROJECT_NAME = "dps-projectTitle";
@Reference
DPSConnectionAdapterFactory dpsConnectionAdapterFactory;
@Override
protected void perform(SlingHttpServletRequest request, HtmlResponse response, List<Modification> modifications) {
I18n i18n = new I18n((HttpServletRequest)request);
ResourceResolver resolver = request.getResourceResolver();
String projectTitle = null;
RequestParameter appInstanceParam = request.getRequestParameter("appInstance");
if (appInstanceParam == null) {
String message = i18n.get("Missing mandatory parameter {0}", null, new Object[]{"appInstance"});
String title = i18n.get("Error");
this.generateError(response, message, title);
return;
}
Resource appPublishInstance = resolver.getResource(appInstanceParam.toString());
if (appPublishInstance == null) {
String message = i18n.get("Unable to resolve mobile publish application {0}", null, new Object[]{appInstanceParam.toString()});
String title = i18n.get("Error");
this.generateError(response, message, title);
return;
}
MobileResource mobileResource = (MobileResource)appPublishInstance.adaptTo(MobileResource.class);
if (!mobileResource.isA(new String[]{"app-publish"})) {
String message = i18n.get("The resource {0} must be a mobile publish application", null, new Object[]{mobileResource.getPath()});
String title = i18n.get("Error");
this.generateError(response, message, title);
return;
}
String configPath = request.getParameter("dps-cloudConfig");
if (StringUtils.isEmpty((CharSequence)configPath)) {
String message = i18n.get("Missing mandatory parameter {0}", null, new Object[]{"dps-cloudConfig"});
String title = i18n.get("Error");
this.generateError(response, message, title);
return;
}
String projectId = request.getParameter("dps-projectId");
Session session = (Session)resolver.adaptTo(Session.class);
try {
if (!session.nodeExists(configPath) && StringUtils.isEmpty((CharSequence)configPath)) {
String message = i18n.get("Invalid Cloud Config Path {0}", "path", new Object[]{configPath});
String title = i18n.get("Error");
this.generateError(response, message, title);
return;
}
if (StringUtils.isNotEmpty((CharSequence)projectId)) {
Page page = (Page)appPublishInstance.adaptTo(Page.class);
ConfigurationManager configurationManager = (ConfigurationManager)resolver.adaptTo(ConfigurationManager.class);
Configuration dpsConfiguration = configurationManager.getConfiguration(configPath);
if (dpsConfiguration == null) {
String message = i18n.get("Experience Manager Mobile CloudService configuration not found on: ", "path", new Object[]{configPath});
String title = i18n.get("Error");
this.generateError(response, message, title);
return;
}
String type = (String)dpsConfiguration.getProperties().getInherited("sling:resourceType", String.class);
if (!type.endsWith("dps-cloudservice-config") && StringUtils.isEmpty((CharSequence)configPath)) {
String message = i18n.get("Invalid Cloud Config configuration (cloudservice-config) {0}", "path", new Object[]{dpsConfiguration.getPath()});
String title = i18n.get("Error");
this.generateError(response, message, title);
return;
}
DPSConnection dpsConnection = this.dpsConnectionAdapterFactory.getAdapter((Object)dpsConfiguration, DPSConnection.class);
boolean validProject = false;
AccessToken accessToken = dpsConnection.getAccessToken();
AuthorizationDao authorizationDao = dpsConnection.getAuthorizationAccess();
for (Project project : authorizationDao.getAuthorizedProjects(accessToken)) {
if (!project.projectGuid.equals(projectId)) continue;
projectTitle = project.projectTitle;
validProject = true;
break;
}
if (!validProject) {
String message = i18n.get("Project ID was invalid: ", "Project GUI", new Object[]{projectId});
String title = i18n.get("Error");
this.generateError(response, message, title);
return;
}
}
}
catch (RepositoryException repEx) {
String message = i18n.get("Respository error while validating and saving properties: {0}", "exception message", new Object[]{repEx.getMessage()});
String title = i18n.get("Error");
this.generateError(response, message, title, (Throwable)repEx);
return;
}
catch (DPSException dpsEx) {
String message = i18n.get("Experience Manager Mobile error while validating and saving properties: {0}", "exception message", new Object[]{dpsEx.getMessage()});
String title = i18n.get("Error");
this.generateError(response, message, title, dpsEx);
return;
}
ModifiableValueMap addPropertiesVM = (ModifiableValueMap)appPublishInstance.getChild("jcr:content").adaptTo(ModifiableValueMap.class);
addPropertiesVM.put((Object)"dps-cloudConfig", (Object)configPath);
if (StringUtils.isNotEmpty((CharSequence)projectId)) {
addPropertiesVM.put((Object)"dps-projectId", (Object)projectId);
addPropertiesVM.put((Object)"dps-projectTitle", (Object)projectTitle);
} else {
addPropertiesVM.remove((Object)"dps-projectId");
addPropertiesVM.remove((Object)"dps-projectTitle");
}
try {
if (session.hasPendingChanges()) {
session.save();
}
}
catch (Exception ex) {
String message = i18n.get("Error saving properties on {0}", "Path to Page", new Object[]{appPublishInstance.getPath()});
String title = i18n.get("Error");
this.generateError(response, message, title, ex);
}
}
protected void bindDpsConnectionAdapterFactory(DPSConnectionAdapterFactory dPSConnectionAdapterFactory) {
this.dpsConnectionAdapterFactory = dPSConnectionAdapterFactory;
}
protected void unbindDpsConnectionAdapterFactory(DPSConnectionAdapterFactory dPSConnectionAdapterFactory) {
if (this.dpsConnectionAdapterFactory == dPSConnectionAdapterFactory) {
this.dpsConnectionAdapterFactory = null;
}
}
}