Search in sources :

Example 76 with Wsdl

use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.

the class WSDLSOAPOperationExtractorImplTestCase method testGetWsdlDefinition.

@Test
public void testGetWsdlDefinition() throws Exception {
    APIMWSDLReader wsdlReader = new APIMWSDLReader(Thread.currentThread().getContextClassLoader().getResource("wsdls/phoneverify.wsdl").toExternalForm());
    byte[] wsdlContent = wsdlReader.getWSDL();
    WSDL11SOAPOperationExtractor processor = new WSDL11SOAPOperationExtractor(wsdlReader);
    Assert.assertTrue("WSDL definition parsing failed", processor.init(wsdlContent));
}
Also used : APIMWSDLReader(org.wso2.carbon.apimgt.impl.utils.APIMWSDLReader) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 77 with Wsdl

use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method validateWSDLAndReset.

/**
 * Validates the provided WSDL and reset the streams as required
 *
 * @param fileInputStream file input stream
 * @param fileDetail file details
 * @param url WSDL url
 * @throws APIManagementException when error occurred during the operation
 */
private WSDLValidationResponse validateWSDLAndReset(InputStream fileInputStream, Attachment fileDetail, String url) throws APIManagementException {
    Map validationResponseMap = validateWSDL(url, fileInputStream, fileDetail, false);
    WSDLValidationResponse validationResponse = (WSDLValidationResponse) validationResponseMap.get(RestApiConstants.RETURN_MODEL);
    if (validationResponse.getWsdlInfo() == null) {
        // Validation failure
        RestApiUtil.handleBadRequest(validationResponse.getError(), log);
    }
    if (fileInputStream != null) {
        if (fileInputStream.markSupported()) {
            // For uploading the WSDL below will require re-reading from the input stream hence resetting
            try {
                fileInputStream.reset();
            } catch (IOException e) {
                throw new APIManagementException("Error occurred while trying to reset the content stream of the " + "WSDL", e);
            }
        } else {
            log.warn("Marking is not supported in 'fileInputStream' InputStream type: " + fileInputStream.getClass() + ". Skipping validating WSDL to avoid re-reading from the " + "input stream.");
        }
    }
    return validationResponse;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) WSDLValidationResponse(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse) IOException(java.io.IOException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Example 78 with Wsdl

use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method validateWSDLDefinition.

/**
 * Validate a provided WSDL definition via a URL or a file/zip
 *
 * @param url WSDL URL
 * @param fileInputStream file/zip input stream
 * @param fileDetail file/zip details
 * @param messageContext messageContext object
 * @return WSDL validation response
 * @throws APIManagementException when error occurred during validation
 */
@Override
public Response validateWSDLDefinition(String url, InputStream fileInputStream, Attachment fileDetail, MessageContext messageContext) throws APIManagementException {
    Map validationResponseMap = validateWSDL(url, fileInputStream, fileDetail, false);
    WSDLValidationResponseDTO validationResponseDTO = (WSDLValidationResponseDTO) validationResponseMap.get(RestApiConstants.RETURN_DTO);
    return Response.ok().entity(validationResponseDTO).build();
}
Also used : WSDLValidationResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WSDLValidationResponseDTO) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Example 79 with Wsdl

use of org.wso2.carbon.apimgt.api.model.Wsdl 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 80 with Wsdl

use of org.wso2.carbon.apimgt.api.model.Wsdl in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method getWSDLOfAPI.

/**
 * Retrieve the WSDL of an API
 *
 * @param apiId UUID of the API
 * @param ifNoneMatch If-None-Match header value
 * @return the WSDL of the API (can be a file or zip archive)
 * @throws APIManagementException when error occurred while trying to retrieve the WSDL
 */
@Override
public Response getWSDLOfAPI(String apiId, String ifNoneMatch, MessageContext messageContext) throws APIManagementException {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        // this will fail if user does not have access to the API or the API does not exist
        // APIIdentifier apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(apiId, organization);
        ResourceFile resource = apiProvider.getWSDL(apiId, organization);
        return RestApiUtil.getResponseFromResourceFile(resource.getName(), resource);
    } catch (APIManagementException e) {
        // to expose the existence of the resource
        if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else if (isAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure("Authorization failure while retrieving wsdl of API: " + apiId, e, log);
        } else {
            throw e;
        }
    }
    return null;
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)64 IOException (java.io.IOException)42 APIMgtWSDLException (org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException)33 Test (org.junit.Test)30 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 API (org.wso2.carbon.apimgt.core.models.API)28 File (java.io.File)27 APIMgtWSDLException (org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException)24 ByteArrayInputStream (java.io.ByteArrayInputStream)23 FileInputStream (java.io.FileInputStream)23 InputStream (java.io.InputStream)22 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)22 WSDLException (javax.wsdl.WSDLException)21 HashMap (java.util.HashMap)20 Resource (org.wso2.carbon.registry.core.Resource)20 MalformedURLException (java.net.MalformedURLException)18 Map (java.util.Map)16 Response (javax.ws.rs.core.Response)16 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)16 SAXException (org.xml.sax.SAXException)16