AudienceManagerConfigurationAdapter.java
6.25 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.inherit.InheritanceValueMap
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Value
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Deactivate
* 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.sling.api.adapter.AdapterFactory
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.aam;
import com.adobe.cq.aam.client.spi.AudienceManagerConfiguration;
import com.day.cq.commons.inherit.InheritanceValueMap;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import org.apache.felix.scr.annotations.*;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import java.util.Arrays;
@Component(immediate=1, metatype=0)
@Service(value={AdapterFactory.class})
@Properties(value={@Property(name="adaptables", value={"org.apache.sling.api.resource.Resource"}), @Property(name="adapters", value={"com.adobe.cq.aam.client.spi.AudienceManagerConfiguration"})})
public class AudienceManagerConfigurationAdapter
implements AdapterFactory {
private static final String CQ_CLOUDSERVICECONFIGS = "cq:cloudserviceconfigs";
private static final Logger LOGGER = LoggerFactory.getLogger(AudienceManagerConfigurationAdapter.class);
@Reference
private ConfigurationManagerFactory configManagerFactory;
@Activate
public void activate() {
LOGGER.debug("Activate");
}
@Deactivate
public void deactivate() {
LOGGER.debug("Deactivate");
}
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
LOGGER.debug("Adapt atempt {} {} ", adaptable, type);
if (type == AudienceManagerConfiguration.class && adaptable instanceof Resource) {
Resource resource = (Resource)adaptable;
ResourceResolver resourceResolver = resource.getResourceResolver();
Page thisPage = this.getParentPage(resource);
if (thisPage != null) {
ConfigurationManager configManager;
Configuration config;
LOGGER.debug("Got Page {} {} ", (Object)thisPage.getPath());
Object[] cloudServiceConfigs = this.getStringValues(thisPage, "cq:cloudserviceconfigs");
LOGGER.debug("Got Cloud service configs {} ", (Object)Arrays.toString(cloudServiceConfigs));
if (cloudServiceConfigs.length != 0 && (config = (configManager = this.configManagerFactory.getConfigurationManager(resourceResolver)).getConfiguration("audiencemanager", (String[])cloudServiceConfigs)) != null) {
try {
return (AdapterType)new AudienceManagerConfiguration(config.getContentResource());
}
catch (IllegalArgumentException e) {
LOGGER.debug("Resource {} is not a resource of type {} ", (Object)config.getPath(), (Object)"cq/personalization/audiencemanager/components/cloudservices/audiencemanagerpage");
}
}
} else {
LOGGER.debug("Resource {} not adaptable to a Page ", (Object)resource.getPath());
}
}
LOGGER.debug("No Config found");
return null;
}
private String[] getStringValues(Page page, String propertyName) {
ValueMap properties = page.getProperties();
if (properties instanceof InheritanceValueMap) {
LOGGER.debug("Page has inheritable value map");
return (String[])((InheritanceValueMap)properties).getInherited(propertyName, String[].class);
}
LOGGER.debug("Page doe not have value map, wont check for inheritance.");
return this.getValues((Node)page.getContentResource().adaptTo(Node.class), propertyName);
}
private String[] getValues(Node node, String propertyName) {
block4 : {
try {
if (node == null || !node.hasProperty(propertyName)) break block4;
javax.jcr.Property p = node.getProperty(propertyName);
if (p.isMultiple()) {
Value[] v = p.getValues();
String[] vs = new String[v.length];
for (int i = 0; i < v.length; ++i) {
vs[i] = v[i].getString();
}
break block4;
}
return new String[]{p.getString()};
}
catch (RepositoryException e) {
LOGGER.debug(e.getMessage(), (Throwable)e);
}
}
return new String[0];
}
private Page getParentPage(Resource resource) {
Page page = (Page)resource.adaptTo(Page.class);
while (page == null && resource != null) {
if ((resource = resource.getParent()) == null) continue;
page = (Page)resource.adaptTo(Page.class);
}
return page;
}
protected void bindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
this.configManagerFactory = configurationManagerFactory;
}
protected void unbindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
if (this.configManagerFactory == configurationManagerFactory) {
this.configManagerFactory = null;
}
}
}