OutboxManagerImpl.java 7.31 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.annotation.Nonnull
 *  javax.annotation.Nullable
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.jackrabbit.commons.JcrUtils
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.replication.impl;

import com.day.cq.replication.AccessDeniedException;
import com.day.cq.replication.Outbox;
import com.day.cq.replication.OutboxManager;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.impl.OutboxImpl;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.jcr.Node;
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.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service(value={OutboxManager.class})
public class OutboxManagerImpl
implements OutboxManager {
    private static final Logger log = LoggerFactory.getLogger(OutboxManagerImpl.class);
    public static final String DEFAULT_OUTBOX_PATH = "/var/replication/outbox";
    public static final String OUTBOXES_PATH = "/var/replication/outboxes";
    @Reference
    protected SlingRepository repository;

    @Nullable
    @Override
    public Outbox getOutbox(Session session, String name, boolean autoCreate) throws ReplicationException, AccessDeniedException {
        if (name == null || "..".equals(name) || name.indexOf(47) > 0) {
            throw new ReplicationException("Illegal outbox name: " + name);
        }
        return this.internalGetOutbox(session, "/var/replication/outboxes/" + name, autoCreate);
    }

    @Nonnull
    @Override
    public Outbox getDefaultOutbox(Session session) throws ReplicationException {
        return this.internalGetOutbox(session, "/var/replication/outbox", true);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    private Outbox internalGetOutbox(Session session, String path, boolean autoCreate) throws ReplicationException {
        Outbox outbox;
        Node outbox2 = null;
        if (session.nodeExists(path)) {
            outbox2 = session.getNode(path);
        } else if (autoCreate) {
            outbox2 = JcrUtils.getOrCreateByPath((String)path, (String)"sling:Folder", (String)"sling:OrderedFolder", (Session)session, (boolean)true);
        }
        if (outbox2 != null) return new OutboxImpl(outbox2);
        Session systemSession = null;
        try {
            systemSession = this.repository.loginService("replicationService", null);
            if (session.nodeExists(path)) {
                throw new AccessDeniedException("Not allowed on " + path);
            }
            outbox = null;
            if (systemSession == null) return outbox;
        }
        catch (Throwable var7_8) {
            try {
                if (systemSession == null) throw var7_8;
                systemSession.logout();
                throw var7_8;
            }
            catch (RepositoryException e) {
                throw new ReplicationException("Error accessing outbox", (Exception)e);
            }
        }
        systemSession.logout();
        return outbox;
    }

    @Deprecated
    @Override
    public synchronized void put(InputStream in) throws ReplicationException {
        Session systemSession = null;
        try {
            systemSession = this.repository.loginService("replicationService", null);
            this.getDefaultOutbox(systemSession).put(null, in);
        }
        catch (RepositoryException e) {
            throw new ReplicationException("Error while adding outbox element.", (Exception)e);
        }
        finally {
            if (systemSession != null) {
                systemSession.logout();
            }
        }
    }

    @Deprecated
    @Override
    public synchronized void put(ReplicationAction action) throws ReplicationException {
        Session systemSession = null;
        try {
            systemSession = this.repository.loginService("replicationService", null);
            this.getDefaultOutbox(systemSession).put(action, null);
        }
        catch (RepositoryException e) {
            throw new ReplicationException("Error while adding outbox element.", (Exception)e);
        }
        finally {
            if (systemSession != null) {
                systemSession.logout();
            }
        }
    }

    @Deprecated
    @Override
    public synchronized void put(ReplicationAction action, InputStream in) throws ReplicationException {
        Session systemSession = null;
        try {
            systemSession = this.repository.loginService("replicationService", null);
            this.getDefaultOutbox(systemSession).put(action, in);
        }
        catch (RepositoryException e) {
            throw new ReplicationException("Error while adding outbox element.", (Exception)e);
        }
        finally {
            if (systemSession != null) {
                systemSession.logout();
            }
        }
    }

    @Deprecated
    @Override
    public synchronized void fetch(Session session, Calendar time, OutputStream out) throws ReplicationException {
        this.getDefaultOutbox(session).fetch(time, out);
    }

    @Deprecated
    @Override
    public boolean checkPermission(Session session) {
        try {
            this.getDefaultOutbox(session);
            return true;
        }
        catch (ReplicationException e) {
            log.error("Cannot access outbox. User [{}] doesn't have sufficient permissions on [{}]", (Object)session.getUserID(), (Object)"/var/replication/outbox");
            return false;
        }
    }

    @Activate
    protected void activate(ComponentContext ctx) throws RepositoryException {
        Session session = null;
        try {
            session = this.repository.loginService("replicationService", null);
            JcrUtils.getOrCreateByPath((String)"/var/replication/outbox", (String)"sling:Folder", (String)"sling:Folder", (Session)session, (boolean)false);
            JcrUtils.getOrCreateByPath((String)"/var/replication/outboxes", (String)"sling:Folder", (String)"sling:Folder", (Session)session, (boolean)false);
            session.save();
        }
        finally {
            if (session != null) {
                session.logout();
            }
        }
    }

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

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