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