BinaryLessContentBuilder.java 7.11 KB
/*
 * 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;
        }
    }
}