Search in sources :

Example 16 with AbstractExchange

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

the class AbstractExchangeSnapshot method toAbstractExchange.

public AbstractExchange toAbstractExchange() {
    Exchange exc = new Exchange(null);
    if (getRequest() != null)
        exc.setRequest(this.getRequest().toRequest());
    if (getResponse() != null)
        exc.setResponse(this.getResponse().toResponse());
    exc.setOriginalRequestUri(getOriginalRequestUri());
    exc.setTime(getTime());
    exc.setErrorMessage(getErrorMessage());
    exc.setStatus(getStatus());
    exc.setTimeReqSent(getTimeReqSent());
    exc.setTimeReqReceived(getTimeReqReceived());
    exc.setTimeResSent(getTimeResSent());
    exc.setTimeResReceived(getTimeResReceived());
    exc.setDestinations(getDestinations().stream().collect(Collectors.toList()));
    exc.setRemoteAddr(getRemoteAddr());
    exc.setRemoteAddrIp(getRemoteAddrIp());
    exc.setId(getId());
    exc.setRule(getRule());
    setServer(getServer());
    return exc;
}
Also used : AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) Exchange(com.predic8.membrane.core.exchange.Exchange)

Example 17 with AbstractExchange

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

the class ExchangeComparator method compare.

@SuppressWarnings({ "rawtypes", "unchecked" })
public int compare(AbstractExchange e1, AbstractExchange e2) {
    if (e1.getResponse() == null || e2.getResponse() == null)
        return 0;
    for (ExchangeAccessor accessor : accessors) {
        Comparable comp1 = (Comparable) accessor.get(e1);
        Comparable comp2 = (Comparable) accessor.get(e2);
        int result = comp1.compareTo(comp2);
        if (result != 0) {
            if (ascending) {
                return comp1.compareTo(comp2);
            } else {
                return comp2.compareTo(comp1);
            }
        }
    }
    return 0;
}
Also used : ExchangeAccessor(com.predic8.membrane.core.exchange.accessors.ExchangeAccessor)

Example 18 with AbstractExchange

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

the class AdminRESTInterceptor method getRaw.

@Mapping("/admin/rest/exchanges/(-?\\d+)/(response|request)/raw")
public Response getRaw(QueryParameter params, String relativeRootPath) throws Exception {
    AbstractExchange exc = router.getExchangeStore().getExchangeById(params.getGroupInt(1));
    if (exc == null) {
        return Response.notFound().build();
    }
    Message msg = params.getGroup(2).equals("response") ? exc.getResponse() : exc.getRequest();
    if (msg == null) {
        return Response.noContent().build();
    }
    // TODO uses body.toString that doesn't handle different encodings than utf-8
    return Response.ok().contentType(MimeType.TEXT_PLAIN_UTF8).body(msg.toString()).build();
}
Also used : Message(com.predic8.membrane.core.http.Message) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange)

Example 19 with AbstractExchange

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

the class ElasticSearchExchangeStore method removeAllExchanges.

@Override
public void removeAllExchanges(AbstractExchange[] exchanges) {
    StringBuilder sb = Stream.of(exchanges).map(exc -> exc.getId()).collect(() -> {
        StringBuilder acc = new StringBuilder();
        acc.append("[");
        return acc;
    }, (acc, id) -> acc.append(id).append(","), (acc1, acc2) -> acc1.append(",").append(acc2));
    sb.deleteCharAt(sb.length() - 1);
    sb.append("]");
    String exchangeIdsAsJsonArray = sb.toString();
    try {
        Exchange exc = new Request.Builder().post(getElasticSearchExchangesPath() + "_delete_by_query").body("{\n" + "  \"query\": {\n" + "    \"bool\": {\n" + "      \"must\": [\n" + "        {\n" + "          \"wildcard\": {\n" + "            \"issuer\": \"" + documentPrefix + "\"\n" + "          }\n" + "        },\n" + "        {\n" + "          \"terms\": {\n" + "            \"id\": \"" + exchangeIdsAsJsonArray + "\"\n" + "          }\n" + "        }\n" + "      ]\n" + "    }\n" + "  }\n" + "}").header("Content-Type", "application/json").buildExchange();
        client.call(exc);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
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) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Example 20 with AbstractExchange

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

Aggregations

AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)17 IOException (java.io.IOException)7 Exchange (com.predic8.membrane.core.exchange.Exchange)5 AbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot)4 DynamicAbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot)4 Message (com.predic8.membrane.core.http.Message)4 IExchangesStoreListener (com.predic8.membrane.core.model.IExchangesStoreListener)4 StatisticCollector (com.predic8.membrane.core.rules.StatisticCollector)4 UnknownHostException (java.net.UnknownHostException)4 CacheBuilder (com.google.common.cache.CacheBuilder)3 AbstractExchangeViewerListener (com.predic8.membrane.core.model.AbstractExchangeViewerListener)3 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 MCElement (com.predic8.membrane.annot.MCElement)2 AbstractBody (com.predic8.membrane.core.http.AbstractBody)2 Header (com.predic8.membrane.core.http.Header)2 JSONContent (com.predic8.membrane.core.interceptor.rest.JSONContent)2 SQLException (java.sql.SQLException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Cache (com.google.common.cache.Cache)1