ReportingServicesSettingsProvider.java
13.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoException
* com.adobe.granite.crypto.CryptoSupport
* com.day.cq.analytics.sitecatalyst.AnalyticsPageNameContext
* com.day.cq.analytics.sitecatalyst.AnalyticsPageNameResolver
* com.day.cq.analytics.sitecatalyst.Framework
* com.day.cq.analytics.sitecatalyst.SitecatalystUtil
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.http.Header
* org.apache.http.HttpEntity
* org.apache.http.HttpResponse
* org.apache.http.NameValuePair
* org.apache.http.StatusLine
* org.apache.http.client.HttpClient
* org.apache.http.client.entity.EntityBuilder
* org.apache.http.client.methods.CloseableHttpResponse
* org.apache.http.client.methods.HttpPost
* org.apache.http.client.methods.HttpUriRequest
* org.apache.http.client.utils.HttpClientUtils
* org.apache.http.impl.client.CloseableHttpClient
* org.apache.http.impl.client.HttpClientBuilder
* org.apache.http.message.BasicNameValuePair
* org.apache.http.osgi.services.HttpClientBuilderFactory
* org.apache.http.util.EntityUtils
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.wrappers.ValueMapDecorator
* org.apache.sling.commons.json.JSONObject
* org.apache.sling.commons.osgi.PropertiesUtil
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.contentinsight.impl;
import com.adobe.cq.contentinsight.ProviderSettingsProvider;
import com.adobe.cq.contentinsight.impl.ConfigurationUtil;
import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.cq.analytics.sitecatalyst.AnalyticsPageNameContext;
import com.day.cq.analytics.sitecatalyst.AnalyticsPageNameResolver;
import com.day.cq.analytics.sitecatalyst.Framework;
import com.day.cq.analytics.sitecatalyst.SitecatalystUtil;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
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.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.osgi.services.HttpClientBuilderFactory;
import org.apache.http.util.EntityUtils;
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.api.wrappers.ValueMapDecorator;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, label="Reporting Services settings provider", description="Provides configured settings and generates authentication token for Reporting Services.")
@Service
public class ReportingServicesSettingsProvider
implements ProviderSettingsProvider {
private final Logger log;
private static final String DEFAULT_URL = "https://services1.omniture.com/p/rs/0.5";
private static final String DEFAULT_PROXY_URL = "/libs/cq/contentinsight/content/proxy.reportingservices.json";
@Property(name="reportingservices.url", label="URL", description="Reporting Service's base URL", value={"https://services1.omniture.com/p/rs/0.5"})
private String url;
@Property(name="reportingservices.proxy.url", label="IE 9 Proxy URL", description="URL of proxy for IE 9 support", propertyPrivate=1, value={"/libs/cq/contentinsight/content/proxy.reportingservices.json"})
private String proxyUrl;
@Reference
private ConfigurationManagerFactory configurationManagerFactory;
@Reference
private CryptoSupport cryptoSupport;
@Reference
private AnalyticsPageNameResolver pageNameResolver;
@Reference
private HttpClientBuilderFactory clientBuilderFactory;
public ReportingServicesSettingsProvider() {
this.log = LoggerFactory.getLogger(this.getClass());
}
@Override
public String getName() {
return "reportingservices";
}
@Override
public ValueMap getSettings(Resource resource) {
boolean active = false;
HashMap<String, Object> settings = new HashMap<String, Object>();
ConfigurationManager mgr = this.configurationManagerFactory.getConfigurationManager(resource.getResourceResolver());
Configuration configuration = ConfigurationUtil.findConfiguration(mgr, resource, "sitecatalyst");
if (configuration == null) {
configuration = mgr.getConfiguration(resource.getPath());
}
if (configuration != null) {
String reportSuite = SitecatalystUtil.getPublishPreferredReportSuite((Configuration)configuration);
if (reportSuite != null) {
settings.put("collection", "dms/" + reportSuite);
}
AnalyticsPageNameContext context = new AnalyticsPageNameContext(configuration, (Framework)configuration.getContentResource().adaptTo(Framework.class), resource.getResourceResolver(), resource, null, resource.getPath());
settings.put("pageName", this.pageNameResolver.getPageName(context));
String segment = (String)configuration.getInherited("segment", (Object)"");
if (!StringUtils.isBlank((String)segment)) {
settings.put("segment", segment);
}
String company = (String)configuration.getInherited("company", (Object)"");
String username = (String)configuration.getInherited("username", (Object)"");
String password = (String)configuration.getInherited("password", (Object)"");
String sharedSecret = (String)configuration.getInherited("secret", (Object)"");
String server = (String)configuration.getInherited("server", (Object)"");
if (!StringUtils.isBlank((String)sharedSecret)) {
if (this.cryptoSupport.isProtected(sharedSecret)) {
try {
sharedSecret = this.cryptoSupport.unprotect(sharedSecret);
}
catch (CryptoException e) {
this.log.error("Failed to decrypt the Analytics user's shared secret " + sharedSecret);
}
settings.put("sharedsecret", sharedSecret);
settings.put("username", username + ":" + company);
if (!StringUtils.isBlank((String)server)) {
settings.put("url", this.generateRsUrlForSharedSecret(server));
active = true;
} else {
this.log.error("Could not obtain the analytics server URL from the Analytics cloud config.");
}
}
} else {
try {
Map<String, String> auth;
if (this.cryptoSupport.isProtected(password)) {
password = this.cryptoSupport.unprotect(password);
}
if (!(auth = this.getAuthenticationDetails(this.url, company, username, password)).isEmpty()) {
settings.putAll(auth);
active = true;
} else {
this.log.error("Authentication with Reporting Services failed");
}
settings.put("defaultLink", configuration.getInherited("serviceAdminUrl", (Object)""));
}
catch (Exception e) {
this.log.error("Requesting Reporting Services' authentication token failed: ", (Throwable)e);
}
}
}
settings.put("proxyUrl", resource.getResourceResolver().map(this.proxyUrl));
settings.put("active", active);
return new ValueMapDecorator(settings);
}
private String generateRsUrlForSharedSecret(String analyticsUrl) {
String omnitureAPIUrl = analyticsUrl.substring(0, StringUtils.ordinalIndexOf((String)analyticsUrl, (String)"/", (int)3));
return omnitureAPIUrl + "/rs/0.5/";
}
@Override
public boolean isActive(Resource resource) {
ConfigurationManager mgr = this.configurationManagerFactory.getConfigurationManager(resource.getResourceResolver());
Configuration configuration = ConfigurationUtil.findConfiguration(mgr, resource, "sitecatalyst");
return configuration != null;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private Map<String, String> getAuthenticationDetails(String url, String company, String username, String password) throws Exception {
CloseableHttpClient httpClient = this.clientBuilderFactory.newBuilder().build();
HashMap<String, String> details = new HashMap<String, String>();
try {
HttpPost post = new HttpPost(url + "/AuthToken/");
EntityBuilder entityBuilder = EntityBuilder.create();
entityBuilder.setParameters(new NameValuePair[]{new BasicNameValuePair("username", company + "/" + username), new BasicNameValuePair("password", password)});
post.setEntity(entityBuilder.build());
CloseableHttpResponse httpResponse = null;
try {
Header location;
httpResponse = httpClient.execute((HttpUriRequest)post);
int status = httpResponse.getStatusLine().getStatusCode();
if (status == 200) {
JSONObject json = new JSONObject(EntityUtils.toString((HttpEntity)httpResponse.getEntity()));
details.put("headerName", "x-adobe-rs-auth");
details.put("headerValue", company + "/" + username + ":" + json.getString("token"));
} else if (status == 302 && (location = httpResponse.getFirstHeader("Location")) != null) {
Map<String, String> map = this.getAuthenticationDetails(location.getValue().replaceAll("/AuthToken/", ""), company, username, password);
return map;
}
}
finally {
HttpClientUtils.closeQuietly((HttpResponse)httpResponse);
}
details.put("url", url);
}
finally {
HttpClientUtils.closeQuietly((HttpClient)httpClient);
}
return details;
}
@Activate
protected void activate(ComponentContext ctx) {
this.url = PropertiesUtil.toString(ctx.getProperties().get("reportingservices.url"), (String)"https://services1.omniture.com/p/rs/0.5");
this.proxyUrl = PropertiesUtil.toString(ctx.getProperties().get("reportingservices.proxy.url"), (String)"/libs/cq/contentinsight/content/proxy.reportingservices.json");
}
protected void bindConfigurationManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
this.configurationManagerFactory = configurationManagerFactory;
}
protected void unbindConfigurationManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
if (this.configurationManagerFactory == configurationManagerFactory) {
this.configurationManagerFactory = null;
}
}
protected void bindCryptoSupport(CryptoSupport cryptoSupport) {
this.cryptoSupport = cryptoSupport;
}
protected void unbindCryptoSupport(CryptoSupport cryptoSupport) {
if (this.cryptoSupport == cryptoSupport) {
this.cryptoSupport = null;
}
}
protected void bindPageNameResolver(AnalyticsPageNameResolver analyticsPageNameResolver) {
this.pageNameResolver = analyticsPageNameResolver;
}
protected void unbindPageNameResolver(AnalyticsPageNameResolver analyticsPageNameResolver) {
if (this.pageNameResolver == analyticsPageNameResolver) {
this.pageNameResolver = null;
}
}
protected void bindClientBuilderFactory(HttpClientBuilderFactory httpClientBuilderFactory) {
this.clientBuilderFactory = httpClientBuilderFactory;
}
protected void unbindClientBuilderFactory(HttpClientBuilderFactory httpClientBuilderFactory) {
if (this.clientBuilderFactory == httpClientBuilderFactory) {
this.clientBuilderFactory = null;
}
}
}