Search in sources :

Example 76 with InputStream

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

the class ApiFileDAOImpl method getImage.

/**
 * @see ApiDAO#getImage(String apiID)
 */
@Override
public InputStream getImage(String apiID) throws APIMgtDAOException {
    API api = getAPI(apiID);
    if (api == null) {
        String errorMsg = "Unable to find API with Id: " + apiID;
        log.error(errorMsg);
        throw new APIMgtDAOException(errorMsg, ExceptionCodes.API_NOT_FOUND);
    }
    String thumbnailPath = APIFileUtils.getAPIBaseDirectory(storagePath, new FileApi(api)) + File.separator + APIMgtConstants.APIFileUtilConstants.THUMBNAIL_FILE_NAME;
    return APIFileUtils.getThumbnailImage(thumbnailPath);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) FileApi(org.wso2.carbon.apimgt.core.models.FileApi)

Example 77 with InputStream

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

the class PolicyDAOImpl method getApplicationPolicyByUuid.

@Override
public ApplicationPolicy getApplicationPolicyByUuid(String uuid) throws APIMgtDAOException {
    try {
        final String query = "SELECT UUID, NAME, QUOTA_TYPE, TIME_UNIT, UNIT_TIME, QUOTA, QUOTA_UNIT, DESCRIPTION, " + "DISPLAY_NAME, CUSTOM_ATTRIBUTES, IS_DEPLOYED from AM_APPLICATION_POLICY WHERE UUID = ?";
        ApplicationPolicy applicationPolicy;
        try (Connection conn = DAOUtil.getConnection();
            PreparedStatement statement = conn.prepareStatement(query)) {
            statement.setString(1, uuid);
            statement.execute();
            try (ResultSet rs = statement.getResultSet()) {
                if (rs.next()) {
                    applicationPolicy = new ApplicationPolicy(rs.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
                    setCommonPolicyDetails(applicationPolicy, rs);
                    InputStream inputStream = rs.getBinaryStream(APIMgtConstants.ThrottlePolicyConstants.COLUMN_CUSTOM_ATTRIB);
                    if (inputStream != null) {
                        applicationPolicy.setCustomAttributes(IOUtils.toByteArray(inputStream));
                    }
                    return applicationPolicy;
                } else {
                    // not found
                    String msg = "Application Policy not found for id: " + uuid;
                    log.warn(msg);
                    throw new APIMgtDAOException(msg, ExceptionCodes.POLICY_NOT_FOUND);
                }
            } catch (IOException e) {
                // error
                throw new APIMgtDAOException("Internal error", e);
            }
        }
    } catch (SQLException e) {
        String errorMsg = "Error in retrieving Application Policy with id: " + uuid;
        log.error(errorMsg, e);
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 78 with InputStream

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

the class PolicyDAOImpl method createSubscriptionPolicyFromResultSet.

/**
 * Creates a Subscription Policy from the results set
 *
 * @param identifier policy id
 * @param rs {@link ResultSet} instance
 * @return {@link SubscriptionPolicy} instance
 * @throws SQLException if any error occurs while creating the Subscription Policy from the result set
 * @throws APIMgtDAOException if any error occurs while retrieving custom attributes for this Subscription Policy
 */
private SubscriptionPolicy createSubscriptionPolicyFromResultSet(String identifier, ResultSet rs) throws SQLException, APIMgtDAOException {
    SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(rs.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
    setCommonPolicyDetails(subscriptionPolicy, rs);
    setSubscriptionPolicyDetals(subscriptionPolicy, rs);
    InputStream binary = rs.getBinaryStream(APIMgtConstants.ThrottlePolicyConstants.COLUMN_CUSTOM_ATTRIB);
    if (binary != null) {
        byte[] customAttrib;
        try {
            customAttrib = IOUtils.toByteArray(binary);
            if (customAttrib.length > 0) {
                subscriptionPolicy.setCustomAttributes(customAttrib);
            }
        } catch (IOException e) {
            String errorMsg = "An Error occurred while retrieving custom attributes for subscription policy with " + "identifier: " + identifier;
            log.error(errorMsg, e);
            throw new APIMgtDAOException(errorMsg, e);
        }
    }
    return subscriptionPolicy;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 79 with InputStream

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

the class PolicyDAOImpl method getCustomPolicyByUuid.

@Override
public CustomPolicy getCustomPolicyByUuid(String uuid) throws APIMgtDAOException {
    String query = "SELECT NAME, DESCRIPTION, UUID, KEY_TEMPLATE, IS_DEPLOYED, SIDDHI_QUERY FROM AM_CUSTOM_POLICY" + " WHERE UUID = ? ";
    CustomPolicy customPolicy = null;
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setString(1, uuid);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            if (resultSet.next()) {
                String siddhiQuery = null;
                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);
            }
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting custom policy with UUID : " + uuid, e);
    }
    return customPolicy;
}
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) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 80 with InputStream

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

the class AbstractAPIManager method getDocumentationContent.

/**
 * This method used to get the content of a documentation
 *
 * @param docId Document ID
 * @return {@link InputStream} Input stream for document content
 * @throws APIManagementException if the requested documentation content is not available
 */
public DocumentContent getDocumentationContent(String docId) throws APIManagementException {
    try {
        DocumentInfo documentInfo = getDocumentationSummary(docId);
        DocumentContent.Builder documentContentBuilder = new DocumentContent.Builder();
        if (documentInfo != null) {
            documentContentBuilder.documentInfo(documentInfo);
            if (DocumentInfo.SourceType.FILE.equals(documentInfo.getSourceType())) {
                InputStream inputStream = getApiDAO().getDocumentFileContent(docId);
                if (inputStream != null) {
                    documentContentBuilder = documentContentBuilder.fileContent(inputStream);
                } else {
                    throw new APIManagementException("Couldn't find file content of  document", ExceptionCodes.DOCUMENT_CONTENT_NOT_FOUND);
                }
            } else if (documentInfo.getSourceType().equals(DocumentInfo.SourceType.INLINE)) {
                String inlineContent = getApiDAO().getDocumentInlineContent(docId);
                if (inlineContent != null) {
                    documentContentBuilder = documentContentBuilder.inlineContent(inlineContent);
                } else {
                    throw new APIManagementException("Couldn't find inline content of  document", ExceptionCodes.DOCUMENT_CONTENT_NOT_FOUND);
                }
            }
        } else {
            throw new APIManagementException("Couldn't fnd document", ExceptionCodes.DOCUMENT_NOT_FOUND);
        }
        return documentContentBuilder.build();
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while retrieving document content";
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) DocumentContent(org.wso2.carbon.apimgt.core.models.DocumentContent) InputStream(java.io.InputStream) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

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