Search in sources :

Example 1 with ThreatProtectionPolicy

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

the class ThreatProtectionDAOImpl method updatePolicy.

/**
 * Update a threat protection policy
 * @param policy Policy to be updated
 * @param connection SQL Connection
 * @throws APIMgtDAOException If failed to update policy
 */
private void updatePolicy(ThreatProtectionPolicy policy, Connection connection) throws APIMgtDAOException {
    final String sqlQuery = "UPDATE " + THREAT_PROTECTION_TABLE + " SET NAME = ?, " + "TYPE = ?, " + "POLICY = ? " + "WHERE UUID = ?";
    try (PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
        preparedStatement.setString(1, policy.getName());
        preparedStatement.setString(2, policy.getType());
        preparedStatement.setBytes(3, policy.getPolicy().getBytes("UTF-8"));
        preparedStatement.setString(4, policy.getUuid());
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        String errorMsg = "Error updating 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 2 with ThreatProtectionPolicy

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

the class ThreatProtectionMappingUtil method toThreatProtectionPolicyDTO.

/**
 * Converts ThreatProtectionPolicy core model ThreatProtectionPolicyDTO rest api core model
 * @param policy apimgt core model of ThreatProtectionPolicy
 * @return ThreatProtectionPolicyDTO rest api core model
 */
public static ThreatProtectionPolicyDTO toThreatProtectionPolicyDTO(ThreatProtectionPolicy policy) {
    if (policy == null)
        return null;
    ThreatProtectionPolicyDTO dto = new ThreatProtectionPolicyDTO();
    dto.setUuid(policy.getUuid());
    dto.setName(policy.getName());
    dto.setType(policy.getType());
    dto.setPolicy(policy.getPolicy());
    return dto;
}
Also used : ThreatProtectionPolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ThreatProtectionPolicyDTO)

Example 3 with ThreatProtectionPolicy

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

the class ThreatProtectionPoliciesApiServiceImplTestCase method createThreatProtectionPolicy.

private ThreatProtectionPolicy createThreatProtectionPolicy() {
    ThreatProtectionPolicy policy = new ThreatProtectionPolicy();
    policy.setName("TEST-POLICY");
    policy.setType("XML");
    policy.setUuid(UUID.randomUUID().toString());
    return policy;
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy)

Example 4 with ThreatProtectionPolicy

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

the class ThreatProtectionApiServiceImplTestCase method threatProtectionPoliciesPolicyIdGetTestCase.

@Test
public void threatProtectionPoliciesPolicyIdGetTestCase() throws Exception {
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getLoggedInUsername(Mockito.any())).thenReturn("username");
    PowerMockito.mockStatic(RestAPIPublisherUtil.class);
    APIPublisher publisher = Mockito.mock(APIPublisher.class);
    Mockito.when(RestAPIPublisherUtil.getApiPublisher(Mockito.anyString())).thenReturn(publisher);
    ThreatProtectionPolicy policy = Mockito.mock(ThreatProtectionPolicy.class);
    Mockito.when(publisher.getThreatProtectionPolicy(Mockito.anyString())).thenReturn(policy);
    ThreatProtectionPoliciesApiServiceImpl apiService = new ThreatProtectionPoliciesApiServiceImpl();
    Response response = apiService.threatProtectionPoliciesPolicyIdGet("POLICY", getRequest());
    Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with ThreatProtectionPolicy

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

the class ThreatProtectionPoliciesApiServiceImpl method threatProtectionPoliciesGet.

/**
 * Get a list of all threat protection policies
 *
 * @param request
 * @return List of threat protection policies
 * @throws NotFoundException
 */
@Override
public Response threatProtectionPoliciesGet(Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        List<ThreatProtectionPolicy> list = apiPublisher.getThreatProtectionPolicies();
        ThreatProtectionPolicyListDTO listDTO = new ThreatProtectionPolicyListDTO();
        for (ThreatProtectionPolicy policy : list) {
            listDTO.addListItem(MappingUtil.toThreatProtectionPolicyDTO(policy));
        }
        return Response.ok().entity(listDTO).build();
    } catch (APIManagementException e) {
        log.error(e.getMessage(), e);
    }
    return Response.status(500).entity("Internal Server Error.").build();
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher)

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