Search in sources :

Example 6 with WorkflowResponseDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.dto.WorkflowResponseDTO 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();
    }
}
Also used : ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) WorkflowResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.WorkflowResponseDTO) StringTokenizer(java.util.StringTokenizer) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with WorkflowResponseDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.dto.WorkflowResponseDTO 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();
}
Also used : 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) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) URI(java.net.URI) WorkflowResponseDTO(org.wso2.carbon.apimgt.rest.api.store.dto.WorkflowResponseDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) GatewayException(org.wso2.carbon.apimgt.core.exception.GatewayException) SubscriptionResponse(org.wso2.carbon.apimgt.core.models.SubscriptionResponse) ExceptionCodes(org.wso2.carbon.apimgt.core.exception.ExceptionCodes) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionDTO) 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 8 with WorkflowResponseDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.dto.WorkflowResponseDTO in project carbon-apimgt by wso2.

the class MiscMappingUtil method fromWorkflowResponseToDTO.

/**
 * Map WorkflowResponse to WorkflowResponseDTO
 *
 * @param response WorkflowResponse object
 * @return WorkflowResponseDTO mapped WorkflowResponseDTO
 */
public static WorkflowResponseDTO fromWorkflowResponseToDTO(WorkflowResponse response) {
    WorkflowResponseDTO responseDTO = new WorkflowResponseDTO();
    responseDTO.setWorkflowStatus(WorkflowStatusEnum.valueOf(response.getWorkflowStatus().toString()));
    responseDTO.setJsonPayload(response.getJSONPayload());
    return responseDTO;
}
Also used : WorkflowResponseDTO(org.wso2.carbon.apimgt.rest.api.store.dto.WorkflowResponseDTO)

Example 9 with WorkflowResponseDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.dto.WorkflowResponseDTO in project carbon-apimgt by wso2.

the class WorkflowsApiServiceImpl method workflowsWorkflowReferenceIdPut.

@Override
public Response workflowsWorkflowReferenceIdPut(String workflowReferenceId, WorkflowRequestDTO body, Request request) throws NotFoundException {
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        Workflow workflow = apiMgtAdminService.retrieveWorkflow(workflowReferenceId);
        if (workflow == null) {
            String errorMessage = "Workflow entry not found for: " + workflowReferenceId;
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.WORKFLOW_NOT_FOUND);
            Map<String, String> paramList = new HashMap<>();
            paramList.put(APIMgtConstants.ExceptionsConstants.WORKFLOW_REF_ID, workflowReferenceId);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
            log.error(errorMessage, e);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        } else {
            if (WorkflowStatus.APPROVED == workflow.getStatus()) {
                String errorMessage = "Workflow is already in complete state";
                APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.WORKFLOW_ALREADY_COMPLETED);
                Map<String, String> paramList = new HashMap<>();
                paramList.put(APIMgtConstants.ExceptionsConstants.WORKFLOW_REF_ID, workflowReferenceId);
                ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
                log.error(errorMessage, e);
                return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
            } else {
                WorkflowExecutor workflowExecutor = WorkflowExecutorFactory.getInstance().getWorkflowExecutor(workflow.getWorkflowType());
                if (body == null) {
                    RestApiUtil.handleBadRequest("Request payload is missing", log);
                }
                if (body.getDescription() != null) {
                    workflow.setWorkflowDescription(body.getDescription());
                }
                if (body.getStatus() == null) {
                    String errorMessage = "Workflow status is not defined";
                    APIManagementException e = new APIManagementException(errorMessage, ExceptionCodes.WORKFLOW_STATE_MISSING);
                    Map<String, String> paramList = new HashMap<>();
                    paramList.put(APIMgtConstants.ExceptionsConstants.WORKFLOW_REF_ID, workflowReferenceId);
                    ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
                    log.error(errorMessage, e);
                    return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
                } else {
                    workflow.setStatus(WorkflowStatus.valueOf(body.getStatus().toString()));
                }
                if (body.getAttributes() != null) {
                    Map<String, String> existingAttributs = workflow.getAttributes();
                    Map<String, String> newAttributes = body.getAttributes();
                    if (existingAttributs == null) {
                        workflow.setAttributes(newAttributes);
                    } else {
                        newAttributes.forEach(existingAttributs::putIfAbsent);
                        workflow.setAttributes(existingAttributs);
                    }
                }
                WorkflowResponse response = apiMgtAdminService.completeWorkflow(workflowExecutor, workflow);
                WorkflowResponseDTO workflowResponseDTO = WorkflowMappingUtil.toWorkflowResponseDTO(response);
                return Response.ok().entity(workflowResponseDTO).build();
            }
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while completing workflow for reference : " + workflowReferenceId + ". " + e.getMessage();
        Map<String, String> paramList = new HashMap<>();
        paramList.put(APIMgtConstants.ExceptionsConstants.WORKFLOW_REF_ID, workflowReferenceId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) Workflow(org.wso2.carbon.apimgt.core.workflow.Workflow) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) WorkflowExecutor(org.wso2.carbon.apimgt.core.api.WorkflowExecutor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with WorkflowResponseDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.dto.WorkflowResponseDTO in project carbon-apimgt by wso2.

the class WorkflowMappingUtil method toWorkflowDTO.

/**
 * Map Workflow to WorkflowDTO
 * @param response WorkflowResponse object
 * @return WorkflowResponseDTO mapped WorkflowResponseDTO
 */
public static WorkflowDTO toWorkflowDTO(Workflow response) {
    WorkflowDTO workflowDTO = new WorkflowDTO();
    if (response != null) {
        workflowDTO.setCreatedTime(response.getCreatedTime().toString());
        workflowDTO.setDescription(response.getWorkflowDescription());
        workflowDTO.setType(response.getWorkflowType());
        workflowDTO.setReferenceId(response.getExternalWorkflowReference());
        workflowDTO.setWorkflowStatus(response.getStatus().toString());
    }
    return workflowDTO;
}
Also used : WorkflowDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.WorkflowDTO)

Aggregations

WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)6 HashMap (java.util.HashMap)5 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)5 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)5 WorkflowResponseDTO (org.wso2.carbon.apimgt.rest.api.store.dto.WorkflowResponseDTO)5 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 Map (java.util.Map)4 ErrorHandler (org.wso2.carbon.apimgt.core.exception.ErrorHandler)4 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)4 Test (org.testng.annotations.Test)3 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)3 Application (org.wso2.carbon.apimgt.core.models.Application)3 WorkflowResponseDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.WorkflowResponseDTO)3 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)2 ExceptionCodes (org.wso2.carbon.apimgt.core.exception.ExceptionCodes)2 WorkflowResponseDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.WorkflowResponseDTO)2 ApplicationDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationDTO)2 StringTokenizer (java.util.StringTokenizer)1 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)1