Search in sources :

Example 1 with Quota

use of com.predic8.membrane.core.interceptor.apimanagement.policy.Quota 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 Quota

use of com.predic8.membrane.core.interceptor.apimanagement.policy.Quota in project service-proxy by membrane.

the class AMQuota method setResponseToServiceUnavailable.

private void setResponseToServiceUnavailable(Exchange exc, PolicyQuota pq) {
    // TODO do a better response here
    Header hd = new Header();
    DateTimeFormatter dtFormatter = DateTimeFormat.forPattern("HH:mm:ss aa");
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    JsonGenerator jgen = null;
    try {
        jgen = new JsonFactory().createGenerator(os);
        jgen.writeStartObject();
        jgen.writeObjectField("Statuscode", 429);
        jgen.writeObjectField("Message", "Quota Exceeded");
        jgen.writeEndObject();
        jgen.close();
    } catch (IOException ignored) {
    }
    Response resp = Response.ResponseBuilder.newInstance().status(429, "Too Many Requests.").header(hd).contentType("application/json").body(os.toByteArray()).build();
    exc.setResponse(resp);
}
Also used : Response(com.predic8.membrane.core.http.Response) Header(com.predic8.membrane.core.http.Header) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Aggregations

JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 Header (com.predic8.membrane.core.http.Header)1 Response (com.predic8.membrane.core.http.Response)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 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)1