Search in sources :

Example 1 with RouterStreamRef

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);
}
Also used : ProcessCors(org.webpieces.router.api.routebldr.ProcessCors) SneakyThrow(org.webpieces.util.exceptions.SneakyThrow) LoggerFactory(org.slf4j.LoggerFactory) RouterHeader(org.webpieces.ctx.api.RouterHeader) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Function(java.util.function.Function) ArrayList(java.util.ArrayList) NotFoundException(org.webpieces.http.exception.NotFoundException) Strings(com.google.common.base.Strings) RouterRequest(org.webpieces.ctx.api.RouterRequest) RequestContext(org.webpieces.ctx.api.RequestContext) Map(java.util.Map) RouterInfo(org.webpieces.router.impl.model.RouterInfo) WebpiecesException(org.webpieces.util.exceptions.WebpiecesException) Logger(org.slf4j.Logger) RouterFutureUtil(org.webpieces.router.impl.RouterFutureUtil) AccessResult(org.webpieces.router.api.routebldr.AccessResult) SpecificRouterInvokeException(org.webpieces.router.api.exceptions.SpecificRouterInvokeException) TimeUnit(java.util.concurrent.TimeUnit) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) List(java.util.List) HttpMethod(org.webpieces.ctx.api.HttpMethod) XFuture(org.webpieces.util.futures.XFuture) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) Http2HeaderName(com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName) MatchResult2(org.webpieces.router.impl.model.MatchResult2) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef)

Example 2 with RouterStreamRef

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);
}
Also used : LoadedController(org.webpieces.router.impl.loader.LoadedController) CancelReason(com.webpieces.http2.api.dto.lowlevel.CancelReason) Function(java.util.function.Function) FutureHelper(org.webpieces.util.futures.FutureHelper) ResponseStreamHandle(com.webpieces.http2.api.streaming.ResponseStreamHandle) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) ControllerException(org.webpieces.router.api.exceptions.ControllerException) MethodMeta(org.webpieces.router.api.routes.MethodMeta) XFuture(org.webpieces.util.futures.XFuture) RequestContext(org.webpieces.ctx.api.RequestContext) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Parameter(java.lang.reflect.Parameter) ServiceInvoker(org.webpieces.router.impl.routeinvoker.ServiceInvoker) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) StreamService(org.webpieces.router.api.streams.StreamService) Method(java.lang.reflect.Method) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) WebpiecesException(org.webpieces.util.exceptions.WebpiecesException) LoadedController(org.webpieces.router.impl.loader.LoadedController) XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) Method(java.lang.reflect.Method) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) StreamRef(com.webpieces.http2.api.streaming.StreamRef) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) CancelReason(com.webpieces.http2.api.dto.lowlevel.CancelReason) Parameter(java.lang.reflect.Parameter) RequestContext(org.webpieces.ctx.api.RequestContext)

Example 3 with RouterStreamRef

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);
}
Also used : RequestStreamWriter2(org.webpieces.router.impl.routeinvoker.RequestStreamWriter2) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) NullWriter(org.webpieces.router.impl.routeinvoker.NullWriter) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef)

Example 4 with RouterStreamRef

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);
}
Also used : XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) NotFoundException(org.webpieces.http.exception.NotFoundException) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef)

Example 5 with RouterStreamRef

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);
}
Also used : Logger(org.slf4j.Logger) RstStreamFrame(com.webpieces.http2.api.dto.lowlevel.RstStreamFrame) RouterFutureUtil(org.webpieces.router.impl.RouterFutureUtil) LoggerFactory(org.slf4j.LoggerFactory) SpecificRouterInvokeException(org.webpieces.router.api.exceptions.SpecificRouterInvokeException) Function(java.util.function.Function) InternalErrorRouteFailedException(org.webpieces.router.api.exceptions.InternalErrorRouteFailedException) FutureHelper(org.webpieces.util.futures.FutureHelper) NotFoundException(org.webpieces.http.exception.NotFoundException) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) List(java.util.List) XFuture(org.webpieces.util.futures.XFuture) RequestContext(org.webpieces.ctx.api.RequestContext) Http2ErrorCode(com.webpieces.http2.api.dto.lowlevel.lib.Http2ErrorCode) Map(java.util.Map) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) RouterInfo(org.webpieces.router.impl.model.RouterInfo) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) SupressedExceptionLog(org.webpieces.logging.SupressedExceptionLog) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) NotFoundException(org.webpieces.http.exception.NotFoundException) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef)

Aggregations

RouterStreamRef (org.webpieces.router.impl.routeinvoker.RouterStreamRef)9 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)8 XFuture (org.webpieces.util.futures.XFuture)7 Function (java.util.function.Function)5 ProxyStreamHandle (org.webpieces.router.impl.proxyout.ProxyStreamHandle)5 List (java.util.List)4 Map (java.util.Map)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 RequestContext (org.webpieces.ctx.api.RequestContext)4 NotFoundException (org.webpieces.http.exception.NotFoundException)4 SpecificRouterInvokeException (org.webpieces.router.api.exceptions.SpecificRouterInvokeException)3 RouterFutureUtil (org.webpieces.router.impl.RouterFutureUtil)3 RouterInfo (org.webpieces.router.impl.model.RouterInfo)3 FutureHelper (org.webpieces.util.futures.FutureHelper)3 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)2 RstStreamFrame (com.webpieces.http2.api.dto.lowlevel.RstStreamFrame)2 Http2ErrorCode (com.webpieces.http2.api.dto.lowlevel.lib.Http2ErrorCode)2 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)2 Http2HeaderName (com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName)2