Search in sources :

Example 36 with InputStream

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

the class SiddhiQLBaseVisitorImpl method visitQuery.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public Query visitQuery(@NotNull SiddhiQLParser.QueryContext ctx) {
    try {
        Query query = Query.query().from((InputStream) visit(ctx.query_input()));
        if (ctx.query_section() != null) {
            query.select((Selector) visit(ctx.query_section()));
        }
        if (ctx.output_rate() != null) {
            query.output((OutputRate) visit(ctx.output_rate()));
        }
        for (SiddhiQLParser.AnnotationContext annotationContext : ctx.annotation()) {
            query.annotation((Annotation) visit(annotationContext));
        }
        if (ctx.query_output() != null) {
            query.outStream((OutputStream) visit(ctx.query_output()));
        }
        populateQueryContext(query, ctx);
        return query;
    } finally {
        activeStreams.clear();
    }
}
Also used : SiddhiQLParser(org.wso2.siddhi.query.compiler.SiddhiQLParser) StoreQuery(org.wso2.siddhi.query.api.execution.query.StoreQuery) Query(org.wso2.siddhi.query.api.execution.query.Query)

Example 37 with InputStream

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

the class ApiDAOImpl method updateEndpoint.

/**
 * Update an Endpoint
 *
 * @param endpoint Endpoint Object.
 * @return Success of the update operation.
 * @throws APIMgtDAOException If failed to update endpoint.
 */
@Override
public boolean updateEndpoint(Endpoint endpoint) throws APIMgtDAOException {
    final String query = "UPDATE AM_ENDPOINT SET ENDPOINT_CONFIGURATION = ?,TPS = ?,TYPE = " + "?,SECURITY_CONFIGURATION =?, LAST_UPDATED_TIME = ?, GATEWAY_CONFIG = ? WHERE UUID = ?";
    try (Connection connection = DAOUtil.getConnection()) {
        connection.setAutoCommit(false);
        try (PreparedStatement statement = connection.prepareStatement(query)) {
            InputStream byteArrayInputStream = IOUtils.toInputStream(endpoint.getEndpointConfig());
            statement.setBinaryStream(1, byteArrayInputStream);
            if (endpoint.getMaxTps() != null) {
                statement.setLong(2, endpoint.getMaxTps());
            } else {
                statement.setNull(2, Types.INTEGER);
            }
            statement.setString(3, endpoint.getType());
            statement.setBinaryStream(4, IOUtils.toInputStream(endpoint.getSecurity()));
            statement.setTimestamp(5, Timestamp.valueOf(LocalDateTime.now()));
            statement.setBinaryStream(6, IOUtils.toInputStream(endpoint.getConfig()));
            statement.setString(7, endpoint.getId());
            statement.execute();
            connection.commit();
            return true;
        } catch (SQLException e) {
            connection.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating Endpoint: " + endpoint.getName(), e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating Endpoint: " + endpoint.getName(), e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 38 with InputStream

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

the class ApiDAOImpl method addDocumentFileContent.

/**
 * @see ApiDAO#addDocumentFileContent(String, InputStream, String, String)
 */
@Override
public void addDocumentFileContent(String resourceID, InputStream content, String dataType, String updatedBy) throws APIMgtDAOException {
    try (Connection connection = DAOUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            if (ApiResourceDAO.updateBinaryResource(connection, resourceID, content, dataType, updatedBy) == 0) {
                String msg = "Cannot add file content for non existing document: " + resourceID + ", updated by: " + updatedBy;
                throw new APIMgtDAOException(msg, ExceptionCodes.DOCUMENT_NOT_FOUND);
            }
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            String msg = "adding document file content for document: " + resourceID + ", updatedBy: " + updatedBy;
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + msg, e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        String msg = "adding document file content for document: " + resourceID + ", updatedBy: " + 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 39 with InputStream

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

the class PolicyDAOImpl method getCustomPolicies.

@Override
public List<CustomPolicy> getCustomPolicies() throws APIMgtDAOException {
    List<CustomPolicy> customPolicyList = new ArrayList<>();
    String getQuery = "SELECT NAME, DESCRIPTION, UUID, KEY_TEMPLATE, IS_DEPLOYED, SIDDHI_QUERY FROM " + "AM_CUSTOM_POLICY";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(getQuery)) {
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                String siddhiQuery = null;
                CustomPolicy customPolicy = new CustomPolicy(resultSet.getString("NAME"));
                customPolicy.setDescription(resultSet.getString("DESCRIPTION"));
                customPolicy.setUuid(resultSet.getString("UUID"));
                customPolicy.setKeyTemplate(resultSet.getString("KEY_TEMPLATE"));
                customPolicy.setDeployed(resultSet.getBoolean("IS_DEPLOYED"));
                InputStream siddhiQueryBlob = resultSet.getBinaryStream("SIDDHI_QUERY");
                if (siddhiQueryBlob != null) {
                    try {
                        siddhiQuery = IOUtils.toString(siddhiQueryBlob);
                    } catch (IOException e) {
                        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "reading siddhi query stream", e);
                    }
                }
                customPolicy.setSiddhiQuery(siddhiQuery);
                customPolicyList.add(customPolicy);
            }
        }
        return customPolicyList;
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting custom policies", e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 40 with InputStream

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

the class APIFileUtils method extractUploadedArchive.

/**
 * Extracts the APIs to the file system by reading the incoming {@link InputStream} object
 * uploadedApiArchiveInputStream
 *
 * @param uploadedApiArchiveInputStream Incoming {@link InputStream}
 * @param importedDirectoryName         directory to extract the archive
 * @param apiArchiveLocation            full path of the archive location
 * @param extractLocation               full path to the location to which the archive will be written
 * @return location to which APIs were extracted
 * @throws APIMgtDAOException if an error occurs while extracting the archive
 */
public static String extractUploadedArchive(InputStream uploadedApiArchiveInputStream, String importedDirectoryName, String apiArchiveLocation, String extractLocation) throws APIMgtDAOException {
    String archiveExtractLocation;
    try {
        // create api import directory structure
        APIFileUtils.createDirectory(extractLocation);
        // create archive
        createArchiveFromInputStream(uploadedApiArchiveInputStream, apiArchiveLocation);
        // extract the archive
        archiveExtractLocation = extractLocation + File.separator + importedDirectoryName;
        extractArchive(apiArchiveLocation, archiveExtractLocation);
    } catch (APIMgtDAOException e) {
        APIFileUtils.deleteDirectory(extractLocation);
        String errorMsg = "Error in accessing uploaded API archive";
        log.error(errorMsg, e);
        throw new APIMgtDAOException(errorMsg, e);
    }
    return archiveExtractLocation;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)

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