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

com.axway.apim.adapter.apis.APIManagerOrganizationAdapter Maven / Gradle / Ivy

package com.axway.apim.adapter.apis;

import com.axway.apim.adapter.APIManagerAdapter;
import com.axway.apim.adapter.CacheType;
import com.axway.apim.adapter.apis.APIManagerAPIAccessAdapter.Type;
import com.axway.apim.api.model.APIAccess;
import com.axway.apim.api.model.Image;
import com.axway.apim.api.model.Organization;
import com.axway.apim.lib.CoreParameters;
import com.axway.apim.lib.error.AppException;
import com.axway.apim.lib.error.ErrorCode;
import com.axway.apim.lib.utils.Utils;
import com.axway.apim.lib.utils.rest.*;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.util.EntityUtils;
import org.ehcache.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

public class APIManagerOrganizationAdapter {

    public static final String ORGANIZATIONS = "/organizations/";
    private static final Logger LOG = LoggerFactory.getLogger(APIManagerOrganizationAdapter.class);

    private final CoreParameters cmd;

    ObjectMapper mapper = APIManagerAdapter.mapper;

    Map apiManagerResponse = new HashMap<>();

    Cache organizationCache;

    public APIManagerOrganizationAdapter(APIManagerAdapter apiManagerAdapter) {
        cmd = CoreParameters.getInstance();
        organizationCache = apiManagerAdapter.getCache(CacheType.organizationCache, String.class, String.class);
    }

