BinaryLessDurboImportTransformer.java 3.54 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.durbo.DurboInput
 *  com.day.durbo.DurboInput$Node
 *  com.day.durbo.DurboInput$Property
 *  javax.jcr.Binary
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Value
 *  javax.jcr.ValueFactory
 *  org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.replication.impl.content.durbo;

import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.impl.bl.BinaryLessUtils;
import com.day.cq.replication.impl.content.durbo.DurboImportConfiguration;
import com.day.cq.replication.impl.content.durbo.DurboImportTransformer;
import com.day.durbo.DurboInput;
import java.io.IOException;
import java.util.List;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BinaryLessDurboImportTransformer
extends DurboImportTransformer {
    private static final Logger logger = LoggerFactory.getLogger(DurboImportTransformer.class);

    BinaryLessDurboImportTransformer(DurboImportConfiguration configuration, Session session, DurboInput input) throws RepositoryException {
        super(configuration, session, input);
    }

    @Override
    protected void importSingleValue(DurboInput.Property property, Node createdNode, String name, Value value) throws RepositoryException {
        String propertyName = BinaryLessUtils.decodeProperty(name);
        if (propertyName != null) {
            String propertyString = property.getString();
            try {
                String referenceString;
                int u = propertyString.lastIndexOf(58);
                int size = 0;
                if (u > 0) {
                    referenceString = propertyString.substring(0, u);
                    size = Integer.valueOf(propertyString.substring(u + 1));
                } else {
                    referenceString = propertyString;
                }
                SimpleReferenceBinary ref = new SimpleReferenceBinary(referenceString);
                Binary referencedBinary = this.session.getValueFactory().createValue((Binary)ref).getBinary();
                if (size > 0 && (long)size != referencedBinary.getSize()) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("the referenced binary exists but content length doesn't match, need to resend it");
                    }
                    throw new RepositoryException("referenced binary content length doesn't match");
                }
                createdNode.setProperty(propertyName, referencedBinary);
                if (logger.isDebugEnabled()) {
                    logger.debug("property {} of {} set using a reference", new Object[]{propertyName, createdNode});
                }
            }
            catch (Throwable t) {
                this.failedPaths.add(createdNode.getPath() + "/" + propertyName);
                if (logger.isWarnEnabled()) {
                    logger.warn("could not retrieve binary from reference for property {} of node {}", new Object[]{propertyName, createdNode});
                }
                createdNode.setProperty(propertyName, "");
            }
        } else {
            createdNode.setProperty(name, value);
        }
    }
}