Search in sources :

Example 1 with ApiManagementConfiguration

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);
        }
    }
}
Also used : ApiManagementConfiguration(com.predic8.membrane.core.interceptor.apimanagement.ApiManagementConfiguration) BaseLocationApplicationContext(com.predic8.membrane.core.config.spring.BaseLocationApplicationContext) MCAttribute(com.predic8.membrane.annot.MCAttribute)

Example 2 with ApiManagementConfiguration

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));
}
Also used : AMRateLimiter(com.predic8.membrane.core.interceptor.apimanagement.rateLimiter.AMRateLimiter) ArrayList(java.util.ArrayList) Exchange(com.predic8.membrane.core.exchange.Exchange) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Outcome(com.predic8.membrane.core.interceptor.Outcome) Test(org.junit.Test)

Example 3 with ApiManagementConfiguration

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);
    }
}
Also used : Policy(com.predic8.membrane.core.interceptor.apimanagement.policy.Policy) Test(org.junit.Test)

Example 4 with ApiManagementConfiguration

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));
}
Also used : ArrayList(java.util.ArrayList) AMQuota(com.predic8.membrane.core.interceptor.apimanagement.quota.AMQuota) IOException(java.io.IOException) Exchange(com.predic8.membrane.core.exchange.Exchange) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Outcome(com.predic8.membrane.core.interceptor.Outcome) Test(org.junit.Test)

Example 5 with ApiManagementConfiguration

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);
}
Also used : MalformedURLException(java.net.MalformedURLException) ApiManagementConfiguration(com.predic8.membrane.core.interceptor.apimanagement.ApiManagementConfiguration) BaseLocationApplicationContext(com.predic8.membrane.core.config.spring.BaseLocationApplicationContext) URL(java.net.URL)

Aggregations

Test (org.junit.Test)3 BaseLocationApplicationContext (com.predic8.membrane.core.config.spring.BaseLocationApplicationContext)2 Exchange (com.predic8.membrane.core.exchange.Exchange)2 Outcome (com.predic8.membrane.core.interceptor.Outcome)2 ApiManagementConfiguration (com.predic8.membrane.core.interceptor.apimanagement.ApiManagementConfiguration)2 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)2 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 MCAttribute (com.predic8.membrane.annot.MCAttribute)1 Policy (com.predic8.membrane.core.interceptor.apimanagement.policy.Policy)1 AMQuota (com.predic8.membrane.core.interceptor.apimanagement.quota.AMQuota)1 AMRateLimiter (com.predic8.membrane.core.interceptor.apimanagement.rateLimiter.AMRateLimiter)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1