use of org.wso2.carbon.apimgt.core.models.policy.CustomPolicy in project carbon-apimgt by wso2.
the class PolicyDAOImpl method getCustomPolicies.
@Override
public List<CustomPolicy> getCustomPolicies() throws APIMgtDAOException {
List<CustomPolicy> customPolicyList = new ArrayList<>();
String getQuery = "SELECT NAME, DESCRIPTION, UUID, KEY_TEMPLATE, IS_DEPLOYED, SIDDHI_QUERY FROM " + "AM_CUSTOM_POLICY";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(getQuery)) {
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
String siddhiQuery = null;
CustomPolicy 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);
customPolicyList.add(customPolicy);
}
}
return customPolicyList;
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting custom policies", e);
}
}
use of org.wso2.carbon.apimgt.core.models.policy.CustomPolicy in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createDefaultCustomPolicySiddhiApp.
public static String createDefaultCustomPolicySiddhiApp() {
CustomPolicy policy = createDefaultCustomPolicy();
String siddhiApp = "@App:name('custom_" + policy.getPolicyName() + "')" + "\n@App:description('ExecutionPlan for custom_" + policy.getPolicyName() + "')\n" + "\n@source(type='inMemory', topic='apim', @map(type='passThrough'))\n" + "define stream RequestStream (messageID string, appKey string, appTier string, " + "subscriptionKey string, apiKey string, apiTier string, subscriptionTier string," + " resourceKey string, resourceTier string, userId string, apiContext string, " + "apiVersion string, appTenant string, apiTenant string, appId string, apiName string, " + "propertiesMap string);\n" + "\n@sink(type='jms', @map(type='text'),\n" + "factory.initial='org.wso2.andes.jndi.PropertiesFileInitialContextFactory'," + " provider.url='tcp://localhost:5672', destination='TEST.FOO'," + " connection.factory.type='topic',\n" + "connection.factory.jndi.name='TopicConnectionFactory')\n" + "define stream GlobalThrottleStream (throttleKey string, isThrottled bool, " + "expiryTimeStamp long);\n" + "\n" + policy.getSiddhiQuery() + "\n" + "\nfrom ResultStream#throttler:emitOnStateChange(throttleKey, isThrottled)" + "\nselect *\n" + "insert into GlobalThrottleStream;\n";
return siddhiApp;
}
use of org.wso2.carbon.apimgt.core.models.policy.CustomPolicy in project carbon-apimgt by wso2.
the class CustomPolicyMappingUtil method fromCustomPolicyToDTO.
/**
* Converts a single Custom Policy model object into DTO object.
*
* @param globalPolicy Custom Policy model object
* @return DTO object derived from the Policy model object
* @throws UnsupportedThrottleLimitTypeException - If error occurs
*/
public static CustomRuleDTO fromCustomPolicyToDTO(CustomPolicy globalPolicy) throws UnsupportedThrottleLimitTypeException {
CustomRuleDTO policyDTO = new CustomRuleDTO();
policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(globalPolicy, policyDTO);
policyDTO.setKeyTemplate(globalPolicy.getKeyTemplate());
policyDTO.setSiddhiQuery(globalPolicy.getSiddhiQuery());
return policyDTO;
}
use of org.wso2.carbon.apimgt.core.models.policy.CustomPolicy in project carbon-apimgt by wso2.
the class CustomPolicyMappingUtil method fromCustomPolicyArrayToListDTO.
/**
* Converts an array of Custom Policy model objects into REST API DTO objects.
*
* @param customPolicies An array of custom policy model objects
* @return A List DTO of Custom Policy DTOs derived from the array of model objects
* @throws UnsupportedThrottleLimitTypeException - If error occurs
*/
public static CustomRuleListDTO fromCustomPolicyArrayToListDTO(List<CustomPolicy> customPolicies) throws UnsupportedThrottleLimitTypeException {
CustomRuleListDTO listDTO = new CustomRuleListDTO();
List<CustomRuleDTO> customPolicyDTOList = new ArrayList<>();
if (customPolicies != null) {
for (CustomPolicy policy : customPolicies) {
CustomRuleDTO dto = fromCustomPolicyToDTO(policy);
customPolicyDTOList.add(dto);
}
}
listDTO.setCount(customPolicyDTOList.size());
listDTO.setList(customPolicyDTOList);
return listDTO;
}
use of org.wso2.carbon.apimgt.core.models.policy.CustomPolicy in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesThrottlingCustomGet.
/**
* Retrieves all custom policies.
*
* @param ifNoneMatch If-None-Match header value
* @param ifModifiedSince If-Modified-Since header value
* @param request msf4j request object
* @return All matched Global Throttle policies to the given request
* @throws NotFoundException if an error occurred when particular resource does not exits in the system.
*/
@Override
public Response policiesThrottlingCustomGet(String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
if (log.isDebugEnabled()) {
log.debug("Received Custom Policy GET request.");
}
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
List<CustomPolicy> policies = apiMgtAdminService.getCustomRules();
CustomRuleListDTO customRuleListDTO = CustomPolicyMappingUtil.fromCustomPolicyArrayToListDTO(policies);
return Response.ok().entity(customRuleListDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while retrieving custom policies";
org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
Aggregations