BrightEdgeProxyServlet.java
11.3 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoSupport
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
* javax.servlet.ServletException
* javax.servlet.ServletOutputStream
* org.apache.commons.io.IOUtils
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.sling.SlingServlet
* org.apache.http.HttpEntity
* org.apache.http.HttpHost
* org.apache.http.HttpResponse
* org.apache.http.StatusLine
* org.apache.http.auth.AuthScheme
* org.apache.http.auth.AuthScope
* org.apache.http.auth.Credentials
* org.apache.http.auth.UsernamePasswordCredentials
* org.apache.http.client.AuthCache
* org.apache.http.client.CredentialsProvider
* org.apache.http.client.HttpClient
* org.apache.http.client.methods.CloseableHttpResponse
* org.apache.http.client.methods.HttpPost
* org.apache.http.client.methods.HttpUriRequest
* org.apache.http.client.protocol.HttpClientContext
* org.apache.http.client.utils.HttpClientUtils
* org.apache.http.entity.ContentType
* org.apache.http.entity.mime.MultipartEntityBuilder
* org.apache.http.impl.auth.BasicScheme
* org.apache.http.impl.client.BasicAuthCache
* org.apache.http.impl.client.BasicCredentialsProvider
* org.apache.http.impl.client.CloseableHttpClient
* org.apache.http.impl.client.HttpClientBuilder
* org.apache.http.osgi.services.HttpClientBuilderFactory
* org.apache.http.protocol.HttpContext
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.osgi.PropertiesUtil
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.contentinsight.impl.servlets;
import com.adobe.cq.contentinsight.impl.ConfigurationUtil;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Dictionary;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
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.protocol.HttpClientContext;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.osgi.services.HttpClientBuilderFactory;
import org.apache.http.protocol.HttpContext;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingServlet(generateComponent=1, metatype=1, resourceTypes={"cq/contentinsight/proxy"}, extensions={"json"}, selectors={"brightedge"}, methods={"POST"}, label="BrightEdge API proxy servlet", description="Proxy servlet for BrightEdge API adding authentication information")
public class BrightEdgeProxyServlet
extends SlingAllMethodsServlet {
private static final long serialVersionUID = -8002334255989952189L;
private static final String DEFAULT_URL = "https://api.brightedge.com/internal/recommendations";
private final Logger log;
@Reference
private ConfigurationManagerFactory configurationManagerFactory;
@Reference
private CryptoSupport cryptoSupport;
@Reference
private HttpClientBuilderFactory clientBuilderFactory;
@Property(name="brightedge.url", label="URL", description="BrightEdge's base URL", value={"https://api.brightedge.com/internal/recommendations"})
private String url;
public BrightEdgeProxyServlet() {
this.log = LoggerFactory.getLogger(this.getClass());
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException {
String pageUrl = request.getParameter("url");
boolean isTestConnect = "true".equalsIgnoreCase(request.getParameter("testConnect"));
RequestParameter html = request.getRequestParameter("html");
String suffix = request.getRequestPathInfo().getSuffix();
if (suffix == null && !isTestConnect) {
response.setStatus(400);
return;
}
Resource resource = request.getResourceResolver().resolve(suffix);
if (resource == null && !isTestConnect) {
response.setStatus(400);
return;
}
String username = "";
String password = "";
ConfigurationManager mgr = resource != null ? this.configurationManagerFactory.getConfigurationManager(resource.getResourceResolver()) : null;
Configuration configuration = ConfigurationUtil.findConfiguration(mgr, resource, "brightedge");
InputStream htmlContentIS = null;
if (configuration != null) {
username = (String)configuration.getInherited("username", (Object)"");
password = (String)configuration.getInherited("password", (Object)"");
} else if (isTestConnect) {
username = request.getParameter("u");
password = request.getParameter("p");
pageUrl = "/test/connection";
htmlContentIS = new ByteArrayInputStream(new byte[0]);
}
CloseableHttpClient httpClient = null;
try {
if (htmlContentIS == null) {
htmlContentIS = html.getInputStream();
}
if (pageUrl != null && pageUrl.length() != 0 && htmlContentIS != null) {
try {
if (this.cryptoSupport.isProtected(password)) {
password = this.cryptoSupport.unprotect(password);
}
}
catch (Exception e) {
this.log.error("Requesting Reporting Services' authentication token failed: ", (Throwable)e);
}
URL urlObj = new URL(this.url);
HttpHost target = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol());
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, (Credentials)new UsernamePasswordCredentials(username, password));
httpClient = this.clientBuilderFactory.newBuilder().setDefaultCredentialsProvider((CredentialsProvider)credsProvider).build();
HttpPost post = new HttpPost(this.url + "?url=" + this.url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("html_file", htmlContentIS, ContentType.DEFAULT_BINARY, "html.tmp");
HttpEntity entity = builder.build();
post.setEntity(entity);
BasicAuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(target, (AuthScheme)basicAuth);
HttpClientContext localContext = HttpClientContext.create();
localContext.setAuthCache((AuthCache)authCache);
CloseableHttpResponse postResponse = null;
try {
postResponse = httpClient.execute((HttpUriRequest)post, (HttpContext)localContext);
int status = postResponse.getStatusLine().getStatusCode();
if (200 != status) {
this.log.debug("Got error response from BrightEdge: response code=" + status);
}
IOUtils.copy((InputStream)postResponse.getEntity().getContent(), (OutputStream)response.getOutputStream());
response.setStatus(status);
}
finally {
HttpClientUtils.closeQuietly((HttpResponse)postResponse);
}
}
response.setStatus(400);
}
catch (IOException e) {
this.log.error(e.getMessage(), (Throwable)e);
response.setStatus(500);
}
finally {
HttpClientUtils.closeQuietly((HttpClient)httpClient);
}
}
protected void activate(ComponentContext ctx) {
this.url = PropertiesUtil.toString(ctx.getProperties().get("brightedge.url"), (String)"https://api.brightedge.com/internal/recommendations");
}
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 bindClientBuilderFactory(HttpClientBuilderFactory httpClientBuilderFactory) {
this.clientBuilderFactory = httpClientBuilderFactory;
}
protected void unbindClientBuilderFactory(HttpClientBuilderFactory httpClientBuilderFactory) {
if (this.clientBuilderFactory == httpClientBuilderFactory) {
this.clientBuilderFactory = null;
}
}
}