Search in sources :

Example 41 with ServiceProxy

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

the class SwaggerRewriterInterceptor method handleResponse.

@Override
public Outcome handleResponse(Exchange exc) throws Exception {
    // replacement in swagger.json
    if (exc.getRequest().getUri().endsWith(swaggerJson) && exc.getResponseContentType().equalsIgnoreCase(MediaType.APPLICATION_JSON_VALUE)) {
        Swagger swagBody = new SwaggerParser().parse(exc.getResponse().getBodyAsStringDecoded());
        swagBody.setHost(exc2originalHostPort(exc));
        exc.getResponse().setBodyContent(Json.pretty(swagBody).getBytes(exc.getResponse().getCharset()));
    }
    // replacement in json and javascript (specifically UI)
    if (rewriteUI && (exc.getRequest().getUri().matches("/.*.js(on)?") || exc.getResponse().getHeader().getContentType() != null && exc.getResponse().getHeader().getContentType().equals(MediaType.TEXT_HTML_VALUE))) {
        String from = "(http(s)?://)" + Pattern.quote(((ServiceProxy) exc.getRule()).getTarget().getHost()) + "(/.*\\.js(on)?)";
        String to = "$1" + exc2originalHostPort(exc) + "$3";
        byte[] body = exc.getResponse().getBodyAsStringDecoded().replaceAll(from, to).getBytes(exc.getResponse().getCharset());
        exc.getResponse().setBodyContent(body);
    }
    return super.handleResponse(exc);
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) Swagger(io.swagger.models.Swagger)

Example 42 with ServiceProxy

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

the class AMRateLimitInterceptorTest method testHandleRequestRateLimit5SecondConcurrency.

@Test
public void testHandleRequestRateLimit5SecondConcurrency() throws Exception {
    final Exchange exc = new Exchange(null);
    exc.setResponse(Response.ResponseBuilder.newInstance().build());
    exc.setProperty(Exchange.API_KEY, "junit");
    exc.setRule(new ServiceProxy());
    exc.getRule().setName("junit API");
    final AMRateLimiter rli = new AMRateLimiter();
    ApiManagementConfiguration amc = new ApiManagementConfiguration(System.getProperty("user.dir"), "src\\test\\resources\\apimanagement\\api.yaml");
    rli.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 = rli.handleRequest(exc);
                    if (out == Outcome.CONTINUE) {
                        continues.incrementAndGet();
                    } else if (out == Outcome.RETURN) {
                        returns.incrementAndGet();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        threads.add(t);
        t.start();
    }
    for (Thread t : threads) {
        t.join();
    }
    assertEquals(5, continues.get());
    assertEquals(995, returns.get());
    Thread.sleep(2000);
    assertEquals(Outcome.CONTINUE, rli.handleRequest(exc));
}
Also used : AMRateLimiter(com.predic8.membrane.core.interceptor.apimanagement.rateLimiter.AMRateLimiter) ArrayList(java.util.ArrayList) 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 43 with ServiceProxy

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

the class BasicAuthenticationInterceptorIntegrationTest method setup.

@Before
public void setup() throws Exception {
    Rule rule = new ServiceProxy(new ServiceProxyKey("localhost", "*", ".*", 3001), "thomas-bayer.com", 80);
    router.getRuleManager().addProxyAndOpenPortIfNew(rule);
    BasicAuthenticationInterceptor interceptor = new BasicAuthenticationInterceptor();
    List<User> users = new ArrayList<User>();
    users.add(new User("admin", "admin"));
    interceptor.setUsers(users);
    router.addUserFeatureInterceptor(interceptor);
    router.init();
}
Also used : ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) User(com.predic8.membrane.core.interceptor.authentication.session.StaticUserDataProvider.User) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) ArrayList(java.util.ArrayList) Rule(com.predic8.membrane.core.rules.Rule) Before(org.junit.Before)

Example 44 with ServiceProxy

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

the class ClusterBalancerTest method setUp.

@Override
@Before
public void setUp() throws Exception {
    extracor = new XMLElementSessionIdExtractor();
    extracor.setLocalName("session");
    extracor.setNamespace("http://predic8.com/session/");
    r = new HttpRouter();
    lb = new LoadBalancingInterceptor();
    lb.setSessionIdExtractor(extracor);
    lb.setName("Default");
    ServiceProxy sp = new ServiceProxy(new ServiceProxyKey(3011), "predic8.com", 80);
    sp.getInterceptors().add(lb);
    r.getRuleManager().addProxyAndOpenPortIfNew(sp);
    r.init();
    BalancerUtil.up(r, "Default", "Default", "localhost", 2000);
    BalancerUtil.up(r, "Default", "Default", "localhost", 3000);
}
Also used : ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy)

Example 45 with ServiceProxy

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

the class ClusterNotificationInterceptorTest method setUp.

@Override
@Before
public void setUp() throws Exception {
    Rule rule = new ServiceProxy(new ServiceProxyKey("localhost", "*", ".*", 3002), "thomas-bayer.com", 80);
    router = new HttpRouter();
    router.getRuleManager().addProxyAndOpenPortIfNew(rule);
    interceptor = new ClusterNotificationInterceptor();
    router.addUserFeatureInterceptor(interceptor);
    lbi = new LoadBalancingInterceptor();
    lbi.setName("Default");
    Rule rule2 = new ServiceProxy(new ServiceProxyKey("localhost", "*", ".*", 3003), "thomas-bayer.com", 80);
    router.getRuleManager().addProxyAndOpenPortIfNew(rule2);
    rule2.getInterceptors().add(lbi);
    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)

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