Search in sources :

Example 16 with Outcome

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

the class EchoInterceptor method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
    Outcome outcome = exc.echo();
    exc.getResponse().getHeader().removeFields(Header.CONTENT_LENGTH);
    String body = exc.getRequest().getUri() + "\n" + new String(exc.getRequest().getBody().getContent(), Constants.UTF_8_CHARSET);
    exc.getResponse().setBodyContent(body.getBytes(Constants.UTF_8_CHARSET));
    return outcome;
}
Also used : Outcome(com.predic8.membrane.core.interceptor.Outcome)

Example 17 with Outcome

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

the class LoadBalancingInterceptorTest method setUp.

@Before
public void setUp() throws Exception {
    service1 = new HttpRouter();
    mockInterceptor1 = new DummyWebServiceInterceptor();
    ServiceProxy sp1 = new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", 2000), "thomas-bayer.com", 80);
    sp1.getInterceptors().add(new AbstractInterceptor() {

        @Override
        public Outcome handleResponse(Exchange exc) throws Exception {
            exc.getResponse().getHeader().add("Connection", "close");
            return Outcome.CONTINUE;
        }
    });
    sp1.getInterceptors().add(mockInterceptor1);
    service1.getRuleManager().addProxyAndOpenPortIfNew(sp1);
    service1.init();
    service2 = new HttpRouter();
    mockInterceptor2 = new DummyWebServiceInterceptor();
    ServiceProxy sp2 = new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", 3000), "thomas-bayer.com", 80);
    sp2.getInterceptors().add(new AbstractInterceptor() {

        @Override
        public Outcome handleResponse(Exchange exc) throws Exception {
            exc.getResponse().getHeader().add("Connection", "close");
            return Outcome.CONTINUE;
        }
    });
    sp2.getInterceptors().add(mockInterceptor2);
    service2.getRuleManager().addProxyAndOpenPortIfNew(sp2);
    service2.init();
    balancer = new HttpRouter();
    ServiceProxy sp3 = new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", 7000), "thomas-bayer.com", 80);
    balancingInterceptor = new LoadBalancingInterceptor();
    balancingInterceptor.setName("Default");
    sp3.getInterceptors().add(balancingInterceptor);
    balancer.getRuleManager().addProxyAndOpenPortIfNew(sp3);
    enableFailOverOn5XX(balancer);
    balancer.init();
    BalancerUtil.lookupBalancer(balancer, "Default").up("Default", "localhost", 2000);
    BalancerUtil.lookupBalancer(balancer, "Default").up("Default", "localhost", 3000);
    roundRobinStrategy = new RoundRobinStrategy();
    byThreadStrategy = new ByThreadStrategy();
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) LoadBalancingInterceptor(com.predic8.membrane.core.interceptor.balancer.LoadBalancingInterceptor) ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) ByThreadStrategy(com.predic8.membrane.core.interceptor.balancer.ByThreadStrategy) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) HttpRouter(com.predic8.membrane.core.HttpRouter) RoundRobinStrategy(com.predic8.membrane.core.interceptor.balancer.RoundRobinStrategy) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) DummyWebServiceInterceptor(com.predic8.membrane.core.services.DummyWebServiceInterceptor) Before(org.junit.Before)

Example 18 with Outcome

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

the class MyInterceptor method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
    System.out.println("MyInterceptor invoked.");
    System.out.println("Request headers:");
    for (HeaderField headerField : exc.getRequest().getHeader().getAllHeaderFields()) System.out.print(headerField.getHeaderName() + ": " + headerField.getValue());
    return Outcome.CONTINUE;
}
Also used : HeaderField(com.predic8.membrane.core.http.HeaderField)

Example 19 with Outcome

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

the class Exchange method echo.

public Outcome echo() throws IOException {
    ResponseBuilder builder = Response.ok();
    byte[] content = getRequest().getBody().getContent();
    builder.body(content);
    String contentType = getRequest().getHeader().getContentType();
    if (contentType != null)
        builder.header(Header.CONTENT_TYPE, contentType);
    String contentEncoding = getRequest().getHeader().getContentEncoding();
    if (contentEncoding != null)
        builder.header(Header.CONTENT_ENCODING, contentEncoding);
    setResponse(builder.build());
    return Outcome.RETURN;
}
Also used : ResponseBuilder(com.predic8.membrane.core.http.Response.ResponseBuilder)

Example 20 with Outcome

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

the class DynamicAdminPageInterceptor method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
    log.debug("request: " + exc.getOriginalRequestUri());
    exc.setTimeReqSent(System.currentTimeMillis());
    Outcome o = dispatchRequest(exc);
    exc.setReceived();
    exc.setTimeResReceived(System.currentTimeMillis());
    return o;
}
Also used : Outcome(com.predic8.membrane.core.interceptor.Outcome)

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