Search in sources :

Example 46 with ServiceProxy

use of com.predic8.membrane.core.rules.ServiceProxy 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 ServiceProxy

use of com.predic8.membrane.core.rules.ServiceProxy in project service-proxy by membrane.

the class AMStatisticsCollectorTest method testThreadedStatisticCollection.

@Test
public void testThreadedStatisticCollection() throws InterruptedException {
    final AMStatisticsCollector amSc = new AMStatisticsCollector();
    amSc.setCollectTimeInSeconds(2);
    ArrayList<Thread> threads = new ArrayList<Thread>();
    for (int i = 0; i < 1000; i++) {
        final int j = i;
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Exchange exc = new Exchange(null);
                    exc.setRequest(new Request.Builder().header("Test", "Test").body("Hello").build());
                    exc.setResponse(new Response.ResponseBuilder().header("Test", "Test").body("Hello back").build());
                    exc.setProperty(Exchange.API_KEY, "junit-" + j);
                    exc.setRule(new ServiceProxy());
                    exc.getRule().setName("junit API");
                    for (int k = 0; k < 10; k++) {
                        amSc.addExchangeToQueue(exc);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        threads.add(t);
        t.start();
    }
    for (Thread t : threads) {
        t.join();
    }
    Thread.sleep(amSc.getCollectTimeInSeconds() * 1000 * 2);
    amSc.shutdown();
}
Also used : ArrayList(java.util.ArrayList) Request(com.predic8.membrane.core.http.Request) AMStatisticsCollector(com.predic8.membrane.core.interceptor.apimanagement.statistics.AMStatisticsCollector) Exchange(com.predic8.membrane.core.exchange.Exchange) Response(com.predic8.membrane.core.http.Response) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) Test(org.junit.Test)

Example 48 with ServiceProxy

use of com.predic8.membrane.core.rules.ServiceProxy in project service-proxy by membrane.

the class LoadBalancingWithClusterManagerTest method startLB.

private void startLB() throws Exception {
    LoadBalancingInterceptor lbi = new LoadBalancingInterceptor();
    lbi.setName("Default");
    XMLElementSessionIdExtractor extractor = new XMLElementSessionIdExtractor();
    extractor.setLocalName("session");
    extractor.setNamespace("http://predic8.com/session/");
    lbi.setSessionIdExtractor(extractor);
    ServiceProxy lbiRule = new ServiceProxy(new ServiceProxyKey("localhost", "*", ".*", 3017), "thomas-bayer.com", 80);
    lbiRule.getInterceptors().add(lbi);
    ClusterNotificationInterceptor cni = new ClusterNotificationInterceptor();
    ServiceProxy cniRule = new ServiceProxy(new ServiceProxyKey("localhost", "*", ".*", 3012), "thomas-bayer.com", 80);
    cniRule.getInterceptors().add(cni);
    lb = new HttpRouter();
    lb.getRuleManager().addProxyAndOpenPortIfNew(lbiRule);
    lb.getRuleManager().addProxyAndOpenPortIfNew(cniRule);
    lb.init();
}
Also used : ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) HttpRouter(com.predic8.membrane.core.HttpRouter)

Example 49 with ServiceProxy

use of com.predic8.membrane.core.rules.ServiceProxy in project service-proxy by membrane.

the class LoadBalancingWithClusterManagerTest method startNode.

private DummyWebServiceInterceptor startNode(HttpRouter node, int port) throws Exception {
    DummyWebServiceInterceptor service1 = new DummyWebServiceInterceptor();
    node.addUserFeatureInterceptor(service1);
    node.getRuleManager().addProxyAndOpenPortIfNew(new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", port), "thomas-bayer.com", 80));
    node.init();
    return service1;
}
Also used : ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) DummyWebServiceInterceptor(com.predic8.membrane.core.services.DummyWebServiceInterceptor)

Example 50 with ServiceProxy

use of com.predic8.membrane.core.rules.ServiceProxy in project service-proxy by membrane.

the class RegExReplaceInterceptorTest method testReplace.

@Test
public void testReplace() throws Exception {
    router = Router.init("src/test/resources/regex-monitor-beans.xml");
    Rule serverRule = new ServiceProxy(new ServiceProxyKey("localhost", "*", ".*", 3009), "www.predic8.de", 80);
    router.getRuleManager().addProxyAndOpenPortIfNew(serverRule);
    router.init();
    try {
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod("http://localhost:3009");
        method.setRequestHeader(Header.CONTENT_TYPE, MimeType.TEXT_XML_UTF8);
        method.setRequestHeader(Header.SOAP_ACTION, "");
        assertEquals(200, client.executeMethod(method));
        assertTrue(new String(method.getResponseBody()).contains("Membrane RegEx Replacement Is Cool"));
    } finally {
        router.shutdown();
    }
}
Also used : ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Rule(com.predic8.membrane.core.rules.Rule) Test(org.junit.Test)

Aggregations

ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)47 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)34 HttpRouter (com.predic8.membrane.core.HttpRouter)25 Before (org.junit.Before)19 Rule (com.predic8.membrane.core.rules.Rule)17 Exchange (com.predic8.membrane.core.exchange.Exchange)16 Outcome (com.predic8.membrane.core.interceptor.Outcome)14 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)12 IOException (java.io.IOException)9 Test (org.junit.Test)9 LoadBalancingInterceptor (com.predic8.membrane.core.interceptor.balancer.LoadBalancingInterceptor)5 ArrayList (java.util.ArrayList)4 Router (com.predic8.membrane.core.Router)3 Mapping (com.predic8.membrane.core.interceptor.rewrite.RewriteInterceptor.Mapping)3 AbstractServiceProxy (com.predic8.membrane.core.rules.AbstractServiceProxy)3 ProxyRule (com.predic8.membrane.core.rules.ProxyRule)3 HttpClientConfiguration (com.predic8.membrane.core.transport.http.client.HttpClientConfiguration)3 URISyntaxException (java.net.URISyntaxException)3 Interceptor (com.predic8.membrane.core.interceptor.Interceptor)2 MockInterceptor (com.predic8.membrane.core.interceptor.MockInterceptor)2