SegmentAdapterFactory.java
6.67 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.personalization.Segment
* com.day.cq.personalization.Segment$Kind
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.PathNotFoundException
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Value
* org.apache.sling.api.adapter.AdapterFactory
* org.apache.sling.api.resource.Resource
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.personalization.impl;
import com.day.cq.personalization.Segment;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class SegmentAdapterFactory
implements AdapterFactory {
static final String[] ADAPTABLE_CLASSES = new String[]{Resource.class.getName()};
static final String[] ADAPTER_CLASSES = new String[]{Segment.class.getName()};
private static final String RT_LOGIC_AND = "cq/personalization/components/traits/logic/and";
private static final String RT_LOGIC_OR = "cq/personalization/components/traits/logic/or";
private static final String RT_REFERENCE = "cq/personalization/components/traits/reference";
private static final String PROFILE_PREFIX = "profile/";
private static final String PROFILE_UNSUPPORTED = "unsupported";
private static final Map<String, String> RESOURCE_TYPES_TO_PROPERTY_NAMES;
private static final Logger log;
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
try {
if (adaptable instanceof Resource && type == Segment.class) {
Segment adapted = this.adaptNodeToSegment((Node)((Resource)adaptable).adaptTo(Node.class));
return (AdapterType)adapted;
}
}
catch (RepositoryException e) {
log.warn("Unable to adapt " + adaptable + " to a " + type.getName(), (Throwable)e);
}
return null;
}
private Segment adaptNodeToSegment(Node node) throws PathNotFoundException, RepositoryException {
if (node == null) {
return null;
}
Node traits = node.getNode("jcr:content/traits");
String traitResourceType = traits.getProperty("sling:resourceType").getString();
if (!traitResourceType.equals("cq/personalization/components/traits/logic/and")) {
log.warn("Unable to handle root segment at {} of type {}", (Object)node.getPath(), (Object)traitResourceType);
return null;
}
Segment rootSegment = Segment.newLogicSegment((Segment.Kind)Segment.Kind.And);
this.processChildren(rootSegment, traits.getNode("andpar").getNodes());
return rootSegment;
}
private void processChildren(Segment rootSegment, NodeIterator children) throws RepositoryException {
while (children.hasNext()) {
String operator;
Node child = children.nextNode();
String resourceType = child.getProperty("sling:resourceType").getString();
if (resourceType.equals("cq/personalization/components/traits/logic/and")) {
Segment and = Segment.newLogicSegment((Segment.Kind)Segment.Kind.And);
rootSegment.addChild(and);
this.processChildren(and, child.getNode("andpar").getNodes());
continue;
}
if (resourceType.equals("cq/personalization/components/traits/logic/or")) {
Segment or = Segment.newLogicSegment((Segment.Kind)Segment.Kind.Or);
rootSegment.addChild(or);
this.processChildren(or, child.getNode("orpar").getNodes());
continue;
}
if ("cq/personalization/components/traits/reference".equals(resourceType)) {
String segmentPath = child.getProperty("segmentPath").getString();
child = child.getSession().getNode(segmentPath);
Node traits = child.getNode("jcr:content/traits");
this.processChildren(rootSegment, traits.getNode("andpar").getNodes());
continue;
}
String name = RESOURCE_TYPES_TO_PROPERTY_NAMES.get(resourceType);
if (name == null) {
log.warn("Unknown resource type {} at path {} ; skipping.", (Object)resourceType, (Object)child.getPath());
continue;
}
if ("unsupported".equals(name)) {
log.debug("Skippping unsupported segment type {} at path {}", (Object)resourceType, (Object)child.getPath());
continue;
}
if ("profile/".equals(name)) {
name = name + child.getProperty("name").getString();
}
Property valueProperty = child.getProperty("value");
String string = operator = child.hasProperty("operator") ? child.getProperty("operator").getString() : null;
if (valueProperty.isMultiple()) {
ArrayList<String> values = new ArrayList<String>(valueProperty.getValues().length);
for (Value value : valueProperty.getValues()) {
values.add(value.getString());
}
rootSegment.addChild(Segment.newDirectSegment((String)name, (String)operator, values));
continue;
}
rootSegment.addChild(Segment.newDirectSegment((String)name, (String)operator, (String)valueProperty.getString()));
}
}
static {
HashMap<String, String> resourceTypesToPropertyNames = new HashMap<String, String>();
resourceTypesToPropertyNames.put("cq/personalization/components/traits/profileproperty/generic", "profile/");
resourceTypesToPropertyNames.put("cq/personalization/components/traits/profileproperty/age", "profile/age");
resourceTypesToPropertyNames.put("cq/personalization/components/traits/surferinfo/keywords", "profile/keywords");
resourceTypesToPropertyNames.put("cq/personalization/components/traits/percentile", "percentile");
resourceTypesToPropertyNames.put("cq/personalization/components/traits/script", "unsupported");
RESOURCE_TYPES_TO_PROPERTY_NAMES = Collections.unmodifiableMap(resourceTypesToPropertyNames);
log = LoggerFactory.getLogger(SegmentAdapterFactory.class);
}
}