    private void readOrgsFromAPIManager(OrgFilter filter) throws AppException {
        if (apiManagerResponse.get(filter) != null) return;
        if (!APIManagerAdapter.getInstance().hasAdminAccount()) {
            LOG.warn("Using OrgAdmin only to load all organizations.");
        }
        String orgId = "";
        if (filter.getId() != null) {
            orgId = "/" + filter.getId();
        }

        try {
            URI uri = new URIBuilder(cmd.getAPIManagerURL()).setPath(cmd.getApiBasepath() + "/organizations" + orgId)
                    .addParameters(filter.getFilters())
                    .build();
            RestAPICall getRequest = new GETRequest(uri);
            LOG.debug("Load organizations from API-Manager using filter: {}", filter);
            LOG.debug("Load organization with URI: {}", uri);
            try (CloseableHttpResponse httpResponse = (CloseableHttpResponse) getRequest.execute()) {
                if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                    LOG.error("Received Status-Code: {}", httpResponse.getStatusLine().getStatusCode());
                    Utils.logPayload(LOG, httpResponse);
                    throw new AppException("", ErrorCode.API_MANAGER_COMMUNICATION);
                }
                String response = EntityUtils.toString(httpResponse.getEntity());
                if (!orgId.isEmpty()) {
                    // Store it as an Array
                    response = "[" + response + "]";
                    apiManagerResponse.put(filter, response);
                    LOG.debug("Organization id to be cached : {}", orgId);
                    organizationCache.put(orgId, response);
                } else {
                    // We got an Array from API-Manager
                    apiManagerResponse.put(filter, response);
                }
            }
        } catch (Exception e) {
            throw new AppException("Error cant read orgs from API-Manager with filter: " + filter, ErrorCode.API_MANAGER_COMMUNICATION, e);
        }
    }

    public void updateOrganization(Organization desiredOrg, Organization actualOrg) throws AppException {
        createOrUpdateOrganization(desiredOrg, actualOrg);
    }

    public void createOrganization(Organization desiredOrg) throws AppException {
        createOrUpdateOrganization(desiredOrg, null);
    }

    public void createOrUpdateOrganization(Organization desiredOrg, Organization actualOrg) throws AppException {
        try {
            URI uri;
            if (actualOrg == null) {
                if (!APIManagerAdapter.getInstance().hasAdminAccount()) {
                    throw new AppException("Admin account is required to create a new organization", ErrorCode.NO_ADMIN_ROLE_USER);
                }
                uri = new URIBuilder(cmd.getAPIManagerURL()).setPath(cmd.getApiBasepath() + "/organizations").build();
            } else {
                uri = new URIBuilder(cmd.getAPIManagerURL()).setPath(cmd.getApiBasepath() + ORGANIZATIONS + actualOrg.getId()).build();
            }
            FilterProvider filter = new SimpleFilterProvider().setDefaultFilter(
                    SimpleBeanPropertyFilter.serializeAllExcept("image", "createdOn", "apis"));
            mapper.setFilterProvider(filter);
            mapper.setSerializationInclusion(Include.NON_NULL);
            Organization createdOrg = upsertOrganization(uri, actualOrg, desiredOrg);
            desiredOrg.setId(createdOrg.getId());
            saveImage(desiredOrg, actualOrg);
            saveAPIAccess(desiredOrg, actualOrg);
            // Force reload of this organization next time
            organizationCache.remove(createdOrg.getId());
        } catch (Exception e) {
            throw new AppException("Error creating/updating organization", ErrorCode.CANT_CREATE_API_PROXY, e);
        }
    }

    public Organization upsertOrganization(URI uri, Organization actualOrg, Organization desiredOrg) throws AppException {
        try {
            RestAPICall request;
            if (actualOrg == null) {
                String json = mapper.writeValueAsString(desiredOrg);
                HttpEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
                request = new POSTRequest(entity, uri);
            } else {
                desiredOrg.setId(actualOrg.getId());
                if (desiredOrg.getDn() == null) desiredOrg.setDn(actualOrg.getDn());
                String json = mapper.writeValueAsString(desiredOrg);
                HttpEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
                request = new PUTRequest(entity, uri);
            }
            try (CloseableHttpResponse httpResponse = (CloseableHttpResponse) request.execute()) {
                int statusCode = httpResponse.getStatusLine().getStatusCode();
                if (statusCode < 200 || statusCode > 299) {
                    LOG.error("Error creating/updating organization. Response-Code: {}", statusCode);
                    Utils.logPayload(LOG, httpResponse);
                    throw new AppException("Error creating/updating organization. Response-Code: " + statusCode, ErrorCode.API_MANAGER_COMMUNICATION);
                }
                return mapper.readValue(httpResponse.getEntity().getContent(), Organization.class);
            }
        } catch (Exception e) {
            throw new AppException("Error creating/updating organization.", ErrorCode.ACCESS_ORGANIZATION_ERR, e);
        }
    }

    public void deleteOrganization(Organization org) throws AppException {
        try {
            URI uri = new URIBuilder(cmd.getAPIManagerURL()).setPath(cmd.getApiBasepath() + ORGANIZATIONS + org.getId()).build();
            RestAPICall request = new DELRequest(uri);
            try (CloseableHttpResponse httpResponse = (CloseableHttpResponse) request.execute()) {
                int statusCode = httpResponse.getStatusLine().getStatusCode();
                if (statusCode != 204) {
                    LOG.error("Error deleting organization. Response-Code: {}", statusCode);
                    Utils.logPayload(LOG, httpResponse);
                    throw new AppException("Error deleting organization. Response-Code: " + statusCode, ErrorCode.API_MANAGER_COMMUNICATION);
                }
                // Deleted org should also be deleted from the cache
                organizationCache.remove(org.getId());
                LOG.info("Organization: {}  ( {} ) successfully deleted", org.getName(), org.getId());
            }
        } catch (Exception e) {
            throw new AppException("Error deleting organization", ErrorCode.ACCESS_ORGANIZATION_ERR, e);
        }
    }

    private void saveImage(Organization org, Organization actualOrg) throws URISyntaxException, AppException {
        if (org.getImage() == null) return;
        if (actualOrg != null && org.getImage().equals(actualOrg.getImage())) return;
        URI uri = new URIBuilder(cmd.getAPIManagerURL()).setPath(cmd.getApiBasepath() + ORGANIZATIONS + org.getId() + "/image").build();
        InputStream is = org.getImage().getInputStream();
        HttpEntity entity = MultipartEntityBuilder.create()
                .addBinaryBody("file", is, ContentType.create("image/jpeg"), org.getImage().getBaseFilename())
                .build();
        try {
            RestAPICall apiCall = new POSTRequest(entity, uri);
            try (CloseableHttpResponse httpResponse = (CloseableHttpResponse) apiCall.execute()) {
                int statusCode = httpResponse.getStatusLine().getStatusCode();
                if (statusCode < 200 || statusCode > 299) {
                    LOG.error("Error saving/updating organization image. Response-Code: {}", statusCode);
                    Utils.logPayload(LOG, httpResponse);
                }
            }
        } catch (Exception e) {
            throw new AppException("Error uploading organization image", ErrorCode.CANT_CREATE_API_PROXY, e);
        }
    }

    public List getOrgs(OrgFilter filter) throws AppException {
        readOrgsFromAPIManager(filter);
        try {
            List allOrgs = mapper.readValue(this.apiManagerResponse.get(filter), new TypeReference>() {
            });
            allOrgs.removeIf(filter::filter);
            for (Organization org : allOrgs) {
                addImage(org, filter.isIncludeImage());
                addAPIAccess(org, filter.isIncludeAPIAccess());
            }
            Utils.addCustomPropertiesForEntity(allOrgs, this.apiManagerResponse.get(filter), filter);
            return allOrgs;
        } catch (Exception e) {
            LOG.error("Error cant read orgs from API-Manager with filter: {} Returned response: {}", filter, apiManagerResponse);
            throw new AppException("Error cant read orgs from API-Manager with filter: " + filter, ErrorCode.API_MANAGER_COMMUNICATION, e);
        }
    }

    public List getAllOrgs() throws AppException {
        return getOrgs(new OrgFilter.Builder().build());
    }

    public Organization getOrgForName(String orgName) throws AppException {
        return getOrg(new OrgFilter.Builder().hasName(orgName).build());
    }

    public Organization getOrgForId(String orgId) throws AppException {
        return getOrg(new OrgFilter.Builder().hasId(orgId).build());
    }

    public Organization getOrg(OrgFilter filter) throws AppException {
        List orgs = getOrgs(filter);
        if (orgs.size() > 1) {
            throw new AppException("No unique Organization found for filter: " + filter, ErrorCode.UNKNOWN_API);
        }
        if (orgs.isEmpty()) {
            LOG.info("No organization found using filter: {}", filter);
            return null;
        }
        return orgs.get(0);
    }

    void addAPIAccess(Organization org, boolean addAPIAccess) throws AppException {
        if (!addAPIAccess) return;
        try {
            List apiAccess = APIManagerAdapter.getInstance().getAccessAdapter().getAPIAccess(org, Type.organizations, true);
            org.getApiAccess().addAll(apiAccess);
        } catch (Exception e) {
            throw new AppException("Error reading organizations API Access.", ErrorCode.CANT_CREATE_API_PROXY, e);
        }
    }

    private void saveAPIAccess(Organization org, Organization actualOrg) throws AppException {
        if (org.getApiAccess() == null || org.getApiAccess().isEmpty()) return;
        if (actualOrg != null && actualOrg.getApiAccess().size() == org.getApiAccess().size() && new HashSet<>(actualOrg.getApiAccess()).containsAll(org.getApiAccess()))
            return;
        APIManagerAPIAccessAdapter accessAdapter = APIManagerAdapter.getInstance().getAccessAdapter();
        accessAdapter.saveAPIAccess(org.getApiAccess(), org, Type.organizations);
    }

    void addImage(Organization org, boolean addImage) throws URISyntaxException, AppException {
        if (!addImage) return;
        if (org.getImageUrl() == null) return;
        URI uri = new URIBuilder(cmd.getAPIManagerURL()).setPath(cmd.getApiBasepath() + ORGANIZATIONS + org.getId() + "/image")
                .build();
        Image image = APIManagerAdapter.getImageFromAPIM(uri, "org-image");
        org.setImage(image);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy