Search in sources :

Example 1 with OpenAPIDefinitionValidationResponseDTO

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

the class ApisApiServiceImpl method validateOpenAPIDefinition.

/**
 * Validate API Definition and retrieve as the response
 *
 * @param url URL of the OpenAPI definition
 * @param fileInputStream InputStream for the provided file
 * @param fileDetail File meta-data
 * @param returnContent Whether to return the definition content
 * @param inlineApiDefinition Swagger API definition String
 * @param messageContext CXF message context
 * @return API Definition validation response
 */
@Override
public Response validateOpenAPIDefinition(Boolean returnContent, String url, InputStream fileInputStream, Attachment fileDetail, String inlineApiDefinition, MessageContext messageContext) {
    // Validate and retrieve the OpenAPI definition
    Map validationResponseMap = null;
    try {
        validationResponseMap = validateOpenAPIDefinition(url, fileInputStream, fileDetail, inlineApiDefinition, returnContent, false);
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error occurred while validating API Definition", e, log);
    }
    OpenAPIDefinitionValidationResponseDTO validationResponseDTO = (OpenAPIDefinitionValidationResponseDTO) validationResponseMap.get(RestApiConstants.RETURN_DTO);
    return Response.ok().entity(validationResponseDTO).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) OpenAPIDefinitionValidationResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OpenAPIDefinitionValidationResponseDTO) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Example 2 with OpenAPIDefinitionValidationResponseDTO

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

the class APIMappingUtil method getOpenAPIDefinitionValidationResponseFromModel.

public static OpenAPIDefinitionValidationResponseDTO getOpenAPIDefinitionValidationResponseFromModel(APIDefinitionValidationResponse model, boolean returnContent) {
    OpenAPIDefinitionValidationResponseDTO responseDTO = new OpenAPIDefinitionValidationResponseDTO();
    responseDTO.setIsValid(model.isValid());
    if (model.isValid()) {
        APIDefinitionValidationResponse.Info modelInfo = model.getInfo();
        if (modelInfo != null) {
            OpenAPIDefinitionValidationResponseInfoDTO infoDTO = new OpenAPIDefinitionValidationResponseInfoDTO();
            infoDTO.setOpenAPIVersion(modelInfo.getOpenAPIVersion());
            infoDTO.setName(modelInfo.getName());
            infoDTO.setVersion(modelInfo.getVersion());
            infoDTO.setContext(modelInfo.getContext());
            infoDTO.setDescription(modelInfo.getDescription());
            infoDTO.setEndpoints(modelInfo.getEndpoints());
            responseDTO.setInfo(infoDTO);
        }
        if (returnContent) {
            responseDTO.setContent(model.getContent());
        }
    } else {
        responseDTO.setErrors(getErrorListItemsDTOsFromErrorHandlers(model.getErrorItems()));
    }
    return responseDTO;
}
Also used : OpenAPIDefinitionValidationResponseInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OpenAPIDefinitionValidationResponseInfoDTO) OpenAPIDefinitionValidationResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OpenAPIDefinitionValidationResponseDTO) APIDefinitionValidationResponse(org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse)

Example 3 with OpenAPIDefinitionValidationResponseDTO

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

the class ApisApiServiceImpl method importOpenAPIDefinition.

private APIDTO importOpenAPIDefinition(InputStream definition, String definitionUrl, String inlineDefinition, APIDTO apiDTOFromProperties, Attachment fileDetail, ServiceEntry service, String organization) throws APIManagementException {
    // Validate and retrieve the OpenAPI definition
    Map validationResponseMap = null;
    boolean isServiceAPI = false;
    try {
        if (service != null) {
            isServiceAPI = true;
        }
        validationResponseMap = validateOpenAPIDefinition(definitionUrl, definition, fileDetail, inlineDefinition, true, isServiceAPI);
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error occurred while validating API Definition", e, log);
    }
    OpenAPIDefinitionValidationResponseDTO validationResponseDTO = (OpenAPIDefinitionValidationResponseDTO) validationResponseMap.get(RestApiConstants.RETURN_DTO);
    APIDefinitionValidationResponse validationResponse = (APIDefinitionValidationResponse) validationResponseMap.get(RestApiConstants.RETURN_MODEL);
    if (!validationResponseDTO.isIsValid()) {
        ErrorDTO errorDTO = APIMappingUtil.getErrorDTOFromErrorListItems(validationResponseDTO.getErrors());
        throw RestApiUtil.buildBadRequestException(errorDTO);
    }
    // Only HTTP or WEBHOOK type APIs should be allowed
    if (!(APIDTO.TypeEnum.HTTP.equals(apiDTOFromProperties.getType()) || APIDTO.TypeEnum.WEBHOOK.equals(apiDTOFromProperties.getType()))) {
        throw RestApiUtil.buildBadRequestException("The API's type is not supported when importing an OpenAPI definition");
    }
    // Import the API and Definition
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    if (isServiceAPI) {
        apiDTOFromProperties.setType(PublisherCommonUtils.getAPIType(service.getDefinitionType(), null));
    }
    API apiToAdd = PublisherCommonUtils.prepareToCreateAPIByDTO(apiDTOFromProperties, apiProvider, RestApiCommonUtil.getLoggedInUsername(), organization);
    if (isServiceAPI) {
        apiToAdd.setServiceInfo("key", service.getKey());
        apiToAdd.setServiceInfo("md5", service.getMd5());
        apiToAdd.setEndpointConfig(PublisherCommonUtils.constructEndpointConfigForService(service.getServiceUrl(), null));
    }
    boolean syncOperations = apiDTOFromProperties.getOperations().size() > 0;
    // Rearrange paths according to the API payload and save the OpenAPI definition
    APIDefinition apiDefinition = validationResponse.getParser();
    SwaggerData swaggerData;
    String definitionToAdd = validationResponse.getJsonContent();
    if (syncOperations) {
        PublisherCommonUtils.validateScopes(apiToAdd);
        swaggerData = new SwaggerData(apiToAdd);
        definitionToAdd = apiDefinition.populateCustomManagementInfo(definitionToAdd, swaggerData);
    }
    definitionToAdd = OASParserUtil.preProcess(definitionToAdd);
    Set<URITemplate> uriTemplates = apiDefinition.getURITemplates(definitionToAdd);
    Set<Scope> scopes = apiDefinition.getScopes(definitionToAdd);
    apiToAdd.setUriTemplates(uriTemplates);
    apiToAdd.setScopes(scopes);
    // Set extensions from API definition to API object
    apiToAdd = OASParserUtil.setExtensionsToAPI(definitionToAdd, apiToAdd);
    if (!syncOperations) {
        PublisherCommonUtils.validateScopes(apiToAdd);
        swaggerData = new SwaggerData(apiToAdd);
        definitionToAdd = apiDefinition.populateCustomManagementInfo(validationResponse.getJsonContent(), swaggerData);
    }
    // adding the API and definition
    apiToAdd.setSwaggerDefinition(definitionToAdd);
    API addedAPI = apiProvider.addAPI(apiToAdd);
    // apiProvider.saveSwaggerDefinition(apiToAdd, definitionToAdd);
    // retrieving the added API for returning as the response
    // this would provide the updated templates
    addedAPI = apiProvider.getAPIbyUUID(addedAPI.getUuid(), organization);
    return APIMappingUtil.fromAPItoDTO(addedAPI);
}
Also used : SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) APIDefinitionValidationResponse(org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Scope(org.wso2.carbon.apimgt.api.model.Scope) OpenAPIDefinitionValidationResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OpenAPIDefinitionValidationResponseDTO) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Example 4 with OpenAPIDefinitionValidationResponseDTO

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

