use of org.webpieces.router.impl.routeinvoker.RouterStreamRef in project webpieces by deanhiller.
the class EScopedRouter method invokeRouter.
private RouterStreamRef invokeRouter(AbstractRouter router, RequestContext ctx, ProxyStreamHandle handler, boolean isCorsRequest) {
RouterStreamRef streamRef = invokeWithProtection(router, ctx, handler, isCorsRequest);
XFuture<StreamWriter> writer = streamRef.getWriter().handle((r, t) -> {
if (t == null)
return XFuture.completedFuture(r);
XFuture<StreamWriter> fut = new XFuture<>();
Throwable exc = convert(router.getMatchInfo(), t);
fut.completeExceptionally(exc);
return fut;
}).thenCompose(Function.identity());
return new RouterStreamRef("eScoped2", writer, streamRef);
}
use of org.webpieces.router.impl.routeinvoker.RouterStreamRef in project webpieces by deanhiller.
the class StreamProxy method openStream.
@Override
public RouterStreamRef openStream(MethodMeta meta, ProxyStreamHandle handle) {
RequestContext requestCtx = meta.getCtx();
LoadedController loadedController = meta.getLoadedController();
Object instance = loadedController.getControllerInstance();
Method controllerMethod = loadedController.getControllerMethod();
Parameter[] parameters = loadedController.getParameters();
if (parameters.length != 1)
throw new IllegalArgumentException("Your method='" + controllerMethod + "' MUST one parameter and does not. It needs to take a RouterStreamHandler");
else if (!ResponseStreamHandle.class.equals(parameters[0].getType()))
throw new IllegalArgumentException("The single parameter must be RouterStreamHandle and was not for this method='" + controllerMethod + "'");
else if (!StreamRef.class.equals(controllerMethod.getReturnType()))
throw new IllegalArgumentException("The return value must be a subclass of StreamRef and was not for this method='" + controllerMethod + "'");
StreamRef streamRef = invokeStream(meta, controllerMethod, instance, requestCtx, handle);
XFuture<StreamWriter> writer = streamRef.getWriter();
XFuture<StreamWriter> newFuture = futureUtil.catchBlockWrap(() -> writer, (t) -> convert(loadedController, t));
Function<CancelReason, XFuture<Void>> cancelFunc = (reason) -> streamRef.cancel(reason);
return new RouterStreamRef("streamProxy", newFuture, cancelFunc);
}
use of org.webpieces.router.impl.routeinvoker.RouterStreamRef in project webpieces by deanhiller.
the class RequestResponseStream method openStream.
@Override
public RouterStreamRef openStream(MethodMeta meta, ProxyStreamHandle handle) {
boolean endOfStream = meta.getCtx().getRequest().originalRequest.isEndOfStream();
if (endOfStream) {
// If there is no body, just invoke to process OR IN CASE of InternalError or NotFound, there is NO need
// to wait for the request body and we can respond early, which stops wasting CPU of reading in their body
meta.getCtx().getRequest().body = DataWrapperGeneratorFactory.EMPTY;
XFuture<StreamWriter> invokeSvc = invoker.invokeSvc(meta, i18nBundleName, service, processor, handle).thenApply(voidd -> new NullWriter());
return new RouterStreamRef("reqRespStreamProxy", invokeSvc, null);
}
// At this point, we don't have the end of the stream so return a request writer that calls invoke when complete
RequestStreamWriter2 writer = new RequestStreamWriter2(requestBodyParsers, meta, (newInfo) -> invoker.invokeSvc(newInfo, i18nBundleName, service, processor, handle));
XFuture<StreamWriter> w = XFuture.completedFuture(writer);
return new RouterStreamRef("requestRespStream", w, null);
}
use of org.webpieces.router.impl.routeinvoker.RouterStreamRef 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.router.impl.routeinvoker.RouterStreamRef 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);
}
Aggregations