RepositoryTransportHandler.java 5.27 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.replication.impl.transport;

import com.day.cq.replication.AgentConfig;
import com.day.cq.replication.Outbox;
import com.day.cq.replication.OutboxManager;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationContent;
import com.day.cq.replication.ReplicationContentFactory;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.ReplicationResult;
import com.day.cq.replication.ReplicationTransaction;
import com.day.cq.replication.TransportContext;
import com.day.cq.replication.TransportHandler;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service(value={TransportHandler.class})
public class RepositoryTransportHandler
implements TransportHandler {
    private static final Logger log = LoggerFactory.getLogger(RepositoryTransportHandler.class);
    private static final String URI_PREFIX = "repo://";
    @Reference
    protected SlingRepository repository;
    @Reference
    protected OutboxManager outboxManager;

    @Override
    public boolean canHandle(AgentConfig config) {
        String uri = config == null ? null : config.getTransportURI();
        return uri != null && uri.startsWith("repo://");
    }

    @Override
    public ReplicationResult deliver(TransportContext ctx, ReplicationTransaction tx) throws ReplicationException {
        block16 : {
            ReplicationContent rc = tx.getContent();
            if (rc == null) {
                return ReplicationResult.OK;
            }
            String uri = ctx.getConfig().getTransportURI();
            String path = uri.substring("repo://".length() - 1);
            Session systemSession = null;
            try {
                Outbox outbox;
                systemSession = this.repository.loginService("replicationService", null);
                if ("/var/replication/outbox".equals(path)) {
                    outbox = this.outboxManager.getDefaultOutbox(systemSession);
                } else if (path.startsWith("/var/replication/outboxes")) {
                    String name = path.substring("/var/replication/outboxes".length() + 1);
                    outbox = this.outboxManager.getOutbox(systemSession, name, true);
                    if (outbox == null) {
                        throw new ReplicationException("Unable to retrieve outbox at " + path);
                    }
                } else {
                    log.error("Invalid outbox path: {}. Either /var/replication/outbox or below /var/replication/outboxes", (Object)path);
                    throw new ReplicationException("Unsupported outbox path: " + path);
                }
                log.info("using outbox for repository transport: {}", (Object)outbox.getPath());
                InputStream in = rc.getInputStream();
                if (in != null) {
                    outbox.put(tx.getAction(), in);
                    break block16;
                }
                if (tx.getAction().getType() == ReplicationActionType.ACTIVATE) {
                    ReplicationResult replicationResult = ReplicationResult.OK;
                    return replicationResult;
                }
                outbox.put(tx.getAction());
            }
            catch (IOException e) {
                String msg = "Unable to get content input stream.";
                throw new ReplicationException(msg, e);
            }
            catch (RepositoryException e) {
                throw new ReplicationException("Error while accessing repository", (Exception)e);
            }
            finally {
                if (systemSession != null) {
                    systemSession.logout();
                }
            }
        }
        return ReplicationResult.OK;
    }

    public ReplicationResult poll(TransportContext ctx, ReplicationTransaction tx, List<ReplicationContent> result, ReplicationContentFactory factory) throws ReplicationException {
        String msg = "Unsupported operation.";
        throw new ReplicationException(msg);
    }

    protected void bindRepository(SlingRepository slingRepository) {
        this.repository = slingRepository;
    }

    protected void unbindRepository(SlingRepository slingRepository) {
        if (this.repository == slingRepository) {
            this.repository = null;
        }
    }

    protected void bindOutboxManager(OutboxManager outboxManager) {
        this.outboxManager = outboxManager;
    }

    protected void unbindOutboxManager(OutboxManager outboxManager) {
        if (this.outboxManager == outboxManager) {
            this.outboxManager = null;
        }
    }
}