Search in sources :

Example 16 with ThreatProtectionPolicy

use of org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy in project carbon-apimgt by wso2.

the class APIGatewayPublisherImpl method deleteThreatProtectionPolicy.

/**
 * {@inheritDoc}
 */
@Override
public void deleteThreatProtectionPolicy(ThreatProtectionPolicy policy) throws GatewayException {
    ThreatProtectionEvent event = new ThreatProtectionEvent(APIMgtConstants.GatewayEventTypes.THREAT_PROTECTION_POLICY_DELETE);
    event.setPolicy(policy);
    publishToThreatProtectionTopic(event);
}
Also used : ThreatProtectionEvent(org.wso2.carbon.apimgt.core.models.events.ThreatProtectionEvent)

Example 17 with ThreatProtectionPolicy

use of org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy in project carbon-apimgt by wso2.

the class APIGatewayPublisherImpl method addThreatProtectionPolicy.

/**
 * {@inheritDoc}
 */
@Override
public void addThreatProtectionPolicy(ThreatProtectionPolicy policy) throws GatewayException {
    ThreatProtectionEvent event = new ThreatProtectionEvent(APIMgtConstants.GatewayEventTypes.THREAT_PROTECTION_POLICY_ADD);
    event.setPolicy(policy);
    publishToThreatProtectionTopic(event);
}
Also used : ThreatProtectionEvent(org.wso2.carbon.apimgt.core.models.events.ThreatProtectionEvent)

Example 18 with ThreatProtectionPolicy

use of org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy in project carbon-apimgt by wso2.

the class ThreatProtectionDAOImpl method addPolicy.

/**
 * Add a threat protection policy to database
 * @param policy Threat protection policy
 * @param connection SQL Connection
 * @throws APIMgtDAOException If failed to add policy
 */
private void addPolicy(ThreatProtectionPolicy policy, Connection connection) throws APIMgtDAOException {
    final String sqlQuery = "INSERT INTO " + THREAT_PROTECTION_TABLE + " (UUID, NAME, TYPE, POLICY) " + " VALUES (?, ?, ?, ?)";
    try (PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
        preparedStatement.setString(1, policy.getUuid());
        preparedStatement.setString(2, policy.getName());
        preparedStatement.setString(3, policy.getType());
        preparedStatement.setBytes(4, policy.getPolicy().getBytes("UTF-8"));
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        String errorMsg = "Error adding Threat Protection policy";
        throw new APIMgtDAOException(errorMsg, e);
    } catch (UnsupportedEncodingException e) {
        String errorMsg = "Charset error in threat protection policy";
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PreparedStatement(java.sql.PreparedStatement)

Example 19 with ThreatProtectionPolicy

use of org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy in project carbon-apimgt by wso2.

the class ThreatProtectionDAOImpl method getPolicy.

/**
 * Get a single threat protection policy
 * @param policyId Threat protection policy id
 * @param connection SQL Connection
 * @return The threat protection policy with policy id. null if not found.
 * @throws APIMgtDAOException If failed to retrieve the policy
 */
private ThreatProtectionPolicy getPolicy(String policyId, Connection connection) throws APIMgtDAOException {
    final String sqlQuery = "SELECT UUID, NAME, TYPE, POLICY " + " FROM " + THREAT_PROTECTION_TABLE + " WHERE UUID = ?";
    try (PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
        preparedStatement.setString(1, policyId);
        try (ResultSet rs = preparedStatement.executeQuery()) {
            ThreatProtectionPolicy policy = null;
            if (rs.next()) {
                policy = new ThreatProtectionPolicy();
                policy.setUuid(rs.getString(F_UUID));
                policy.setName(rs.getString(F_NAME));
                policy.setType(rs.getString(F_TYPE));
                policy.setPolicy(new String(rs.getBytes(F_POLICY), "UTF-8"));
            } else {
                log.warn("No Threat Protection Policy found for PolicyId: " + policyId);
            }
            return policy;
        } catch (UnsupportedEncodingException e) {
            String errorMsg = "Charset error in threat protection policy";
            throw new APIMgtDAOException(errorMsg, e);
        }
    } catch (SQLException e) {
        String errorMsg = "Error getting threat protection policy";
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PreparedStatement(java.sql.PreparedStatement)

Example 20 with ThreatProtectionPolicy

use of org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy in project carbon-apimgt by wso2.

the class ThreatProtectionDAOImpl method getPolicies.

/**
 * Get a list of all threat protection policies
 * @param connection SQL Connection
 * @return  List of threat protection policies
 * @throws APIMgtDAOException if failed to retrieve the list of policies
 */
private List<ThreatProtectionPolicy> getPolicies(Connection connection) throws APIMgtDAOException {
    final String sqlQuery = "SELECT UUID, NAME, TYPE, POLICY " + " FROM " + THREAT_PROTECTION_TABLE;
    try (PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
        try (ResultSet rs = preparedStatement.executeQuery()) {
            List<ThreatProtectionPolicy> list = new ArrayList<>();
            while (rs.next()) {
                ThreatProtectionPolicy policy = new ThreatProtectionPolicy();
                policy.setUuid(rs.getString(F_UUID));
                policy.setName(rs.getString(F_NAME));
                policy.setType(rs.getString(F_TYPE));
                policy.setPolicy(new String(rs.getBytes(F_POLICY), "UTF-8"));
                list.add(policy);
            }
            return list;
        } catch (UnsupportedEncodingException e) {
            String errorMsg = "Charset error in threat protection policy";
            throw new APIMgtDAOException(errorMsg, e);
        }
    } catch (SQLException e) {
        String errorMsg = "Error getting threat protection policies";
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PreparedStatement(java.sql.PreparedStatement)

Aggregations

ThreatProtectionPolicy (org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy)27 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)12 Test (org.testng.annotations.Test)10 ThreatProtectionDAO (org.wso2.carbon.apimgt.core.dao.ThreatProtectionDAO)9 ArrayList (java.util.ArrayList)7 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)6 Response (javax.ws.rs.core.Response)5 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)3 APIManagerFactory (org.wso2.carbon.apimgt.core.impl.APIManagerFactory)3 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)3 ThreatProtectionEvent (org.wso2.carbon.apimgt.core.models.events.ThreatProtectionEvent)3 ResultSet (java.sql.ResultSet)2 ThreatProtectionPolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.ThreatProtectionPolicyDTO)2 ThreatProtectionPolicyDTO (org.wso2.carbon.apimgt.rest.api.core.dto.ThreatProtectionPolicyDTO)2