StaticTransportHandler.java 7.02 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.io.FileUtils
 *  org.apache.commons.io.IOUtils
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Deactivate
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.replication.impl.transport;

import com.day.cq.replication.AgentConfig;
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.ReplicationLog;
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.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service(value={TransportHandler.class})
public class StaticTransportHandler
implements TransportHandler {
    private final Logger logger;
    private static final String TRANSPORT_SCHEME = "static";
    private static final String PN_DIRECTORY = "directory";
    private static final String MT_ZIP = "application/zip";

    public StaticTransportHandler() {
        this.logger = LoggerFactory.getLogger(this.getClass());
    }

    @Activate
    private void activate(ComponentContext context) {
        this.logger.info("Static Transport Handler started.");
    }

    @Deactivate
    private void deactivate(ComponentContext context) {
        this.logger.info("Static Transport Handler stopped.");
    }

    @Override
    public boolean canHandle(AgentConfig config) {
        if (config != null) {
            String uri = config.getTransportURI();
            String directory = (String)config.getProperties().get("directory", String.class);
            if (directory != null) {
                directory = ResourceUtil.normalize((String)directory);
            }
            return uri.startsWith("static") && directory != null && new File(directory).isAbsolute();
        }
        return false;
    }

    @Override
    public ReplicationResult deliver(TransportContext ctx, ReplicationTransaction tx) throws ReplicationException {
        URI transportUri;
        ReplicationLog log = tx.getLog();
        String s = ctx.getConfig().getTransportURI();
        try {
            transportUri = new URI(s);
        }
        catch (URISyntaxException e) {
            String msg = String.format("Syntax error in Transport URI %s: %s", s, e.getMessage());
            log.error(msg);
            return new ReplicationResult(false, 0, msg);
        }
        if (!transportUri.getScheme().equals("static")) {
            String msg = String.format("Unexpected scheme in Transport URI %s: should be %s", s, "static");
            log.error(msg);
            return new ReplicationResult(false, 0, msg);
        }
        s = ResourceUtil.normalize((String)((String)ctx.getConfig().getProperties().get("directory", String.class)));
        if (s == null) {
            String msg = "Target directory not specified.";
            log.error(msg);
            return new ReplicationResult(false, 0, msg);
        }
        if (!new File(s).isAbsolute()) {
            String msg = "Target directory must be absolute.";
            log.error(msg);
            return new ReplicationResult(false, 0, msg);
        }
        File directory = new File(s);
        if (!directory.isDirectory()) {
            String msg = String.format("Target is not a directory: %s", s);
            log.error(msg);
            return new ReplicationResult(false, 0, msg);
        }
        if (tx.getAction().getType() == ReplicationActionType.DELETE || tx.getAction().getType() == ReplicationActionType.DEACTIVATE) {
            return ReplicationResult.OK;
        }
        ReplicationContent content = tx.getContent();
        String contentType = content.getContentType();
        if (tx.getAction().getType() == ReplicationActionType.TEST) {
            throw new ReplicationException("Test replication not supported by this transport handler.");
        }
        if (tx.getAction().getType() == ReplicationActionType.INTERNAL_POLL || tx.getAction().getType() == ReplicationActionType.REVERSE) {
            throw new ReplicationException("Reverse replication not supported by this transport handler.");
        }
        if (content == ReplicationContent.VOID) {
            log.info("Nothing to replicate for " + tx.getAction().getPath());
            return new ReplicationResult(true, 0, "Done");
        }
        if (!contentType.equals("application/zip")) {
            String msg = String.format("Unexpected replication content type: %s", contentType);
            throw new ReplicationException(msg);
        }
        ZipInputStream in = null;
        try {
            InputStream input = content.getInputStream();
            if (input != null) {
                in = new ZipInputStream(input);
                this.extractZip(directory, in, log);
            }
        }
        catch (IOException e) {
            String msg = String.format("Unable to deserialize replication content: %s", e.getMessage());
            throw new ReplicationException(msg, e);
        }
        finally {
            IOUtils.closeQuietly((InputStream)in);
        }
        return ReplicationResult.OK;
    }

    private void extractZip(File directory, ZipInputStream in, ReplicationLog log) throws IOException {
        ZipEntry entry;
        while ((entry = in.getNextEntry()) != null) {
            String name = entry.getName();
            if (name.startsWith("/")) {
                name = name.substring(1);
            }
            File target = new File(directory, name);
            log.debug("Extracting entry %s to %s", entry.getName(), target.getPath());
            FileUtils.writeByteArrayToFile((File)target, (byte[])IOUtils.toByteArray((InputStream)in));
        }
    }

    public ReplicationResult poll(TransportContext ctx, ReplicationTransaction tx, List<ReplicationContent> result, ReplicationContentFactory factory) throws ReplicationException {
        throw new ReplicationException("Not implemented");
    }
}