use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.
the class EScopedRouter method invokeRouteImpl.
public RouterStreamRef invokeRouteImpl(RequestContext ctx, ProxyStreamHandle handler, String subPath) {
if ("".equals(subPath))
return findAndInvokeRoute(ctx, handler, subPath);
else if (!subPath.startsWith("/"))
throw new IllegalArgumentException("path must start with /");
String prefix = subPath;
int index = subPath.indexOf("/", 1);
if (index == 1) {
XFuture<StreamWriter> future = new XFuture<>();
future.completeExceptionally(new NotFoundException("Bad path=" + ctx.getRequest().relativePath + " request=" + ctx.getRequest()));
return new RouterStreamRef("badPath", future, null);
} else if (index > 1) {
prefix = subPath.substring(0, index);
}
EScopedRouter routeInfo = getPathPrefixToNextRouter().get(prefix);
if (routeInfo != null) {
if (index < 0)
return routeInfo.invokeRoute(ctx, handler, "");
String newRelativePath = subPath.substring(index, subPath.length());
return routeInfo.invokeRoute(ctx, handler, newRelativePath);
}
return findAndInvokeRoute(ctx, handler, subPath);
}
use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.
the class DScopedRouter method invokeRouteCatchNotFound.
/**
* NOTE: We have to catch any exception from the method processNotFound so we can't catch and call internalServerError in this
* method without nesting even more!!! UGH, more nesting sucks
*/
private RouterStreamRef invokeRouteCatchNotFound(RequestContext ctx, ProxyStreamHandle handler, String subPath) {
RouterStreamRef streamRef = super.invokeRoute(ctx, handler, subPath);
XFuture<StreamWriter> writer = streamRef.getWriter().handle((r, t) -> {
if (t == null)
return XFuture.completedFuture(r);
if (t instanceof NotFoundException)
return notFound((NotFoundException) t, ctx, handler);
return futureUtil.failedFuture(t);
}).thenCompose(Function.identity());
return new RouterStreamRef("DScopedNotFoundCheck", writer, streamRef);
}
use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.
the class DScopedRouter method invokeRoute.
@Override
public RouterStreamRef invokeRoute(RequestContext ctx, ProxyStreamHandle handler, String subPath) {
RouterStreamRef streamRef = invokeRouteCatchNotFound(ctx, handler, subPath);
XFuture<StreamWriter> writer = streamRef.getWriter().handle((r, t) -> {
if (t == null)
return XFuture.completedFuture(r);
return tryRenderWebAppErrorControllerResult(ctx, handler, t);
}).thenCompose(Function.identity());
XFuture<StreamWriter> proxyWriter = writer.thenApply(w -> createProxy(w, ctx, handler));
return new RouterStreamRef("dScopedRouter", proxyWriter, streamRef);
}
use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.
the class ConnectedChannels method closeChannels.
public XFuture<Void> closeChannels() {
// first prevent other threads from calling above functions ever again
closed = true;
List<XFuture<Void>> futures = new ArrayList<>();
for (Channel c : connectedChannels.keySet()) {
futures.add(c.close());
}
@SuppressWarnings("rawtypes") XFuture[] array = futures.toArray(new XFuture[0]);
return XFuture.allOf(array);
}
use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.
the class IntegTestClientNotRead method write.
private void write(Channel channel, String reason, final int counter) {
log.info("write from client. reason=" + reason);
byte[] data = new byte[2000];
ByteBuffer buffer = ByteBuffer.wrap(data);
XFuture<Void> write = channel.write(buffer);
final int count = counter + 1;
if (counter >= 100) {
write.thenAccept(p -> write(channel, "wrote data from client", count)).whenComplete((r, e) -> finished(r, e));
} else {
write.thenAcceptAsync(p -> write(channel, "wrote data async", 0), executor).whenComplete((r, e) -> finished(r, e));
}
}
Aggregations