MobileClientCloudServicePushwooshHandler.java
7.67 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoException
* com.adobe.granite.crypto.CryptoSupport
* com.day.cq.i18n.I18n
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.WCMException
* javax.jcr.RepositoryException
* javax.jcr.Session
* 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.resource.ModifiableValueMap
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.mobilecloudservices.impl;
import com.adobe.cq.mobile.mobilecloudservices.impl.AbstractMobileClientCloudServicePushHandler;
import com.adobe.cq.mobile.platform.impl.MobileAppException;
import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
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 java.util.ArrayList;
import java.util.Calendar;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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.resource.ModifiableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service
@Properties(value={@Property(name="cloudservice.id", value={"rps-pushwoosh"})})
public class MobileClientCloudServicePushwooshHandler
extends AbstractMobileClientCloudServicePushHandler {
public static final String ID_PW_CLOUD_SERVICE = "rps-pushwoosh";
private static final String PROPERTY_PW_APP_ID = "appID";
private static final String PROPERTY_PW_ACCESS_TOKEN = "appAPIAccessToken";
private static final String PROPERTY_PW_GCM_ID = "gcmID";
private static final String DEFAULT_SERVICE_PATH = "/etc/cloudservices/rps-pushwoosh";
private static final String DEFAULT_RESOURCE_TYPE = "mobileapps/components/rps-pushwoosh";
private static final String DEFAULT_CQ_TEMPLATE = "/libs/mobileapps/templates/rps-pushwoosh";
private static final String DEFAULT_CONFIG_TITLE = "Provisioned Pushwoosh Configuration";
private String[] mandatoryProperties = new String[]{"appID", "appAPIAccessToken", "gcmID"};
@Reference
private CryptoSupport cryptoSupport;
private final Logger logger;
public MobileClientCloudServicePushwooshHandler() {
this.logger = LoggerFactory.getLogger(this.getClass());
}
@Override
public Resource createConfiguration(Map<String, Object> properties, I18n i18n, ResourceResolver resourceResolver) throws MobileAppException {
this.logger.info("Creating Pushwoosh Cloud Service Configuration...");
this.checkProperties(properties, i18n);
String pageTitle = this.getStringProperty(properties, "configName");
if (StringUtils.isBlank((CharSequence)pageTitle)) {
pageTitle = "Provisioned Pushwoosh Configuration";
}
String pageName = this.getName(properties, "Provisioned Pushwoosh Configuration");
Page newConfiguration = this.createPGBuildConfiguration(pageName, properties, pageTitle, resourceResolver);
this.saveConfigurationPublicNode((Resource)newConfiguration.adaptTo(Resource.class));
return resourceResolver.getResource(newConfiguration.getPath());
}
@Override
public void checkProperties(Map<String, Object> properties, I18n i18n) throws MobileAppException {
ArrayList<String> missing = new ArrayList<String>(this.mandatoryProperties.length + 1);
for (String nextMandatoryProperty : this.mandatoryProperties) {
String value = this.getStringProperty(properties, nextMandatoryProperty);
if (!StringUtils.isBlank((CharSequence)value)) continue;
missing.add(nextMandatoryProperty);
}
if (!missing.isEmpty()) {
String message = "Missing mandatory parameters: ";
if (i18n != null) {
message = i18n.get(message);
}
message = message + StringUtils.join(missing, (String)", ");
throw new MobileAppException(message);
}
}
@Override
public String getServicePath() {
return "/etc/cloudservices/rps-pushwoosh";
}
private Page createPGBuildConfiguration(String pageName, Map<String, Object> properties, String title, ResourceResolver resolver) throws MobileAppException {
Page configPage;
PageManager pm = (PageManager)resolver.adaptTo(PageManager.class);
I18n i18n = (I18n)resolver.adaptTo(I18n.class);
try {
configPage = pm.create("/etc/cloudservices/rps-pushwoosh", pageName, "/libs/mobileapps/templates/rps-pushwoosh", title, true);
}
catch (WCMException wcmEx) {
this.logger.error("Could not create Pushwoosh cloud configuration: " + pageName);
throw new MobileAppException(i18n.get("Could not create Pushwoosh cloud configuration: {0}", "config name", new Object[]{pageName}));
}
Session session = (Session)resolver.adaptTo(Session.class);
String userId = session.getUserID();
Calendar now = Calendar.getInstance();
String appId = this.getStringProperty(properties, "appID");
String accessToken = this.getStringProperty(properties, "appAPIAccessToken");
String gcmId = this.getStringProperty(properties, "gcmID");
ModifiableValueMap mvp = (ModifiableValueMap)configPage.getContentResource().adaptTo(ModifiableValueMap.class);
try {
mvp.put((Object)"appID", (Object)appId);
mvp.put((Object)"appAPIAccessToken", (Object)this.cryptoSupport.protect(accessToken));
mvp.put((Object)"gcmID", (Object)gcmId);
mvp.put((Object)"sling:resourceType", (Object)"mobileapps/components/rps-pushwoosh");
mvp.put((Object)"jcr:lastModified", (Object)now);
mvp.put((Object)"jcr:lastModifiedBy", (Object)userId);
mvp.put((Object)"cq:lastModified", (Object)now);
mvp.put((Object)"cq:lastModifiedBy", (Object)userId);
mvp.put((Object)"cq:cloudservicename", (Object)"rps-pushwoosh");
if (session.hasPendingChanges()) {
session.save();
}
}
catch (CryptoException cryptoEx) {
this.logger.error("Could not set the Pushwoosh cloud configuration password.");
throw new MobileAppException(i18n.get("Could not set the Pushwoosh cloud configuration password."));
}
catch (RepositoryException repEx) {
this.logger.error("Error saving properties on new cloud config: " + configPage.getPath());
throw new MobileAppException(i18n.get("Error saving properties on new cloud config: {0}", "path to config", new Object[]{configPage.getPath()}));
}
return configPage;
}
protected void bindCryptoSupport(CryptoSupport cryptoSupport) {
this.cryptoSupport = cryptoSupport;
}
protected void unbindCryptoSupport(CryptoSupport cryptoSupport) {
if (this.cryptoSupport == cryptoSupport) {
this.cryptoSupport = null;
}
}
}