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;
}
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();
}
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();
}
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();
}
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);
}
}
Aggregations