use of org.wso2.carbon.apimgt.core.exception.ExceptionCodes in project carbon-apimgt by wso2.
the class ApplicationsApiServiceImpl method applicationsApplicationIdPut.
/**
* Updates an existing application
*
* @param applicationId application Id
* @param body Application details to be updated
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return Updated application details as the response
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response applicationsApplicationIdPut(String applicationId, ApplicationDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
ApplicationDTO updatedApplicationDTO = null;
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIStore apiConsumer = RestApiUtil.getConsumer(username);
String existingFingerprint = applicationsApplicationIdGetFingerprint(applicationId, null, null, request);
if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
return Response.status(Response.Status.PRECONDITION_FAILED).build();
}
Application application = ApplicationMappingUtil.fromDTOtoApplication(body, username);
if (!ApplicationStatus.APPLICATION_APPROVED.equals(application.getStatus())) {
String errorMessage = "Application " + applicationId + " is not active";
ExceptionCodes exceptionCode = ExceptionCodes.APPLICATION_INACTIVE;
APIManagementException e = new APIManagementException(errorMessage, exceptionCode);
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
WorkflowResponse updateResponse = apiConsumer.updateApplication(applicationId, application);
if (WorkflowStatus.REJECTED == updateResponse.getWorkflowStatus()) {
String errorMessage = "Update request for application " + applicationId + " is rejected";
ExceptionCodes exceptionCode = ExceptionCodes.WORKFLOW_REJCECTED;
APIManagementException e = new APIManagementException(errorMessage, exceptionCode);
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
// retrieves the updated application and send as the response
Application updatedApplication = apiConsumer.getApplication(applicationId, username);
updatedApplicationDTO = ApplicationMappingUtil.fromApplicationToDTO(updatedApplication);
String newFingerprint = applicationsApplicationIdGetFingerprint(applicationId, null, null, request);
// be in either pending or approved state) send back the workflow response
if (ApplicationStatus.APPLICATION_ONHOLD.equals(updatedApplication.getStatus())) {
WorkflowResponseDTO workflowResponse = MiscMappingUtil.fromWorkflowResponseToDTO(updateResponse);
URI location = new URI(RestApiConstants.RESOURCE_PATH_APPLICATIONS + "/" + applicationId);
return Response.status(Response.Status.ACCEPTED).header(RestApiConstants.LOCATION_HEADER, location).entity(workflowResponse).build();
}
return Response.ok().entity(updatedApplicationDTO).header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").build();
} catch (APIManagementException e) {
String errorMessage = "Error while updating application: " + body.getName();
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_NAME, body.getName());
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (URISyntaxException e) {
String errorMessage = "Error while adding location header in response for application : " + body.getName();
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_NAME, body.getName());
ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler, paramList);
log.error(errorMessage, e);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.core.exception.ExceptionCodes in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsPost.
/**
* Adds a new subscription
*
* @param body Subscription details to be added
* @param request msf4j request object
* @return Newly added subscription as the response
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response subscriptionsPost(SubscriptionDTO body, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
SubscriptionDTO subscriptionDTO = null;
URI location = null;
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
String applicationId = body.getApplicationId();
String apiId = body.getApiIdentifier();
String tier = body.getPolicy();
Application application = apiStore.getApplicationByUuid(applicationId);
if (application != null && !ApplicationStatus.APPLICATION_APPROVED.equals(application.getStatus())) {
String errorMessage = "Application " + applicationId + " is not active";
ExceptionCodes exceptionCode = ExceptionCodes.APPLICATION_INACTIVE;
APIManagementException e = new APIManagementException(errorMessage, exceptionCode);
Map<String, String> paramList = new HashMap<>();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
if (application != null) {
SubscriptionResponse addSubResponse = apiStore.addApiSubscription(apiId, applicationId, tier);
String subscriptionId = addSubResponse.getSubscriptionUUID();
Subscription subscription = apiStore.getSubscriptionByUUID(subscriptionId);
location = new URI(RestApiConstants.RESOURCE_PATH_SUBSCRIPTION + "/" + subscriptionId);
subscriptionDTO = SubscriptionMappingUtil.fromSubscriptionToDTO(subscription);
// be in either pending or approved state) send back the workflow response
if (SubscriptionStatus.ON_HOLD == subscription.getStatus()) {
WorkflowResponseDTO workflowResponse = MiscMappingUtil.fromWorkflowResponseToDTO(addSubResponse.getWorkflowResponse());
return Response.status(Response.Status.ACCEPTED).header(RestApiConstants.LOCATION_HEADER, location).entity(workflowResponse).build();
}
} else {
String errorMessage = null;
ExceptionCodes exceptionCode = null;
exceptionCode = ExceptionCodes.APPLICATION_NOT_FOUND;
errorMessage = "Application not found";
APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, exceptionCode);
Map<String, String> paramList = new HashMap<>();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
} catch (GatewayException e) {
String errorMessage = "Failed to add subscription of API : " + body.getApiIdentifier() + " to gateway";
log.error(errorMessage, e);
return Response.status(Response.Status.ACCEPTED).build();
} catch (APIManagementException e) {
String errorMessage = "Error while adding subscriptions";
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, body.getApiIdentifier());
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, body.getApplicationId());
paramList.put(APIMgtConstants.ExceptionsConstants.TIER, body.getPolicy());
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (URISyntaxException e) {
String errorMessage = "Error while adding location header in response for subscription : " + body.getSubscriptionId();
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.SUBSCRIPTION_ID, body.getSubscriptionId());
ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler, paramList);
log.error(errorMessage, e);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
return Response.status(Response.Status.CREATED).header(RestApiConstants.LOCATION_HEADER, location).entity(subscriptionDTO).build();
}
Aggregations