Search in sources :

Example 16 with NotFoundException

use of org.wso2.carbon.apimgt.rest.api.admin.NotFoundException in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsApplicationIdDelete.

/**
 * Deletes an existing application
 *
 * @param applicationId     ID of the application
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return 200 response if the deletion was successful
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response applicationsApplicationIdDelete(String applicationId, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    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();
        }
        apiConsumer.deleteApplication(applicationId);
    } catch (APIManagementException e) {
        String errorMessage = "Error while deleting application: " + applicationId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        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();
    }
    return Response.ok().build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 17 with NotFoundException

use of org.wso2.carbon.apimgt.rest.api.admin.NotFoundException in project carbon-apimgt by wso2.

the class ApplicationsApiServiceImpl method applicationsGet.

/**
 * Retrieves all applications that qualifies for the search query
 *
 * @param query       Search query
 * @param limit       Max number of applications to return
 * @param offset      Starting position of pagination
 * @param ifNoneMatch If-None-Match header value
 * @param request     msf4j request object
 * @return A list of qualifying application DTOs as the response
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response applicationsGet(String query, Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
    ApplicationListDTO applicationListDTO = null;
    String username = RestApiUtil.getLoggedInUsername(request);
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    try {
        APIStore apiConsumer = RestApiUtil.getConsumer(username);
        List<Application> allMatchedApps = new ArrayList<>();
        if (StringUtils.isBlank(query)) {
            allMatchedApps = apiConsumer.getApplications(username);
        } else {
            Application application = apiConsumer.getApplicationByName(username, query);
            if (application != null) {
                allMatchedApps = new ArrayList<>();
                allMatchedApps.add(application);
            }
        }
        // allMatchedApps are already sorted to application name
        applicationListDTO = ApplicationMappingUtil.fromApplicationsToDTO(allMatchedApps, limit, offset);
        ApplicationMappingUtil.setPaginationParams(applicationListDTO, limit, offset, allMatchedApps.size());
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving applications";
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_NAME, query);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.ok().entity(applicationListDTO).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) ArrayList(java.util.ArrayList) Application(org.wso2.carbon.apimgt.core.models.Application) ApplicationListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationListDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 18 with NotFoundException

use of org.wso2.carbon.apimgt.rest.api.admin.NotFoundException 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();
    }
}
Also used : ApplicationDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationDTO) ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) WorkflowResponseDTO(org.wso2.carbon.apimgt.rest.api.store.dto.WorkflowResponseDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) ExceptionCodes(org.wso2.carbon.apimgt.core.exception.ExceptionCodes) Application(org.wso2.carbon.apimgt.core.models.Application) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 19 with NotFoundException

use of org.wso2.carbon.apimgt.rest.api.admin.NotFoundException in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdRatingsRatingIdGet.

@Override
public Response apisApiIdRatingsRatingIdGet(String apiId, String ratingId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        Rating rating = apiStore.getRatingByUUID(apiId, ratingId);
        RatingDTO ratingDTO = RatingMappingUtil.fromRatingToDTO(rating);
        return Response.ok().entity(ratingDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving rating for given API " + apiId;
        Map<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        paramList.put(APIMgtConstants.ExceptionsConstants.RATING_ID, ratingId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Rating(org.wso2.carbon.apimgt.core.models.Rating) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) RatingDTO(org.wso2.carbon.apimgt.rest.api.store.dto.RatingDTO) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 20 with NotFoundException

use of org.wso2.carbon.apimgt.rest.api.admin.NotFoundException in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdCommentsCommentIdPut.

/**
 * @param commentId         Comment ID
 * @param apiId             API ID
 * @param body              comment body
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return comment update response
 * @throws NotFoundException if this method is not defined in ApisApiServiceImpl
 */
@Override
public Response apisApiIdCommentsCommentIdPut(String commentId, String apiId, CommentDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String existingFingerprint = apisApiIdCommentsCommentIdPutFingerprint(commentId, apiId, body, ifMatch, ifUnmodifiedSince, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
            return Response.status(Response.Status.PRECONDITION_FAILED).build();
        }
        Comment comment = CommentMappingUtil.fromDTOToComment(body, username);
        apiStore.updateComment(comment, commentId, apiId, username);
        Comment updatedComment = apiStore.getCommentByUUID(commentId, apiId);
        CommentDTO updatedCommentDTO = CommentMappingUtil.fromCommentToDTO(updatedComment);
        String newFingerprint = getEtag(commentId, request.getProperty("LOGGED_IN_USER").toString());
        return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(updatedCommentDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while updating comment : " + commentId;
        Map<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, body.getApiId());
        paramList.put(APIMgtConstants.ExceptionsConstants.COMMENT_ID, commentId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) 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) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)171 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)142 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)120 Response (javax.ws.rs.core.Response)105 Test (org.junit.Test)100 HashMap (java.util.HashMap)99 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)99 Request (org.wso2.msf4j.Request)75 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)48 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)44 ArrayList (java.util.ArrayList)35 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)31 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)29 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)25 Map (java.util.Map)24 ApplicationCreationResponse (org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse)23 PoliciesApiServiceImpl (org.wso2.carbon.apimgt.rest.api.admin.impl.PoliciesApiServiceImpl)20 Application (org.wso2.carbon.apimgt.core.models.Application)19 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)15 API (org.wso2.carbon.apimgt.core.models.API)12