Search in sources :

Example 1 with AbstractExchangeSnapshot

use of com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot 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 AbstractExchangeSnapshot

use of com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot in project service-proxy by membrane.

the class DynamicAbstractExchangeSnapshot method addObservers.

public static void addObservers(AbstractExchange exc, AbstractExchangeSnapshot excCopy, Consumer<AbstractExchangeSnapshot> callback) {
    MessageObserver obs = new MessageObserver() {

        @Override
        public void bodyRequested(AbstractBody body) {
        }

        @Override
        public void bodyComplete(AbstractBody body) {
            update(callback, excCopy, exc);
        }
    };
    exc.addExchangeViewerListener(new AbstractExchangeViewerListener() {

        @Override
        public void addResponse(Response response) {
            response.addObserver(obs);
        }

        @Override
        public void setExchangeFinished() {
            update(callback, excCopy, exc);
        }
    });
    Stream.of(exc.getRequest(), exc.getResponse()).forEach(msg -> {
        if (msg == null)
            return;
        if (msg.containsObserver(obs))
            return;
        msg.addObserver(obs);
    });
    update(callback, excCopy, exc);
}
Also used : Response(com.predic8.membrane.core.http.Response) MessageObserver(com.predic8.membrane.core.http.MessageObserver) AbstractBody(com.predic8.membrane.core.http.AbstractBody) AbstractExchangeViewerListener(com.predic8.membrane.core.model.AbstractExchangeViewerListener)

Example 3 with AbstractExchangeSnapshot

use of com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot in project service-proxy by membrane.

the class ElasticSearchExchangeStore method getFromElasticSearchById.

private AbstractExchangeSnapshot getFromElasticSearchById(int id) {
    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" + "            \"id\": \"" + id + "\"\n" + "          }\n" + "        }\n" + "      ]\n" + "    }\n" + "  }\n" + "}").header("Content-Type", "application/json").buildExchange();
        exc = client.call(exc);
        Map res = responseToMap(exc);
        Map excJson = getSourceElementFromElasticSearchResponse(res).get(0);
        return mapper.readValue(mapper.writeValueAsString(excJson), AbstractExchangeSnapshot.class);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) Exchange(com.predic8.membrane.core.exchange.Exchange) CacheBuilder(com.google.common.cache.CacheBuilder) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 4 with AbstractExchangeSnapshot

use of com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot in project service-proxy by membrane.

the class ElasticSearchExchangeStore method snap.

@Override
public void snap(AbstractExchange exc, Interceptor.Flow flow) {
    AbstractExchangeSnapshot excCopy = null;
    try {
        if (flow == Interceptor.Flow.REQUEST) {
            excCopy = new DynamicAbstractExchangeSnapshot(exc, this::addForElasticSearch);
            addForElasticSearch(excCopy);
        } else {
            excCopy = getExchangeDtoById((int) exc.getId());
            DynamicAbstractExchangeSnapshot.addObservers(exc, excCopy, this::addForElasticSearch);
            excCopy = excCopy.updateFrom(exc);
            addForElasticSearch(excCopy);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : DynamicAbstractExchangeSnapshot(com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot) AbstractExchangeSnapshot(com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) DynamicAbstractExchangeSnapshot(com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot)

Example 5 with AbstractExchangeSnapshot

use of com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot in project service-proxy by membrane.

the class ElasticSearchExchangeStore method sendToElasticSearch.

private void sendToElasticSearch(List<AbstractExchangeSnapshot> exchanges) throws Exception {
    StringBuilder data = exchanges.stream().map(exchange -> wrapForBulkOperationElasticSearch(index, type, getLocalMachineNameWithSuffix() + "-" + exchange.getId(), collectExchangeDataFrom(exchange))).collect(StringBuilder::new, (sb, str) -> sb.append(str), (sb1, sb2) -> sb1.append(sb2));
    Exchange elasticSearchExc = new Request.Builder().post(location + "/_bulk").header("Content-Type", "application/x-ndjson").body(data.toString()).buildExchange();
    client.call(elasticSearchExc);
}
Also used : java.util(java.util) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) LoggerFactory(org.slf4j.LoggerFactory) DynamicAbstractExchangeSnapshot(com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot) InetAddress(java.net.InetAddress) Exchange(com.predic8.membrane.core.exchange.Exchange) MCElement(com.predic8.membrane.annot.MCElement) Interceptor(com.predic8.membrane.core.interceptor.Interceptor) RuleKey(com.predic8.membrane.core.rules.RuleKey) MCAttribute(com.predic8.membrane.annot.MCAttribute) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) AbstractExchangeSnapshot(com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) Rule(com.predic8.membrane.core.rules.Rule) TimeUnit(java.util.concurrent.TimeUnit) IOUtils(org.apache.commons.io.IOUtils) Stream(java.util.stream.Stream) com.predic8.membrane.core.http(com.predic8.membrane.core.http) CacheBuilder(com.google.common.cache.CacheBuilder) HttpClient(com.predic8.membrane.core.transport.http.HttpClient) Cache(com.google.common.cache.Cache) StatisticCollector(com.predic8.membrane.core.rules.StatisticCollector) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) Exchange(com.predic8.membrane.core.exchange.Exchange) CacheBuilder(com.google.common.cache.CacheBuilder)

Aggregations

IOException (java.io.IOException)4 UnknownHostException (java.net.UnknownHostException)4 CacheBuilder (com.google.common.cache.CacheBuilder)3 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)3 Exchange (com.predic8.membrane.core.exchange.Exchange)3 AbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot)3 DynamicAbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Cache (com.google.common.cache.Cache)1 MCAttribute (com.predic8.membrane.annot.MCAttribute)1 MCElement (com.predic8.membrane.annot.MCElement)1 com.predic8.membrane.core.http (com.predic8.membrane.core.http)1 AbstractBody (com.predic8.membrane.core.http.AbstractBody)1 MessageObserver (com.predic8.membrane.core.http.MessageObserver)1 Response (com.predic8.membrane.core.http.Response)1 Interceptor (com.predic8.membrane.core.interceptor.Interceptor)1 AbstractExchangeViewerListener (com.predic8.membrane.core.model.AbstractExchangeViewerListener)1 Rule (com.predic8.membrane.core.rules.Rule)1 RuleKey (com.predic8.membrane.core.rules.RuleKey)1 StatisticCollector (com.predic8.membrane.core.rules.StatisticCollector)1