Search in sources :

Example 1 with Field

use of com.predic8.membrane.core.interceptor.formvalidation.FormValidationInterceptor.Field in project service-proxy by membrane.

the class ApiManagementConfiguration method parsePolicies.

private Map<String, Policy> parsePolicies(Map<String, Object> yaml) {
    Map<String, Policy> result = new HashMap<String, Policy>();
    Object policies = yaml.get("policies");
    if (policies == null) {
        log.warn("No policies in policy file");
        return result;
    }
    List<Object> yamlPolicies = (List<Object>) policies;
    for (Object yamlPolicyObj : yamlPolicies) {
        if (yamlPolicyObj == null) {
            continue;
        }
        LinkedHashMap<String, Object> yamlPolicy = (LinkedHashMap<String, Object>) yamlPolicyObj;
        for (Object polObj : yamlPolicy.values()) {
            if (polObj == null) {
                continue;
            }
            LinkedHashMap<String, Object> yamlPolicyDef = (LinkedHashMap<String, Object>) polObj;
            Policy policy = new Policy();
            Object name = yamlPolicyDef.get("id");
            if (name == null) {
                log.warn("Policy object found, but no \"id\" field");
                continue;
            }
            String policyName = (String) name;
            policy.setName(policyName);
            Object serviceProxiesObj = yamlPolicyDef.get("serviceProxy");
            if (serviceProxiesObj == null) {
                log.warn("Policy object found, but no service proxies specified ");
                continue;
            }
            List<String> serviceProxyNames = (List<String>) serviceProxiesObj;
            for (String sp : serviceProxyNames) {
                policy.getServiceProxies().add(sp);
            }
            // Optionals like rateLimit/quota etc. follow
            Object rateLimitObj = yamlPolicyDef.get("rateLimit");
            if (rateLimitObj != null) {
                LinkedHashMap<String, Object> rateLimitData = (LinkedHashMap<String, Object>) rateLimitObj;
                RateLimit rateLimit = new RateLimit();
                int requests = parseInteger(rateLimitData.get("requests"), RateLimit.REQUESTS_DEFAULT);
                int interval = parseInteger(rateLimitData.get("interval"), RateLimit.INTERVAL_DEFAULT);
                rateLimit.setRequests(requests);
                rateLimit.setInterval(interval);
                policy.setRateLimit(rateLimit);
            }
            Object quotaObj = yamlPolicyDef.get("quota");
            if (quotaObj != null) {
                LinkedHashMap<String, Object> quota = (LinkedHashMap<String, Object>) quotaObj;
                Object quotaSizeObj = quota.get("size");
                long quotaNumber = getQuotaNumber(quotaSizeObj);
                int quotaInterval = parseInteger(quota.get("interval"), Quota.INTERVAL_DEFAULT);
                Quota q = new Quota();
                q.setSize(quotaNumber);
                q.setInterval(quotaInterval);
                policy.setQuota(q);
            }
            parseUnauthenticatedField(yamlPolicyDef, policy);
            result.put(policyName, policy);
        }
    }
    return result;
}
Also used : Policy(com.predic8.membrane.core.interceptor.apimanagement.policy.Policy) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) RateLimit(com.predic8.membrane.core.interceptor.apimanagement.policy.RateLimit) Quota(com.predic8.membrane.core.interceptor.apimanagement.policy.Quota)

Example 2 with Field

use of com.predic8.membrane.core.interceptor.formvalidation.FormValidationInterceptor.Field in project service-proxy by membrane.

the class FormValidationInterceptorTest method testValidation.

@Test
public void testValidation() throws Exception {
    FormValidationInterceptor interceptor = new FormValidationInterceptor();
    interceptor.init(new HttpRouter());
    Field article = new Field();
    article.setName("article");
    article.setRegex("banana|apple");
    Field amount = new Field();
    amount.setName("amount");
    amount.setRegex("\\d+");
    List<Field> fields = new ArrayList<Field>();
    fields.add(amount);
    fields.add(article);
    interceptor.setFields(fields);
    Exchange exc = getExchange("/buy?article=pizza&amount=five");
    assertEquals(Outcome.ABORT, interceptor.handleRequest(exc));
    assertEquals(400, exc.getResponse().getStatusCode());
    exc = getExchange("/buy?article=pizza&amount=2");
    assertEquals(Outcome.ABORT, interceptor.handleRequest(exc));
    assertEquals(400, exc.getResponse().getStatusCode());
    exc = getExchange("/buy?article=banana&amount=five");
    assertEquals(Outcome.ABORT, interceptor.handleRequest(exc));
    assertEquals(400, exc.getResponse().getStatusCode());
    exc = getExchange("/buy?article=banana&amount=5");
    assertEquals(Outcome.CONTINUE, interceptor.handleRequest(exc));
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Field(com.predic8.membrane.core.interceptor.formvalidation.FormValidationInterceptor.Field) HttpRouter(com.predic8.membrane.core.HttpRouter)

Aggregations

HttpRouter (com.predic8.membrane.core.HttpRouter)1 Exchange (com.predic8.membrane.core.exchange.Exchange)1 Policy (com.predic8.membrane.core.interceptor.apimanagement.policy.Policy)1 Quota (com.predic8.membrane.core.interceptor.apimanagement.policy.Quota)1 RateLimit (com.predic8.membrane.core.interceptor.apimanagement.policy.RateLimit)1 Field (com.predic8.membrane.core.interceptor.formvalidation.FormValidationInterceptor.Field)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1