the class ApisApiServiceImpl method validateOpenAPIDefinition.

/**
 * Validate the provided OpenAPI definition (via file or url) and return a Map with the validation response
 * information.
 *
 * @param url OpenAPI definition url
 * @param fileInputStream file as input stream
 * @param apiDefinition Swagger API definition String
 * @param returnContent whether to return the content of the definition in the response DTO
 * @return Map with the validation response information. A value with key 'dto' will have the response DTO
 *  of type OpenAPIDefinitionValidationResponseDTO for the REST API. A value with key 'model' will have the
 *  validation response of type APIDefinitionValidationResponse coming from the impl level.
 */
private Map validateOpenAPIDefinition(String url, InputStream fileInputStream, Attachment fileDetail, String apiDefinition, Boolean returnContent, Boolean isServiceAPI) throws APIManagementException {
    // validate inputs
    handleInvalidParams(fileInputStream, fileDetail, url, apiDefinition, isServiceAPI);
    OpenAPIDefinitionValidationResponseDTO responseDTO;
    APIDefinitionValidationResponse validationResponse = new APIDefinitionValidationResponse();
    if (url != null) {
        validationResponse = OASParserUtil.validateAPIDefinitionByURL(url, returnContent);
    } else if (fileInputStream != null) {
        try {
            if (fileDetail != null) {
                String filename = fileDetail.getContentDisposition().getFilename();
                if (filename.endsWith(".zip")) {
                    validationResponse = OASParserUtil.extractAndValidateOpenAPIArchive(fileInputStream, returnContent);
                } else {
                    String openAPIContent = IOUtils.toString(fileInputStream, RestApiConstants.CHARSET);
                    validationResponse = OASParserUtil.validateAPIDefinition(openAPIContent, returnContent);
                }
            } else {
                String openAPIContent = IOUtils.toString(fileInputStream, RestApiConstants.CHARSET);
                validationResponse = OASParserUtil.validateAPIDefinition(openAPIContent, returnContent);
            }
        } catch (IOException e) {
            RestApiUtil.handleInternalServerError("Error while reading file content", e, log);
        }
    } else if (apiDefinition != null) {
        validationResponse = OASParserUtil.validateAPIDefinition(apiDefinition, returnContent);
    }
    responseDTO = APIMappingUtil.getOpenAPIDefinitionValidationResponseFromModel(validationResponse, returnContent);
    Map response = new HashMap();
    response.put(RestApiConstants.RETURN_MODEL, validationResponse);
    response.put(RestApiConstants.RETURN_DTO, responseDTO);
    return response;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) OpenAPIDefinitionValidationResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OpenAPIDefinitionValidationResponseDTO) IOException(java.io.IOException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) APIDefinitionValidationResponse(org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse)

Aggregations

OpenAPIDefinitionValidationResponseDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OpenAPIDefinitionValidationResponseDTO)4 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 APIDefinitionValidationResponse (org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse)3 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 IOException (java.io.IOException)1 APIDefinition (org.wso2.carbon.apimgt.api.APIDefinition)1 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)1 API (org.wso2.carbon.apimgt.api.model.API)1 Scope (org.wso2.carbon.apimgt.api.model.Scope)1 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)1 SwaggerData (org.wso2.carbon.apimgt.api.model.SwaggerData)1 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)1 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)1 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)1 OpenAPIDefinitionValidationResponseInfoDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OpenAPIDefinitionValidationResponseInfoDTO)1