ReplicationContentWrapper.java 5.13 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.day.cq.replication.impl.queue;

import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationContent;
import com.day.cq.replication.ReplicationContentFacade;
import com.day.cq.replication.impl.queue.ReplicationQueueImpl;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Vector;

public class ReplicationContentWrapper
implements ReplicationContent {
    public static final String BATCH_ACTION_PATH = "batch:";
    private final List<Entry> entries;
    private long contentLength;
    private Vector<InputStream> streams;

    public ReplicationContentWrapper(List<Entry> entries) {
        this.entries = entries;
    }

    private void init() {
        if (this.streams == null) {
            Vector<InputStream> streamVector = new Vector<InputStream>();
            long size = 0;
            StringBuilder sb = new StringBuilder();
            boolean first = true;
            for (Entry entry : this.entries) {
                if (entry.failed) continue;
                boolean added = false;
                try {
                    InputStream is = entry.content.getInputStream();
                    if (is != null) {
                        streamVector.add(this.getInputStream(entry.content.getContentLength()));
                        streamVector.add(is);
                        size += 8;
                        size += entry.content.getContentLength();
                        added = true;
                    }
                }
                catch (IOException is) {
                    // empty catch block
                }
                if (added) {
                    if (first) {
                        first = false;
                    } else {
                        sb.append(':');
                    }
                    sb.append(entry.path);
                    sb.append(':');
                    sb.append(entry.action);
                    continue;
                }
                entry.failed = true;
            }
            try {
                byte[] batchPathsBytes = sb.toString().getBytes("UTF-8");
                streamVector.add(0, new ByteArrayInputStream(batchPathsBytes));
                streamVector.add(0, this.getInputStream(batchPathsBytes.length));
                size += 8;
                size += (long)batchPathsBytes.length;
            }
            catch (UnsupportedEncodingException batchPathsBytes) {
                // empty catch block
            }
            this.streams = streamVector;
            this.contentLength = size;
        }
    }

    private InputStream getInputStream(long length) {
        byte[] bytes = ByteBuffer.allocate(8).order(ByteOrder.BIG_ENDIAN).putLong(length).array();
        return new ByteArrayInputStream(bytes);
    }

    @Override
    public InputStream getInputStream() throws IOException {
        this.init();
        Vector<InputStream> streamVector = this.streams;
        this.streams = null;
        return new SequenceInputStream(streamVector.elements());
    }

    @Override
    public String getContentType() {
        return this.entries.get((int)0).content.getContentType();
    }

    @Override
    public long getContentLength() {
        this.init();
        return this.contentLength;
    }

    @Override
    public long getLastModified() {
        return -1;
    }

    @Override
    public void acquire(String agentName) {
        for (Entry rc : this.entries) {
            rc.content.acquire(agentName);
        }
    }

    @Override
    public void release(String agentName) {
        for (Entry rc : this.entries) {
            rc.content.release(agentName);
        }
    }

    @Override
    public Collection<String> getAcquiredBy() {
        HashSet<String> result = new HashSet<String>();
        for (Entry rc : this.entries) {
            result.addAll(rc.content.getAcquiredBy());
        }
        return result;
    }

    @Override
    public void destroy() {
        for (Entry rc : this.entries) {
            rc.content.destroy();
        }
    }

    @Override
    public ReplicationContentFacade getFacade() {
        return null;
    }

    public static final class Entry {
        public final String path;
        public final String action;
        public final ReplicationContent content;
        public boolean failed = false;

        public Entry(ReplicationQueueImpl.EntryData data, ReplicationContent content) {
            String path = data.getAction().getPath();
            try {
                path = URLEncoder.encode(path, "UTF-8");
            }
            catch (UnsupportedEncodingException var4_4) {
                // empty catch block
            }
            this.path = path;
            this.action = data.getAction().getType().name();
            this.content = content;
        }
    }

}