Search in sources :

Example 21 with Wsdl

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

the class APIPublisherImpl method extractAndValidateWSDLArchive.

@Override
public WSDLArchiveInfo extractAndValidateWSDLArchive(InputStream inputStream) throws APIMgtDAOException, APIMgtWSDLException {
    String path = System.getProperty(APIMgtConstants.JAVA_IO_TMPDIR) + File.separator + APIMgtConstants.WSDLConstants.WSDL_ARCHIVES_FOLDERNAME + File.separator + UUID.randomUUID().toString();
    String archivePath = path + File.separator + APIMgtConstants.WSDLConstants.WSDL_ARCHIVE_FILENAME;
    String extractedLocation = APIFileUtils.extractUploadedArchive(inputStream, APIMgtConstants.WSDLConstants.EXTRACTED_WSDL_ARCHIVE_FOLDERNAME, archivePath, path);
    if (log.isDebugEnabled()) {
        log.debug("Successfully extracted WSDL archive. Location: " + extractedLocation);
    }
    WSDLProcessor processor = WSDLProcessFactory.getInstance().getWSDLProcessorForPath(extractedLocation);
    if (!processor.canProcess()) {
        throw new APIMgtWSDLException("Unable to process WSDL by the processor " + processor.getClass().getName(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT);
    }
    WSDLArchiveInfo archiveInfo = new WSDLArchiveInfo(path, APIMgtConstants.WSDLConstants.WSDL_ARCHIVE_FILENAME);
    archiveInfo.setWsdlInfo(processor.getWsdlInfo());
    return archiveInfo;
}
Also used : WSDLProcessor(org.wso2.carbon.apimgt.core.api.WSDLProcessor) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) WSDLArchiveInfo(org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo)

Example 22 with Wsdl

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

the class APIPublisherImpl method addAPIFromWSDLArchive.

