Search in sources :

Example 96 with InputStream

use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream in project carbon-apimgt by wso2.

the class APIStoreImpl method getAPIWSDLArchive.

@Override
public WSDLArchiveInfo getAPIWSDLArchive(String apiId, String labelName) throws APIMgtDAOException, APIMgtWSDLException, APINotFoundException, LabelException {
    API api = getApiDAO().getAPI(apiId);
    if (api == null) {
        throw new APINotFoundException("API with id " + apiId + " not found.", ExceptionCodes.API_NOT_FOUND);
    }
    // api.getLabels() should not be null and the labels should contain labelName
    if ((api.getLabels() == null || !api.getLabels().contains(labelName))) {
        throw new LabelException("API with id " + apiId + " does not contain label " + labelName, ExceptionCodes.LABEL_NOT_FOUND_IN_API);
    }
    try (InputStream wsdlZipInputStream = getApiDAO().getWSDLArchive(apiId)) {
        String rootPath = System.getProperty(APIMgtConstants.JAVA_IO_TMPDIR) + File.separator + APIMgtConstants.WSDLConstants.WSDL_ARCHIVES_FOLDERNAME + File.separator + UUID.randomUUID().toString();
        String archivePath = rootPath + File.separator + APIMgtConstants.WSDLConstants.WSDL_ARCHIVE_FILENAME;
        String extractedLocation = APIFileUtils.extractUploadedArchive(wsdlZipInputStream, APIMgtConstants.WSDLConstants.EXTRACTED_WSDL_ARCHIVE_FOLDERNAME, archivePath, rootPath);
        if (log.isDebugEnabled()) {
            log.debug("Successfully extracted WSDL archive in path: " + extractedLocation);
        }
        Label label = getLabelDAO().getLabelByName(labelName);
        WSDLProcessor processor = WSDLProcessFactory.getInstance().getWSDLProcessorForPath(extractedLocation);
        String wsdlPath = processor.getUpdatedWSDLPath(api, label);
        if (log.isDebugEnabled()) {
            log.debug("Successfully updated WSDLs in path [" + extractedLocation + "] with endpoints of label: " + labelName + " and context of API " + api.getContext());
        }
        String wsdlArchiveProcessedFileName = api.getProvider() + "-" + api.getName() + "-" + api.getVersion() + "-" + labelName + "-wsdl";
        APIFileUtils.archiveDirectory(wsdlPath, rootPath, wsdlArchiveProcessedFileName);
        if (log.isDebugEnabled()) {
            log.debug("Successfully archived WSDL files: " + wsdlPath);
        }
        WSDLArchiveInfo archiveInfo = new WSDLArchiveInfo(rootPath, wsdlArchiveProcessedFileName + ".zip");
        archiveInfo.setWsdlInfo(processor.getWsdlInfo());
        return archiveInfo;
    } catch (IOException e) {
        throw new APIMgtWSDLException(e);
    }
}
Also used : WSDLProcessor(org.wso2.carbon.apimgt.core.api.WSDLProcessor) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Label(org.wso2.carbon.apimgt.core.models.Label) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) WSDLArchiveInfo(org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo) IOException(java.io.IOException) LabelException(org.wso2.carbon.apimgt.core.exception.LabelException) APINotFoundException(org.wso2.carbon.apimgt.core.exception.APINotFoundException)

Example 97 with InputStream

use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream in project carbon-apimgt by wso2.

the class APIPublisherImpl method updateAPIWSDL.

