Search in sources :

Example 1 with RuleKey

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

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

the class OAuth2ResourceInterceptor method setPublicURL.

private void setPublicURL(Exchange exc) {
    String xForwardedProto = exc.getRequest().getHeader().getFirstValue(Header.X_FORWARDED_PROTO);
    boolean isHTTPS = xForwardedProto != null ? "https".equals(xForwardedProto) : exc.getRule().getSslInboundContext() != null;
    publicURL = (isHTTPS ? "https://" : "http://") + exc.getOriginalHostHeader();
    RuleKey key = exc.getRule().getKey();
    if (!key.isPathRegExp() && key.getPath() != null)
        publicURL += key.getPath();
    normalizePublicURL();
    initPublicURLOnFirstExchange = false;
}
Also used : RuleKey(com.predic8.membrane.core.rules.RuleKey)

Example 3 with RuleKey

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

the class RuleManager method getMatchingRule.

public Rule getMatchingRule(String hostHeader, String method, String uri, String version, int port, String localIP) {
    for (Rule rule : rules) {
        RuleKey key = rule.getKey();
        log.debug("Host from rule: " + key.getHost() + ";   Host from parameter rule key: " + hostHeader);
        if (!rule.isActive())
            continue;
        if (!key.matchesVersion(version))
            continue;
        if (key.getIp() != null && !key.getIp().equals(localIP))
            continue;
        if (!key.matchesHostHeader(hostHeader))
            continue;
        if (key.getPort() != -1 && port != -1 && key.getPort() != port)
            continue;
        if (!key.getMethod().equals(method) && !key.isMethodWildcard())
            continue;
        if (key.isUsePathPattern() && !key.matchesPath(uri))
            continue;
        if (!key.complexMatch(hostHeader, method, uri, version, port, localIP))
            continue;
        return rule;
    }
    return null;
}
Also used : RuleKey(com.predic8.membrane.core.rules.RuleKey) Rule(com.predic8.membrane.core.rules.Rule)

Example 4 with RuleKey

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

the class AbstractExchange method getPublicUrl.

public String getPublicUrl() {
    String xForwardedProto = getRequest().getHeader().getFirstValue(Header.X_FORWARDED_PROTO);
    boolean isHTTPS = xForwardedProto != null ? "https".equals(xForwardedProto) : getRule().getSslInboundContext() != null;
    String publicURL = (isHTTPS ? "https://" : "http://") + getRequest().getHeader().getHost().replaceFirst(".*:", "");
    RuleKey key = getRule().getKey();
    if (!key.isPathRegExp() && key.getPath() != null)
        publicURL += key.getPath();
    return publicURL;
}
Also used : RuleKey(com.predic8.membrane.core.rules.RuleKey)

Example 5 with RuleKey

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

the class ElasticSearchExchangeStore method getStatistics.

@Override
public StatisticCollector getStatistics(RuleKey ruleKey) {
    StatisticCollector statistics = new StatisticCollector(false);
    List<AbstractExchange> exchangesList = Arrays.asList(getExchanges(ruleKey));
    if (exchangesList == null || exchangesList.isEmpty())
        return statistics;
    for (int i = 0; i < exchangesList.size(); i++) statistics.collectFrom(exchangesList.get(i));
    return statistics;
}
Also used : AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) StatisticCollector(com.predic8.membrane.core.rules.StatisticCollector)

Aggregations

AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)4 RuleKey (com.predic8.membrane.core.rules.RuleKey)3 StatisticCollector (com.predic8.membrane.core.rules.StatisticCollector)3 Rule (com.predic8.membrane.core.rules.Rule)2 CacheBuilder (com.google.common.cache.CacheBuilder)1 Exchange (com.predic8.membrane.core.exchange.Exchange)1 AbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot)1 DynamicAbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot)1 Request (com.predic8.membrane.core.http.Request)1 NullRule (com.predic8.membrane.core.rules.NullRule)1 ProxyRule (com.predic8.membrane.core.rules.ProxyRule)1 AbstractHttpHandler (com.predic8.membrane.core.transport.http.AbstractHttpHandler)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1