MACTenantConfigurationImpl.java 4.33 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.security.user.UserManagementService
 *  com.day.cq.commons.inherit.InheritanceValueMap
 *  com.day.cq.wcm.webservicesupport.Configuration
 *  javax.jcr.RepositoryException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.dam.mac.sync.helper.impl;

import com.adobe.cq.dam.mac.sync.helper.impl.MACTenantConfiguration;
import com.adobe.cq.dam.mac.sync.helper.impl.util.OAuthUtil;
import com.adobe.granite.security.user.UserManagementService;
import com.day.cq.commons.inherit.InheritanceValueMap;
import com.day.cq.wcm.webservicesupport.Configuration;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MACTenantConfigurationImpl
implements MACTenantConfiguration {
    private static final Logger LOG = LoggerFactory.getLogger(MACTenantConfigurationImpl.class);
    private Configuration configuration;
    private InheritanceValueMap properties;
    private String userID = null;
    private UserManagementService userManagementService;

    public MACTenantConfigurationImpl(Configuration configuration, UserManagementService userManagementService) {
        this.configuration = configuration;
        this.properties = configuration.getProperties();
        this.userManagementService = userManagementService;
    }

    @Override
    public String getTenant() {
        String tenantFromProp = this.readWrapper("tenant", String.class);
        if (tenantFromProp != null) {
            return tenantFromProp;
        }
        String tenantURL = this.getTenantURL();
        if (tenantURL != null) {
            String[] split;
            if (tenantURL.startsWith("https://")) {
                tenantURL = tenantURL.replaceFirst("https://", "");
            }
            if ((split = tenantURL.split("\\.")).length >= 2) {
                return split[0];
            }
        }
        return null;
    }

    @Override
    public String getTenantURL() {
        return this.readWrapper("tenantURL", String.class);
    }

    @Override
    public String getDAMUser() {
        if (this.userID == null) {
            try {
                this.userID = OAuthUtil.createReplicationUser(this.configuration.getResource().getResourceResolver(), this.getTenant(), this.userManagementService);
            }
            catch (RepositoryException e) {
                throw new RuntimeException((Throwable)e);
            }
        }
        return this.userID;
    }

    @Override
    public String getOAuthClientId() {
        return this.readWrapper("clientId", String.class);
    }

    @Override
    public String getOAuthAudience() {
        return this.readWrapper("audience", String.class);
    }

    @Override
    public String[] getMACSyncPaths() {
        String configVal = this.readWrapper("macSyncFolders", String.class);
        if (configVal != null) {
            String[] paths = configVal.split(",");
            return paths;
        }
        return new String[0];
    }

    @Override
    public String getPath() {
        return this.configuration.getPath();
    }

    @Override
    public Resource getResource() {
        return this.configuration.getResource();
    }

    @Override
    public boolean isSyncEnabled() {
        return (Boolean)this.properties.get("syncEnabled", (Object)true);
    }

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

    @Override
    public String getOAuthScope() {
        return this.readWrapper("oauthScopes", String.class);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private <T> T readWrapper(String key, Class<T> type) {
        Object result = null;
        try {
            result = this.properties.get(key, type);
            return (T)result;
        }
        catch (Exception e) {
            LOG.warn("Unable to read property " + key + " from configuration properties. Configuration resource was probably removed.");
        }
        finally {
            return (T)result;
        }
    }
}