Search in sources :

Example 6 with WSDLValidationResponse

use of org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method importWSDLDefinition.

/**
 * Import a WSDL file/url or an archive and create an API. The API can be a SOAP or REST depending on the
 * provided implementationType.
 *
 * @param fileInputStream file input stream
 * @param fileDetail file details
 * @param url WSDL url
 * @param additionalProperties API object (json) including additional properties like name, version, context
 * @param implementationType SOAP or SOAPTOREST
 * @return Created API's payload
 * @throws APIManagementException when error occurred during the operation
 */
@Override
public Response importWSDLDefinition(InputStream fileInputStream, Attachment fileDetail, String url, String additionalProperties, String implementationType, MessageContext messageContext) throws APIManagementException {
    try {
        WSDLValidationResponse validationResponse = validateWSDLAndReset(fileInputStream, fileDetail, url);
        if (StringUtils.isEmpty(implementationType)) {
            implementationType = APIDTO.TypeEnum.SOAP.toString();
        }
        boolean isSoapToRestConvertedAPI = APIDTO.TypeEnum.SOAPTOREST.toString().equals(implementationType);
        boolean isSoapAPI = APIDTO.TypeEnum.SOAP.toString().equals(implementationType);
        APIDTO additionalPropertiesAPI = null;
        APIDTO createdApiDTO;
        URI createdApiUri;
        // Minimum requirement name, version, context and endpointConfig.
        additionalPropertiesAPI = new ObjectMapper().readValue(additionalProperties, APIDTO.class);
        String username = RestApiCommonUtil.getLoggedInUsername();
        additionalPropertiesAPI.setProvider(username);
        additionalPropertiesAPI.setType(APIDTO.TypeEnum.fromValue(implementationType));
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        API apiToAdd = PublisherCommonUtils.prepareToCreateAPIByDTO(additionalPropertiesAPI, RestApiCommonUtil.getLoggedInUserProvider(), username, organization);
        apiToAdd.setWsdlUrl(url);
        API createdApi = null;
        if (isSoapAPI) {
            createdApi = importSOAPAPI(validationResponse.getWsdlProcessor().getWSDL(), fileDetail, url, apiToAdd, organization, null);
        } else if (isSoapToRestConvertedAPI) {
            String wsdlArchiveExtractedPath = null;
            if (validationResponse.getWsdlArchiveInfo() != null) {
                wsdlArchiveExtractedPath = validationResponse.getWsdlArchiveInfo().getLocation() + File.separator + APIConstants.API_WSDL_EXTRACTED_DIRECTORY;
            }
            createdApi = importSOAPToRESTAPI(validationResponse.getWsdlProcessor().getWSDL(), fileDetail, url, wsdlArchiveExtractedPath, apiToAdd, organization);
        } else {
            RestApiUtil.handleBadRequest("Invalid implementationType parameter", log);
        }
        createdApiDTO = APIMappingUtil.fromAPItoDTO(createdApi);
        // This URI used to set the location header of the POST response
        createdApiUri = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + createdApiDTO.getId());
        return Response.created(createdApiUri).entity(createdApiDTO).build();
    } catch (IOException | URISyntaxException e) {
        RestApiUtil.handleInternalServerError("Error occurred while importing WSDL", e, log);
    }
    return null;
}
Also used : APIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO) WSDLValidationResponse(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 7 with WSDLValidationResponse

use of org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method updateWSDLOfAPI.

/**
 * Update the WSDL of an API
 *
 * @param apiId UUID of the API
 * @param fileInputStream file data as input stream
 * @param fileDetail file details
 * @param url URL of the WSDL
 * @return 200 OK response if the operation is successful. 400 if the provided inputs are invalid. 500 if a server
 *  error occurred.
 * @throws APIManagementException when error occurred while trying to retrieve the WSDL
 */
@Override
public Response updateWSDLOfAPI(String apiId, String ifMatch, InputStream fileInputStream, Attachment fileDetail, String url, MessageContext messageContext) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    String organization = RestApiUtil.getValidatedOrganization(messageContext);
    // validate if api exists
    APIInfo apiInfo = validateAPIExistence(apiId);
    // validate API update operation permitted based on the LC state
    validateAPIOperationsPerLC(apiInfo.getStatus().toString());
    WSDLValidationResponse validationResponse = validateWSDLAndReset(fileInputStream, fileDetail, url);
    if (StringUtils.isNotBlank(url)) {
        apiProvider.addWSDLResource(apiId, null, url, organization);
    } else {
        ResourceFile wsdlResource;
        if (APIConstants.APPLICATION_ZIP.equals(fileDetail.getContentType().toString()) || APIConstants.APPLICATION_X_ZIP_COMPRESSED.equals(fileDetail.getContentType().toString())) {
            wsdlResource = new ResourceFile(validationResponse.getWsdlProcessor().getWSDL(), APIConstants.APPLICATION_ZIP);
        } else {
            wsdlResource = new ResourceFile(validationResponse.getWsdlProcessor().getWSDL(), fileDetail.getContentType().toString());
        }
        apiProvider.addWSDLResource(apiId, wsdlResource, null, organization);
    }
    return Response.ok().build();
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) WSDLValidationResponse(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse) APIInfo(org.wso2.carbon.apimgt.api.model.APIInfo) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 8 with WSDLValidationResponse

use of org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method validateWSDL.

/**
 * Validate the provided input parameters and returns the validation response DTO (for REST API)
 *  and the intermediate model as a Map
 *
 * @param url WSDL url
 * @param fileInputStream file data stream
 * @param fileDetail file details
 * @param isServiceAPI is service api condition
 * @return the validation response DTO (for REST API) and the intermediate model as a Map
 * @throws APIManagementException if error occurred during validation of the WSDL
 */
