S7ConfigResolverImpl.java
17.6 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoException
* com.adobe.granite.crypto.CryptoSupport
* com.day.cq.commons.inherit.InheritanceValueMap
* com.day.cq.dam.api.Asset
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
* javax.jcr.LoginException
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.jcr.api.SlingRepository
* org.apache.sling.jcr.resource.JcrResourceResolverFactory
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.scene7.impl;
import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.cq.commons.inherit.InheritanceValueMap;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.scene7.api.S7Config;
import com.day.cq.dam.scene7.api.S7ConfigResolver;
import com.day.cq.dam.scene7.impl.model.S7ConfigImpl;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import java.io.InvalidObjectException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.jcr.LoginException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
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.jcr.api.SlingRepository;
import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(label="%cq.scene7.service.name", description="%cq.scene7.service.description")
@Service
public class S7ConfigResolverImpl
implements S7ConfigResolver {
private static final Logger LOG = LoggerFactory.getLogger(S7ConfigResolverImpl.class);
@Reference
private ConfigurationManagerFactory configurationManagerFactory;
@Reference
private CryptoSupport cryptoSupport;
@Reference
private JcrResourceResolverFactory resourceResolverFactory;
@Reference
private SlingRepository slingRepository;
@Override
public S7Config getS7Config(ResourceResolver resourceResolver, String email, String password, String userRegion) {
S7ConfigImpl s7Config = null;
try {
if (!this.cryptoSupport.isProtected(password)) {
password = this.cryptoSupport.protect(password);
}
ConfigurationManager cfgManager = this.configurationManagerFactory.getConfigurationManager(resourceResolver);
Iterator cfgIterator = cfgManager.getConfigurations("/etc/cloudservices/scene7");
while (cfgIterator.hasNext()) {
Configuration configuration = (Configuration)cfgIterator.next();
if (configuration == null) continue;
InheritanceValueMap configProp = configuration.getProperties();
String _email = (String)configProp.get("email", String.class);
String _password = (String)configProp.get("password", String.class);
String _region = (String)configProp.get("region", String.class);
if (_email == null || !_email.equals(email) || _password == null || !_password.equals(password) || _region == null || !_region.equals(userRegion)) continue;
s7Config = new S7ConfigImpl(configuration);
break;
}
if (s7Config == null) {
s7Config = new S7ConfigImpl(email, password, userRegion);
}
}
catch (InvalidObjectException e) {
LOG.error("Error obtaining a valid S7Config object", (Throwable)e);
}
catch (CryptoException e) {
LOG.error("Error encrypting password", (Throwable)e);
}
return s7Config;
}
@Override
public S7Config getS7Config(ResourceResolver resourceResolver, String configPath) {
S7ConfigImpl s7Config = null;
if (configPath != null) {
ConfigurationManager cfgManager = this.configurationManagerFactory.getConfigurationManager(resourceResolver);
Configuration cfg = cfgManager.getConfiguration(configPath);
if (cfg == null) {
return null;
}
try {
s7Config = new S7ConfigImpl(cfg);
}
catch (InvalidObjectException e) {
LOG.error("Invalid S7Config object due to invalid constructor parameters", (Throwable)e);
}
}
return s7Config;
}
@Override
public List<S7Config> getS7Configurations(ResourceResolver resourceResolver, String configRootPath) {
ArrayList<S7Config> s7Configurations = new ArrayList<S7Config>();
if (StringUtils.isNotEmpty((String)configRootPath)) {
ConfigurationManager cfgManager = this.configurationManagerFactory.getConfigurationManager(resourceResolver);
Iterator cfgIterator = cfgManager.getConfigurations(configRootPath);
while (cfgIterator.hasNext()) {
Configuration cfg = (Configuration)cfgIterator.next();
try {
S7ConfigImpl s7Config = new S7ConfigImpl(cfg);
s7Configurations.add(s7Config);
}
catch (InvalidObjectException e) {
LOG.error("Invalid S7Config object due to invalid constructor parameters", (Throwable)e);
}
}
}
return s7Configurations;
}
@Override
public S7Config getSharedS7Config(ResourceResolver resourceResolver, S7Config s7Config) {
try {
ConfigurationManager cfgManager = this.configurationManagerFactory.getConfigurationManager(resourceResolver);
Configuration cfg = cfgManager.getConfiguration(s7Config.getCloudConfigPath());
S7ConfigImpl ret = new S7ConfigImpl(cfg, true);
return ret;
}
catch (Exception e) {
LOG.error("Invalid S7Config object due to invalid constructor parameters", (Throwable)e);
return null;
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Deprecated
@Override
public S7Config getS7ConfigForResource(Resource resource) {
S7Config s7Config;
s7Config = null;
if (resource != null) {
String scene7ConfigurationPath = null;
while (scene7ConfigurationPath == null) {
ValueMap properties = (ValueMap)resource.adaptTo(ValueMap.class);
if (properties != null) {
scene7ConfigurationPath = (String)properties.get("dam:scene7CloudConfigPath", String.class);
if (scene7ConfigurationPath == null && ((scene7ConfigurationPath = this.getConfigFromMetaData(resource)) != null || (resource = resource.getParent()) != null)) continue;
break;
}
LOG.debug("Cannot obtain the ValueMap for resource " + resource.getPath());
break;
}
if (scene7ConfigurationPath != null) {
Session configSession = null;
try {
Resource s7ConfigResource;
configSession = this.slingRepository.loginService("scene7configservice", null);
if (configSession != null && (s7ConfigResource = this.resourceResolverFactory.getResourceResolver(configSession).getResource(scene7ConfigurationPath)) != null) {
s7Config = (S7Config)s7ConfigResource.adaptTo(S7Config.class);
}
}
catch (RepositoryException ee) {
LOG.error("Could not access a S7Config for resource {}", (Object)(resource != null ? resource.getPath() : "null"));
}
finally {
if (configSession != null) {
configSession.logout();
configSession = null;
}
}
}
}
return s7Config;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Deprecated
@Override
public S7Config getS7ConfigForImageUrl(ResourceResolver resourceResolver, String imageUrl) {
if (imageUrl != null) {
List<S7Config> s7Configs = this.getS7Configurations(resourceResolver, "/etc/cloudservices/scene7");
if (s7Configs != null && !s7Configs.isEmpty()) {
Session configSession = null;
try {
configSession = this.slingRepository.loginService("scene7configservice", null);
ResourceResolver s7ConfResourceResolver = this.resourceResolverFactory.getResourceResolver(configSession);
s7Configs = this.getS7Configurations(s7ConfResourceResolver, "/etc/cloudservices/scene7");
}
catch (LoginException e) {
LOG.error("LoginException. Could not access a S7Config for {}", (Object)imageUrl);
}
catch (RepositoryException e) {
LOG.error("RepositoryException. Could not access a S7Config for {}", (Object)imageUrl);
}
finally {
if (configSession != null) {
configSession.logout();
configSession = null;
}
}
}
String isImage = "is/image/";
String e2 = "e2/";
String[] imageServers = new String[]{isImage, e2};
String imageServer = isImage;
String companyName = "";
for (String server : imageServers) {
if (!imageUrl.contains(server)) continue;
imageServer = server;
break;
}
if (imageUrl.contains(imageServer)) {
String domain = imageUrl.substring(0, imageUrl.indexOf(imageServer));
companyName = imageUrl.substring(imageUrl.indexOf(imageServer) + imageServer.length());
companyName = companyName.substring(0, companyName.indexOf("/"));
for (S7Config s7Config : s7Configs) {
if (s7Config.get("companyname") == null || !s7Config.get("companyname").equals(companyName)) continue;
if (s7Config.getPublishServer() != null && s7Config.getPublishServer().contains(domain) || domain.contains(s7Config.getPublishServer())) {
return s7Config;
}
if (s7Config.getPreviewServer() == null || !s7Config.getPreviewServer().contains(domain) && !domain.contains(s7Config.getPreviewServer())) continue;
return s7Config;
}
}
}
return null;
}
@Override
public S7Config getS7ConfigForAssetPath(ResourceResolver resourceResolver, String assetPath) {
if (assetPath != null && assetPath.length() > 0) {
List<S7Config> s7Configs = this.getS7Configurations(resourceResolver, "/etc/cloudservices/scene7");
for (S7Config s7Config : s7Configs) {
if (s7Config == null || s7Config.getTargetPath() == null || !assetPath.startsWith(s7Config.getTargetPath())) continue;
return s7Config;
}
}
return null;
}
private String getConfigFromMetaData(Resource resource) {
Resource metaData;
String scene7ConfigurationPath = null;
Asset asset = (Asset)resource.adaptTo(Asset.class);
if (asset != null && (metaData = resource.getChild("jcr:content/metadata")) != null) {
scene7ConfigurationPath = (String)((ValueMap)metaData.adaptTo(ValueMap.class)).get("dam:scene7CloudConfigPath", String.class);
}
return scene7ConfigurationPath;
}
@Override
public String getS7ConfigPathForResource(ResourceResolver resolver, Resource resource) {
if (resource != null) {
String scene7ConfigurationPath = null;
while (scene7ConfigurationPath == null) {
ValueMap properties = (ValueMap)resource.adaptTo(ValueMap.class);
if (properties != null) {
scene7ConfigurationPath = (String)properties.get("dam:scene7CloudConfigPath", String.class);
if (scene7ConfigurationPath == null && ((scene7ConfigurationPath = this.getConfigFromMetaData(resource)) != null || (resource = resource.getParent()) != null)) continue;
break;
}
LOG.debug("Cannot obtain the ValueMap for resource " + resource.getPath());
break;
}
if (scene7ConfigurationPath != null) {
return scene7ConfigurationPath;
}
}
return null;
}
@Override
public String getS7ConfigPathForImageUrl(ResourceResolver resolver, String domain, String companyName) {
if (StringUtils.isBlank((String)domain) || StringUtils.isBlank((String)companyName)) {
return null;
}
List<S7Config> s7Configs = this.getS7Configurations(resolver, "/etc/cloudservices/scene7");
if (s7Configs != null && !s7Configs.isEmpty()) {
for (S7Config s7Config : s7Configs) {
if (s7Config.get("companyname") == null || !s7Config.get("companyname").equals(companyName)) continue;
if (s7Config.getPublishServer() != null && s7Config.getPublishServer().contains(domain) || domain.contains(s7Config.getPublishServer())) {
return s7Config.getCloudConfigPath();
}
if (s7Config.getPreviewServer() == null || !s7Config.getPreviewServer().contains(domain) && !domain.contains(s7Config.getPreviewServer())) continue;
return s7Config.getCloudConfigPath();
}
}
return null;
}
@Override
public Map<String, String> parseDomainAndCompanyNameForUrl(String imageUrl) {
if (StringUtils.isBlank((String)imageUrl)) {
return null;
}
HashMap<String, String> map = new HashMap<String, String>();
String isImage = "is/image/";
String e2 = "e2/";
String[] imageServers = new String[]{isImage, e2};
String imageServer = isImage;
String companyName = "";
for (String server : imageServers) {
if (!imageUrl.contains(server)) continue;
imageServer = server;
break;
}
if (imageUrl.contains(imageServer)) {
String domain = imageUrl.substring(0, imageUrl.indexOf(imageServer));
companyName = imageUrl.substring(imageUrl.indexOf(imageServer) + imageServer.length());
companyName = companyName.substring(0, companyName.indexOf("/"));
map.put("domain", domain);
map.put("companyName", companyName);
}
return map;
}
@Override
public String getS7ConfigPathForAssetPath(ResourceResolver resolver, String assetPath) {
if (StringUtils.isBlank((String)assetPath)) {
return null;
}
List<S7Config> s7Configs = this.getS7Configurations(resolver, "/etc/cloudservices/scene7");
if (s7Configs == null || s7Configs.isEmpty()) {
for (S7Config s7Config : s7Configs) {
if (s7Config == null || s7Config.getTargetPath() == null || !assetPath.startsWith(s7Config.getTargetPath())) continue;
return s7Config.getCloudConfigPath();
}
}
return null;
}
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 bindResourceResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
this.resourceResolverFactory = jcrResourceResolverFactory;
}
protected void unbindResourceResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
if (this.resourceResolverFactory == jcrResourceResolverFactory) {
this.resourceResolverFactory = null;
}
}
protected void bindSlingRepository(SlingRepository slingRepository) {
this.slingRepository = slingRepository;
}
protected void unbindSlingRepository(SlingRepository slingRepository) {
if (this.slingRepository == slingRepository) {
this.slingRepository = null;
}
}
}