use of com.webpieces.http2.api.streaming.StreamWriter 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 com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class EScopedRouter method send403Response.
private void send403Response(ProxyStreamHandle handler, String reason) {
Http2Response response = new Http2Response();
response.addHeader(new Http2Header(Http2HeaderName.STATUS, "403"));
response.addHeader(new Http2Header("Webpieces-Reason", reason));
XFuture<StreamWriter> process = handler.process(response);
try {
process.get(10, TimeUnit.SECONDS);
} catch (Exception e) {
throw SneakyThrow.sneak(e);
}
}
use of com.webpieces.http2.api.streaming.StreamWriter in project webpieces by deanhiller.
the class DScopedRouter method tryRenderWebAppErrorControllerResult.
private XFuture<StreamWriter> tryRenderWebAppErrorControllerResult(RequestContext ctx, ProxyStreamHandle handler, Throwable t) {
if (ExceptionWrap.isChannelClosed(t)) {
// if the socket was closed before we responded, do not log a failure
if (log.isTraceEnabled())
log.trace("async exception due to socket being closed", t);
return XFuture.<StreamWriter>completedFuture(new NullStreamWriter());
}
String failedRoute = "<Unknown Route>";
if (t instanceof SpecificRouterInvokeException)
failedRoute = ((SpecificRouterInvokeException) t).getMatchInfo() + "";
if (!(t instanceof SimulateInternalError)) {
log.error("There is three parts to this error message... request, route found, and the exception " + "message. You should\nread the exception message below as well as the RouterRequest and RouteMeta.\n\n" + ctx.getRequest() + "\n\n" + failedRoute + ". \n\nNext, server will try to render apps 5xx page\n\n", t);
SupressedExceptionLog.log(log, t);
}
// page so in that case, fail, and cancel the stream
if (handler.hasSentResponseAlready()) {
RstStreamFrame frame = new RstStreamFrame();
frame.setKnownErrorCode(Http2ErrorCode.CANCEL);
handler.cancel(frame);
return XFuture.completedFuture(new NullStreamWriter());
}
return invokeWebAppErrorController(t, ctx, handler, failedRoute);
}
use of com.webpieces.http2.api.streaming.StreamWriter 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 com.webpieces.http2.api.streaming.StreamWriter 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);
}
Aggregations