BinaryLessContentBuilder.java
7.11 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.durbo.DurboOutput
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.PropertyIterator
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.ReferenceCardinality
* org.apache.felix.scr.annotations.ReferencePolicy
* org.apache.felix.scr.annotations.Service
* org.apache.jackrabbit.api.ReferenceBinary
* org.apache.sling.commons.osgi.PropertiesUtil
* org.apache.sling.jcr.api.SlingRepository
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.replication.impl.content.durbo;
import com.day.cq.replication.ContentBuilder;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationContent;
import com.day.cq.replication.ReplicationContentFactory;
import com.day.cq.replication.ReplicationContentFilterChain;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.impl.bl.BinaryLessUtils;
import com.day.cq.replication.impl.content.durbo.DurboContentBuilder;
import com.day.cq.replication.impl.content.durbo.DurboUtil;
import com.day.durbo.DurboOutput;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.ReferenceBinary;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, inherit=0, label="%contentbuilder.binaryless.name", description="%contentbuilder.binaryless.description")
@Properties(value={@Property(name="service.description", value={"Adobe Granite Binary Less Durbo Content Builder"}), @Property(name="name", value={"binary-less"}, propertyPrivate=1)})
@Reference(referenceInterface=SlingRepository.class, bind="bindRepository", unbind="unbindRepository", policy=ReferencePolicy.STATIC, cardinality=ReferenceCardinality.MANDATORY_UNARY)
@Service(value={ContentBuilder.class})
public class BinaryLessContentBuilder
extends DurboContentBuilder
implements ContentBuilder {
public static final String NAME = "binary-less";
public static final String TITLE = "Binary less";
private final Logger log;
@Property(longValue={4132})
public static final String THRESHOLD = "binary.threshold";
private long threshold;
public BinaryLessContentBuilder() {
this.log = LoggerFactory.getLogger(this.getClass());
}
@Activate
@Override
protected void activate(Map<String, Object> props) {
this.threshold = PropertiesUtil.toLong((Object)props.get("binary.threshold"), (long)4132);
super.activate(props);
}
@Override
public String getTitle() {
return "Binary less";
}
@Override
public ReplicationContent create(Session session, ReplicationAction action, ReplicationContentFactory factory) throws ReplicationException {
return super.create(session, action, factory);
}
@Override
void write(DurboOutput out, Node node, boolean recursive, ReplicationContentFilterChain filters, Map<String, Object> parameters) throws IOException, RepositoryException {
String[] mixinTypes;
Object missingBinariesParameter;
List missingBinaries = new LinkedList();
if (parameters != null && (missingBinariesParameter = parameters.get("missingBinaries")) != null) {
missingBinaries = (List)missingBinariesParameter;
}
boolean isFrozen = this.isFrozenNode(node);
out.writeProperty("jcr:primaryType", this.getPrimaryNodeType(node, isFrozen));
if (node.isNodeType("{http://www.jcp.org/jcr/mix/1.0}referenceable")) {
out.writeProperty("jcr:uuid", this.getUUID(node, isFrozen));
}
if ((mixinTypes = this.getMixinNodeTypes(node, isFrozen)) != null && mixinTypes.length > 0) {
StringBuilder mixinTypeNames = new StringBuilder();
for (String mix : mixinTypes) {
mixinTypeNames.append(mix);
mixinTypeNames.append(',');
}
out.writeProperty("jcr:mixinTypes", mixinTypeNames.toString());
}
PropertyIterator iter = node.getProperties();
while (iter.hasNext()) {
String propertyName;
javax.jcr.Property property = iter.nextProperty();
if (!filters.accept(property) || DurboUtil.IGNORED_PROPERTIES.contains(propertyName = property.getName())) continue;
if (property.getType() == 2) {
String propertyPath = node.getPath() + "/" + propertyName;
Binary binary = property.getBinary();
if (!missingBinaries.contains(propertyPath) && binary instanceof ReferenceBinary && binary.getSize() > this.threshold) {
ReferenceBinary referenceBinary = (ReferenceBinary)binary;
if (referenceBinary.getReference() != null) {
String referencePropertyName = BinaryLessUtils.encodeProperty(propertyName);
out.writeProperty(referencePropertyName, referenceBinary.getReference() + ':' + binary.getSize());
if (!this.log.isDebugEnabled()) continue;
this.log.debug("node {} contains a reference in property {} for property {}", new Object[]{node.getPath(), referencePropertyName, propertyName});
continue;
}
out.writeProperty(property);
continue;
}
out.writeProperty(property);
continue;
}
out.writeProperty(property);
}
if (recursive) {
iter = node.getNodes();
while (iter.hasNext()) {
Node child = iter.nextNode();
if (!filters.accept(child)) continue;
out.openNode(child.getName());
this.write(out, child, filters.allowDescent(child), filters, parameters);
out.closeNode();
}
}
}
@Override
protected void bindRepository(SlingRepository r) {
this.repository = r;
}
@Override
protected void unbindRepository(SlingRepository r) {
if (this.repository == r) {
this.repository = null;
}
}
}