use of com.predic8.membrane.core.http.MessageObserver 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);
}
}
use of com.predic8.membrane.core.http.MessageObserver 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);
}
use of com.predic8.membrane.core.http.MessageObserver 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));
}
use of com.predic8.membrane.core.http.MessageObserver 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;
}
}
Aggregations