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

com.axway.apim.api.export.impl.APIResultHandler Maven / Gradle / Ivy

package com.axway.apim.api.export.impl;

import com.axway.apim.adapter.APIManagerAdapter;
import com.axway.apim.adapter.apis.APIFilter;
import com.axway.apim.adapter.apis.APIFilter.Builder;
import com.axway.apim.adapter.apis.APIFilter.Builder.APIType;
import com.axway.apim.adapter.apis.APIFilter.METHOD_TRANSLATION;
import com.axway.apim.adapter.apis.APIFilter.POLICY_TRANSLATION;
import com.axway.apim.adapter.apis.APIManagerPoliciesAdapter.PolicyType;
import com.axway.apim.api.API;
import com.axway.apim.api.export.ExportAPI;
import com.axway.apim.api.export.lib.params.APIExportParams;
import com.axway.apim.api.model.CustomProperties.Type;
import com.axway.apim.api.model.*;
import com.axway.apim.lib.Result;
import com.axway.apim.lib.error.AppException;
import com.axway.apim.lib.error.ErrorCode;
import com.axway.apim.lib.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Constructor;
import java.util.*;

public abstract class APIResultHandler {

    private static final Logger LOG = LoggerFactory.getLogger(APIResultHandler.class);
    APIExportParams params;

    protected Result result;
    protected ExportHelper exportHelper;


    public enum APIListImpl {
        JSON_EXPORTER(JsonAPIExporter.class),
        YAML_EXPORTER(YamlAPIExporter.class),
        CONSOLE_EXPORTER(ConsoleAPIExporter.class),
        CSV_EXPORTER(CSVAPIExporter.class),
        DAT_EXPORTER(DATAPIExporter.class),
        API_DELETE_HANDLER(DeleteAPIHandler.class),
        API_PUBLISH_HANDLER(PublishAPIHandler.class),
        API_UNPUBLISH_HANDLER(UnPublishAPIHandler.class),
        API_CHANGE_HANDLER(APIChangeHandler.class),
        API_APPROVE_HANDLER(ApproveAPIHandler.class),
        API_UPGRADE_ACCESS_HANDLE(UpgradeAccessAPIHandler.class),
        API_GRANT_ACCESS_HANDLER(GrantAccessAPIHandler.class),
        API_REVOKE_ACCESS_HANDLER(RevokeAccessAPIHandler.class),

        API_CHECK_CERTS_HANDLER(CheckCertificatesAPIHandler.class);

        private final Class implClass;

        @SuppressWarnings({"rawtypes", "unchecked"})
        APIListImpl(Class clazz) {
            this.implClass = clazz;
        }

        public Class getClazz() {
            return implClass;
        }
    }

    protected APIResultHandler(APIExportParams params) {
        this.params = params;
        this.result = new Result();
        this.exportHelper = new ExportHelper(params);
    }

    protected APIResultHandler(APIExportParams params, Result result) {
        this.params = params;
        this.result = result;
        this.exportHelper = new ExportHelper(params);
    }

    public static APIResultHandler create(APIListImpl exportImpl, APIExportParams params) throws AppException {
        try {
            Object[] intArgs = new Object[]{params};
            Constructor constructor = exportImpl.getClazz().getConstructor(APIExportParams.class);
            return constructor.newInstance(intArgs);
        } catch (Exception e) {
            throw new AppException("Error initializing API export handler", ErrorCode.UNXPECTED_ERROR, e);
        }
    }

    public abstract void execute(List apis) throws AppException;

    public Result getResult() {
        return result;
    }

    public void setResult(Result result) {
        this.result = result;
    }

    public abstract APIFilter getFilter();

    protected Builder getBaseAPIFilterBuilder() {
        return new Builder(APIType.CUSTOM)
            .hasVHost(params.getVhost())
            .hasApiPath(params.getApiPath())
            .hasPolicyName(params.getPolicy())
            .hasId(params.getId())
            .hasName(params.getName())
            .hasOrganization(params.getOrganization())
            .hasTag(params.getTag())
            .hasState(params.getState())
            .hasBackendBasepath(params.getBackend())
            .hasInboundSecurity(params.getInboundSecurity())
            .hasOutboundAuthentication(params.getOutboundAuthentication())
            .includeCustomProperties(getAPICustomProperties())
            .translateMethods(METHOD_TRANSLATION.AS_NAME)
            .translatePolicies(POLICY_TRANSLATION.TO_NAME)
            .useFEAPIDefinition(params.isUseFEAPIDefinition())
            .isCreatedOnAfter(params.getCreatedOnAfter())
            .isCreatedOnBefore(params.getCreatedOnBefore())
            .failOnError(false);
    }

    protected List getAPICustomProperties() {
        try {
            return APIManagerAdapter.getInstance().getCustomPropertiesAdapter().getCustomPropertyNames(Type.api);
        } catch (AppException e) {
            LOG.error("Error reading custom properties configuration from API-Manager");
            return Collections.emptyList();
        }
    }

