Search in sources :

Example 11 with AbstractInterceptor

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

the class ExceptionHandlingTest method setUp.

@Before
public void setUp() throws Exception {
    router = new HttpRouter();
    router.getTransport().setPrintStackTrace(printStackTrace);
    ServiceProxy sp2 = new ServiceProxy(new ServiceProxyKey("*", "*", ".*", getPort()), "", -1);
    sp2.getInterceptors().add(new AbstractInterceptor() {

        @Override
        public Outcome handleRequest(Exchange exc) throws Exception {
            throw new Exception("secret");
        }
    });
    router.getRuleManager().addProxyAndOpenPortIfNew(sp2);
    router.init();
}
Also used : 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) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) HttpRouter(com.predic8.membrane.core.HttpRouter) ParseException(org.apache.http.ParseException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Before(org.junit.Before)

Example 12 with AbstractInterceptor

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

the class BoundConnectionTest method setUp.

@Before
public void setUp() throws Exception {
    router = new HttpRouter();
    ServiceProxy sp1 = new ServiceProxy(new ServiceProxyKey("*", "*", ".*", 3021), "localhost", 3022);
    router.getRuleManager().addProxyAndOpenPortIfNew(sp1);
    ServiceProxy sp2 = new ServiceProxy(new ServiceProxyKey("*", "*", ".*", 3022), "", -1);
    sp2.getInterceptors().add(new AbstractInterceptor() {

        @Override
        public Outcome handleRequest(Exchange exc) throws Exception {
            exc.getRequest().readBody();
            exc.setResponse(Response.ok("OK.").build());
            connectionHash = ((HttpServerHandler) exc.getHandler()).getSrcOut().hashCode();
            return Outcome.RETURN;
        }
    });
    router.getRuleManager().addProxyAndOpenPortIfNew(sp2);
    router.init();
}
Also used : 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) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) HttpRouter(com.predic8.membrane.core.HttpRouter) IOException(java.io.IOException) EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) Before(org.junit.Before)

Example 13 with AbstractInterceptor

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

the class HttpKeepAliveTest method testConnectionClose.

