use of com.predic8.membrane.core.interceptor.apimanagement.ApiManagementConfiguration in project service-proxy by membrane.
the class SimpleApiConfig method setUrl.
/**
* @description the url to the configuration file
* @default api.yaml
*/
@MCAttribute
public void setUrl(String url) {
this.url = url;
if (context != null) {
if (amc == null) {
String workingDir = ((BaseLocationApplicationContext) context).getBaseLocation();
amc = new ApiManagementConfiguration(workingDir, this.url);
} else {
amc.setLocation(this.url);
}
}
}
use of com.predic8.membrane.core.interceptor.apimanagement.ApiManagementConfiguration in project service-proxy by membrane.
the class AMRateLimitInterceptorTest method testHandleRequestRateLimit5SecondConcurrency.
@Test
public void testHandleRequestRateLimit5SecondConcurrency() throws Exception {
final Exchange exc = new Exchange(null);
exc.setResponse(Response.ResponseBuilder.newInstance().build());
exc.setProperty(Exchange.API_KEY, "junit");
exc.setRule(new ServiceProxy());
exc.getRule().setName("junit API");
final AMRateLimiter rli = new AMRateLimiter();
ApiManagementConfiguration amc = new ApiManagementConfiguration(System.getProperty("user.dir"), "src\\test\\resources\\apimanagement\\api.yaml");
rli.setAmc(amc);
ArrayList<Thread> threads = new ArrayList<Thread>();
final AtomicInteger continues = new AtomicInteger();
final AtomicInteger returns = new AtomicInteger();
for (int i = 0; i < 1000; i++) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
Outcome out = rli.handleRequest(exc);
if (out == Outcome.CONTINUE) {
continues.incrementAndGet();
} else if (out == Outcome.RETURN) {
returns.incrementAndGet();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
threads.add(t);
t.start();
}
for (Thread t : threads) {
t.join();
}
assertEquals(5, continues.get());
assertEquals(995, returns.get());
Thread.sleep(2000);
assertEquals(Outcome.CONTINUE, rli.handleRequest(exc));
}
use of com.predic8.membrane.core.interceptor.apimanagement.ApiManagementConfiguration in project service-proxy by membrane.
the class ApiManagementConfigurationTest method testParseYaml.
@Test
public void testParseYaml() throws Exception {
String source = new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir") + "\\src\\test\\resources\\apimanagement\\api.yaml")), Charset.defaultCharset());
ApiManagementConfiguration conf = new ApiManagementConfiguration();
conf.setLocation(source);
Map<String, Policy> policies = conf.getPolicies();
for (Policy p : policies.values()) {
System.out.println(p);
}
Map<String, Key> keys = conf.getKeys();
for (Key k : keys.values()) {
System.out.println(k);
}
}
use of com.predic8.membrane.core.interceptor.apimanagement.ApiManagementConfiguration in project service-proxy by membrane.
the class AMQuotaInterceptorTest method testAMQuota.
@Test
public void testAMQuota() throws IOException, InterruptedException {
final Exchange exc = new Exchange(null);
exc.setRequest(new Request.Builder().header("Test", "test").body("hello").build());
exc.setResponse(new Response.ResponseBuilder().header("Test2", "test2").body("Hello back!").build());
exc.setProperty(Exchange.API_KEY, "junit");
exc.setRule(new ServiceProxy());
exc.getRule().setName("junit API");
ApiManagementConfiguration amc = new ApiManagementConfiguration(System.getProperty("user.dir"), "src\\test\\resources\\apimanagement\\api.yaml");
long reqSize = exc.getRequest().getHeader().toString().getBytes().length + exc.getRequest().getHeader().getContentLength();
long respSize = exc.getResponse().getHeader().toString().getBytes().length + exc.getResponse().getHeader().getContentLength();
assertEquals(31 + 5, reqSize);
assertEquals(34 + 11, respSize);
final AMQuota amq = new AMQuota();
amq.setAmc(amc);
ArrayList<Thread> threads = new ArrayList<Thread>();
final AtomicInteger continues = new AtomicInteger();
final AtomicInteger returns = new AtomicInteger();
for (int i = 0; i < 1000; i++) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
Outcome out = amq.handleRequest(exc);
if (out == Outcome.CONTINUE) {
continues.incrementAndGet();
} else if (out == Outcome.RETURN) {
returns.incrementAndGet();
}
amq.handleResponse(exc);
} catch (Exception e) {
e.printStackTrace();
}
}
});
threads.add(t);
// t.start();
// doing sync because else we cant predictably count request/response pairs
t.run();
}
for (Thread t : threads) {
t.join();
}
// the limit is ( or should be ) 120B
// 31+5 ( Req ) + 34+11 ( Resp ) = 81 for every completed exchange
// the second request adds another 31+5 -> 81 + 36 = 117 < 120B -> after the second request it should block because the limit is 120b and the following response would bring it over the limit ( responses never block, only requests )
assertEquals(2, continues.get());
assertEquals(998, returns.get());
Thread.sleep(2000);
assertEquals(Outcome.CONTINUE, amq.handleRequest(exc));
}
use of com.predic8.membrane.core.interceptor.apimanagement.ApiManagementConfiguration in project service-proxy by membrane.
the class EtcdRegistryApiConfig method initAmc.
public void initAmc() {
String workingDir = ((BaseLocationApplicationContext) context).getBaseLocation();
String etcdUrlForAmc = null;
URL u = null;
try {
u = new URL(getUrl());
} catch (MalformedURLException e) {
try {
u = new URL("http://" + getUrl());
} catch (MalformedURLException ignored) {
}
}
if (u == null) {
log.error("Url malformed: " + getUrl());
}
etcdUrlForAmc = "etcd://" + u.getHost() + ":" + u.getPort();
amc = new ApiManagementConfiguration(workingDir, etcdUrlForAmc, membraneId);
}
Aggregations