Search in sources :

Example 1 with Port

use of com.predic8.wsdl.Port in project service-proxy by membrane.

the class ElasticSearchExchangeStore method getExchanges.

@Override
public AbstractExchange[] getExchanges(RuleKey ruleKey) {
    int port = ruleKey.getPort();
    try {
        Exchange exc = new Request.Builder().post(getElasticSearchExchangesPath() + "_search").body("{\n" + "  \"query\": {\n" + "    \"bool\": {\n" + "      \"must\": [\n" + "        {\n" + "          \"wildcard\": {\n" + "            \"issuer\": \"" + documentPrefix + "\"\n" + "          }\n" + "        },\n" + "        {\n" + "          \"match\": {\n" + "            \"rule.port\": \"" + port + "\"\n" + "          }\n" + "        }\n" + "      ]\n" + "    }\n" + "  }\n" + "}").header("Content-Type", "application/json").buildExchange();
        exc = client.call(exc);
        List source = getSourceElementFromElasticSearchResponse(responseToMap(exc));
        AbstractExchangeSnapshot[] snapshots = mapper.readValue(mapper.writeValueAsString(source), AbstractExchangeSnapshot[].class);
        return Stream.of(snapshots).map(snapshot -> snapshot.toAbstractExchange()).collect(Collectors.toList()).toArray(new AbstractExchange[0]);
    } catch (Exception e) {
        e.printStackTrace();
        return new AbstractExchange[0];
    }
}
Also used : AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) Exchange(com.predic8.membrane.core.exchange.Exchange) CacheBuilder(com.google.common.cache.CacheBuilder) DynamicAbstractExchangeSnapshot(com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot) AbstractExchangeSnapshot(com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 2 with Port

use of com.predic8.wsdl.Port in project service-proxy by membrane.

the class IndexInterceptor method getServiceInfo.

private ServiceInfo getServiceInfo(Exchange exc, AbstractServiceProxy sp) {
    if (sp.getInterceptors().size() == 1 && sp.getInterceptors().get(0) instanceof IndexInterceptor)
        return null;
    ServiceProxyKey k = (ServiceProxyKey) sp.getKey();
    ServiceInfo ri = new ServiceInfo();
    ri.method = k.getMethod();
    // NOTE: when running as servlet, we have no idea what the protocol was
    ri.ssl = sp.getSslInboundContext() != null;
    String protocol = ri.ssl ? "https" : "http";
    String host = k.isHostWildcard() ? new HostColonPort(ri.ssl, exc.getRequest().getHeader().getHost()).host : fullfillRegexp(ServiceProxyKey.createHostPattern(k.getHost()));
    if (host == null || host.length() == 0)
        host = exc.getHandler().getLocalAddress().getHostAddress().toString();
    int port = k.getPort();
    if (port == -1 || !exc.getHandler().isMatchLocalPort())
        port = exc.getHandler().getLocalPort();
    String path;
    if (!k.isUsePathPattern()) {
        path = "/";
    } else if (k.isPathRegExp()) {
        path = fullfillRegexp(k.getPath());
    } else {
        path = "/" + StringUtils.removeStart(k.getPath(), "/");
    }
    if (!"".equals(exc.getHandler().getContextPath(exc))) {
        path = StringUtils.removeEnd(exc.getHandler().getContextPath(exc), "/") + "/" + StringUtils.removeStart(path, "/");
    }
    ri.name = sp.getName();
    if (path != null)
        ri.url = protocol + "://" + host + ":" + port + path;
    ri.host = k.isHostWildcard() ? "" : StringEscapeUtils.escapeHtml(k.getHost());
    ri.port = k.getPort() == -1 ? "" : "" + k.getPort();
    ri.path = k.isUsePathPattern() ? "<tt>" + StringEscapeUtils.escapeHtml(k.getPath()) + "</tt>" + (k.isPathRegExp() ? " (regex)" : "") : "";
    return ri;
}
Also used : ServiceProxyKey(com.predic8.membrane.core.rules.ServiceProxyKey) HostColonPort(com.predic8.membrane.core.transport.http.HostColonPort)

Example 3 with Port

use of com.predic8.wsdl.Port in project service-proxy by membrane.

the class STOMPClient method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
    String login = exc.getRequest().getHeader().getFirstValue("login");
    String passcode = exc.getRequest().getHeader().getFirstValue("passcode");
    String host = exc.getRequest().getHeader().getFirstValue("host");
    String acceptVersion = exc.getRequest().getHeader().getFirstValue("accept-version");
    boolean isStomp1_0 = login != null && passcode != null;
    boolean isStomp1_1orAbove = host != null && acceptVersion != null;
    if (isStomp1_0 || isStomp1_1orAbove) {
        Connection c = connectionManager.getConnection(this.host, port, connectionConfiguration.getLocalAddr(), sslOutboundProvider, connectionConfiguration.getTimeout());
        exc.getRequest().writeSTOMP(c.out);
        HttpClient.setupConnectionForwarding(exc, c, "STOMP", getRouter().getStatistics().getStreamPumpStats());
    } else {
        exc.setResponse(Response.badRequest().build());
    }
    return Outcome.RETURN;
}
Also used : Connection(com.predic8.membrane.core.transport.http.Connection)