private Map validateWSDL(String url, InputStream fileInputStream, Attachment fileDetail, Boolean isServiceAPI) throws APIManagementException {
    handleInvalidParams(fileInputStream, fileDetail, url, null, isServiceAPI);
    WSDLValidationResponseDTO responseDTO;
    WSDLValidationResponse validationResponse = new WSDLValidationResponse();
    if (url != null) {
        try {
            URL wsdlUrl = new URL(url);
            validationResponse = APIMWSDLReader.validateWSDLUrl(wsdlUrl);
        } catch (MalformedURLException e) {
            RestApiUtil.handleBadRequest("Invalid/Malformed URL : " + url, log);
        }
    } else if (fileInputStream != null && !isServiceAPI) {
        String filename = fileDetail.getContentDisposition().getFilename();
        try {
            if (filename.endsWith(".zip")) {
                validationResponse = APIMWSDLReader.extractAndValidateWSDLArchive(fileInputStream);
            } else if (filename.endsWith(".wsdl")) {
                validationResponse = APIMWSDLReader.validateWSDLFile(fileInputStream);
            } else {
                RestApiUtil.handleBadRequest("Unsupported extension type of file: " + filename, log);
            }
        } catch (APIManagementException e) {
            String errorMessage = "Internal error while validating the WSDL from file:" + filename;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    } else if (fileInputStream != null) {
        try {
            validationResponse = APIMWSDLReader.validateWSDLFile(fileInputStream);
        } catch (APIManagementException e) {
            String errorMessage = "Internal error while validating the WSDL definition input stream";
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    responseDTO = APIMappingUtil.fromWSDLValidationResponseToDTO(validationResponse);
    Map response = new HashMap();
    response.put(RestApiConstants.RETURN_MODEL, validationResponse);
    response.put(RestApiConstants.RETURN_DTO, responseDTO);
    return response;
}
Also used : MalformedURLException(java.net.MalformedURLException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) WSDLValidationResponse(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse) WSDLValidationResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WSDLValidationResponseDTO) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) URL(java.net.URL)

Example 9 with WSDLValidationResponse

use of org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse in project carbon-apimgt by wso2.

the class ImportUtils method validateWSDLFromArchive.

/**
 * Validate WSDL definition from the archive directory and return it.
 *
 * @param pathToArchive Path to API archive
 * @throws APIImportExportException If an error due to an invalid WSDL definition
 */
private static void validateWSDLFromArchive(String pathToArchive, APIDTO apiDto) throws APIManagementException {
    try {
        byte[] wsdlDefinition = loadWsdlFile(pathToArchive, apiDto);
        WSDLValidationResponse wsdlValidationResponse = APIMWSDLReader.getWsdlValidationResponse(APIMWSDLReader.getWSDLProcessor(wsdlDefinition));
        if (!wsdlValidationResponse.isValid()) {
            throw new APIManagementException("Error occurred while importing the API. Invalid WSDL definition found. " + wsdlValidationResponse.getError());
        }
    } catch (IOException | APIManagementException e) {
        throw new APIManagementException("Error while reading API meta information from path: " + pathToArchive, e, ExceptionCodes.ERROR_READING_META_DATA);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) WSDLValidationResponse(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse) IOException(java.io.IOException)

Example 10 with WSDLValidationResponse

use of org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse in project carbon-apimgt by wso2.

the class APIMappingUtil method fromWSDLValidationResponseToDTO.

/**
 * Converts a WSDL validation response model to DTO.
 *
 * @param validationResponse validation response model
 * @return Converted WSDL validation response model to DTO
 */
public static WSDLValidationResponseDTO fromWSDLValidationResponseToDTO(WSDLValidationResponse validationResponse) {
    WSDLValidationResponseDTO wsdlValidationResponseDTO = new WSDLValidationResponseDTO();
    WSDLInfo wsdlInfo;
    if (validationResponse.isValid()) {
        wsdlValidationResponseDTO.setIsValid(true);
        wsdlInfo = validationResponse.getWsdlInfo();
        WSDLValidationResponseWsdlInfoDTO wsdlInfoDTO = new WSDLValidationResponseWsdlInfoDTO();
        wsdlInfoDTO.setVersion(wsdlInfo.getVersion());
        List<WSDLValidationResponseWsdlInfoEndpointsDTO> endpointsDTOList = fromEndpointsMapToWSDLValidationResponseEndpointsDTO(wsdlInfo.getEndpoints());
        wsdlInfoDTO.setEndpoints(endpointsDTOList);
        wsdlValidationResponseDTO.setWsdlInfo(wsdlInfoDTO);
    } else {
        wsdlValidationResponseDTO.setIsValid(false);
        wsdlValidationResponseDTO.setErrors(getErrorListItemsDTOsFromErrorHandler(validationResponse.getError()));
    }
    return wsdlValidationResponseDTO;
}
Also used : WSDLInfo(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLInfo) WSDLValidationResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WSDLValidationResponseDTO) WSDLValidationResponseWsdlInfoEndpointsDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WSDLValidationResponseWsdlInfoEndpointsDTO) WSDLValidationResponseWsdlInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WSDLValidationResponseWsdlInfoDTO)

Aggregations

WSDLValidationResponse (org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse)10 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)6 IOException (java.io.IOException)3 WSDLProcessor (org.wso2.carbon.apimgt.impl.wsdl.WSDLProcessor)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ResourceFile (org.wso2.carbon.apimgt.api.model.ResourceFile)2 WSDLValidationResponseDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WSDLValidationResponseDTO)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)1 API (org.wso2.carbon.apimgt.api.model.API)1 APIInfo (org.wso2.carbon.apimgt.api.model.APIInfo)1 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)1 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)1