Search in sources :

Example 1 with CustomPolicy

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);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 2 with CustomPolicy

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;
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)

Example 3 with CustomPolicy

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;
}
Also used : CustomRuleDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleDTO)

Example 4 with CustomPolicy

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;
}
Also used : CustomRuleDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleDTO) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) ArrayList(java.util.ArrayList) CustomRuleListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleListDTO)

Example 5 with CustomPolicy

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();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Aggregations

CustomPolicy (org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)29 Test (org.testng.annotations.Test)12 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)10 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)7 ArrayList (java.util.ArrayList)6 CustomRuleDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleDTO)6 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 Response (javax.ws.rs.core.Response)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)4 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)4 PoliciesApiServiceImpl (org.wso2.carbon.apimgt.rest.api.admin.impl.PoliciesApiServiceImpl)4 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)4 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2