AMSUtils.java
6.88 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* javax.jcr.RepositoryException
* org.apache.commons.codec.binary.Base64
* org.apache.commons.io.IOUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.mobileservices.impl.util;
import com.adobe.cq.mobile.platform.impl.MobileAppException;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.jcr.RepositoryException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AMSUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(AMSUtils.class);
private static final String AMS_SERVICE_NAME = "mobileservices";
private static final String DPS_METADATA_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
private static SecureRandom rand = new SecureRandom();
public static JSONObject getADBMobileConfigJSON(Configuration config) throws JSONException, RepositoryException, IOException {
Resource content = config.getContentResource();
Resource adbmobileconfig = content.getChild("adbmobileconfig");
if (adbmobileconfig != null) {
InputStream jsonConfig = (InputStream)adbmobileconfig.adaptTo(InputStream.class);
try {
String json = IOUtils.toString((InputStream)jsonConfig);
return new JSONObject(json);
}
catch (IOException e) {
LOGGER.error("Unable to read {}", (Object)adbmobileconfig.getPath());
throw e;
}
}
LOGGER.warn("Unable to locate a adbmobileconfig entry for the config {}", (Object)config.getPath());
return null;
}
public static String getAMSVersionString(Configuration config) {
return AMSUtils.getDateAsAMSString(new Date(AMSUtils.getLastModified(config)));
}
public static Long getLastModified(Configuration config) {
long ts = config.getLastModified();
if (ts == 0) {
ts = (Long)config.getInherited("cq:lastModified", (Object)ts);
}
return ts;
}
public static String getDateAsAMSString(Date date) {
String stringDate = "";
if (date != null) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
stringDate = formatter.format(date);
}
return stringDate;
}
public static Configuration getAMSConfiguration(ConfigurationManager configurationManagerService, Page page) {
Configuration config = null;
try {
String[] cloudServiceConfigPathsArray = (String[])page.getProperties().get("cq:cloudserviceconfigs", String[].class);
if (cloudServiceConfigPathsArray != null) {
config = configurationManagerService.getConfiguration("mobileservices", cloudServiceConfigPathsArray);
}
}
catch (Exception ex) {
LOGGER.warn("Failed to get AMS account configuration", (Throwable)ex);
}
return config;
}
public static String getReportSuites(Configuration configuration) {
String[] accounts = (String[])configuration.getInherited("reportsuites", (Object)new String[0]);
String s_account = "";
boolean first = true;
for (int i = 0; i < accounts.length; ++i) {
String rsid = accounts[i].contains(";") ? accounts[i].substring(0, accounts[i].indexOf(";")) : accounts[i];
s_account = s_account + (first ? "" : ",") + rsid;
first = false;
}
return s_account;
}
public static String generateWSSEHeader(String clientId, String clientSecret) throws IOException, NoSuchAlgorithmException {
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
StringBuffer wsseHeader = new StringBuffer();
String created = dateFormatter.format(new Date());
ByteArrayOutputStream digest = new ByteArrayOutputStream(40);
byte[] nonce = new byte[20];
rand.nextBytes(nonce);
digest.write(nonce);
digest.write(created.getBytes());
digest.write(clientSecret.getBytes());
wsseHeader.append("UsernameToken Username=\"");
wsseHeader.append(clientId);
wsseHeader.append("\", PasswordDigest=\"");
wsseHeader.append(Base64.encodeBase64String((byte[])AMSUtils.toSHA1(digest.toByteArray())));
wsseHeader.append("\", Nonce=\"");
wsseHeader.append(Base64.encodeBase64String((byte[])nonce));
wsseHeader.append("\", Created=\"");
wsseHeader.append(created);
wsseHeader.append("\"");
return wsseHeader.toString();
}
public static Resource getCloudServiceConfiguration(Resource resource) {
if (resource == null || ResourceUtil.isNonExistingResource((Resource)resource) || resource.getParent() == null) {
return null;
}
Resource nextResource = resource;
while (nextResource.getParent() != null && nextResource.getParent().getParent() != null && !nextResource.getParent().getParent().getPath().equals("/etc/cloudservices")) {
nextResource = nextResource.getParent();
}
return nextResource;
}
public static boolean verifyMobileServicesFrameworkResource(Resource resource, boolean throwException) throws MobileAppException {
Resource content = resource.getChild("jcr:content");
boolean valid = content.isResourceType("mobileapps/components/mobileservicesframework");
if (!valid && throwException) {
String message = "Invalid resource. It must be an AMS Framework resource.";
LOGGER.error(message);
throw new MobileAppException(message);
}
return valid;
}
private static byte[] toSHA1(byte[] data) throws NoSuchAlgorithmException {
MessageDigest md = null;
md = MessageDigest.getInstance("SHA-1");
return md.digest(data);
}
}