AgentConfigImpl.java 11.1 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.crypto.CryptoException
 *  com.adobe.granite.crypto.CryptoSupport
 *  org.apache.commons.lang.text.StrSubstitutor
 *  org.apache.jackrabbit.util.Text
 *  org.apache.sling.api.resource.ValueMap
 *  org.apache.sling.api.wrappers.ValueMapDecorator
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.replication.impl;

import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.cq.replication.AgentConfig;
import com.day.cq.replication.AgentConfigGroup;
import java.io.ByteArrayOutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.Key;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.lang.text.StrSubstitutor;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AgentConfigImpl
implements AgentConfig {
    private static final Logger log = LoggerFactory.getLogger(AgentConfigImpl.class);
    protected static final String DEFAULT_SERIALIZATION_TYPE = "durbo";
    protected static final long DEFAULT_RETRY_DELAY = 60000;
    protected static final int DEFAULT_MAX_RETRIES = -1;
    protected static final String DEFAULT_LOG_LEVEL = "INFO";
    static final String DISTRIBUTION_SERIALIZATION_TYPE = "distribution";
    static final String DISTRIBUTION_TRANSPORT_PREFIX = "distribution://";
    private final String id;
    private final String configPath;
    private final ValueMap properties;
    private final AgentConfigGroup configGroup;
    private final CryptoSupport crypto;
    private final String[] uris;
    private final String serializationType;

    public AgentConfigImpl(String id, Map<String, Object> props, String configPath, AgentConfigGroup group, CryptoSupport crypto) {
        this.id = id;
        this.configPath = configPath;
        this.properties = new ValueMapDecorator(props);
        this.configGroup = group;
        this.crypto = crypto;
        String serializationType = (String)this.properties.get("serializationType", (Object)"durbo");
        String transportUri = (String)this.properties.get("transportUri", String.class);
        if (transportUri != null && transportUri.startsWith("distribution://")) {
            serializationType = "distribution";
        }
        if ("distribution".equals(serializationType) && !transportUri.startsWith("distribution://")) {
            transportUri = "distribution://" + this.getAgentId();
        }
        this.serializationType = serializationType;
        this.uris = AgentConfigImpl.splitUris(transportUri);
    }

    @Override
    public ValueMap getProperties() {
        return this.properties;
    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public String getAgentId() {
        return Text.getName((String)this.id);
    }

    @Override
    public String getConfigPath() {
        return this.configPath;
    }

    @Override
    public AgentConfigGroup getConfigGroup() {
        return this.configGroup;
    }

    @Override
    public String getSerializationType() {
        return this.serializationType;
    }

    @Override
    public String getName() {
        return (String)this.properties.get("jcr:title", String.class);
    }

    @Override
    public String getAgentUserId() {
        return (String)this.properties.get("userId", String.class);
    }

    @Override
    public String getLogLevel() {
        return (String)this.properties.get("logLevel", (Object)"INFO");
    }

    @Override
    public boolean isSpecific() {
        return (Boolean)this.properties.get("triggerSpecific", (Object)false) != false || this.isTriggeredOnDistribute() || this.usedForReverseReplication();
    }

    @Override
    public boolean isTriggeredOnModification() {
        return (Boolean)this.properties.get("triggerModified", (Object)false);
    }

    @Override
    public boolean isTriggeredOnOffTime() {
        return (Boolean)this.properties.get("triggerOnOffTime", (Object)false);
    }

    @Override
    public boolean isTriggeredOnDistribute() {
        return (Boolean)this.properties.get("triggerDistribute", (Object)false);
    }

    @Override
    public boolean usedForReverseReplication() {
        return (Boolean)this.properties.get("reverseReplication", (Object)false);
    }

    @Override
    public boolean isEnabled() {
        return (Boolean)this.properties.get("enabled", (Object)false);
    }

    @Override
    public boolean isTriggeredOnReceive() {
        return (Boolean)this.properties.get("triggerReceive", (Object)false);
    }

    @Override
    public boolean isBatchMode() {
        return (Boolean)this.properties.get("queueBatchMode", (Object)false);
    }

    @Override
    public long getBatchWaitTime() {
        return ((Integer)this.properties.get("queueBatchWaitTime", (Object)2)).intValue();
    }

    @Override
    public long getBatchMaxSize() {
        return ((Integer)this.properties.get("queueBatchMaxSize", (Object)5242880)).intValue();
    }

    @Override
    public int getMaxRetries() {
        return (Integer)this.properties.get("maxRetries", (Object)-1);
    }

    @Override
    public long getRetryDelay() {
        long delay = (Long)this.properties.get("retryDelay", (Object)60000);
        return delay < 1000 ? 1000 : delay;
    }

    @Override
    public void checkValid() {
        String[] uris = this.getAllTransportURIs();
        AgentConfigImpl.checkValidUris(uris);
        if (this.isBatchMode()) {
            if (this.getBatchMaxSize() < 1024) {
                throw new IllegalArgumentException("Maximum batch size for batch processing is too low: " + this.getBatchMaxSize());
            }
            if (this.getBatchWaitTime() < 1) {
                throw new IllegalArgumentException("Batch wait time for batch processing is too low: " + this.getBatchWaitTime());
            }
        }
    }

    private static void checkValidUris(String[] uris) {
        if (uris != null && uris.length > 0) {
            for (String uri : uris) {
                if (uri == null) {
                    throw new IllegalArgumentException("Transport URI is not specified.");
                }
                HashMap<String, String> props = new HashMap<String, String>();
                props.put("path", "path");
                props.put("action", "activate");
                StrSubstitutor replace = new StrSubstitutor(props, "{", "}");
                try {
                    new URI(replace.replace(uri));
                    continue;
                }
                catch (URISyntaxException e) {
                    throw new IllegalArgumentException("Transport URI is malformed: " + e.getMessage());
                }
            }
        } else {
            throw new IllegalArgumentException("Transport URI is not specified.");
        }
    }

    @Override
    public String getTransportURI() {
        String[] uris = this.getAllTransportURIs();
        if (uris == null || uris.length == 0) {
            return null;
        }
        return uris[0];
    }

    @Override
    public String[] getAllTransportURIs() {
        return this.uris;
    }

    @Override
    public String getTransportUser() {
        return (String)this.properties.get("transportUser", String.class);
    }

    @Override
    public boolean noVersions() {
        return (Boolean)this.properties.get("noVersioning", (Object)false);
    }

    @Override
    public boolean noStatusUpdate() {
        return (Boolean)this.properties.get("noStatusUpdate", (Object)false);
    }

    @Override
    public String getTransportPassword() {
        String pwd = (String)this.properties.get("transportPassword", String.class);
        return this.getPassword(pwd);
    }

    @Override
    public boolean isInMaintenanceMode() {
        return (Boolean)this.properties.get("maintenanceMode", (Object)false);
    }

    @Override
    public boolean aliasUpdate() {
        return (Boolean)this.properties.get("aliasUpdate", (Object)false);
    }

    static String legacyDecrypt(String s) {
        String PREFIX = "{DES}";
        int KEY_LENGTH = 8;
        if (!s.startsWith("{DES}")) {
            return null;
        }
        try {
            byte[] data = new byte[(s.length() - "{DES}".length()) / 2];
            int i = "{DES}".length();
            int b = 0;
            while (i < s.length()) {
                data[b] = (byte)(Integer.parseInt(s.substring(i, i + 2), 16) & 255);
                i += 2;
                ++b;
            }
            SecretKeySpec key = new SecretKeySpec(data, 0, 8, "DES");
            Cipher cipher = Cipher.getInstance("DES");
            ByteArrayOutputStream out = new ByteArrayOutputStream(data.length);
            cipher.init(2, key);
            out.write(cipher.update(data, 8, data.length - 8));
            out.write(cipher.doFinal());
            return out.toString("utf-8");
        }
        catch (Exception e) {
            log.warn("Unable to decrypt data: " + e);
            return null;
        }
    }

    @Override
    public String getSSLConfig() {
        return (String)this.properties.get("ssl", (Object)"default");
    }

    @Override
    public boolean allowsExpiredCertificates() {
        return (Boolean)this.properties.get("protocolHTTPExpired", (Object)false);
    }

    @Override
    public boolean isOAuthEnabled() {
        return (Boolean)this.properties.get("enableOauth", (Object)false);
    }

    private String getPassword(String pwd) {
        if (pwd != null) {
            String dec = AgentConfigImpl.legacyDecrypt(pwd);
            if (dec == null && this.crypto != null && this.crypto.isProtected(pwd)) {
                try {
                    dec = this.crypto.unprotect(pwd);
                }
                catch (CryptoException e) {
                    log.error("Error while unprotecting password: {}", (Object)e.toString());
                }
            }
            if (dec != null) {
                pwd = dec;
            }
        }
        return pwd;
    }

    public String getDistributionAgentName() {
        String transportUri = this.getTransportURI();
        if (transportUri.startsWith("distribution://")) {
            return transportUri.replace("distribution://", "");
        }
        return null;
    }

    private static String[] splitUris(String urisString) {
        if (urisString == null) {
            return null;
        }
        urisString = urisString.trim();
        String[] uris = urisString.split(",");
        ArrayList<String> list = new ArrayList<String>();
        for (String uri : uris) {
            if ((uri = uri.trim()).length() <= 0) continue;
            list.add(uri);
        }
        String[] resultUris = list.toArray(new String[list.size()]);
        if (resultUris.length > 1) {
            try {
                AgentConfigImpl.checkValidUris(resultUris);
            }
            catch (IllegalArgumentException e) {
                resultUris = new String[]{urisString};
            }
        }
        return resultUris;
    }
}