Search in sources :

Example 36 with ErrorDTO

use of org.wso2.carbon.apimgt.rest.api.authenticator.dto.ErrorDTO 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 37 with ErrorDTO

use of org.wso2.carbon.apimgt.rest.api.authenticator.dto.ErrorDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisValidateDefinitionPost.

/**
 * Validates a provided API definition
 *
 * @param type            API definition type (SWAGGER or WSDL)
 * @param fileInputStream file content stream
 * @param fileDetail      file details
 * @param url             URL of the definition
 * @param request         msf4j request
 * @return API definition validation information
 * @throws NotFoundException
 */
@Override
public Response apisValidateDefinitionPost(String type, InputStream fileInputStream, FileInfo fileDetail, String url, Request request) throws NotFoundException {
    String errorMessage = "Error while validating the definition";
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        if (StringUtils.isBlank(type)) {
            type = APIDefinitionValidationResponseDTO.DefinitionTypeEnum.SWAGGER.toString();
        }
        Response responseIfParamsInvalid = buildResponseIfParamsInvalid(type, fileInputStream, url);
        if (responseIfParamsInvalid != null) {
            return responseIfParamsInvalid;
        }
        if (APIDefinitionValidationResponseDTO.DefinitionTypeEnum.SWAGGER.toString().equals(type)) {
            if (log.isDebugEnabled()) {
                log.debug("Validating a swagger file.");
            }
            // TODO implement swagger validation
            return Response.noContent().build();
        } else {
            // WSDL type
            WSDLProcessor processor = null;
            WSDLInfo info = null;
            if (!StringUtils.isBlank(url)) {
                processor = WSDLProcessFactory.getInstance().getWSDLProcessor(url);
                info = processor.getWsdlInfo();
                if (log.isDebugEnabled()) {
                    log.debug("Successfully validated WSDL URL " + url);
                }
            } else {
                if (fileDetail.getFileName().endsWith(".zip")) {
                    WSDLArchiveInfo archiveInfo = apiPublisher.extractAndValidateWSDLArchive(fileInputStream);
                    info = archiveInfo.getWsdlInfo();
                    if (log.isDebugEnabled()) {
                        log.debug("Successfully validated WSDL archive " + fileDetail.getFileName());
                    }
                } else if (fileDetail.getFileName().endsWith(".wsdl")) {
                    byte[] wsdlContent = IOUtils.toByteArray(fileInputStream);
                    processor = WSDLProcessFactory.getInstance().getWSDLProcessor(wsdlContent);
                    info = processor.getWsdlInfo();
                    if (log.isDebugEnabled()) {
                        log.debug("Successfully validated WSDL file " + fileDetail.getFileName());
                    }
                } else {
                    String msg = "Unsupported extension type of file: " + fileDetail.getFileName();
                    log.error(msg);
                    ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900700L, msg);
                    return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
                }
            }
            if (info != null) {
                APIDefinitionValidationResponseDTO responseDTO = MappingUtil.toWSDLValidationResponseDTO(info);
                return Response.ok(responseDTO).build();
            }
            APIDefinitionValidationResponseDTO responseDTO = new APIDefinitionValidationResponseDTO();
            responseDTO.isValid(false);
            return Response.ok().entity(responseDTO).build();
        }
    } catch (APIManagementException e) {
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    } catch (IOException e) {
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
        log.error(errorMessage, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
    }
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) Response(javax.ws.rs.core.Response) WSDLProcessor(org.wso2.carbon.apimgt.core.api.WSDLProcessor) WSDLInfo(org.wso2.carbon.apimgt.core.models.WSDLInfo) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) WSDLArchiveInfo(org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo) IOException(java.io.IOException) APIDefinitionValidationResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.APIDefinitionValidationResponseDTO)

Example 38 with ErrorDTO

use of org.wso2.carbon.apimgt.rest.api.authenticator.dto.ErrorDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdLifecycleLifecyclePendingTaskDelete.

/**
 * Remove pending lifecycle state change workflow tasks.
 *
 * @param apiId   api id
 * @param request msf4j request object
 * @return Empty payload
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdLifecycleLifecyclePendingTaskDelete(String apiId, Request request) throws NotFoundException {
    try {
        String username = RestApiUtil.getLoggedInUsername(request);
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        apiPublisher.removePendingLifecycleWorkflowTaskForAPI(apiId);
        return Response.ok().build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while removing pending task for API state change for api " + apiId;
        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();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) Map(java.util.Map) HashMap(java.util.HashMap)

Example 39 with ErrorDTO

use of org.wso2.carbon.apimgt.rest.api.authenticator.dto.ErrorDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdThreatProtectionPoliciesDelete.

/**
 * Delete a threat protection policy from an API
 * @param apiId APIID
 * @param policyId Threat protection policy id
 * @param request MSF4J Request
 * @return HTTP status 200 if success, 500 otherwise
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdThreatProtectionPoliciesDelete(String apiId, String policyId, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        if (!apiPublisher.isAPIExists(apiId)) {
            ErrorDTO errorDTO = new ErrorDTO();
            errorDTO.setCode(404l);
            errorDTO.setDescription("Specified API was not found");
            return Response.status(404).entity(errorDTO).build();
        }
        apiPublisher.deleteThreatProtectionPolicy(apiId, policyId);
        return Response.ok().build();
    } catch (APIManagementException e) {
        log.error(e.getMessage(), e);
        return Response.status(500).entity("Internal Server Error.").build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher)

Example 40 with ErrorDTO

use of org.wso2.carbon.apimgt.rest.api.authenticator.dto.ErrorDTO in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdDocumentsGet.

/**
 * Retrieves a list of documents of an API
 *
 * @param apiId       UUID of API
 * @param limit       maximum documents to return
 * @param offset      starting position of the pagination
 * @param ifNoneMatch If-None-Match header value
 * @param request     msf4j request object
 * @return a list of document DTOs
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdDocumentsGet(String apiId, Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        List<DocumentInfo> documentInfos = apiPublisher.getAllDocumentation(apiId, offset, limit);
        DocumentListDTO documentListDTO = MappingUtil.toDocumentListDTO(documentInfos);
        return Response.status(Response.Status.OK).entity(documentListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while getting list of documents" + apiId;
        HashMap<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();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) DocumentListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.DocumentListDTO)

Aggregations

ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)155 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)154 HashMap (java.util.HashMap)106 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)48 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)45 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)40 Map (java.util.Map)25 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)15 ErrorHandler (org.wso2.carbon.apimgt.core.exception.ErrorHandler)15 IOException (java.io.IOException)11 Application (org.wso2.carbon.apimgt.core.models.Application)11 URI (java.net.URI)10 URISyntaxException (java.net.URISyntaxException)10 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)9 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)7 ArrayList (java.util.ArrayList)6 API (org.wso2.carbon.apimgt.core.models.API)6 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)6 Label (org.wso2.carbon.apimgt.core.models.Label)6 ErrorDTO (org.wso2.carbon.apimgt.rest.api.authenticator.dto.ErrorDTO)6