Search in sources :

Example 1 with AbstractBody

use of com.predic8.membrane.core.http.AbstractBody in project service-proxy by membrane.

the class LimitedMemoryExchangeStore method oldSnap.

private void oldSnap(AbstractExchange exc, Flow flow) {
    // TODO: [fix me] support multi-snap
    // TODO: [fix me] snap message headers and request *here*, not in observer/response
    exc.addExchangeViewerListener(new AbstractExchangeViewerListener() {

        @Override
        public void setExchangeFinished() {
            inflight.remove(exc);
        }
    });
    if (flow == Flow.REQUEST) {
        exc.getRequest().addObserver(new MessageObserver() {

            @Override
            public void bodyRequested(AbstractBody body) {
            }

            @Override
            public void bodyComplete(AbstractBody body) {
                Response r = exc.getResponse();
                if (r != null) {
                    AbstractBody b = r.getBody();
                    if (b != null && b.isRead())
                        // request-bodyComplete might occur after response-bodyComplete
                        return;
                }
                // System.out.println("Exchange put inflight " + exc.hashCode() + " " + exc.getRequest().getStartLine());
                inflight.put(exc, exc.getRequest());
                modify();
            }
        });
        return;
    }
    try {
        Message m = exc.getResponse();
        if (m != null)
            m.addObserver(new MessageObserver() {

                public void bodyRequested(AbstractBody body) {
                }

                public void bodyComplete(AbstractBody body) {
                    snapInternal(exc, flow);
                    inflight.remove(exc);
                    modify();
                // System.out.println("Exchange remove inflight " + exc.hashCode());
                }
            });
        else {
            inflight.remove(exc);
            modify();
        // System.out.println("Exchange remove inflight " + exc.hashCode() + " (2)");
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : AbstractExchangeViewerListener(com.predic8.membrane.core.model.AbstractExchangeViewerListener)

Example 2 with AbstractBody

use of com.predic8.membrane.core.http.AbstractBody 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 3 with AbstractBody

use of com.predic8.membrane.core.http.AbstractBody 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 4 with AbstractBody

use of com.predic8.membrane.core.http.AbstractBody in project service-proxy by membrane.

the class LimitedMemoryExchangeStore method addObservers.

private void addObservers(AbstractExchange exc, AbstractExchange excCopy, Flow flow) throws Exception {
    Message msg = null;
    if (flow == Flow.REQUEST) {
        msg = exc.getRequest();
    } else
        msg = exc.getResponse();
    msg.addObserver(new MessageObserver() {

        @Override
        public void bodyRequested(AbstractBody body) {
        }

        @Override
        public void bodyComplete(AbstractBody body) {
            try {
                cleanSnapshot(Exchange.updateCopy(exc, excCopy));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    });
    exc.addExchangeViewerListener(new AbstractExchangeViewerListener() {

        @Override
        public void setExchangeFinished() {
            try {
                cleanSnapshot(Exchange.updateCopy(exc, excCopy));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    cleanSnapshot(Exchange.updateCopy(exc, excCopy));
}
Also used : AbstractExchangeViewerListener(com.predic8.membrane.core.model.AbstractExchangeViewerListener)

Example 5 with AbstractBody

use of com.predic8.membrane.core.http.AbstractBody in project service-proxy by membrane.

the class HttpServerHandler method process.

private void process() throws Exception {
    try {
        DNSCache dnsCache = getTransport().getRouter().getDnsCache();
        InetAddress remoteAddr = sourceSocket.getInetAddress();
        String ip = dnsCache.getHostAddress(remoteAddr);
        exchange.setRemoteAddrIp(ip);
        exchange.setRemoteAddr(getTransport().isReverseDNS() ? dnsCache.getHostName(remoteAddr) : ip);
        exchange.setRequest(srcReq);
        exchange.setOriginalRequestUri(srcReq.getUri());
        if (exchange.getRequest().getHeader().is100ContinueExpected()) {
            final Request request = exchange.getRequest();
            request.addObserver(new MessageObserver() {

                public void bodyRequested(AbstractBody body) {
                    try {
                        if (request.getHeader().is100ContinueExpected()) {
                            // request body from client so that interceptors can handle it
                            Response.continue100().build().write(srcOut);
                            // remove "Expect: 100-continue" since we already sent "100 Continue"
                            request.getHeader().removeFields(Header.EXPECT);
                        }
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }

                public void bodyComplete(AbstractBody body) {
                }
            });
        }
        invokeHandlers();
        exchange.blockResponseIfNeeded();
    } catch (AbortException e) {
        log.debug("Aborted");
        exchange.finishExchange(true, e.getMessage());
        removeBodyFromBuffer();
        writeResponse(exchange.getResponse());
        log.debug("exchange set aborted");
        return;
    }
    try {
        removeBodyFromBuffer();
        writeResponse(exchange.getResponse());
        exchange.setCompleted();
        log.debug("exchange set completed");
    } catch (Exception e) {
        exchange.finishExchange(true, e.getMessage());
        throw e;
    }
}
Also used : MessageObserver(com.predic8.membrane.core.http.MessageObserver) Request(com.predic8.membrane.core.http.Request) AbstractBody(com.predic8.membrane.core.http.AbstractBody) DNSCache(com.predic8.membrane.core.util.DNSCache) InetAddress(java.net.InetAddress) IOException(java.io.IOException) EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) SocketException(java.net.SocketException) SSLException(javax.net.ssl.SSLException) SocketTimeoutException(java.net.SocketTimeoutException)

Aggregations

AbstractBody (com.predic8.membrane.core.http.AbstractBody)3 AbstractExchangeViewerListener (com.predic8.membrane.core.model.AbstractExchangeViewerListener)3 MessageObserver (com.predic8.membrane.core.http.MessageObserver)2 IOException (java.io.IOException)2 Request (com.predic8.membrane.core.http.Request)1 Response (com.predic8.membrane.core.http.Response)1 DNSCache (com.predic8.membrane.core.util.DNSCache)1 EndOfStreamException (com.predic8.membrane.core.util.EndOfStreamException)1 InetAddress (java.net.InetAddress)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 SSLException (javax.net.ssl.SSLException)1