Search in sources :

Example 6 with AbstractServiceProxy

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

the class AdminPageBuilder method createServiceProxyVisualization.

public void createServiceProxyVisualization(AbstractServiceProxy proxy, String relativeRootPath) {
    List<Interceptor> leftStack = new ArrayList<Interceptor>(), rightStack = new ArrayList<Interceptor>();
    List<Interceptor> list = new ArrayList<Interceptor>(proxy.getInterceptors());
    list.add(new // fake interceptor so that both stacks end with the same size
    AbstractInterceptor() {

        @Override
        public EnumSet<Flow> getFlow() {
            return Flow.Set.REQUEST_RESPONSE;
        }
    });
    // build left and right stacks
    for (Interceptor i : list) {
        EnumSet<Flow> f = i.getFlow();
        if (i instanceof ResponseInterceptor) {
            for (Interceptor i2 : ((ResponseInterceptor) i).getInterceptors()) rightStack.add(i2);
        } else if (i instanceof RequestInterceptor) {
            for (Interceptor i3 : ((RequestInterceptor) i).getInterceptors()) leftStack.add(i3);
        } else if (f.contains(Flow.REQUEST)) {
            if (f.contains(Flow.RESPONSE)) {
                // fill left and right to same height
                while (leftStack.size() < rightStack.size()) leftStack.add(null);
                while (rightStack.size() < leftStack.size()) rightStack.add(null);
                // put i into both
                leftStack.add(i);
                rightStack.add(i);
            } else {
                leftStack.add(i);
            }
        } else {
            if (f.contains(Flow.RESPONSE)) {
                rightStack.add(i);
            }
        }
    }
    boolean noTarget = proxy.getTargetURL() == null && proxy.getTargetHost() == null;
    table().cellspacing("0px").cellpadding("0px").classAttr("spv").style("width:662px");
    createListenerRow(proxy);
    if (leftStack.size() > 1 || !noTarget) {
        createBeginArrowsRow();
        for (int i = 0; i < leftStack.size() - 1 - (noTarget ? 1 : 0); i++) {
            tr().style("background:url(\"" + relativeRootPath + "/admin/images/spv-middle.png\");background-repeat:repeat-y;display:inline-table");
            createInterceptorRow(leftStack, rightStack, i, false);
            end();
        }
        createEndArrowsRow();
        if (noTarget) {
            tr();
            createInterceptorRow(leftStack, rightStack, leftStack.size() - 2, true);
            end();
        } else {
            createTargetRow(proxy);
        }
    }
    end();
}
Also used : RequestInterceptor(com.predic8.membrane.core.interceptor.flow.RequestInterceptor) LoadBalancingInterceptor(com.predic8.membrane.core.interceptor.balancer.LoadBalancingInterceptor) Interceptor(com.predic8.membrane.core.interceptor.Interceptor) ResponseInterceptor(com.predic8.membrane.core.interceptor.flow.ResponseInterceptor) RequestInterceptor(com.predic8.membrane.core.interceptor.flow.RequestInterceptor) AbstractInterceptor(com.predic8.membrane.core.interceptor.AbstractInterceptor) Flow(com.predic8.membrane.core.interceptor.Interceptor.Flow) ResponseInterceptor(com.predic8.membrane.core.interceptor.flow.ResponseInterceptor)

Example 7 with AbstractServiceProxy

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

the class AdminRESTInterceptor method getProxies.

@Mapping("/admin/rest/proxies(/?\\?.*)?")
public Response getProxies(final QueryParameter params, String relativeRootPath) throws Exception {
    final List<AbstractServiceProxy> proxies = getServiceProxies();
    if ("order".equals(params.getString("sort"))) {
        if (params.getString("order", "asc").equals("desc"))
            Collections.reverse(proxies);
    } else {
        Collections.sort(proxies, ComparatorFactory.getAbstractServiceProxyComparator(params.getString("sort", "name"), params.getString("order", "asc")));
    }
    final int offset = params.getInt("offset", 0);
    int max = params.getInt("max", proxies.size());
    final List<AbstractServiceProxy> paginated = proxies.subList(offset, Math.min(offset + max, proxies.size()));
    return json(new JSONContent() {

        public void write(JsonGenerator gen) throws Exception {
            gen.writeStartObject();
            gen.writeArrayFieldStart("proxies");
            int i = offset;
            if (params.getString("order", "asc").equals("desc"))
                i = proxies.size() - i + 1;
            for (AbstractServiceProxy p : paginated) {
                gen.writeStartObject();
                gen.writeNumberField("order", i += params.getString("order", "asc").equals("desc") ? -1 : 1);
                gen.writeStringField("name", p.toString());
                gen.writeBooleanField("active", p.isActive());
                if (!p.isActive())
                    gen.writeStringField("error", p.getErrorState());
                gen.writeNumberField("listenPort", p.getKey().getPort());
                gen.writeStringField("virtualHost", p.getKey().getHost());
                gen.writeStringField("method", p.getKey().getMethod());
                gen.writeStringField("path", p.getKey().getPath());
                gen.writeStringField("targetHost", p.getTargetHost());
                gen.writeNumberField("targetPort", p.getTargetPort());
                gen.writeNumberField("count", p.getCount());
                gen.writeObjectFieldStart("actions");
                if (!isReadOnly()) {
                    gen.writeStringField("delete", "/admin/service-proxy/delete?name=" + URLEncoder.encode(RuleUtil.getRuleIdentifier(p), "UTF-8"));
                }
                if (!p.isActive())
                    gen.writeStringField("start", "/admin/service-proxy/start?name=" + URLEncoder.encode(RuleUtil.getRuleIdentifier(p), "UTF-8"));
                gen.writeEndObject();
                gen.writeEndObject();
            }
            gen.writeEndArray();
            gen.writeNumberField("total", proxies.size());
            gen.writeEndObject();
        }
    });
}
Also used : AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) JSONContent(com.predic8.membrane.core.interceptor.rest.JSONContent) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) SQLException(java.sql.SQLException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) IOException(java.io.IOException)

Example 8 with AbstractServiceProxy

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

the class RuleMatchingInterceptor method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
    if (exc.getRule() != null)
        return Outcome.CONTINUE;
    Rule rule = getRule(exc);
    exc.setRule(rule);
    if (exc.getRule().getSslOutboundContext() != null) {
        exc.setProperty(Exchange.SSL_CONTEXT, exc.getRule().getSslOutboundContext());
    }
    if (rule instanceof NullRule) {
        handleNoRuleFound(exc);
        return Outcome.ABORT;
    }
    if (xForwardedForEnabled && (rule instanceof AbstractServiceProxy))
        insertXForwardedFor(exc);
    return Outcome.CONTINUE;
}
Also used : AbstractServiceProxy(com.predic8.membrane.core.rules.AbstractServiceProxy) NullRule(com.predic8.membrane.core.rules.NullRule) Rule(com.predic8.membrane.core.rules.Rule) NullRule(com.predic8.membrane.core.rules.NullRule) ProxyRule(com.predic8.membrane.core.rules.ProxyRule)

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