All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.axway.apim.lib.CoreParameters Maven / Gradle / Ivy

package com.axway.apim.lib;

import com.axway.apim.adapter.CacheType;
import com.axway.apim.lib.error.AppException;
import com.axway.apim.lib.error.ErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CoreParameters implements Parameters {

    private static final Logger LOG = LoggerFactory.getLogger(CoreParameters.class);
    public static final String CLIENT_APPS_MODE = "clientAppsMode";
    public static final String CLIENT_ORGS_MODE = "clientOrgsMode";

    public enum Mode {
        replace,
        ignore,
        add;

        public static Mode valueOfDefault(String key) {
            for (Mode e : values()) {
                if (e.name().equals(key)) {
                    return e;
                }
            }
            return Mode.add;
        }
    }

    public static final String APIM_CLI_HOME = "AXWAY_APIM_CLI_HOME";
    private static final String DEFAULT_API_BASEPATH = "/api/portal/v1.4";
    private URI apiManagerUrl = null;
    private static CoreParameters instance;
    private List cachesToClear = null;
    int defaultPort = 8075;

    /**
     * These properties are used for instance to translate key in given config files
     */
    private Map properties;
    private String stage;
    private String returnCodeMapping;
    private String clearCache;
    private String hostname;
    private int port = -1;
    private String username;
    private String password;
    private boolean force;
    private boolean ignoreQuotas;
    private boolean zeroDowntimeUpdate;
    private Mode quotaMode;
    private Mode clientAppsMode;
    private Mode clientOrgsMode;
    private String detailsExportFile;
    private boolean rollback = true;
    private boolean ignoreCache = false;
    private String apimCLIHome;
    private String proxyHost;
    private Integer proxyPort;
    private String proxyUsername;
    private String proxyPassword;
    private int retryDelay;
    private int timeout;
    private boolean disableCompression;
    private boolean overrideSpecBasePath;

    public CoreParameters() {
        instance = this;
    }

    public static synchronized CoreParameters getInstance() {
        if (instance == null) {
            instance = new CoreParameters();
        }
        return instance;
    }

    public String getStage() {
        return stage;
    }

    public void setStage(String stage) {
        this.stage = stage;
    }

    public String getReturnCodeMapping() {
        if (returnCodeMapping != null) return returnCodeMapping;
        return getFromProperties("returnCodeMapping");
    }

    public void setReturnCodeMapping(String returnCodeMapping) {
        this.returnCodeMapping = returnCodeMapping;
    }

    public String getClearCache() {
        if (clearCache != null) return clearCache;
        return getFromProperties("clearCache");
    }

    public void setClearCache(String clearCache) {
        this.clearCache = clearCache;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    public String getHostname2() {
        if (this.apiManagerUrl != null) {
            return this.apiManagerUrl.getHost();
        } else {
            if (hostname != null) return hostname;
            return getFromProperties("host");
        }
    }

    public void setPort(int port) {
        this.port = port;
    }

    public int getPort2() {
        if (port == -1) {
            String portStr = getFromProperties("port");
            if (portStr != null) {
                return Integer.parseInt(portStr);
            } else {
                return defaultPort;
            }
        }
        return port;
    }


    public String getApiBasepath() {
        return DEFAULT_API_BASEPATH;
    }

    public String getUsername() {
        if (username != null)
            return username;
        if (getFromProperties("username") != null) {
            return getFromProperties("username");
        }
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        if (password != null)
            return password;
        if (getFromProperties("password") != null) {
            return getFromProperties("password");
        }
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isForce() {
        return force;
    }

    public void setForce(boolean force) {
        this.force = force;
    }

    public void setIgnoreQuotas(boolean ignoreQuotas) {
        this.ignoreQuotas = ignoreQuotas;
    }

    public boolean isIgnoreQuotas() {
        return ignoreQuotas;
    }

    public Mode getQuotaMode() {
        if (quotaMode != null) return quotaMode;
        if (getFromProperties("quotaMode") != null) {
            return Mode.valueOf(getFromProperties("quotaMode"));
        }
        return Mode.add;
    }

    public void setQuotaMode(Mode quotaMode) {
        if (quotaMode == null) return;
        this.quotaMode = quotaMode;
    }

    public boolean isIgnoreClientApps() {
        if (clientAppsMode == Mode.ignore) return true;
        if (getFromProperties(CLIENT_APPS_MODE) != null) {
            return Boolean.parseBoolean(getFromProperties(CLIENT_APPS_MODE));
        }
        return false;
    }

    public Mode getClientAppsMode() {
        if (clientAppsMode != null) return clientAppsMode;
        if (getFromProperties(CLIENT_APPS_MODE) != null) {
            return Mode.valueOf(getFromProperties(CLIENT_APPS_MODE));
        }
        return Mode.add;
    }

    public void setClientAppsMode(Mode clientAppsMode) {
        if (clientAppsMode == null) return; // Stick with the default
        this.clientAppsMode = clientAppsMode;
    }

    public boolean isIgnoreClientOrgs() {
        if (clientOrgsMode == Mode.ignore) return true;
        if (getFromProperties(CLIENT_ORGS_MODE) != null) {
            return Boolean.parseBoolean(getFromProperties(CLIENT_ORGS_MODE));
        }
        return false;
    }

    public Mode getClientOrgsMode() {
        if (clientOrgsMode != null) return clientOrgsMode;
        if (getFromProperties(CLIENT_ORGS_MODE) != null) {
            return Mode.valueOf(getFromProperties(CLIENT_ORGS_MODE));
        }
        return Mode.add;
    }

    public void setClientOrgsMode(Mode clientOrgsMode) {
        if (clientOrgsMode == null) return; // Stick with the default
        this.clientOrgsMode = clientOrgsMode;
    }

    public void setAPIManagerURL(String apiManagerUrl) throws AppException {
        if (apiManagerUrl == null) return;
        try {
            this.apiManagerUrl = new URI(apiManagerUrl);
        } catch (URISyntaxException e) {
            throw new AppException("Error parsing up API-Manager URL: " + apiManagerUrl, ErrorCode.INVALID_PARAMETER, e);
        }
    }

    public URI getAPIManagerURL() throws AppException {
        try {
            if (apiManagerUrl == null) {
                return new URI("https://" + this.getHostname2() + ":" + this.getPort2());
            }
            return apiManagerUrl;
        } catch (URISyntaxException e) {
            throw new AppException("Error setting up API-Manager URL", ErrorCode.INVALID_PARAMETER, e);
        }
    }

    public String getDetailsExportFile() {
        return detailsExportFile;
    }

    public void setDetailsExportFile(String detailsExportFile) {
        this.detailsExportFile = detailsExportFile;
    }

    public boolean isRollback() {
        return rollback;
    }

    public void setRollback(boolean rollback) {
        this.rollback = rollback;
    }


    public boolean isIgnoreCache() {
        return ignoreCache;
    }

    public void setIgnoreCache(boolean ignoreCache) {
        this.ignoreCache = ignoreCache;
    }

    public String getApimCLIHome() {
        return apimCLIHome;
    }

    public void setApimCLIHome(String apimCLIHome) {
        this.apimCLIHome = apimCLIHome;
    }

    public String getProxyHost() {
        return proxyHost;
    }

    public void setProxyHost(String proxyHost) {
        this.proxyHost = proxyHost;
    }

    public Integer getProxyPort() {
        if (proxyPort == null) return -1;
        return proxyPort;
    }

    public void setProxyPort(Integer proxyPort) {
        this.proxyPort = proxyPort;
    }

    public String getProxyUsername() {
        return proxyUsername;
    }

    public void setProxyUsername(String proxyUsername) {
        this.proxyUsername = proxyUsername;
    }

    public String getProxyPassword() {
        return proxyPassword;
    }

    public void setProxyPassword(String proxyPassword) {
        this.proxyPassword = proxyPassword;
    }

    public int getRetryDelay() {
        if (retryDelay == 0) return 1000;
        return retryDelay;
    }

    public void setRetryDelay(String retryDelay) {
        this.retryDelay = 1000;
        if (retryDelay == null || retryDelay.equals("null")) {
            return;
        }
        try {
            this.retryDelay = Integer.parseInt(retryDelay);
            LOG.info("Retrying unexpected API-Manager REST-API responses with a delay of {} milliseconds.", this.retryDelay);
        } catch (Exception e) {
            LOG.error("Error while parsing given retryDelay: {} as a milliseconds. Using default of 1000 milliseconds.", retryDelay);
        }
    }

    public int getTimeout() {
        if (timeout == 0) return 30000;
        return timeout;
    }

    public void setTimeout(String timeout) {
        if (timeout == null) {
            return;
        }
        try {
            this.timeout = Integer.parseInt(timeout);
            LOG.info("API Manager timeout : {} milliseconds", timeout);
        } catch (Exception e) {
            LOG.error("Error while parsing given timeout : {}", timeout);
        }
    }

    public boolean isZeroDowntimeUpdate() {
        return zeroDowntimeUpdate;
    }

    public void setZeroDowntimeUpdate(boolean zeroDowntimeUpdate) {
        this.zeroDowntimeUpdate = zeroDowntimeUpdate;
    }

    public List clearCaches() {
        if (getClearCache() == null) return Collections.emptyList();
        if (cachesToClear != null) return cachesToClear;
        cachesToClear = createCacheList(getClearCache());
        return cachesToClear;
    }

    protected List createCacheList(String configString) {
        List cachesList = new ArrayList<>();
        for (String cacheName : configString.split(",")) {
            if (cacheName.equals("ALL")) cacheName = "*";
            cacheName = cacheName.trim();
            if (cacheName.contains("*")) {
                Pattern pattern = Pattern.compile(cacheName.replace("*", ".*").toLowerCase());
                for (CacheType cacheType : CacheType.values()) {
                    Matcher matcher = pattern.matcher(cacheType.name().toLowerCase());
                    if (matcher.matches()) {
                        cachesList.add(cacheType);
                    }
                }
            } else {
                try {
                    cachesList.add(CacheType.valueOf(cacheName));
                } catch (IllegalArgumentException e) {
                    LOG.error("Unknown cache: {} configured.", cacheName);
                    LOG.error("Available caches: {}", Arrays.asList(CacheType.values()));
                }
            }
        }
        return cachesList;
    }


    public void validateRequiredParameters() throws AppException {
        boolean parameterMissing = false;
        if (getUsername() == null) {
            parameterMissing = true;
            LOG.error("Required parameter: 'username' is missing.");
        }
        if (getPassword() == null) {
            parameterMissing = true;
            LOG.error("Required parameter: 'password' is missing.");
        }
        if (getHostname2() == null) {
            parameterMissing = true;
            LOG.error("Required parameter: host or apimanagerUrl is missing.");
        }
        if (parameterMissing) {
            LOG.error("Missing required parameters. Use either Command-Line-Options or Environment.Properties to provided required parameters.");
            LOG.error("Get help with option -help");
            LOG.error("");
            throw new AppException("Missing required parameters.", ErrorCode.MISSING_PARAMETER);
        }
    }

    public Map getProperties() {
        return this.properties;
    }

    public void setProperties(Map properties) {
        this.properties = properties;
    }

    private String getFromProperties(String key) {
        if (this.properties == null) return null;
        return this.properties.get(key);
    }


    public boolean isDisableCompression() {
        return disableCompression;
    }

    public void setDisableCompression(boolean disableCompression) {
        this.disableCompression = disableCompression;
    }

    public boolean isOverrideSpecBasePath() {
        return overrideSpecBasePath;
    }

    public void setOverrideSpecBasePath(boolean overrideSpecBasePath) {
        this.overrideSpecBasePath = overrideSpecBasePath;
    }

    @Override
    public String toString() {
        return "[hostname=" + hostname + ", username=" + username + ", stage=" + stage + "]";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy