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);
}
}
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;
}
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;
}
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());
}
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();
}
Aggregations