MACTenantConfigurationValidationServlet.java
10.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.dam.mac.sync.api.SyncAgent
* com.adobe.cq.dam.mac.sync.api.SyncAgentFactory
* com.adobe.granite.crypto.CryptoException
* com.day.cq.i18n.I18n
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.sling.SlingServlet
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.LoginException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.osgi.framework.BundleContext
* org.osgi.service.cm.ConfigurationAdmin
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.dam.mac.sync.helper.impl.servlets;
import com.adobe.cq.dam.mac.sync.api.SyncAgent;
import com.adobe.cq.dam.mac.sync.api.SyncAgentFactory;
import com.adobe.cq.dam.mac.sync.helper.MacSyncHelper;
import com.adobe.cq.dam.mac.sync.helper.impl.MACSyncClient;
import com.adobe.cq.dam.mac.sync.helper.impl.MACSyncClientServiceTracker;
import com.adobe.cq.dam.mac.sync.helper.impl.MACTenantConfiguration;
import com.adobe.cq.dam.mac.sync.helper.impl.MACTenantConfigurationResolver;
import com.adobe.cq.dam.mac.sync.helper.impl.util.OAuthUtil;
import com.adobe.granite.crypto.CryptoException;
import com.day.cq.i18n.I18n;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.LoginException;
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.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.osgi.framework.BundleContext;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingServlet(methods={"POST"}, resourceTypes={"dam/components/marketingcloud/config", "dam/components/mediaportal/config"}, selectors={"validate"}, extensions={"html"})
public class MACTenantConfigurationValidationServlet
extends SlingAllMethodsServlet {
private static final Logger LOG = LoggerFactory.getLogger(MACTenantConfigurationValidationServlet.class);
private static final String CLIENT_ID = "clientId";
private static final String SCOPES = "oauthScopes";
private static final String TENANT_URL_PARAM = "tenantURL";
private static final String TENANT_ID = "tenantId";
private static final String AUDIENCE = "audience";
private static final String MAC_CONFIG_RESOURCE_TYPE = "dam/components/marketingcloud/config";
private static final String MP_CONFIG_RESOURCE_TYPE = "dam/components/mediaportal/config";
private static final String MP_KEY_PAIR_ALIAS = "mpreplication";
private static final long TIMEOUT = 5000;
private BundleContext bundleContext;
@Reference
private ConfigurationAdmin configurationAdmin = null;
@Reference
private SyncAgentFactory agentFactory = null;
@Reference
private MacSyncHelper macSyncHelper = null;
@Reference
private MACTenantConfigurationResolver mtcr = null;
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
block40 : {
response.setContentType("application/json");
String clientId = request.getParameter("clientId");
String scopes = request.getParameter("oauthScopes");
String tenantURL = request.getParameter("tenantURL");
String tenant = request.getParameter("tenantId");
String audience = request.getParameter("audience");
JSONObject ret = new JSONObject();
I18n i18n = new I18n((HttpServletRequest)request);
boolean error = false;
String errorMessage = null;
MACSyncClientServiceTracker st = null;
try {
if (StringUtils.isEmpty((String)clientId) || StringUtils.isEmpty((String)scopes) || StringUtils.isEmpty((String)tenantURL) || StringUtils.isEmpty((String)audience)) {
LOG.error("Invalid request: one of the required parameters is missing or does not have a value.");
error = true;
errorMessage = "Invalid request: one of the required parameters is missing or does not have a value.";
break block40;
}
Resource configRes = request.getResource();
String pid = null;
pid = null != configRes && configRes.getResourceType().startsWith("dam/components/mediaportal/config") ? OAuthUtil.updateAccessTokenProvider(this.configurationAdmin, clientId, scopes, audience, "mpreplication") : OAuthUtil.updateAccessTokenProvider(this.configurationAdmin, clientId, scopes, audience);
ResourceResolver rr = request.getResourceResolver();
MACTenantConfiguration macConfiguration = this.mtcr.getConfiguration(rr, configRes.getPath());
this.macSyncHelper.updateReplicationAgentProviderPid(this.agentFactory.getSyncAgent("replication"), rr, macConfiguration, pid);
st = new MACSyncClientServiceTracker(this.bundleContext, pid);
st.open();
MACSyncClient syncClient = (MACSyncClient)st.waitForService(5000);
if (syncClient != null) {
int status = syncClient.validateConnection(clientId, tenantURL, scopes, tenant);
if (status == 401) {
error = true;
errorMessage = i18n.get("Invalid configuration.");
}
break block40;
}
error = true;
errorMessage = i18n.get("not able to setup mac sync, could not get MACSyncClient Service.");
}
catch (MalformedURLException e) {
LOG.error("Malformed tenant URL: " + tenantURL, (Throwable)e);
error = true;
errorMessage = i18n.get("Malformed tenant URL.");
}
catch (IOException e) {
LOG.error("Unable to reach tenant server", (Throwable)e);
error = true;
errorMessage = "Unable to reach the tenant server. Is the tenant URL correct?";
}
catch (LoginException e) {
LOG.error("Unable to impersonate user", (Throwable)e);
error = true;
errorMessage = "Unable to validate connection";
}
catch (CryptoException e) {
LOG.error("Unable to get access token", (Throwable)e);
error = true;
errorMessage = "Unable to get access token";
}
catch (URISyntaxException e) {
LOG.error("Wrong tenant url", (Throwable)e);
error = true;
errorMessage = "Error parsing tenant url. Is the tenant URL correct?";
}
catch (IllegalArgumentException e) {
LOG.error("Configuration is not setup correctly", (Throwable)e);
error = true;
errorMessage = "Configuration is not setup correctly";
}
catch (InterruptedException e) {
LOG.error("Interrupted while waiting for service", (Throwable)e);
error = true;
errorMessage = "Interrupted while waiting for service MACSyncClient";
}
finally {
PrintWriter out = response.getWriter();
if (error) {
try {
ret.put("error", (Object)errorMessage);
}
catch (JSONException e) {
LOG.error("JSON exception while building response", (Throwable)e);
}
}
out.write(ret.toString());
if (st != null) {
st.close();
}
}
}
}
@Activate
private void activate(ComponentContext componentContext) {
this.bundleContext = componentContext.getBundleContext();
}
protected void bindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
this.configurationAdmin = configurationAdmin;
}
protected void unbindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
if (this.configurationAdmin == configurationAdmin) {
this.configurationAdmin = null;
}
}
protected void bindAgentFactory(SyncAgentFactory syncAgentFactory) {
this.agentFactory = syncAgentFactory;
}
protected void unbindAgentFactory(SyncAgentFactory syncAgentFactory) {
if (this.agentFactory == syncAgentFactory) {
this.agentFactory = null;
}
}
protected void bindMacSyncHelper(MacSyncHelper macSyncHelper) {
this.macSyncHelper = macSyncHelper;
}
protected void unbindMacSyncHelper(MacSyncHelper macSyncHelper) {
if (this.macSyncHelper == macSyncHelper) {
this.macSyncHelper = null;
}
}
protected void bindMtcr(MACTenantConfigurationResolver mACTenantConfigurationResolver) {
this.mtcr = mACTenantConfigurationResolver;
}
protected void unbindMtcr(MACTenantConfigurationResolver mACTenantConfigurationResolver) {
if (this.mtcr == mACTenantConfigurationResolver) {
this.mtcr = null;
}
}
}