Search in sources :

Example 26 with OperationPolicyData

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

the class ApiMgtDAO method getLightWeightVersionOfAllOperationPolicies.

/**
 * Get the list of all operation policies. If the API UUID is provided, this will return all the operation policies
 * for that API. If not, it will return the common operation policies which are not bound to any API.
 * This list will include policy specification of each policy and policy ID. It will not contain the
 * policy definition as it is not useful for the operation.
 *
 * @param apiUUID      UUID of the API if exists. Null for common operation policies
 * @param organization Organization name
 * @return List of Operation Policies
 * @throws APIManagementException
 */
public List<OperationPolicyData> getLightWeightVersionOfAllOperationPolicies(String apiUUID, String organization) throws APIManagementException {
    String dbQuery;
    if (apiUUID != null) {
        dbQuery = SQLConstants.OperationPolicyConstants.GET_ALL_API_SPECIFIC_OPERATION_POLICIES_WITHOUT_CLONED_POLICIES;
    } else {
        dbQuery = SQLConstants.OperationPolicyConstants.GET_ALL_COMMON_OPERATION_POLICIES;
    }
    List<OperationPolicyData> policyDataList = new ArrayList<>();
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(dbQuery)) {
        statement.setString(1, organization);
        if (apiUUID != null) {
            statement.setString(2, apiUUID);
        }
        ResultSet rs = statement.executeQuery();
        while (rs.next()) {
            OperationPolicyData policyData = new OperationPolicyData();
            policyData.setOrganization(organization);
            policyData.setPolicyId(rs.getString("POLICY_UUID"));
            policyData.setMd5Hash(rs.getString("POLICY_MD5"));
            policyData.setSpecification(populatePolicySpecificationFromRS(rs));
            if (apiUUID != null) {
                policyData.setApiUUID(apiUUID);
            }
            policyDataList.add(policyData);
        }
    } catch (SQLException e) {
        handleException("Failed to get all the operation policy for tenant " + organization, e);
    }
    return policyDataList;
}
Also used : OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 27 with OperationPolicyData

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

the class ApiMgtDAO method getAPISpecificOperationPolicyByPolicyID.

private OperationPolicyData getAPISpecificOperationPolicyByPolicyID(Connection connection, String policyId, String apiUUID, String organization, boolean isWithPolicyDefinition) throws SQLException {
    String dbQuery = SQLConstants.OperationPolicyConstants.GET_API_SPECIFIC_OPERATION_POLICY_FROM_POLICY_ID;
    OperationPolicyData policyData = null;
    PreparedStatement statement = connection.prepareStatement(dbQuery);
    statement.setString(1, policyId);
    statement.setString(2, organization);
    statement.setString(3, apiUUID);
    ResultSet rs = statement.executeQuery();
    if (rs.next()) {
        policyData = new OperationPolicyData();
        policyData.setPolicyId(policyId);
        policyData.setApiUUID(apiUUID);
        policyData.setOrganization(organization);
        policyData.setMd5Hash(rs.getString("POLICY_MD5"));
        policyData.setRevisionUUID(rs.getString("REVISION_UUID"));
        policyData.setClonedCommonPolicyId(rs.getString("CLONED_POLICY_UUID"));
        policyData.setSpecification(populatePolicySpecificationFromRS(rs));
    }
    rs.close();
    statement.close();
    if (isWithPolicyDefinition && policyData != null) {
        if (isWithPolicyDefinition && policyData != null) {
            populatePolicyDefinitions(connection, policyId, policyData);
        }
    }
    return policyData;
}
Also used : OperationPolicyData(org.wso2.carbon.apimgt.api.model.OperationPolicyData) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 28 with OperationPolicyData

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

the class ApiMgtDAO method addCommonOperationPolicy.

/**
 * Operation policy implementation
 */
/**
 * Add a new common operation policy to the database. This will first add the operation policy content to the
 * AM_OPERATION_POLICY table and another entry to AM_COMMON_OPERATION_POLICY table.
 *
 * @param policyData Operation policy data.
 * @return UUID of the newly created shared policy
 * @throws APIManagementException
 */
