Search in sources :

Example 46 with Outcome

use of com.predic8.membrane.core.interceptor.Outcome 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 47 with Outcome

use of com.predic8.membrane.core.interceptor.Outcome in project service-proxy by membrane.

the class AbortExchangeTest method setup.

@Before
public void setup() throws Exception {
    router = new HttpRouter();
    LimitedMemoryExchangeStore es = new LimitedMemoryExchangeStore();
    router.setExchangeStore(es);
    router.getTransport().getInterceptors().add(2, new ExchangeStoreInterceptor(es));
    ServiceProxy sp2 = new ServiceProxy(new ServiceProxyKey("*", "*", ".*", 3031), "", -1);
    sp2.getInterceptors().add(new AbstractInterceptor() {

        @Override
        public Outcome handleRequest(Exchange exc) throws Exception {
            exc.getRequest().readBody();
            exc.setResponse(Response.ok("").body(new InputStream() {

                int l = 0;

                @Override
                public int read() throws IOException {
                    if (l >= 2000000)
                        return -1;
                    return 0;
                }
            }, true).build());
            return Outcome.RETURN;
        }
    });
    router.getRuleManager().addProxyAndOpenPortIfNew(sp2);
    router.init();
}
Also used : AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) Exchange(com.predic8.membrane.core.exchange.Exchange) ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) Outcome(com.predic8.membrane.core.interceptor.Outcome) InputStream(java.io.InputStream) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) ExchangeStoreInterceptor(com.predic8.membrane.core.interceptor.ExchangeStoreInterceptor) HttpRouter(com.predic8.membrane.core.HttpRouter) IOException(java.io.IOException) Before(org.junit.Before)

Example 48 with Outcome

use of com.predic8.membrane.core.interceptor.Outcome in project service-proxy by membrane.

the class RequestInterceptor method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
    boolean logDebug = log.isDebugEnabled();
    for (Interceptor i : getInterceptors()) {
        EnumSet<Flow> f = i.getFlow();
        if (!f.contains(Flow.REQUEST))
            continue;
        if (logDebug)
            log.debug("Invoking request handler: " + i.getDisplayName() + " on exchange: " + exc);
        Outcome o = i.handleRequest(exc);
        if (o != Outcome.CONTINUE)
            return o;
    }
    return Outcome.CONTINUE;
}
Also used : Outcome(com.predic8.membrane.core.interceptor.Outcome) Interceptor(com.predic8.membrane.core.interceptor.Interceptor)

Aggregations

Outcome (com.predic8.membrane.core.interceptor.Outcome)25 Exchange (com.predic8.membrane.core.exchange.Exchange)19 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)16 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)14 IOException (java.io.IOException)14 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)10 Test (org.junit.Test)9 HttpRouter (com.predic8.membrane.core.HttpRouter)8 URISyntaxException (java.net.URISyntaxException)7 Before (org.junit.Before)6 ArrayList (java.util.ArrayList)4 Header (com.predic8.membrane.core.http.Header)3 Session (com.predic8.membrane.core.interceptor.authentication.session.SessionManager.Session)3 Rule (com.predic8.membrane.core.rules.Rule)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Body (com.predic8.membrane.core.http.Body)2 HttpClientConfiguration (com.predic8.membrane.core.transport.http.client.HttpClientConfiguration)2 MalformedURLException (java.net.MalformedURLException)2 InvalidParameterException (java.security.InvalidParameterException)2