Example 4 with Port

use of com.predic8.wsdl.Port in project service-proxy by membrane.

the class LoadBalancerMultiple4Test method test.

@Test
public void test() throws IOException, InterruptedException {
    File base = getExampleDir("loadbalancer-multiple-4");
    AssertUtils.replaceInFile(new File(base, "proxies.xml"), "8080", "3023");
    AssertUtils.replaceInFile(new File(base, "proxies.xml"), "8081", "3024");
    Process2 sl = new Process2.Builder().in(base).script("service-proxy").waitForMembrane().start();
    try {
        assertEquals(1, LoadBalancerUtil.getRespondingNode("http://localhost:3023/service"));
        assertEquals(2, LoadBalancerUtil.getRespondingNode("http://localhost:3023/service"));
        assertEquals(1, LoadBalancerUtil.getRespondingNode("http://localhost:3023/service"));
        assertEquals(2, LoadBalancerUtil.getRespondingNode("http://localhost:3023/service"));
        assertEquals(3, LoadBalancerUtil.getRespondingNode("http://localhost:3024/service"));
        assertEquals(4, LoadBalancerUtil.getRespondingNode("http://localhost:3024/service"));
        assertEquals(3, LoadBalancerUtil.getRespondingNode("http://localhost:3024/service"));
        assertEquals(4, LoadBalancerUtil.getRespondingNode("http://localhost:3024/service"));
        String status = getAndAssert200("http://localhost:9000/admin/clusters/show?balancer=balancer1&cluster=Default");
        assertNodeStatus(status, "localhost", 4000, "UP");
        assertNodeStatus(status, "localhost", 4001, "UP");
        getAndAssert(204, "http://localhost:9010/clustermanager/down?balancer=balancer1&host=localhost&port=4001");
        Thread.sleep(1000);
        status = getAndAssert200("http://localhost:9000/admin/clusters/show?balancer=balancer1&cluster=Default");
        assertNodeStatus(status, "localhost", 4000, "UP");
        assertNodeStatus(status, "localhost", 4001, "DOWN");
        assertEquals(1, LoadBalancerUtil.getRespondingNode("http://localhost:3023/service"));
        assertEquals(1, LoadBalancerUtil.getRespondingNode("http://localhost:3023/service"));
    } finally {
        sl.killScript();
    }
}
Also used : Process2(com.predic8.membrane.examples.Process2) File(java.io.File) Test(org.junit.Test)

Example 5 with Port

use of com.predic8.wsdl.Port in project service-proxy by membrane.

the class JSONSchemaValidationTest method test.

@Test
public void test() throws IOException, InterruptedException {
    File baseDir = getExampleDir("validation" + File.separator + "json-schema");
    Process2 sl = new Process2.Builder().in(baseDir).script("service-proxy").waitForMembrane().start();
    try {
        for (int port : new int[] { 2000, 2001 }) {
            postAndAssert(200, "http://localhost:" + port + "/", readFileToString(new File(baseDir, "good" + port + ".json")));
            postAndAssert(400, "http://localhost:" + port + "/", readFileToString(new File(baseDir, "bad" + port + ".json")));
        }
    } finally {
        sl.killScript();
    }
}
Also used : Process2(com.predic8.membrane.examples.Process2) File(java.io.File) Test(org.junit.Test)

Aggregations

ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)9 Test (org.junit.Test)7 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)6 StringWriter (java.io.StringWriter)6 Exchange (com.predic8.membrane.core.exchange.Exchange)5 Rule (com.predic8.membrane.core.rules.Rule)5 Process2 (com.predic8.membrane.examples.Process2)5 File (java.io.File)5 ProxyRule (com.predic8.membrane.core.rules.ProxyRule)4 HttpRouter (com.predic8.membrane.core.HttpRouter)3 Request (com.predic8.membrane.core.http.Request)3 Response (com.predic8.membrane.core.http.Response)3 LoadBalancingInterceptor (com.predic8.membrane.core.interceptor.balancer.LoadBalancingInterceptor)3 Node (com.predic8.membrane.core.interceptor.balancer.Node)3 Definitions (com.predic8.wsdl.Definitions)3 Port (com.predic8.wsdl.Port)3 ArrayList (java.util.ArrayList)3 EtcdNodeInformation (com.predic8.membrane.core.cloud.etcd.EtcdNodeInformation)2 IPortChangeListener (com.predic8.membrane.core.model.IPortChangeListener)2 AbstractServiceProxy (com.predic8.membrane.core.rules.AbstractServiceProxy)2