@Override
public String updateAPIWSDL(String apiId, InputStream inputStream) throws APIMgtDAOException, APIMgtWSDLException {
    byte[] wsdlContent;
    try {
        wsdlContent = IOUtils.toByteArray(inputStream);
        WSDLProcessor processor = WSDLProcessFactory.getInstance().getWSDLProcessor(wsdlContent);
        if (!processor.canProcess()) {
            throw new APIMgtWSDLException("Unable to process WSDL by the processor " + processor.getClass().getName(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT);
        }
        if (log.isDebugEnabled()) {
            log.debug("Successfully validated the content of WSDL. API uuid: " + apiId);
        }
        getApiDAO().addOrUpdateWSDL(apiId, wsdlContent, getUsername());
        if (log.isDebugEnabled()) {
            log.debug("Successfully added WSDL to the DB. API uuid: " + apiId);
        }
        return new String(wsdlContent, APIMgtConstants.ENCODING_UTF_8);
    } catch (IOException e) {
        throw new APIMgtWSDLException("Error while updating WSDL of API " + apiId, e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
    }
}
Also used : WSDLProcessor(org.wso2.carbon.apimgt.core.api.WSDLProcessor) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) IOException(java.io.IOException)

Example 98 with InputStream

use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream in project carbon-apimgt by wso2.

the class APIStoreImpl method addCompositeApiFromDefinition.

/**
 * {@inheritDoc}
 */
@Override
public String addCompositeApiFromDefinition(InputStream apiDefinition) throws APIManagementException {
    try {
        String apiDefinitionString = IOUtils.toString(apiDefinition);
        CompositeAPI.Builder apiBuilder = apiDefinitionFromSwagger20.generateCompositeApiFromSwaggerResource(getUsername(), apiDefinitionString);
        apiBuilder.apiDefinition(apiDefinitionString);
        addCompositeApi(apiBuilder);
        return apiBuilder.getId();
    } catch (IOException e) {
        throw new APIManagementException("Couldn't Generate ApiDefinition from file", ExceptionCodes.API_DEFINITION_MALFORMED);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) IOException(java.io.IOException)

Example 99 with InputStream

use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testSingleWSDLForAPI.

@Test
public void testSingleWSDLForAPI() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI().apiDefinition(SampleTestObjectCreator.apiDefinition);
    API api = builder.build();
    testAddGetEndpoint();
    apiDAO.addAPI(api);
    // there can't be any WSDLs added at first
    boolean isWSDLExists = apiDAO.isWSDLExists(api.getId());
    Assert.assertFalse(isWSDLExists);
    boolean isWSDLArchiveExists = apiDAO.isWSDLArchiveExists(api.getId());
    Assert.assertFalse(isWSDLArchiveExists);
    // add a WSDL
    byte[] wsdlContentBytes = SampleTestObjectCreator.createDefaultWSDL11Content();
    apiDAO.addOrUpdateWSDL(api.getId(), wsdlContentBytes, ADMIN);
    // retrieves and check whether they are same
    String receivedFromDB = apiDAO.getWSDL(api.getId());
    Assert.assertEquals(new String(wsdlContentBytes), receivedFromDB);
    // now there should be a single WSDL for API exists but no WSDL archives
    isWSDLExists = apiDAO.isWSDLExists(api.getId());
    Assert.assertTrue(isWSDLExists);
    isWSDLArchiveExists = apiDAO.isWSDLArchiveExists(api.getId());
    Assert.assertFalse(isWSDLArchiveExists);
    // update the WSDL file
    wsdlContentBytes = SampleTestObjectCreator.createAlternativeWSDL11Content();
    apiDAO.addOrUpdateWSDL(api.getId(), wsdlContentBytes, ADMIN);
    // retrieves and check whether updated successfully
    receivedFromDB = apiDAO.getWSDL(api.getId());
    Assert.assertEquals(new String(wsdlContentBytes), receivedFromDB);
    // update with a WSDL archive
    InputStream wsdl11ArchiveInputStream = SampleTestObjectCreator.createDefaultWSDL11ArchiveInputStream();
    byte[] wsdlArchiveBytesDefault = IOUtils.toByteArray(SampleTestObjectCreator.createDefaultWSDL11ArchiveInputStream());
    apiDAO.addOrUpdateWSDLArchive(api.getId(), wsdl11ArchiveInputStream, ADMIN);
    // retrieves and check whether successfully updated
    InputStream wsdlArchiveInputStreamFromDB = apiDAO.getWSDLArchive(api.getId());
    byte[] streamFromDBBytes = IOUtils.toByteArray(wsdlArchiveInputStreamFromDB);
    Assert.assertEquals(wsdlArchiveBytesDefault.length, streamFromDBBytes.length);
    // removes and validate
    apiDAO.removeWSDLArchiveOfAPI(api.getId());
    isWSDLExists = apiDAO.isWSDLExists(api.getId());
    Assert.assertFalse(isWSDLExists);
    isWSDLArchiveExists = apiDAO.isWSDLArchiveExists(api.getId());
    Assert.assertFalse(isWSDLArchiveExists);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Example 100 with InputStream

use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream in project carbon-apimgt by wso2.

the class ApiDAOImplIT method testGetImage.

@Test(description = "Get image from API")
public void testGetImage() throws Exception {
    ApiDAO apiDAO = DAOFactory.getApiDAO();
    testAddGetEndpoint();
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    apiDAO.addAPI(api);
    apiDAO.updateImage(api.getId(), SampleTestObjectCreator.createDefaultThumbnailImage(), "image/jpg", ADMIN);
    InputStream image = apiDAO.getImage(api.getId());
    Assert.assertNotNull(image);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)80 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)67 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)58 InputStream (java.io.InputStream)54 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)54 Event (org.wso2.siddhi.core.event.Event)48 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)47 IOException (java.io.IOException)32 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)24 ByteArrayInputStream (java.io.ByteArrayInputStream)20 API (org.wso2.carbon.apimgt.core.models.API)18 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)17 FileInputStream (java.io.FileInputStream)15 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)13 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)12 Response (javax.ws.rs.core.Response)11 HashMap (java.util.HashMap)9 APIMgtWSDLException (org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException)8 File (java.io.File)7 Connection (java.sql.Connection)7