Search in sources :

Example 66 with GovernanceException

use of org.wso2.carbon.governance.api.exception.GovernanceException in project carbon-apimgt by wso2.

the class APIUtil method getLightWeightAPI.

/**
 * This method is used to get an API in the Light Weight manner.
 *
 * @param artifact generic artfact
 * @return this will return an API for the selected artifact.
 * @throws APIManagementException , if invalid json config for the API or Api cannot be retrieved from the artifact
 */
public static API getLightWeightAPI(GovernanceArtifact artifact) throws APIManagementException {
    API api;
    try {
        String providerName = artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
        String apiName = artifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
        String apiVersion = artifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
        APIIdentifier apiIdentifier = new APIIdentifier(providerName, apiName, apiVersion);
        api = new API(apiIdentifier);
        int apiId = ApiMgtDAO.getInstance().getAPIID(artifact.getId());
        if (apiId == -1) {
            return null;
        }
        // set uuid
        api.setUUID(artifact.getId());
        api.setRating(getAverageRating(apiId));
        api.setThumbnailUrl(artifact.getAttribute(APIConstants.API_OVERVIEW_THUMBNAIL_URL));
        api.setStatus(getLcStateFromArtifact(artifact));
        api.setContext(artifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT));
        api.setVisibility(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY));
        api.setVisibleRoles(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES));
        api.setVisibleTenants(artifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_TENANTS));
        api.setTransports(artifact.getAttribute(APIConstants.API_OVERVIEW_TRANSPORTS));
        api.setInSequence(artifact.getAttribute(APIConstants.API_OVERVIEW_INSEQUENCE));
        api.setOutSequence(artifact.getAttribute(APIConstants.API_OVERVIEW_OUTSEQUENCE));
        api.setFaultSequence(artifact.getAttribute(APIConstants.API_OVERVIEW_FAULTSEQUENCE));
        api.setDescription(artifact.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION));
        api.setResponseCache(artifact.getAttribute(APIConstants.API_OVERVIEW_RESPONSE_CACHING));
        api.setType(artifact.getAttribute(APIConstants.API_OVERVIEW_TYPE));
        int cacheTimeout = APIConstants.API_RESPONSE_CACHE_TIMEOUT;
        try {
            cacheTimeout = Integer.parseInt(artifact.getAttribute(APIConstants.API_OVERVIEW_CACHE_TIMEOUT));
        } catch (NumberFormatException e) {
        // ignore
        }
        api.setCacheTimeout(cacheTimeout);
        String apiLevelTier = ApiMgtDAO.getInstance().getAPILevelTier(apiId);
        api.setApiLevelPolicy(apiLevelTier);
        Set<Tier> availablePolicy = new HashSet<Tier>();
        String[] subscriptionPolicy = ApiMgtDAO.getInstance().getPolicyNames(PolicyConstants.POLICY_LEVEL_SUB, replaceEmailDomainBack(providerName));
        List<String> definedPolicyNames = Arrays.asList(subscriptionPolicy);
        String policies = artifact.getAttribute(APIConstants.API_OVERVIEW_TIER);
        if (!StringUtils.isEmpty(policies)) {
            String[] policyNames = policies.split("\\|\\|");
            for (String policyName : policyNames) {
                if (definedPolicyNames.contains(policyName) || APIConstants.UNLIMITED_TIER.equals(policyName)) {
                    Tier p = new Tier(policyName);
                    availablePolicy.add(p);
                } else {
                    log.warn("Unknown policy: " + policyName + " found on API: " + apiName);
                }
            }
        }
        api.addAvailableTiers(availablePolicy);
        String tenantDomainName = MultitenantUtils.getTenantDomain(replaceEmailDomainBack(providerName));
        api.setMonetizationCategory(getAPIMonetizationCategory(availablePolicy, tenantDomainName));
        api.setRedirectURL(artifact.getAttribute(APIConstants.API_OVERVIEW_REDIRECT_URL));
        api.setApiOwner(artifact.getAttribute(APIConstants.API_OVERVIEW_OWNER));
        api.setAdvertiseOnly(Boolean.parseBoolean(artifact.getAttribute(APIConstants.API_OVERVIEW_ADVERTISE_ONLY)));
        api.setEndpointConfig(artifact.getAttribute(APIConstants.API_OVERVIEW_ENDPOINT_CONFIG));
        api.setSubscriptionAvailability(artifact.getAttribute(APIConstants.API_OVERVIEW_SUBSCRIPTION_AVAILABILITY));
        api.setSubscriptionAvailableTenants(artifact.getAttribute(APIConstants.API_OVERVIEW_SUBSCRIPTION_AVAILABLE_TENANTS));
        api.setAsDefaultVersion(Boolean.parseBoolean(artifact.getAttribute(APIConstants.API_OVERVIEW_IS_DEFAULT_VERSION)));
        api.setImplementation(artifact.getAttribute(APIConstants.PROTOTYPE_OVERVIEW_IMPLEMENTATION));
        api.setTechnicalOwner(artifact.getAttribute(APIConstants.API_OVERVIEW_TEC_OWNER));
        api.setTechnicalOwnerEmail(artifact.getAttribute(APIConstants.API_OVERVIEW_TEC_OWNER_EMAIL));
        api.setBusinessOwner(artifact.getAttribute(APIConstants.API_OVERVIEW_BUSS_OWNER));
        api.setBusinessOwnerEmail(artifact.getAttribute(APIConstants.API_OVERVIEW_BUSS_OWNER_EMAIL));
        String environments = artifact.getAttribute(APIConstants.API_OVERVIEW_ENVIRONMENTS);
        api.setEnvironments(extractEnvironmentsForAPI(environments));
        api.setCorsConfiguration(getCorsConfigurationFromArtifact(artifact));
        try {
            api.setEnvironmentList(extractEnvironmentListForAPI(artifact.getAttribute(APIConstants.API_OVERVIEW_ENDPOINT_CONFIG)));
        } catch (ParseException e) {
            String msg = "Failed to parse endpoint config JSON of API: " + apiName + " " + apiVersion;
            throw new APIManagementException(msg, e);
        } catch (ClassCastException e) {
            String msg = "Invalid endpoint config JSON found in API: " + apiName + " " + apiVersion;
            throw new APIManagementException(msg, e);
        }
    } catch (GovernanceException e) {
        String msg = "Failed to get API from artifact";
        throw new APIManagementException(msg, e);
    }
    return api;
}
Also used : Tier(org.wso2.carbon.apimgt.api.model.Tier) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) Endpoint(org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ParseException(org.json.simple.parser.ParseException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 67 with GovernanceException

use of org.wso2.carbon.governance.api.exception.GovernanceException in project carbon-apimgt by wso2.

the class APIUtil method getDocumentation.

/**
 * Create the Documentation from artifact
 *
 * @param artifact Documentation artifact
 * @return Documentation
 * @throws APIManagementException if failed to create Documentation from artifact
 */
public static Documentation getDocumentation(GenericArtifact artifact, String docCreatorName) throws APIManagementException {
    Documentation documentation;
    try {
        DocumentationType type;
        String docType = artifact.getAttribute(APIConstants.DOC_TYPE);
        if (docType.equalsIgnoreCase(DocumentationType.HOWTO.getType())) {
            type = DocumentationType.HOWTO;
        } else if (docType.equalsIgnoreCase(DocumentationType.PUBLIC_FORUM.getType())) {
            type = DocumentationType.PUBLIC_FORUM;
        } else if (docType.equalsIgnoreCase(DocumentationType.SUPPORT_FORUM.getType())) {
            type = DocumentationType.SUPPORT_FORUM;
        } else if (docType.equalsIgnoreCase(DocumentationType.API_MESSAGE_FORMAT.getType())) {
            type = DocumentationType.API_MESSAGE_FORMAT;
        } else if (docType.equalsIgnoreCase(DocumentationType.SAMPLES.getType())) {
            type = DocumentationType.SAMPLES;
        } else {
            type = DocumentationType.OTHER;
        }
        documentation = new Documentation(type, artifact.getAttribute(APIConstants.DOC_NAME));
        documentation.setId(artifact.getId());
        documentation.setSummary(artifact.getAttribute(APIConstants.DOC_SUMMARY));
        String visibilityAttr = artifact.getAttribute(APIConstants.DOC_VISIBILITY);
        Documentation.DocumentVisibility documentVisibility = Documentation.DocumentVisibility.API_LEVEL;
        if (visibilityAttr != null) {
            if (visibilityAttr.equals(Documentation.DocumentVisibility.API_LEVEL.name())) {
                documentVisibility = Documentation.DocumentVisibility.API_LEVEL;
            } else if (visibilityAttr.equals(Documentation.DocumentVisibility.PRIVATE.name())) {
                documentVisibility = Documentation.DocumentVisibility.PRIVATE;
            } else if (visibilityAttr.equals(Documentation.DocumentVisibility.OWNER_ONLY.name())) {
                documentVisibility = Documentation.DocumentVisibility.OWNER_ONLY;
            }
        }
        documentation.setVisibility(documentVisibility);
        Documentation.DocumentSourceType docSourceType = Documentation.DocumentSourceType.INLINE;
        String artifactAttribute = artifact.getAttribute(APIConstants.DOC_SOURCE_TYPE);
        if (artifactAttribute.equals(Documentation.DocumentSourceType.MARKDOWN.name())) {
            docSourceType = Documentation.DocumentSourceType.MARKDOWN;
        } else if (artifactAttribute.equals(Documentation.DocumentSourceType.URL.name())) {
            docSourceType = Documentation.DocumentSourceType.URL;
        } else if (artifactAttribute.equals(Documentation.DocumentSourceType.FILE.name())) {
            docSourceType = Documentation.DocumentSourceType.FILE;
        }
        documentation.setSourceType(docSourceType);
        if ("URL".equals(artifact.getAttribute(APIConstants.DOC_SOURCE_TYPE))) {
            documentation.setSourceUrl(artifact.getAttribute(APIConstants.DOC_SOURCE_URL));
        }
        if (docSourceType == Documentation.DocumentSourceType.FILE) {
            String filePath = prependTenantPrefix(artifact.getAttribute(APIConstants.DOC_FILE_PATH), docCreatorName);
            documentation.setFilePath(prependWebContextRoot(filePath));
        }
        if (documentation.getType() == DocumentationType.OTHER) {
            documentation.setOtherTypeName(artifact.getAttribute(APIConstants.DOC_OTHER_TYPE_NAME));
        }
    } catch (GovernanceException e) {
        throw new APIManagementException("Failed to get documentation from artifact: " + e);
    }
    return documentation;
}
Also used : DocumentationType(org.wso2.carbon.apimgt.api.model.DocumentationType) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException)

Aggregations

GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)42 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)32 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)29 API (org.wso2.carbon.apimgt.api.model.API)26 Test (org.junit.Test)19 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)19 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)18 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)17 ArrayList (java.util.ArrayList)15 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)15 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)14 Tier (org.wso2.carbon.apimgt.api.model.Tier)13 HashSet (java.util.HashSet)11 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)10 UserStoreException (org.wso2.carbon.user.api.UserStoreException)10 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)9 Endpoint (org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint)9 Resource (org.wso2.carbon.registry.core.Resource)9 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)9 LinkedHashSet (java.util.LinkedHashSet)8