Search in sources :

Example 96 with APIProvider

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

the class OperationPoliciesApiServiceImpl method getCommonOperationPolicyByPolicyId.

/**
 * Get the common operation policy by providing the policy ID
 *
 * @param operationPolicyId UUID of the operation policy
 * @param messageContext    message context
 * @return Operation policy DTO as response
 */
@Override
public Response getCommonOperationPolicyByPolicyId(String operationPolicyId, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        OperationPolicyData existingPolicy = apiProvider.getCommonOperationPolicyByPolicyId(operationPolicyId, organization, false);
        if (existingPolicy != null) {
            OperationPolicyDataDTO policyDataDTO = OperationPolicyMappingUtil.fromOperationPolicyDataToDTO(existingPolicy);
            return Response.ok().entity(policyDataDTO).build();
        } else {
            throw new APIMgtResourceNotFoundException("Couldn't retrieve an existing common policy with ID: " + operationPolicyId, ExceptionCodes.from(ExceptionCodes.OPERATION_POLICY_NOT_FOUND, operationPolicyId));
        }
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_PATH_OPERATION_POLICIES, operationPolicyId, e, log);
        } else {
            String errorMessage = "Error while getting the common operation policy with ID :" + operationPolicyId + " " + e.getMessage();
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    } catch (Exception e) {
        RestApiUtil.handleInternalServerError("An error has occurred while getting the common operation " + " policy with ID: " + operationPolicyId, e, log);
    }
    return null;
}
Also used : OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) OperationPolicyDataDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 97 with APIProvider

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

the class ScopesApiServiceImpl method deleteSharedScope.

/**
 * Delete shared scope.
 *
 * @param scopeId        Scope UUID
 * @param messageContext CXF Message Context
 * @return Deletion Response
 * @throws APIManagementException If an error occurs while deleting shared scope
 */
@Override
public Response deleteSharedScope(String scopeId, MessageContext messageContext) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
    if (StringUtils.isEmpty(scopeId)) {
        throw new APIManagementException("Scope Id cannot be null or empty", ExceptionCodes.SHARED_SCOPE_ID_NOT_SPECIFIED);
    }
    Scope existingScope = apiProvider.getSharedScopeByUUID(scopeId, tenantDomain);
    if (apiProvider.isScopeKeyAssignedToAPI(existingScope.getKey(), tenantDomain)) {
        throw new APIManagementException("Cannot remove the Shared Scope " + scopeId + " as it is used by one " + "or more APIs", ExceptionCodes.from(ExceptionCodes.SHARED_SCOPE_ALREADY_ATTACHED, scopeId));
    }
    apiProvider.deleteSharedScope(existingScope.getKey(), tenantDomain);
    return Response.ok().build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Scope(org.wso2.carbon.apimgt.api.model.Scope) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 98 with APIProvider

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

the class ScopesApiServiceImpl method getSharedScopeUsages.

@Override
public Response getSharedScopeUsages(String scopeId, MessageContext messageContext) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
    int tenantId = APIUtil.getTenantIdFromTenantDomain(tenantDomain);
    if (StringUtils.isEmpty(scopeId)) {
        throw new APIManagementException("Scope Id cannot be null or empty", ExceptionCodes.SHARED_SCOPE_ID_NOT_SPECIFIED);
    }
    SharedScopeUsage sharedScopeUsage = apiProvider.getSharedScopeUsage(scopeId, tenantId);
    SharedScopeUsageDTO sharedScopeUsageDTO = SharedScopeMappingUtil.fromSharedScopeUsageToDTO(sharedScopeUsage);
    return Response.ok().entity(sharedScopeUsageDTO).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SharedScopeUsageDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SharedScopeUsageDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) SharedScopeUsage(org.wso2.carbon.apimgt.api.model.SharedScopeUsage)

Example 99 with APIProvider

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

the class ScopesApiServiceImpl method getSharedScope.

/**
 * Get shared scope by Id.
 *
 * @param scopeId        UUID of the scope
 * @param messageContext CXF Message Context
 * @return Shared Scope DTO
 * @throws APIManagementException If an error occurs while getting shared scope
 */
@Override
public Response getSharedScope(String scopeId, MessageContext messageContext) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
    if (StringUtils.isEmpty(scopeId)) {
        throw new APIManagementException("Scope Id cannot be null or empty", ExceptionCodes.SHARED_SCOPE_ID_NOT_SPECIFIED);
    }
    Scope scope = apiProvider.getSharedScopeByUUID(scopeId, tenantDomain);
    ScopeDTO scopeDTO = SharedScopeMappingUtil.fromScopeToDTO(scope);
    return Response.ok().entity(scopeDTO).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Scope(org.wso2.carbon.apimgt.api.model.Scope) ScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 100 with APIProvider

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

the class ScopesApiServiceImpl method addSharedScope.

/**
 * Add Shared Scope.
 *
 * @param body           Scope DTO object to add
 * @param messageContext CXF Message Context
 * @return Created Scope as DTO
 * @throws APIManagementException If an error occurs while adding shared scope.
 */
@Override
public Response addSharedScope(ScopeDTO body, MessageContext messageContext) throws APIManagementException {
    String scopeName = body.getName();
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
        if (StringUtils.isEmpty(scopeName)) {
            throw new APIManagementException("Shared Scope Name cannot be null or empty", ExceptionCodes.SHARED_SCOPE_NAME_NOT_SPECIFIED);
        }
        if (StringUtils.isEmpty(body.getDisplayName())) {
            throw new APIManagementException("Shared scope Display Name cannot be null or empty", ExceptionCodes.SHARED_SCOPE_DISPLAY_NAME_NOT_SPECIFIED);
        }
        if (apiProvider.isScopeKeyExist(scopeName, APIUtil.getTenantIdFromTenantDomain(tenantDomain))) {
            throw new APIManagementException(ExceptionCodes.from(ExceptionCodes.SCOPE_ALREADY_REGISTERED, scopeName));
        }
        Scope scopeToAdd = SharedScopeMappingUtil.fromDTOToScope(body);
        String sharedScopeId = apiProvider.addSharedScope(scopeToAdd, tenantDomain);
        // Get registered shared scope
        Scope createdScope = apiProvider.getSharedScopeByUUID(sharedScopeId, tenantDomain);
        ScopeDTO createdScopeDTO = SharedScopeMappingUtil.fromScopeToDTO(createdScope);
        String createdScopeURIString = RestApiConstants.RESOURCE_PATH_SHARED_SCOPES_SCOPE_ID.replace(RestApiConstants.SHARED_SCOPE_ID_PARAM, createdScopeDTO.getId());
        URI createdScopeURI = new URI(createdScopeURIString);
        return Response.created(createdScopeURI).entity(createdScopeDTO).build();
    } catch (URISyntaxException e) {
        throw new APIManagementException("Error while creating shared scope: " + scopeName, e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Scope(org.wso2.carbon.apimgt.api.model.Scope) ScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) URI(java.net.URI)

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