Search in sources :

Example 11 with AbstractExchange

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

the class AdminRESTInterceptor method getExchanges.

@Mapping("/admin/rest/exchanges(/?\\?.*)?")
public Response getExchanges(QueryParameter params, String relativeRootPath) throws Exception {
    if (params.getString("waitForModification") != null) {
        getRouter().getExchangeStore().waitForModification(params.getLong("waitForModification"));
    }
    List<AbstractExchange> exchanges;
    synchronized (getRouter().getExchangeStore().getAllExchangesAsList()) {
        exchanges = new ArrayList<AbstractExchange>(getRouter().getExchangeStore().getAllExchangesAsList());
    }
    exchanges = filter(params, exchanges);
    Collections.sort(exchanges, ComparatorFactory.getAbstractExchangeComparator(params.getString("sort", "time"), params.getString("order", "desc")));
    int offset = params.getInt("offset", 0);
    int max = params.getInt("max", exchanges.size());
    final int total = exchanges.size();
    final List<AbstractExchange> paginated = exchanges.subList(offset, Math.min(offset + max, exchanges.size()));
    return json(new JSONContent() {

        public void write(JsonGenerator gen) throws Exception {
            gen.writeStartObject();
            gen.writeArrayFieldStart("exchanges");
            for (AbstractExchange e : paginated) {
                writeExchange(e, gen);
            }
            gen.writeEndArray();
            gen.writeNumberField("total", total);
            gen.writeNumberField("lastModified", getRouter().getExchangeStore().getLastModified());
            gen.writeEndObject();
        }
    });
}
Also used : JSONContent(com.predic8.membrane.core.interceptor.rest.JSONContent) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) SQLException(java.sql.SQLException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) IOException(java.io.IOException)

Example 12 with AbstractExchange

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

the class AdminRESTInterceptor method getRequestHeader.

@Mapping("/admin/rest/exchanges/(-?\\d+)/(response|request)/header")
public Response getRequestHeader(QueryParameter params, String relativeRootPath) throws Exception {
    final AbstractExchange exc = router.getExchangeStore().getExchangeById(params.getGroupInt(1));
    if (exc == null) {
        return Response.notFound().build();
    }
    final Message msg = params.getGroup(2).equals("response") ? exc.getResponse() : exc.getRequest();
    if (msg == null) {
        return Response.noContent().build();
    }
    return json(new JSONContent() {

        public void write(JsonGenerator gen) throws Exception {
            gen.writeStartObject();
            gen.writeArrayFieldStart("headers");
            for (HeaderField hf : msg.getHeader().getAllHeaderFields()) {
                gen.writeStartObject();
                gen.writeStringField("name", hf.getHeaderName().toString());
                gen.writeStringField("value", hf.getValue());
                gen.writeEndObject();
            }
            gen.writeEndArray();
            gen.writeEndObject();
        }
    });
}
Also used : Message(com.predic8.membrane.core.http.Message) JSONContent(com.predic8.membrane.core.interceptor.rest.JSONContent) HeaderField(com.predic8.membrane.core.http.HeaderField) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) SQLException(java.sql.SQLException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) IOException(java.io.IOException)

Example 13 with AbstractExchange

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

the class AdminRESTInterceptor method getRequestBody.

@Mapping("/admin/rest/exchanges/(-?\\d+)/(response|request)/body")
public Response getRequestBody(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();
    String ct = params.getGroup(2).equals("response") ? exc.getResponseContentType() : exc.getRequestContentType();
    if (msg == null || msg.isBodyEmpty()) {
        return Response.noContent().build();
    }
    ResponseBuilder rb = Response.ok().contentType(ct).body(msg.getBodyAsStream(), false);
    String contentEncoding = msg.getHeader().getContentEncoding();
    if (contentEncoding != null)
        rb.header(Header.CONTENT_ENCODING, contentEncoding);
    return rb.build();
}
Also used : Message(com.predic8.membrane.core.http.Message) ResponseBuilder(com.predic8.membrane.core.http.Response.ResponseBuilder) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange)

Example 14 with AbstractExchange

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

the class StatisticCollector method collectFrom.

public void collectFrom(AbstractExchange exc) {
    totalCount++;
    if (exc.getStatus() == ExchangeState.FAILED) {
        errorCount++;
        if (!countErrorExchanges)
            return;
    }
    long timeReqSent = exc.getTimeReqSent();
    if (timeReqSent == 0)
        // this Exchange did not reach the HTTPClientInterceptor
        return;
    long timeResSent = exc.getTimeResSent();
    if (timeResSent == 0)
        // this Exchange is not yet completed
        return;
    goodCount++;
    int time = (int) (timeResSent - timeReqSent);
    if (time < minTime)
        minTime = time;
    if (time > maxTime)
        maxTime = time;
    totalTime += time;
    try {
        AbstractBody requestBody = exc.getRequest().getBody();
        totalBytesSent += requestBody.isRead() ? requestBody.getLength() : 0;
        AbstractBody responseBody = exc.getResponse().getBody();
        totalBytesReceived += responseBody.isRead() ? responseBody.getLength() : 0;
    } catch (IOException e) {
        log.warn("", e);
    }
}
Also used : AbstractBody(com.predic8.membrane.core.http.AbstractBody) IOException(java.io.IOException)

Example 15 with AbstractExchange

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

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