@Override
public String addAPIFromWSDLArchive(API.APIBuilder apiBuilder, InputStream inputStream, boolean isHttpBinding) throws APIManagementException {
    WSDLArchiveInfo archiveInfo = extractAndValidateWSDLArchive(inputStream);
    if (log.isDebugEnabled()) {
        log.debug("Successfully extracted and validated WSDL file. Location: " + archiveInfo.getAbsoluteFilePath());
    }
    apiBuilder.uriTemplates(APIMWSDLUtils.getUriTemplatesForWSDLOperations(archiveInfo.getWsdlInfo().getHttpBindingOperations(), isHttpBinding));
    String uuid = addAPI(apiBuilder);
    if (log.isDebugEnabled()) {
        log.debug("Successfully added the API. uuid: " + uuid);
    }
    try (InputStream fileInputStream = new FileInputStream(archiveInfo.getAbsoluteFilePath())) {
        getApiDAO().addOrUpdateWSDLArchive(uuid, fileInputStream, getUsername());
        if (log.isDebugEnabled()) {
            log.debug("Successfully added/updated the WSDL archive. uuid: " + uuid);
        }
        if (APIMgtConstants.WSDLConstants.WSDL_VERSION_20.equals(archiveInfo.getWsdlInfo().getVersion())) {
            log.info("Extraction of HTTP Binding operations is not supported for WSDL 2.0.");
        }
        return uuid;
    } catch (IOException e) {
        throw new APIMgtWSDLException("Unable to process WSDL archive at " + archiveInfo.getAbsoluteFilePath(), e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
    } finally {
        try {
            APIFileUtils.deleteDirectory(archiveInfo.getLocation());
        } catch (APIMgtDAOException e) {
            // This is not a blocker. Give a warning and continue
            log.warn("Error occured while deleting processed WSDL artifacts folder : " + archiveInfo.getLocation());
        }
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) WSDLArchiveInfo(org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 23 with Wsdl

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

the class APIPublisherImpl method addAPIFromWSDLFile.

@Override
public String addAPIFromWSDLFile(API.APIBuilder apiBuilder, InputStream inputStream, boolean isHttpBinding) throws APIManagementException {
    byte[] wsdlContent;
    try {
        wsdlContent = IOUtils.toByteArray(inputStream);
    } catch (IOException e) {
        throw new APIMgtWSDLException("Error while converting input stream to byte array", e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
    }
    WSDLProcessor processor = WSDLProcessFactory.getInstance().getWSDLProcessor(wsdlContent);
    apiBuilder.uriTemplates(APIMWSDLUtils.getUriTemplatesForWSDLOperations(processor.getWsdlInfo().getHttpBindingOperations(), isHttpBinding));
    if (!processor.canProcess()) {
        throw new APIMgtWSDLException("Unable to process WSDL by the processor " + processor.getClass().getName(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT);
    }
    String uuid = addAPI(apiBuilder);
    if (log.isDebugEnabled()) {
        log.debug("Successfully added the API. uuid: " + uuid);
    }
    getApiDAO().addOrUpdateWSDL(uuid, wsdlContent, getUsername());
    if (log.isDebugEnabled()) {
        log.debug("Successfully added the WSDL file to database. API uuid: " + uuid);
    }
    if (APIMgtConstants.WSDLConstants.WSDL_VERSION_20.equals(processor.getWsdlInfo().getVersion())) {
        log.info("Extraction of HTTP Binding operations is not supported for WSDL 2.0.");
    }
    return uuid;
}
Also used : WSDLProcessor(org.wso2.carbon.apimgt.core.api.WSDLProcessor) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) IOException(java.io.IOException)

Example 24 with Wsdl

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

the class APIStoreImplTestCase method testGetAPIWSDLArchive.

@Test(description = "Retrieve a WSDL archive of an API")
public void testGetAPIWSDLArchive() throws APIManagementException, IOException {
    final String labelName = "SampleLabel";
    Label label = SampleTestObjectCreator.createLabel(labelName, SampleTestObjectCreator.LABEL_TYPE_STORE).build();
    List<String> labels = new ArrayList<>();
    labels.add(label.getName());
    API api = SampleTestObjectCreator.createDefaultAPI().labels(labels).build();
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    LabelDAO labelDAO = Mockito.mock(LabelDAO.class);
    APIStore apiStore = getApiStoreImpl(apiDAO, labelDAO);
    Mockito.when(apiDAO.getAPI(api.getId())).thenReturn(api);
    Mockito.when(labelDAO.getLabelByName(labelName)).thenReturn(label);
    Mockito.when(apiDAO.getWSDLArchive(api.getId())).thenReturn(SampleTestObjectCreator.createDefaultWSDL11ArchiveInputStream());
    String expectedEndpoint = SampleTestObjectCreator.ACCESS_URL + labelName + (api.getContext().startsWith("/") ? api.getContext() : "/" + api.getContext());
    WSDLArchiveInfo archiveInfo = apiStore.getAPIWSDLArchive(api.getId(), label.getName());
    Assert.assertNotNull(archiveInfo);
    Assert.assertNotNull(archiveInfo.getWsdlInfo());
    Assert.assertNotNull(archiveInfo.getWsdlInfo().getEndpoints());
    Map<String, String> endpoints = archiveInfo.getWsdlInfo().getEndpoints();
    Assert.assertTrue(endpoints.containsValue(expectedEndpoint));
    Assert.assertFalse(endpoints.containsValue(SampleTestObjectCreator.ORIGINAL_ENDPOINT_STOCK_QUOTE));
    Assert.assertFalse(endpoints.containsValue(SampleTestObjectCreator.ORIGINAL_ENDPOINT_WEATHER));
}
Also used : Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) WSDLArchiveInfo(org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 25 with Wsdl

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

the class APIStoreImplTestCase method testGetAPIWSDL.

@Test(description = "Retrieve a WSDL of an API")
public void testGetAPIWSDL() throws APIManagementException, IOException {
    final String labelName = "SampleLabel";
    Label label = SampleTestObjectCreator.createLabel(labelName, SampleTestObjectCreator.LABEL_TYPE_STORE).build();
    List<String> labels = new ArrayList<>();
    labels.add(label.getName());
    API api = SampleTestObjectCreator.createDefaultAPI().labels(labels).build();
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    LabelDAO labelDAO = Mockito.mock(LabelDAO.class);
    APIStore apiStore = getApiStoreImpl(apiDAO, labelDAO);
    Mockito.when(apiDAO.getAPI(api.getId())).thenReturn(api);
    Mockito.when(labelDAO.getLabelByName(labelName)).thenReturn(label);
    Mockito.when(apiDAO.getWSDL(api.getId())).thenReturn(new String(SampleTestObjectCreator.createDefaultWSDL11Content()));
    String expectedEndpoint = SampleTestObjectCreator.ACCESS_URL + labelName + (api.getContext().startsWith("/") ? api.getContext() : "/" + api.getContext());
    String updatedWSDL = apiStore.getAPIWSDL(api.getId(), label.getName());
    Assert.assertTrue(updatedWSDL.contains(expectedEndpoint));
}
Also used : Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

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