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();
}
});
}
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();
}
});
}
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();
}
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);
}
}
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);
}
Aggregations