Search in sources :

Example 1 with AbstractServiceProxy

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

the class DispatchingInterceptor method getForwardingDestination.

public static String getForwardingDestination(Exchange exc) throws Exception {
    AbstractServiceProxy p = (AbstractServiceProxy) exc.getRule();
    if (p.getTargetURL() != null) {
        log.debug("destination: " + p.getTargetURL());
        return p.getTargetURL();
    }
    if (p.getTargetHost() != null) {
        String url = new URL(p.getTargetScheme(), p.getTargetHost(), p.getTargetPort(), exc.getRequest().getUri()).toString();
        log.debug("destination: " + url);
        return url;
    }
    return exc.getRequest().getUri();
}
Also used : AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) URL(java.net.URL)

Example 2 with AbstractServiceProxy

use of com.predic8.membrane.core.rules.AbstractServiceProxy 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 AbstractServiceProxy

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

the class DynamicAdminPageInterceptor method handleServiceProxyShowRequest.

@Mapping("/admin/service-proxy/show/?(\\?.*)?")
public Response handleServiceProxyShowRequest(final Map<String, String> params, final String relativeRootPath) throws Exception {
    final StringWriter writer = new StringWriter();
    final AbstractServiceProxy rule = (AbstractServiceProxy) RuleUtil.findRuleByIdentifier(router, params.get("name"));
    return respond(new AdminPageBuilder(writer, router, relativeRootPath, params, readOnly) {

        @Override
        protected int getSelectedTab() {
            return TAB_ID_SERVICE_PROXIES;
        }

        @Override
        protected String getTitle() {
            return super.getTitle() + " " + rule.toString() + " ServiceProxy";
        }

        @Override
        protected void createTabContent() throws Exception {
            h1().text(rule.toString() + " ServiceProxy").end();
            script().raw("$(function() {\r\n" + "					$( \"#subtab\" ).tabs();\r\n" + "				});").end();
            div().id("subtab");
            ul();
            li().a().href("#tab1").text("Visualization").end(2);
            li().a().href("#tab2").text("Statistics").end(2);
            // li().a().href("#tab3").text("XML Configuration").end(2);
            end();
            div().id("tab1");
            createServiceProxyVisualization(rule, relativeRootPath);
            end();
            div().id("tab2");
            createStatusCodesTable(rule.getStatisticsByStatusCodes());
            br();
            createButton("View Messages", "calls", null, createQueryString("proxy", rule.toString()));
            end();
            end();
        }
    }.createPage());
}
Also used : AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) StringWriter(java.io.StringWriter)

Example 4 with AbstractServiceProxy

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

the class EtcdRegistryApiConfig method findAdminConsole.

private EtcdNodeInformation findAdminConsole() {
    Object routerObj = context.getBean(Router.class);
    if (routerObj == null)
        throw new RuntimeException("Router not found, cannot publish admin console");
    Router router = (Router) routerObj;
    for (Rule r : router.getRuleManager().getRules()) {
        if (!(r instanceof AbstractServiceProxy))
            continue;
        for (Interceptor i : r.getInterceptors()) {
            if (i instanceof AdminConsoleInterceptor) {
                String name = r.getName();
                String host = ((ServiceProxy) r).getExternalHostname();
                if (host == null)
                    host = getLocalHostname();
                String port = Integer.toString(((AbstractServiceProxy) r).getPort());
                EtcdNodeInformation node = new EtcdNodeInformation(null, null, host, port, name);
                return node;
            }
        }
    }
    throw new RuntimeException("Admin console not found but is needed. Add a service proxy with an admin console.");
}
Also used : AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) AdminConsoleInterceptor(com.predic8.membrane.core.interceptor.administration.AdminConsoleInterceptor) ServiceProxy(com.predic8.membrane.core.rules.ServiceProxy) AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) EtcdNodeInformation(com.predic8.membrane.core.cloud.etcd.EtcdNodeInformation) Router(com.predic8.membrane.core.Router) Rule(com.predic8.membrane.core.rules.Rule) Interceptor(com.predic8.membrane.core.interceptor.Interceptor) AdminConsoleInterceptor(com.predic8.membrane.core.interceptor.administration.AdminConsoleInterceptor)

Example 5 with AbstractServiceProxy

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

the class TestServiceInterceptor method init.

@Override
public void init(final Router router) throws Exception {
    super.init(router);
    wi.init(router);
    Rule r = router.getParentProxy(this);
    if (r instanceof AbstractServiceProxy) {
        final Path path = ((AbstractServiceProxy) r).getPath();
        if (path != null) {
            if (path.isRegExp())
                throw new Exception("<testService> may not be used together with <path isRegExp=\"true\">.");
            final String keyPath = path.getValue();
            final String name = URLUtil.getName(router.getUriFactory(), keyPath);
            wi.setPathRewriter(new PathRewriter() {

                @Override
                public String rewrite(String path2) {
                    try {
                        if (path2.contains("://")) {
                            path2 = new URL(new URL(path2), keyPath).toString();
                        } else {
                            Matcher m = RELATIVE_PATH_PATTERN.matcher(path2);
                            path2 = m.replaceAll("./" + name + "?");
                        }
                    } catch (MalformedURLException e) {
                    }
                    return path2;
                }
            });
        }
    }
}
Also used : AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) Path(com.predic8.membrane.core.config.Path) MalformedURLException(java.net.MalformedURLException) PathRewriter(com.predic8.membrane.core.ws.relocator.Relocator.PathRewriter) Matcher(java.util.regex.Matcher) Rule(com.predic8.membrane.core.rules.Rule) MalformedURLException(java.net.MalformedURLException) SAXParseException(org.xml.sax.SAXParseException) URL(java.net.URL)

Aggregations

AbstractServiceProxy (com.predic8.membrane.core.rules.AbstractServiceProxy)6 Rule (com.predic8.membrane.core.rules.Rule)3 Interceptor (com.predic8.membrane.core.interceptor.Interceptor)2 URL (java.net.URL)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 Router (com.predic8.membrane.core.Router)1 EtcdNodeInformation (com.predic8.membrane.core.cloud.etcd.EtcdNodeInformation)1 Path (com.predic8.membrane.core.config.Path)1 AbstractInterceptor (com.predic8.membrane.core.interceptor.AbstractInterceptor)1 Flow (com.predic8.membrane.core.interceptor.Interceptor.Flow)1 AdminConsoleInterceptor (com.predic8.membrane.core.interceptor.administration.AdminConsoleInterceptor)1 LoadBalancingInterceptor (com.predic8.membrane.core.interceptor.balancer.LoadBalancingInterceptor)1 RequestInterceptor (com.predic8.membrane.core.interceptor.flow.RequestInterceptor)1 ResponseInterceptor (com.predic8.membrane.core.interceptor.flow.ResponseInterceptor)1 JSONContent (com.predic8.membrane.core.interceptor.rest.JSONContent)1 NullRule (com.predic8.membrane.core.rules.NullRule)1 ProxyRule (com.predic8.membrane.core.rules.ProxyRule)1 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)1 ServiceProxyKey (com.predic8.membrane.core.rules.ServiceProxyKey)1