    protected static String getBackendPath(API api) {
        ExportAPI exportAPI = new ExportAPI(api);
        return exportAPI.getBackendBasepath();
    }

    protected static String getUsedSecurity(API api) {
        List usedSecurity = new ArrayList<>();
        Map secProfilesMappedByName = new HashMap<>();

        for (SecurityProfile secProfile : api.getSecurityProfiles()) {
            secProfilesMappedByName.put(secProfile.getName(), secProfile);
        }
        Iterator it;
        it = api.getInboundProfiles().values().iterator();
        while (it.hasNext()) {
            InboundProfile profile = it.next();
            SecurityProfile usedSecProfile = secProfilesMappedByName.get(profile.getSecurityProfile());
            // If Security-Profile null only happens for method overrides, then they are using the API-Default --> Skip this InboundProfile
            if (usedSecProfile == null) continue;
            for (SecurityDevice device : usedSecProfile.getDevices()) {
                if (device.getType() == DeviceType.authPolicy) {
                    String authenticationPolicy = device.getProperties().get("authenticationPolicy");
                    usedSecurity.add(Utils.getExternalPolicyName(authenticationPolicy));
                } else {
                    usedSecurity.add(device.getType().getName());
                }
            }
        }
        return usedSecurity.toString().replace("[", "").replace("]", "");
    }

    protected static List getUsedPolicies(API api, PolicyType type) {
        return getUsedPolicies(api).get(type);
    }

    protected static Map> getUsedPolicies(API api) {
        Iterator it;
        Map> result = new EnumMap<>(PolicyType.class);
        List requestPolicies = new ArrayList<>();
        List routingPolicies = new ArrayList<>();
        List responsePolicies = new ArrayList<>();
        List faultHandlerPolicies = new ArrayList<>();
        it = api.getOutboundProfiles().values().iterator();

        while (it.hasNext()) {
            OutboundProfile profile = it.next();
            if (profile.getRequestPolicy() != null && profile.getRequestPolicy().getName() != null) {
                requestPolicies.add(profile.getRequestPolicy().getName());
            }
            if (profile.getRouteType().equals("policy") && profile.getRoutePolicy() != null && profile.getRoutePolicy().getName() != null) {
                routingPolicies.add(profile.getRoutePolicy().getName());
            }
            if (profile.getResponsePolicy() != null && profile.getResponsePolicy().getName() != null) {
                responsePolicies.add(profile.getResponsePolicy().getName());
            }
            if (profile.getFaultHandlerPolicy() != null && profile.getFaultHandlerPolicy().getName() != null) {
                faultHandlerPolicies.add(profile.getFaultHandlerPolicy().getName());
            }
        }
        result.put(PolicyType.REQUEST, requestPolicies);
        result.put(PolicyType.ROUTING, routingPolicies);
        result.put(PolicyType.RESPONSE, responsePolicies);
        result.put(PolicyType.FAULT_HANDLER, faultHandlerPolicies);
        return result;
    }

    protected static String getCustomProps(API api) {
        if (api.getCustomProperties() == null) return "N/A";
        Iterator it = api.getCustomProperties().keySet().iterator();
        List props = new ArrayList<>();
        while (it.hasNext()) {
            String property = it.next();
            String value = api.getCustomProperties().get(property);
            props.add(property + ": " + value);
        }
        return props.toString().replace("[", "").replace("]", "");
    }

    protected static String getTags(API api) {
        if (api.getTags() == null) return "None";
        Iterator it = api.getTags().keySet().iterator();
        List tags = new ArrayList<>();
        while (it.hasNext()) {
            String tagGroup = it.next();
            String[] tagValues = api.getTags().get(tagGroup);
            tags.add(tagGroup + ": " + Arrays.toString(tagValues));
        }
        return tags.toString().replace("[", "").replace("]", "");
    }

    protected static List getGrantedOrganizations(API api) {
        List grantedOrgs = new ArrayList<>();
        try {
            if (api.getClientOrganizations() == null) return grantedOrgs;
            for (Organization org : api.getClientOrganizations()) {
                grantedOrgs.add(org.getName());
            }
            return grantedOrgs;
        } catch (Exception e) {
            LOG.error("Error getting API client organization", e);
            return grantedOrgs;
        }
    }
    protected APIFilter createFilter() {
        Builder builder = getBaseAPIFilterBuilder();
        switch (params.getWide()) {
            case standard:
            case wide:
                builder.includeQuotas(false);
                builder.includeClientApplications(false);
                builder.includeClientOrganizations(false);
                builder.includeClientAppQuota(false);
                builder.includeQuotas(false);
                break;
            case ultra:
                builder.includeQuotas(true);
                builder.includeClientAppQuota(false);
                builder.includeClientApplications(true);
                builder.includeClientOrganizations(true);
                break;
        }
        return builder.build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy