use of org.wso2.carbon.apimgt.core.exception.APIMgtResourceAlreadyExistsException in project carbon-apimgt by wso2.
the class APIStoreImpl method addApplication.
@Override
public ApplicationCreationResponse addApplication(Application application) throws APIManagementException {
ApplicationCreationResponse applicationResponse = null;
try {
if (getApplicationDAO().isApplicationNameExists(application.getName())) {
String message = "An application already exists with a duplicate name - " + application.getName();
log.error(message);
throw new APIMgtResourceAlreadyExistsException(message, ExceptionCodes.APPLICATION_ALREADY_EXISTS);
}
// Tier validation
Policy tier = application.getPolicy();
if (tier == null) {
String message = "Tier name cannot be null - " + application.getName();
log.error(message);
throw new APIManagementException(message, ExceptionCodes.TIER_CANNOT_BE_NULL);
} else {
Policy policy = getPolicyDAO().getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, tier.getPolicyName());
if (policy == null) {
String message = "Specified tier " + tier.getPolicyName() + " is invalid";
log.error(message);
throw new APIManagementException(message, ExceptionCodes.TIER_CANNOT_BE_NULL);
}
application.setPolicy(policy);
}
// Generate UUID for application
String generatedUuid = UUID.randomUUID().toString();
application.setId(generatedUuid);
String permissionString = application.getPermissionString();
if (permissionString != null && !("").equals(permissionString)) {
HashMap roleNamePermissionList;
roleNamePermissionList = getAPIPermissionArray(permissionString);
application.setPermissionMap(roleNamePermissionList);
}
application.setCreatedTime(LocalDateTime.now());
getApplicationDAO().addApplication(application);
WorkflowExecutor appCreationWFExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
ApplicationCreationWorkflow workflow = new ApplicationCreationWorkflow(getApplicationDAO(), getWorkflowDAO(), getApiGateway());
workflow.setApplication(application);
workflow.setCreatedBy(getUsername());
workflow.setWorkflowReference(application.getId());
workflow.setExternalWorkflowReference(UUID.randomUUID().toString());
workflow.setCreatedTime(LocalDateTime.now());
String workflowDescription = "Application [ " + application.getName() + " ] creation request from application creator - " + getUsername() + " with throttling tier - " + tier.getPolicyName() + "";
workflow.setWorkflowDescription(workflowDescription);
WorkflowResponse response = appCreationWFExecutor.execute(workflow);
workflow.setStatus(response.getWorkflowStatus());
if (WorkflowStatus.CREATED != response.getWorkflowStatus()) {
completeWorkflow(appCreationWFExecutor, workflow);
} else {
getApplicationDAO().updateApplicationState(generatedUuid, APIMgtConstants.ApplicationStatus.APPLICATION_ONHOLD);
addWorkflowEntries(workflow);
}
APIUtils.logDebug("successfully added application with appId " + application.getId(), log);
applicationResponse = new ApplicationCreationResponse(application.getId(), response);
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while creating the application - " + application.getName();
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
} catch (ParseException e) {
String errorMsg = "Error occurred while parsing the permission json from swagger in application - " + application.getName();
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, ExceptionCodes.SWAGGER_PARSE_EXCEPTION);
} catch (WorkflowException e) {
String errorMsg = "Error occurred in workflow";
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, ExceptionCodes.WORKFLOW_EXCEPTION);
}
return applicationResponse;
}
use of org.wso2.carbon.apimgt.core.exception.APIMgtResourceAlreadyExistsException in project carbon-apimgt by wso2.
the class CompositeApisApiServiceImpl method compositeApisPost.
@Override
public Response compositeApisPost(CompositeAPIDTO body, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
CompositeAPI.Builder apiBuilder = CompositeAPIMappingUtil.toAPI(body);
APIStore apiStore = RestApiUtil.getConsumer(username);
Application app = apiStore.getApplicationByUuid(apiBuilder.getApplicationId());
// One application can only have one Composite API in default implementation
if (apiStore.getAPISubscriptionsByApplication(app, ApiType.COMPOSITE).size() > 0) {
String errorMessage = "A Composite API already exists for application : " + app.getId();
APIMgtResourceAlreadyExistsException e = new APIMgtResourceAlreadyExistsException(errorMessage, ExceptionCodes.COMPOSITE_API_ALREADY_EXISTS);
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, app.getId());
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
apiStore.addCompositeApi(apiBuilder);
CompositeAPI returnAPI = apiStore.getCompositeAPIbyId(apiBuilder.build().getId());
return Response.status(Response.Status.CREATED).entity(CompositeAPIMappingUtil.toCompositeAPIDTO(returnAPI)).build();
} catch (APIManagementException e) {
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_NAME, body.getName());
paramList.put(APIMgtConstants.ExceptionsConstants.API_VERSION, body.getVersion());
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
Aggregations