MobileServicesHttpClientImpl.java
12.2 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoSupport
* org.apache.commons.io.IOUtils
* org.apache.commons.lang3.StringUtils
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Modified
* 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.http.HttpEntity
* org.apache.http.HttpResponse
* org.apache.http.StatusLine
* org.apache.http.client.methods.HttpGet
* org.apache.http.client.methods.HttpUriRequest
* org.apache.http.impl.client.CloseableHttpClient
* org.apache.http.impl.client.HttpClientBuilder
* org.apache.http.osgi.services.HttpClientBuilderFactory
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.json.JSONObject
* org.apache.sling.commons.osgi.PropertiesUtil
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.mobileservices.impl.service;
import com.adobe.cq.mobile.mobileservices.impl.service.MobileServicesHttpClient;
import com.adobe.cq.mobile.mobileservices.impl.util.AMSUtils;
import com.adobe.cq.mobile.platform.impl.MobileAppException;
import com.adobe.granite.crypto.CryptoSupport;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Modified;
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.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.osgi.services.HttpClientBuilderFactory;
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.JSONObject;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, label="%mobileservices.service.label", description="%mobileservices.service.description")
@Service(value={MobileServicesHttpClient.class})
@Properties(value={@Property(name="service.description", value={"Adobe Mobile Services HTTP Client"})})
public class MobileServicesHttpClientImpl
implements MobileServicesHttpClient {
@Property(label="%mobileservices.webapi.endpoint.name", description="%mobileservices.webapi.endpoint.description", value={"https://api.omniture.com"})
private static final String AMS_WEB_API_ENDPOINT = "ams.web.api.endpoint";
@Property(label="%mobileservices.webapi.sdkconfig.get.operation.path.name", description="%mobileservices.webapi.sdkconfig.description", value={"/ams/1.0/app/config/"})
private static final String GET_ADB_MOBILE_CONFIG_PATH = "get.sdk.config.path";
@Property(label="%mobileservices.webapi.mobileserviceapps.get.operation.path.name", description="%mobileservices.webapi.mobileserviceapps.description", value={"/ams/1.0/apps"})
private static final String GET_AMS_APPS_PATH = "get.mobileserviceapps.path";
private String apiEndpoint;
private String adbMobileConfigPath;
private String getAMSAppsPath;
@Reference
private CryptoSupport cryptoSupportService;
@Reference
private HttpClientBuilderFactory httpClientBuilderFactory;
private static final String JSON_KEY_ANALYTICS = "analytics";
private static final String JSON_KEY_SERVER = "server";
private static Logger logger = LoggerFactory.getLogger(MobileServicesHttpClientImpl.class);
@Override
public JSONObject getADBMobileConfig(String rsid, Resource resource) throws MobileAppException {
JSONObject json = null;
try {
if (resource == null || ResourceUtil.isNonExistingResource((Resource)resource)) {
String message = "The resource used to get ADB Mobile Config does not exist or is null.";
logger.error(message);
throw new MobileAppException(message);
}
Resource config = AMSUtils.getCloudServiceConfiguration(resource);
if (config == null || ResourceUtil.isNonExistingResource((Resource)config)) {
String message = "Could not find a Cloud Configuration from: " + resource.getPath();
logger.error(message);
throw new MobileAppException(message);
}
json = this.getAMSResponseWithConfig(this.apiEndpoint + this.adbMobileConfigPath + rsid, config);
JSONObject data = json.getJSONObject("data");
if (data != null) {
json = data;
}
Calendar lastImported = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss");
json.accumulate("lastImported", (Object)sdf.format(lastImported.getTime()));
}
catch (Exception ex) {
String message = "ADB Mobile Config error: " + ex.getMessage();
logger.error(message, (Throwable)ex);
throw new MobileAppException(message);
}
return json;
}
@Override
public JSONObject getApps(Resource resource) throws MobileAppException {
Resource config = AMSUtils.getCloudServiceConfiguration(resource);
if (config == null || ResourceUtil.isNonExistingResource((Resource)config)) {
String message = "Could not find a Cloud Configuration from: " + resource.getPath();
logger.error(message);
throw new MobileAppException(message);
}
JSONObject appListJSON = null;
try {
appListJSON = this.getAMSResponseWithConfig(this.apiEndpoint + this.getAMSAppsPath, config);
}
catch (Exception ex) {
throw new MobileAppException(ex.getMessage());
}
return appListJSON;
}
@Override
public JSONObject getApps(String company, String username, String secret, String server) throws MobileAppException {
if (!server.startsWith("https")) {
throw new MobileAppException("Cannot transfer sensitive information over non-secure URL. " + server);
}
if (StringUtils.isBlank((CharSequence)company) || StringUtils.isBlank((CharSequence)username) || StringUtils.isBlank((CharSequence)secret) || StringUtils.isBlank((CharSequence)server)) {
throw new MobileAppException("Invalid account information used.");
}
JSONObject appListJSON = null;
try {
appListJSON = this.getAMSResponse(this.apiEndpoint + this.getAMSAppsPath, company, secret, username);
}
catch (Exception ex) {
throw new MobileAppException(ex.getMessage());
}
return appListJSON;
}
@Override
public String getTrackingServer(Resource mobileServiceAppResource, String rsid) throws MobileAppException {
String trackingServer = null;
boolean isValidFormat = false;
try {
JSONObject analytics;
JSONObject adbMobileConfig = this.getADBMobileConfig(rsid, mobileServiceAppResource);
if (adbMobileConfig != null && adbMobileConfig.has("analytics") && (analytics = adbMobileConfig.getJSONObject("analytics")).has("server")) {
trackingServer = analytics.getString("server");
isValidFormat = true;
}
}
catch (Exception ex) {
throw new MobileAppException(ex.getMessage());
}
if (!isValidFormat) {
logger.error("Bad format on AMS Tracking Server field. Expected 'analytics -> server' in ADB Mobile Config JSON");
}
return trackingServer;
}
private JSONObject getAMSResponseWithConfig(String URL2, Resource analyticsConfig) throws MobileAppException {
Resource analyticsConfigContent = analyticsConfig.getChild("jcr:content");
ValueMap vm = analyticsConfigContent.getValueMap();
String username = (String)vm.get("username", String.class);
String secret = (String)vm.get("secret", String.class);
String company = (String)vm.get("company", String.class);
return this.getAMSResponse(URL2, company, secret, username);
}
private JSONObject getAMSResponse(String URL2, String company, String secret, String username) {
JSONObject responseJSON = null;
HttpGet get = new HttpGet(URL2);
InputStream inputStream = null;
try {
String usernameAuthToken = username + ":" + company;
get.addHeader("Accept", "application/json");
get.addHeader("Content-Type", "application/json");
get.addHeader("x-api-client-id", "aem-mobile");
String unprotectedSecret = secret;
if (this.cryptoSupportService.isProtected(secret)) {
unprotectedSecret = this.cryptoSupportService.unprotect(secret);
}
String wsseHeader = AMSUtils.generateWSSEHeader(usernameAuthToken, unprotectedSecret);
get.addHeader("X-WSSE", wsseHeader);
CloseableHttpClient httpClient = this.httpClientBuilderFactory.newBuilder().build();
HttpResponse response = httpClient.execute((HttpUriRequest)get);
StatusLine statusLine = response.getStatusLine();
if (statusLine != null) {
int status = statusLine.getStatusCode();
if (status != 200) {
throw new MobileAppException("AMS Http request failed: " + response.getStatusLine().getReasonPhrase());
}
} else {
throw new MobileAppException("AMS Http request failed: no status line in response.");
}
inputStream = response.getEntity().getContent();
String json = IOUtils.toString((InputStream)inputStream, (String)"utf-8");
responseJSON = new JSONObject(json);
}
catch (Exception ex) {
if (ex instanceof MobileAppException) {
throw (MobileAppException)ex;
}
String message = "AMS Web Api Manager error: " + ex.getMessage();
logger.error(message, (Throwable)ex);
throw new MobileAppException(message, ex);
}
finally {
IOUtils.closeQuietly((InputStream)inputStream);
}
return responseJSON;
}
@Activate
public void activate(Map<String, Object> properties) {
this.modified(properties);
}
@Modified
protected void modified(Map<String, Object> props) {
this.apiEndpoint = PropertiesUtil.toString((Object)props.get("ams.web.api.endpoint"), (String)"https://api.omniture.com");
this.adbMobileConfigPath = PropertiesUtil.toString((Object)props.get("get.sdk.config.path"), (String)"/ams/1.0/app/config/");
this.getAMSAppsPath = PropertiesUtil.toString((Object)props.get("get.mobileserviceapps.path"), (String)"/ams/1.0/apps");
}
protected void bindCryptoSupportService(CryptoSupport cryptoSupport) {
this.cryptoSupportService = cryptoSupport;
}
protected void unbindCryptoSupportService(CryptoSupport cryptoSupport) {
if (this.cryptoSupportService == cryptoSupport) {
this.cryptoSupportService = null;
}
}
protected void bindHttpClientBuilderFactory(HttpClientBuilderFactory httpClientBuilderFactory) {
this.httpClientBuilderFactory = httpClientBuilderFactory;
}
protected void unbindHttpClientBuilderFactory(HttpClientBuilderFactory httpClientBuilderFactory) {
if (this.httpClientBuilderFactory == httpClientBuilderFactory) {
this.httpClientBuilderFactory = null;
}
}
}