use of org.wso2.carbon.apimgt.api.ErrorHandler in project carbon-apimgt by wso2.
the class ApplicationsApiServiceImpl method applicationsPost.
/**
* Adds a new application
*
* @param body Application details to be added
* @param request msf4j request object
* @return 201 Response if successful
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response applicationsPost(ApplicationDTO body, Request request) throws NotFoundException {
URI location = null;
ApplicationDTO createdApplicationDTO = null;
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIStore apiConsumer = RestApiUtil.getConsumer(username);
Application application = ApplicationMappingUtil.fromDTOtoApplication(body, username);
ApplicationCreationResponse applicationResponse = apiConsumer.addApplication(application);
String applicationUUID = applicationResponse.getApplicationUUID();
Application createdApplication = apiConsumer.getApplication(applicationUUID, username);
createdApplicationDTO = ApplicationMappingUtil.fromApplicationToDTO(createdApplication);
location = new URI(RestApiConstants.RESOURCE_PATH_APPLICATIONS + "/" + createdApplicationDTO.getApplicationId());
// be in either pending or approved state) send back the workflow response
if (ApplicationStatus.APPLICATION_ONHOLD.equals(createdApplication.getStatus())) {
WorkflowResponseDTO workflowResponse = MiscMappingUtil.fromWorkflowResponseToDTO(applicationResponse.getWorkflowResponse());
return Response.status(Response.Status.ACCEPTED).header(RestApiConstants.LOCATION_HEADER, location).entity(workflowResponse).build();
}
} catch (APIManagementException e) {
String errorMessage = "Error while adding new 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();
HashMap<String, String> paramList = new HashMap<String, String>();
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();
}
// .build()
return Response.status(Response.Status.CREATED).header(RestApiConstants.LOCATION_HEADER, location).entity(createdApplicationDTO).build();
}
use of org.wso2.carbon.apimgt.api.ErrorHandler 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.api.ErrorHandler in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdUserRatingPut.
/**
* Add or update raating to an API
*
* @param apiId APIID
* @param body RatingDTO object
* @param request msf4j request
* @return 201 response if successful
* @throws NotFoundException if failed to find method implementation
*/
@Override
public Response apisApiIdUserRatingPut(String apiId, RatingDTO body, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
String ratingId;
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
Rating ratingFromPayload = RatingMappingUtil.fromDTOToRating(username, apiId, body);
Rating existingRating = apiStore.getRatingForApiFromUser(apiId, username);
if (existingRating != null) {
String existingRatingUUID = existingRating.getUuid();
apiStore.updateRating(apiId, existingRatingUUID, ratingFromPayload);
Rating updatedRating = apiStore.getRatingByUUID(apiId, existingRatingUUID);
RatingDTO updatedRatingDTO = RatingMappingUtil.fromRatingToDTO(updatedRating);
return Response.ok().entity(updatedRatingDTO).build();
} else {
ratingId = apiStore.addRating(apiId, ratingFromPayload);
Rating createdRating = apiStore.getRatingByUUID(apiId, ratingId);
RatingDTO createdRatingDTO = RatingMappingUtil.fromRatingToDTO(createdRating);
URI location = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.SUBRESOURCE_PATH_RATINGS + "/" + ratingId);
return Response.status(Response.Status.CREATED).header(RestApiConstants.LOCATION_HEADER, location).entity(createdRatingDTO).build();
}
} catch (APIManagementException e) {
String errorMessage = "Error while adding/updating rating for user " + username + " for given API " + apiId;
Map<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
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 comment";
ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
log.error(errorMessage, e);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.api.ErrorHandler in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisChangeLifecyclePost.
/**
* Change the lifecycle state of an API
*
* @param action lifecycle action
* @param apiId UUID of API
* @param lifecycleChecklist lifecycle check list items
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return 200 OK if the operation is succesful
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response apisChangeLifecyclePost(String action, String apiId, String lifecycleChecklist, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
Map<String, Boolean> lifecycleChecklistMap = new HashMap<>();
WorkflowResponseDTO response = null;
try {
if (lifecycleChecklist != null) {
String[] checkList = lifecycleChecklist.split(",");
for (String checkList1 : checkList) {
StringTokenizer attributeTokens = new StringTokenizer(checkList1, ":");
String attributeName = attributeTokens.nextToken();
Boolean attributeValue = Boolean.valueOf(attributeTokens.nextToken());
lifecycleChecklistMap.put(attributeName, attributeValue);
}
}
if (action.trim().equals(APIMgtConstants.CHECK_LIST_ITEM_CHANGE_EVENT)) {
RestAPIPublisherUtil.getApiPublisher(username).updateCheckListItem(apiId, action, lifecycleChecklistMap);
WorkflowResponse workflowResponse = new GeneralWorkflowResponse();
// since workflows are not defined for checklist items
workflowResponse.setWorkflowStatus(WorkflowStatus.APPROVED);
response = MappingUtil.toWorkflowResponseDTO(workflowResponse);
return Response.ok().entity(response).build();
} else {
WorkflowResponse workflowResponse = RestAPIPublisherUtil.getApiPublisher(username).updateAPIStatus(apiId, action, lifecycleChecklistMap);
response = MappingUtil.toWorkflowResponseDTO(workflowResponse);
// be in either pending or approved state) send back the workflow response
if (WorkflowStatus.CREATED == workflowResponse.getWorkflowStatus()) {
URI location = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId);
return Response.status(Response.Status.ACCEPTED).header(RestApiConstants.LOCATION_HEADER, location).entity(response).build();
} else {
return Response.ok().entity(response).build();
}
}
} catch (APIManagementException e) {
String errorMessage = "Error while updating lifecycle of API" + apiId + " to " + action;
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
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 api : " + apiId;
Map<String, String> paramList = new HashMap<>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
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.api.ErrorHandler in project carbon-apimgt by wso2.
the class LabelInfoApiServiceImpl method labelInfoGet.
/**
* Get label information for labels provided.
*
* @param labels List of labels
* @param ifNoneMatch If-None-Match header value
* @param ifModifiedSince If-Modified-Since header value
* @param request msf4j request object
* @return Label List
* @throws NotFoundException If failed to get the label values
*/
@Override
public Response labelInfoGet(String labels, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
LabelListDTO labelListDTO;
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
if (labels != null) {
List<String> labelNames = Arrays.asList(labels.split(","));
List<Label> labelList = apiStore.getLabelInfo(labelNames, username);
labelListDTO = LabelMappingUtil.toLabelListDTO(labelList);
} else {
// mandatory parameters not provided
String errorMessage = "Labels parameter should be provided";
ErrorHandler errorHandler = ExceptionCodes.PARAMETER_NOT_PROVIDED;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
log.error(errorMessage);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
} catch (APIManagementException e) {
String errorMessage = "Error occurred while retrieving label information";
HashMap<String, String> paramList = new HashMap<String, String>();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
return Response.ok().entity(labelListDTO).build();
}
Aggregations