Search in sources :

Example 1 with ServiceProxy

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

the class UserFeatureInterceptor method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
    Rule predecessorRule = exc.getRule();
    Outcome outcome = flowController.invokeRequestHandlers(exc, predecessorRule.getInterceptors());
    while (isTargetInternalAndContinue(exc, outcome)) {
        log.debug("routing to serviceProxy with name: " + getServiceProxyName(exc));
        // rule matching
        String destination = exc.getDestinations().get(0);
        Rule newRule = getRuleByDest(destination);
        if (newRule == null)
            throw new Exception("No proxy found for destination " + destination);
        exc.setRule(newRule);
        // dispatching
        exc.getDestinations().clear();
        exc.getDestinations().add(DispatchingInterceptor.getForwardingDestination(exc));
        // user feature
        outcome = flowController.invokeRequestHandlers(exc, newRule.getInterceptors());
    }
    exc.setRule(predecessorRule);
    return outcome;
}
Also used : Rule(com.predic8.membrane.core.rules.Rule)

Example 2 with ServiceProxy

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

the class SwaggerRewriterInterceptor method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
    ((ServiceProxy) exc.getRule()).setTargetHost(swagger.getHost());
    URL url = new URL(swaggerUrl);
    exc.getDestinations().set(0, url.getProtocol() + "://" + url.getHost() + (url.getPort() < 0 ? "" : ":" + url.getPort()) + exc.getOriginalRequestUri());
    return super.handleRequest(exc);
}
Also used : ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) URL(java.net.URL)

Example 3 with ServiceProxy

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

the class LargeBodyTest method setup.

public void setup() throws Exception {
    // streaming only works for maxRetries = 1
    hcc = new HttpClientConfiguration();
    hcc.setMaxRetries(1);
    Rule rule = new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", 3040), "thomas-bayer.com", 80);
    rule.getInterceptors().add(new AbstractInterceptor() {

        @Override
        public Outcome handleRequest(Exchange exc) throws Exception {
            exc.setResponse(Response.ok().body("").build());
            return Outcome.RETURN;
        }
    });
    router = new HttpRouter();
    ((HTTPClientInterceptor) router.getTransport().getInterceptors().get(3)).setHttpClientConfig(hcc);
    router.getRuleManager().addProxyAndOpenPortIfNew(rule);
    router.init();
    Rule rule1 = new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", 3041), "localhost", 3040);
    router2 = new HttpRouter();
    ((HTTPClientInterceptor) router2.getTransport().getInterceptors().get(3)).setHttpClientConfig(hcc);
    router2.getRuleManager().addProxyAndOpenPortIfNew(rule1);
    router2.init();
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) HTTPClientInterceptor(com.predic8.membrane.core.interceptor.HTTPClientInterceptor) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) Rule(com.predic8.membrane.core.rules.Rule) HttpRouter(com.predic8.membrane.core.HttpRouter) HttpClientConfiguration(com.predic8.membrane.core.transport.http.client.HttpClientConfiguration) IOException(java.io.IOException)

Example 4 with ServiceProxy

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

the class Http10Test method setUp.

@Before
public void setUp() throws Exception {
    Rule rule = new ServiceProxy(new ServiceProxyKey("localhost", "POST", ".*", 3000), "thomas-bayer.com", 80);
    router = new HttpRouter();
    router.getRuleManager().addProxyAndOpenPortIfNew(rule);
    router.init();
}
Also used : ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) Rule(com.predic8.membrane.core.rules.Rule) HttpRouter(com.predic8.membrane.core.HttpRouter) Before(org.junit.Before)

Example 5 with ServiceProxy

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

the class LoadBalancingInterceptorTest method testFailOverOnStatus500.

@Test
public void testFailOverOnStatus500() throws Exception {
    balancingInterceptor.setDispatchingStrategy(roundRobinStrategy);
    HttpClient client = new HttpClient();
    client.getParams().setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(0, mockInterceptor2.getCount());
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(1, mockInterceptor2.getCount());
    ((ServiceProxy) service1.getRuleManager().getRules().get(0)).getInterceptors().add(0, new AbstractInterceptor() {

        @Override
        public Outcome handleRequest(Exchange exc) throws Exception {
            exc.setResponse(Response.internalServerError().build());
            return Outcome.ABORT;
        }
    });
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(1, mockInterceptor1.getCount());
    assertEquals(2, mockInterceptor2.getCount());
    assertEquals(200, client.executeMethod(getPostMethod()));
    assertEquals(3, mockInterceptor2.getCount());
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Outcome(com.predic8.membrane.core.interceptor.Outcome) HttpClient(org.apache.commons.httpclient.HttpClient) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) Test(org.junit.Test) Http11Test(com.predic8.membrane.integration.Http11Test)

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