@Test
public void testConnectionClose() throws Exception {
    HttpClient client = createHttpClient(500);
    sp1.getInterceptors().add(0, new AbstractInterceptor() {

        @Override
        public Outcome handleResponse(Exchange exc) throws Exception {
            exc.getResponse().getHeader().add(Header.KEEP_ALIVE, "max=2");
            return Outcome.CONTINUE;
        }
    });
    // opens connection 1
    assertEquals(200, issueRequest(client));
    assertEquals(1, set.size());
    assertEquals(1, client.getConnectionManager().getNumberInPool());
    // connection closer did not yet run
    Thread.sleep(600);
    // connection 1 is now dead, but still in pool
    assertEquals(1, client.getConnectionManager().getNumberInPool());
    // opens connection 2
    assertEquals(200, issueRequest(client));
    assertEquals(2, set.size());
    // connection closer runs and closes both
    Thread.sleep(600);
    assertEquals(0, client.getConnectionManager().getNumberInPool());
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Test(org.junit.Test)

Example 14 with AbstractInterceptor

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

the class HttpKeepAliveTest method testTimeoutDefault.

@Test
public void testTimeoutDefault() throws Exception {
    HttpClient client = createHttpClient(1000);
    sp1.getInterceptors().add(0, new AbstractInterceptor() {

        @Override
        public Outcome handleResponse(Exchange exc) throws Exception {
            exc.getResponse().getHeader().add(Header.KEEP_ALIVE, "max=2");
            return Outcome.CONTINUE;
        }
    });
    assertEquals(200, issueRequest(client));
    assertEquals(1, set.size());
    Thread.sleep(1500);
    assertEquals(200, issueRequest(client));
    assertEquals(2, set.size());
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Outcome(com.predic8.membrane.core.interceptor.Outcome) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Test(org.junit.Test)

Example 15 with AbstractInterceptor

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

the class OAuth2ResourceTest method getMockAuthServiceProxy.

private ServiceProxy getMockAuthServiceProxy() throws IOException {
    ServiceProxy sp = new ServiceProxy(new ServiceProxyKey(serverPort), null, 99999);
    WellknownFile wkf = new WellknownFile();
    wkf.setIssuer(getServerAddress());
    wkf.setAuthorizationEndpoint(getServerAddress() + "/auth");
    wkf.setTokenEndpoint(getServerAddress() + "/token");
    wkf.setUserinfoEndpoint(getServerAddress() + "/userinfo");
    wkf.setRevocationEndpoint(getServerAddress() + "/revoke");
    wkf.setJwksUri(getServerAddress() + "/certs");
    wkf.setSupportedResponseTypes("code token");
    wkf.setSupportedSubjectType("public");
    wkf.setSupportedIdTokenSigningAlgValues("RS256");
    wkf.setSupportedScopes("openid email profile");
    wkf.setSupportedTokenEndpointAuthMethods("client_secret_post");
    wkf.setSupportedClaims("sub email username");
    wkf.init(new HttpRouter());
    sp.getInterceptors().add(new AbstractInterceptor() {

        SecureRandom rand = new SecureRandom();

        @Override
        public synchronized Outcome handleRequest(Exchange exc) throws Exception {
            if (exc.getRequestURI().endsWith("/.well-known/openid-configuration")) {
                exc.setResponse(Response.ok(wkf.getWellknown()).build());
            } else if (exc.getRequestURI().startsWith("/auth?")) {
                Map<String, String> params = URLParamUtil.getParams(new URIFactory(), exc);
                exc.setResponse(Response.redirect(getClientAddress() + "/oauth2callback?code=1234&state=" + params.get("state"), false).build());
            } else if (exc.getRequestURI().startsWith("/token")) {
                ObjectMapper om = new ObjectMapper();
                Map<String, String> res = new HashMap<>();
                res.put("access_token", new BigInteger(130, rand).toString(32));
                res.put("token_type", "bearer");
                res.put("expires_in", "1");
                res.put("refresh_token", new BigInteger(130, rand).toString(32));
                exc.setResponse(Response.ok(om.writeValueAsString(res)).contentType("application/json").build());
            } else if (exc.getRequestURI().startsWith("/userinfo")) {
                ObjectMapper om = new ObjectMapper();
                Map<String, String> res = new HashMap<>();
                res.put("username", "dummy");
                exc.setResponse(Response.ok(om.writeValueAsString(res)).contentType("application/json").build());
            }
            if (exc.getResponse() == null)
                exc.setResponse(Response.notFound().build());
            return Outcome.RETURN;
        }
    });
    return sp;
}
Also used : AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) SecureRandom(java.security.SecureRandom) IOException(java.io.IOException) 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) URIFactory(com.predic8.membrane.core.util.URIFactory) BigInteger(java.math.BigInteger) HttpRouter(com.predic8.membrane.core.HttpRouter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)17 Exchange (com.predic8.membrane.core.exchange.Exchange)16 Outcome (com.predic8.membrane.core.interceptor.Outcome)16 IOException (java.io.IOException)12 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)10 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)10 HttpRouter (com.predic8.membrane.core.HttpRouter)8 URISyntaxException (java.net.URISyntaxException)7 Before (org.junit.Before)6 Test (org.junit.Test)6 LoadBalancingInterceptor (com.predic8.membrane.core.interceptor.balancer.LoadBalancingInterceptor)2 HttpClientConfiguration (com.predic8.membrane.core.transport.http.client.HttpClientConfiguration)2 MalformedURLException (java.net.MalformedURLException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Router (com.predic8.membrane.core.Router)1 KeyStore (com.predic8.membrane.core.config.security.KeyStore)1 SSLParser (com.predic8.membrane.core.config.security.SSLParser)1 TrustStore (com.predic8.membrane.core.config.security.TrustStore)1 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)1 CountInterceptor (com.predic8.membrane.core.interceptor.CountInterceptor)1