Search in sources :

Example 21 with ErrorHandler

use of org.wso2.carbon.apimgt.core.exception.ErrorHandler in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdCommentsPost.

/**
 * Update a comment
 *
 * @param apiId   API ID
 * @param body    comment body
 * @param request msf4j request object
 * @return comment update response
 * @throws NotFoundException if this method is not defined in ApisApiServiceImpl
 */
@Override
public Response apisApiIdCommentsPost(String apiId, CommentDTO body, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        Comment comment = CommentMappingUtil.fromDTOToComment(body, username);
        String createdCommentId = apiStore.addComment(comment, apiId);
        Comment createdComment = apiStore.getCommentByUUID(createdCommentId, apiId);
        CommentDTO createdCommentDTO = CommentMappingUtil.fromCommentToDTO(createdComment);
        URI location = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.SUBRESOURCE_PATH_COMMENTS + "/" + createdCommentId);
        String fingerprint = getEtag(comment.getUuid(), request.getProperty("LOGGED_IN_USER").toString());
        return Response.status(Response.Status.CREATED).header(RestApiConstants.LOCATION_HEADER, location).header(HttpHeaders.ETAG, "\"" + fingerprint + "\"").entity(createdCommentDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while adding comment to api : " + apiId;
        Map<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, body.getApiId());
        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();
    }
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) CommentDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Aggregations

ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)18 ErrorHandler (org.wso2.carbon.apimgt.core.exception.ErrorHandler)16 HashMap (java.util.HashMap)13 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)12 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)7 URI (java.net.URI)6 URISyntaxException (java.net.URISyntaxException)6 Map (java.util.Map)5 Application (org.wso2.carbon.apimgt.core.models.Application)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Test (org.testng.annotations.Test)3 WorkflowResponseDTO (org.wso2.carbon.apimgt.rest.api.store.dto.WorkflowResponseDTO)3 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)2 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)2 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)2 ExceptionCodes (org.wso2.carbon.apimgt.core.exception.ExceptionCodes)2 Label (org.wso2.carbon.apimgt.core.models.Label)2 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)2 ErrorDTO (org.wso2.carbon.apimgt.rest.api.authenticator.dto.ErrorDTO)2 APIMgtSecurityException (org.wso2.carbon.apimgt.rest.api.common.exception.APIMgtSecurityException)2