Search in sources :

Example 1 with APIProvider

use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.

the class RegistryPersistenceDocUtil method createDocArtifactContent.

public static GenericArtifact createDocArtifactContent(GenericArtifact artifact, String apiName, String apiVersion, String apiProvider, Documentation documentation) throws DocumentationPersistenceException {
    try {
        artifact.setAttribute(APIConstants.DOC_NAME, documentation.getName());
        artifact.setAttribute(APIConstants.DOC_SUMMARY, documentation.getSummary());
        artifact.setAttribute(APIConstants.DOC_TYPE, documentation.getType().getType());
        artifact.setAttribute(APIConstants.DOC_VISIBILITY, documentation.getVisibility().name());
        Documentation.DocumentSourceType sourceType = documentation.getSourceType();
        switch(sourceType) {
            case INLINE:
                sourceType = Documentation.DocumentSourceType.INLINE;
                break;
            case MARKDOWN:
                sourceType = Documentation.DocumentSourceType.MARKDOWN;
                break;
            case URL:
                sourceType = Documentation.DocumentSourceType.URL;
                break;
            case FILE:
                {
                    sourceType = Documentation.DocumentSourceType.FILE;
                }
                break;
            default:
                throw new DocumentationPersistenceException("Unknown sourceType " + sourceType + " provided for documentation");
        }
        // Therefore setting a default value if it is not set.
        if (documentation.getSourceUrl() == null) {
            documentation.setSourceUrl(" ");
        }
        artifact.setAttribute(APIConstants.DOC_SOURCE_TYPE, sourceType.name());
        artifact.setAttribute(APIConstants.DOC_SOURCE_URL, documentation.getSourceUrl());
        artifact.setAttribute(APIConstants.DOC_FILE_PATH, documentation.getFilePath());
        artifact.setAttribute(APIConstants.DOC_OTHER_TYPE_NAME, documentation.getOtherTypeName());
        String basePath = RegistryPersistenceUtil.replaceEmailDomain(apiProvider) + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion;
        artifact.setAttribute(APIConstants.DOC_API_BASE_PATH, basePath);
    } catch (GovernanceException e) {
        String msg = "Failed to create doc artifact content from :" + documentation.getName();
        log.error(msg, e);
        throw new DocumentationPersistenceException(msg, e);
    }
    return artifact;
}
Also used : DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) Documentation(org.wso2.carbon.apimgt.persistence.dto.Documentation) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException)

Example 2 with APIProvider

use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesMediationMediationPolicyIdGet.

/**
 * Returns a specific global mediation policy by identifier
 *
 * @param mediationPolicyId Mediation policy uuid
 * @param accept            Accept header value
 * @return returns the matched mediation
 */
@Override
public Response policiesMediationMediationPolicyIdGet(String mediationPolicyId, String accept, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        // Get given global mediation policy
        Mediation mediation = apiProvider.getGlobalMediationPolicy(mediationPolicyId);
        if (mediation != null) {
            MediationDTO mediationDTO = MediationMappingUtil.fromMediationToDTO(mediation);
            return Response.ok().entity(mediationDTO).build();
        } else {
            // If global mediation policy not exists
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_POLICY, mediationPolicyId, log);
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving the global mediation policy with id " + mediationPolicyId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) MediationDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.MediationDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) Mediation(org.wso2.carbon.apimgt.api.model.Mediation)

Example 3 with APIProvider

use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.

the class ThrottlingApiServiceImpl method throttlingPoliciesApplicationPolicyIdDelete.

/**
 * Delete an Application level policy specified by uuid
 *
 * @param policyId          uuid of the policy
 * @return 200 OK response if successfully deleted the policy
 */
@Override
public Response throttlingPoliciesApplicationPolicyIdDelete(String policyId, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String username = RestApiCommonUtil.getLoggedInUsername();
        // This will give PolicyNotFoundException if there's no policy exists with UUID
        ApplicationPolicy existingPolicy = apiProvider.getApplicationPolicyByUUID(policyId);
        if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, existingPolicy)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APP_POLICY, policyId, log);
        }
        if (apiProvider.hasAttachments(username, existingPolicy.getPolicyName(), PolicyConstants.POLICY_LEVEL_APP)) {
            String message = "Policy " + policyId + " already attached to an application";
            log.error(message);
            throw new APIManagementException(message);
        }
        apiProvider.deletePolicy(username, PolicyConstants.POLICY_LEVEL_APP, existingPolicy.getPolicyName());
        return Response.ok().build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APP_POLICY, policyId, e, log);
        } else {
            String errorMessage = "Error while deleting Application level policy : " + policyId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 4 with APIProvider

use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.

the class ThrottlingApiServiceImpl method throttlingPoliciesSubscriptionPolicyIdGet.

/**
 * Get a specific Subscription Policy by its uuid
 *
 * @param policyId        uuid of the policy
 * @return Matched Subscription Throttle Policy by the given name
 */
@Override
public Response throttlingPoliciesSubscriptionPolicyIdGet(String policyId, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String username = RestApiCommonUtil.getLoggedInUsername();
        // This will give PolicyNotFoundException if there's no policy exists with UUID
        SubscriptionPolicy subscriptionPolicy = apiProvider.getSubscriptionPolicyByUUID(policyId);
        if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, subscriptionPolicy)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, log);
        }
        SubscriptionThrottlePolicyDTO policyDTO = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyToDTO(subscriptionPolicy);
        // setting policy permissions
        setPolicyPermissionsToDTO(policyDTO);
        return Response.ok().entity(policyDTO).build();
    } catch (APIManagementException | ParseException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, e, log);
        } else {
            String errorMessage = "Error while retrieving Subscription level policy: " + policyId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) ParseException(org.json.simple.parser.ParseException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 5 with APIProvider

use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.

the class ThrottlingApiServiceImpl method throttlingPoliciesCustomRuleIdGet.

/**
 * Get a specific custom rule by its name
 *
 * @param ruleId          uuid of the policy
 * @return Matched Global Throttle Policy by the given name
 */
@Override
public Response throttlingPoliciesCustomRuleIdGet(String ruleId, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String username = RestApiCommonUtil.getLoggedInUsername();
        // only super tenant is allowed to access global policies/custom rules
        checkTenantDomainForCustomRules();
        // This will give PolicyNotFoundException if there's no policy exists with UUID
        GlobalPolicy globalPolicy = apiProvider.getGlobalPolicyByUUID(ruleId);
        if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, globalPolicy)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, log);
        }
        CustomRuleDTO policyDTO = GlobalThrottlePolicyMappingUtil.fromGlobalThrottlePolicyToDTO(globalPolicy);
        return Response.ok().entity(policyDTO).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, e, log);
        } else {
            String errorMessage = "Error while retrieving Custom Rule: " + ruleId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)207 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)198 API (org.wso2.carbon.apimgt.api.model.API)92 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)83 Test (org.junit.Test)82 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)82 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)73 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)65 IOException (java.io.IOException)40 ArrayList (java.util.ArrayList)36 URISyntaxException (java.net.URISyntaxException)34 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)32 URI (java.net.URI)31 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)31 JSONObject (org.json.simple.JSONObject)29 FaultGatewaysException (org.wso2.carbon.apimgt.api.FaultGatewaysException)29 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)29 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)28 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)28 Documentation (org.wso2.carbon.apimgt.api.model.Documentation)23