public String addCommonOperationPolicy(OperationPolicyData policyData) throws APIManagementException {
    String policyUUID = null;
    OperationPolicySpecification policySpecification = policyData.getSpecification();
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            policyUUID = addOperationPolicyContent(connection, policyData);
            String dbQuery = SQLConstants.OperationPolicyConstants.ADD_COMMON_OPERATION_POLICY;
            PreparedStatement statement = connection.prepareStatement(dbQuery);
            statement.setString(1, policyUUID);
            statement.executeUpdate();
            statement.close();
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            handleException("Failed to add common operation policy " + policySpecification.getName(), e);
        }
    } catch (SQLException e) {
        handleException("Failed to add common operation policy " + policySpecification.getName(), e);
    }
    return policyUUID;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)

Example 29 with OperationPolicyData

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

the class ApiMgtDAO method addAPISpecificOperationPolicy.

/**
 * Add a new API specific operation policy to the database
 *
 * @param apiUUID      Unique Identifier of API
 * @param revisionUUID Unique Identifier of API revision
 * @param policyData   Unique Identifier of API
 * @return UUID of the newly created shared policy
 * @throws APIManagementException
 */
public String addAPISpecificOperationPolicy(String apiUUID, String revisionUUID, OperationPolicyData policyData) throws APIManagementException {
    OperationPolicySpecification policySpecification = policyData.getSpecification();
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            String policyID = addAPISpecificOperationPolicy(connection, apiUUID, revisionUUID, policyData, null);
            connection.commit();
            return policyID;
        } catch (SQLException e) {
            connection.rollback();
            handleException("Failed to add API specific operation policy " + policySpecification.getName() + " for API " + apiUUID, e);
        }
    } catch (SQLException e) {
        handleException("Failed to add API specific operation policy " + policySpecification.getName() + " for API " + apiUUID, e);
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)

Example 30 with OperationPolicyData

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

the class ApiMgtDAO method updateOperationPolicy.

/**
 * Update an existing operation policy
 *
 * @param connection DB connection
 * @param policyId   Shared policy UUID
 * @param policyData Updated policy definition
 * @throws SQLException
 */
private void updateOperationPolicy(Connection connection, String policyId, OperationPolicyData policyData) throws SQLException {
    OperationPolicySpecification policySpecification = policyData.getSpecification();
    PreparedStatement statement = connection.prepareStatement(SQLConstants.OperationPolicyConstants.UPDATE_OPERATION_POLICY_CONTENT);
    statement.setString(1, policySpecification.getName());
    statement.setString(2, policySpecification.getDisplayName());
    statement.setString(3, policySpecification.getDescription());
    statement.setString(4, policySpecification.getApplicableFlows().toString());
    statement.setString(5, policySpecification.getSupportedGateways().toString());
    statement.setString(6, policySpecification.getSupportedApiTypes().toString());
    statement.setBinaryStream(7, new ByteArrayInputStream(APIUtil.getPolicyAttributesAsString(policySpecification).getBytes()));
    statement.setString(8, policyData.getOrganization());
    statement.setString(9, policySpecification.getCategory().toString());
    statement.setBoolean(10, policySpecification.isMultipleAllowed());
    statement.setString(11, policyData.getMd5Hash());
    statement.setString(12, policyId);
    statement.executeUpdate();
    statement.close();
    if (policyData.getSynapsePolicyDefinition() != null) {
        updateOperationPolicyDefinition(connection, policyId, policyData.getSynapsePolicyDefinition());
    }
    if (policyData.getCcPolicyDefinition() != null) {
        updateOperationPolicyDefinition(connection, policyId, policyData.getCcPolicyDefinition());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PreparedStatement(java.sql.PreparedStatement) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)

Aggregations

OperationPolicyData (org.wso2.carbon.apimgt.api.model.OperationPolicyData)25 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)17 PreparedStatement (java.sql.PreparedStatement)11 OperationPolicySpecification (org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)11 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)10 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)10 ResultSet (java.sql.ResultSet)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 APIImportExportException (org.wso2.carbon.apimgt.impl.importexport.APIImportExportException)7 OperationPolicy (org.wso2.carbon.apimgt.api.model.OperationPolicy)6 OperationPolicyDataDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDataDTO)6 SdkClientException (com.amazonaws.SdkClientException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 MalformedURLException (java.net.MalformedURLException)5 URISyntaxException (java.net.URISyntaxException)5 UnknownHostException (java.net.UnknownHostException)5 SQLException (java.sql.SQLException)5 JSONException (org.json.JSONException)5 ParseException (org.json.simple.parser.ParseException)5