Search in sources :

Example 91 with InputStream

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

the class CompositeApisApiServiceImplTestCase method testCompositeApisApiIdImplementationPut.

@Test
public void testCompositeApisApiIdImplementationPut() throws APIManagementException, NotFoundException {
    TestUtil.printTestMethodName();
    String apiID = UUID.randomUUID().toString();
    CompositeApisApiServiceImpl compositeApisApiService = new CompositeApisApiServiceImpl();
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
    Request request = TestUtil.getRequest();
    PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
    InputStream implmentation = null;
    FileInfo fileInfo = new FileInfo();
    fileInfo.setFileName("sample.txt");
    fileInfo.setContentType(contentType);
    Mockito.doNothing().doThrow(new IllegalArgumentException()).when(apiStore).updateCompositeApiImplementation(apiID, implmentation);
    Response response = compositeApisApiService.compositeApisApiIdImplementationPut(apiID, implmentation, fileInfo, null, null, request);
    Assert.assertEquals(200, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) FileInfo(org.wso2.msf4j.formparam.FileInfo) InputStream(java.io.InputStream) Request(org.wso2.msf4j.Request) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 92 with InputStream

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

the class ApiDAOImpl method updateImage.

/**
 * @see ApiDAO#updateImage(String, InputStream, String, String)
 */
@Override
public void updateImage(String apiID, InputStream image, String dataType, String updatedBy) throws APIMgtDAOException {
    if (image != null) {
        try (Connection connection = DAOUtil.getConnection()) {
            try {
                connection.setAutoCommit(false);
                if (!ApiResourceDAO.isResourceExistsForCategory(connection, apiID, ResourceCategory.IMAGE)) {
                    ApiResourceDAO.addBinaryResource(connection, apiID, UUID.randomUUID().toString(), ResourceCategory.IMAGE, dataType, image, updatedBy);
                } else {
                    ApiResourceDAO.updateBinaryResourceForCategory(connection, apiID, ResourceCategory.IMAGE, image, updatedBy);
                }
                connection.commit();
            } catch (SQLException e) {
                connection.rollback();
                String msg = "updating Image for API: " + apiID + ", Data Type: " + dataType + ", Updated By: " + updatedBy;
                throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + msg, e);
            } finally {
                connection.setAutoCommit(DAOUtil.isAutoCommit());
            }
        } catch (SQLException e) {
            String msg = "updating Image for API: " + apiID + ", Data Type: " + dataType + ", Updated By: " + updatedBy;
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + msg, e);
        }
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 93 with InputStream

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

the class ApiDAOImpl method addOrUpdateWSDLArchive.

@Override
public void addOrUpdateWSDLArchive(String apiID, InputStream inputStream, String updatedBy) throws APIMgtDAOException {
    try (Connection connection = DAOUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            if (!ApiResourceDAO.isResourceExistsForCategory(connection, apiID, ResourceCategory.WSDL_ZIP)) {
                if (ApiResourceDAO.isResourceExistsForCategory(connection, apiID, ResourceCategory.WSDL_TEXT)) {
                    removeWSDL(apiID);
                }
                ApiResourceDAO.addBinaryResource(connection, apiID, UUID.randomUUID().toString(), ResourceCategory.WSDL_ZIP, MediaType.APPLICATION_OCTET_STREAM, inputStream, updatedBy);
            } else {
                ApiResourceDAO.updateBinaryResourceForCategory(connection, apiID, ResourceCategory.WSDL_ZIP, inputStream, updatedBy);
            }
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding or updating WSDL archive for API(api: " + apiID + ")", e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding or updating WSDL archive for API(api: " + apiID + ")", e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 94 with InputStream

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

the class APIPublisherImpl method updateAPIWSDLArchive.

@Override
public void updateAPIWSDLArchive(String apiId, InputStream inputStream) throws APIMgtDAOException, APIMgtWSDLException {
    WSDLArchiveInfo archiveInfo = null;
    InputStream fileInputStream = null;
    try {
        archiveInfo = extractAndValidateWSDLArchive(inputStream);
        if (log.isDebugEnabled()) {
            log.debug("Successfully extracted and validated WSDL file. Location: " + archiveInfo.getAbsoluteFilePath());
        }
        fileInputStream = new FileInputStream(archiveInfo.getAbsoluteFilePath());
        getApiDAO().addOrUpdateWSDLArchive(apiId, fileInputStream, getUsername());
        if (log.isDebugEnabled()) {
            log.debug("Successfully updated the WSDL archive in DB. API uuid: " + apiId);
        }
    } catch (IOException e) {
        throw new APIMgtWSDLException("Unable to process WSDL archive at " + archiveInfo.getAbsoluteFilePath(), e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
    } finally {
        try {
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            if (archiveInfo != null) {
                APIFileUtils.deleteDirectory(archiveInfo.getLocation());
            }
        } catch (APIMgtDAOException | IOException 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 95 with InputStream

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

the class APIStoreImpl method updateCompositeApi.

/**
 * {@inheritDoc}
 */
@Override
public void updateCompositeApi(CompositeAPI.Builder apiBuilder) throws APIManagementException {
    apiBuilder.provider(getUsername());
    apiBuilder.updatedBy(getUsername());
    CompositeAPI originalAPI = getApiDAO().getCompositeAPI(apiBuilder.getId());
    apiBuilder.createdTime(originalAPI.getCreatedTime());
    // workflow status is an internal property and shouldn't be allowed to update externally
    apiBuilder.workflowStatus(originalAPI.getWorkflowStatus());
    APIUtils.verifyValidityOfApiUpdate(apiBuilder, originalAPI);
    try {
        String updatedSwagger = apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder);
        InputStream gatewayConfig = getApiDAO().getCompositeAPIGatewayConfig(apiBuilder.getId());
        GatewaySourceGenerator gatewaySourceGenerator = getGatewaySourceGenerator();
        APIConfigContext apiConfigContext = new APIConfigContext(apiBuilder.build(), config.getGatewayPackageName());
        gatewaySourceGenerator.setApiConfigContext(apiConfigContext);
        String updatedGatewayConfig = gatewaySourceGenerator.getGatewayConfigFromSwagger(IOUtils.toString(gatewayConfig, StandardCharsets.UTF_8), updatedSwagger);
        CompositeAPI api = apiBuilder.build();
        if (originalAPI.getContext() != null && !originalAPI.getContext().equals(apiBuilder.getContext())) {
            if (isContextExist(api.getContext())) {
                throw new APIManagementException("Context already Exist", ExceptionCodes.API_ALREADY_EXISTS);
            }
        }
        // publishing config to gateway
        gateway.addCompositeAPI(api);
        getApiDAO().updateApiDefinition(api.getId(), updatedSwagger, api.getUpdatedBy());
        getApiDAO().updateCompositeAPIGatewayConfig(api.getId(), new ByteArrayInputStream(updatedGatewayConfig.getBytes(StandardCharsets.UTF_8)), api.getUpdatedBy());
        if (log.isDebugEnabled()) {
            log.debug("API " + api.getName() + "-" + api.getVersion() + " was updated successfully.");
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while updating the API - " + apiBuilder.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    } catch (IOException e) {
        String errorMsg = "Error occurred while reading gateway configuration the API - " + apiBuilder.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_DAO_EXCEPTION);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) IOException(java.io.IOException) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext)

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