Search in sources :

Example 6 with RouterStreamRef

use of org.webpieces.router.impl.routeinvoker.RouterStreamRef 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);
}
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) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef)

Example 7 with RouterStreamRef

use of org.webpieces.router.impl.routeinvoker.RouterStreamRef in project webpieces by deanhiller.

the class DevRouteInvoker method invokeHtmlController.

@Override
public RouterStreamRef invokeHtmlController(InvokeInfo invokeInfo, StreamService dynamicInfo, RouteData data) {
    // special case for if stuff didn't compile and we flag it
    Throwable exc = (Throwable) invokeInfo.getRequestCtx().getRequest().requestState.get(ERROR_KEY);
    if (exc != null) {
        log.error("Could not compile your code", exc);
        RouteInfoForInternalError error = new RouteInfoForInternalError(exc);
        XFuture<Void> future = invokeDevelopmentErrorPage(invokeInfo, error);
        XFuture<StreamWriter> writer = future.thenApply(voidd -> new NullWriter());
        return new RouterStreamRef("notCompileError", writer, null);
    }
    if (invokeInfo.getRequestCtx().getRequest().queryParams.containsKey(DevelopmentController.INTERNAL_ERROR_KEY)) {
        // need to simulate the error to show production page
        throw new SimulateInternalError();
    }
    // }
    return super.invokeHtmlController(invokeInfo, dynamicInfo, data);
}
Also used : StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) RouteInfoForInternalError(org.webpieces.router.impl.services.RouteInfoForInternalError) SimulateInternalError(org.webpieces.router.impl.routers.SimulateInternalError) NullWriter(org.webpieces.router.impl.routeinvoker.NullWriter) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef)

Example 8 with RouterStreamRef

use of org.webpieces.router.impl.routeinvoker.RouterStreamRef in project webpieces by deanhiller.

the class RouterServiceImpl method incomingRequest.

@Override
public StreamRef incomingRequest(Http2Request req, RouterResponseHandler handler) {
    // ******************************************************************************************
    // DO NOT ADD CODE HERE OR ABOVE THIS METHOD in RouterService.  This is our CATCH-ALL point so
    // ANY code above that is not protected from our catch and respond to clients
    // ******************************************************************************************
    String txId = generate();
    // I do NOT like doing Guice creation on the request path(Dagger creation would probably be ok) BUT this
    // is very worth it AND customers can swap out these critical classes if they need to quickly temporarily fix a bug while
    // we work on the bug.  We can easily give customers bug fixes like add binder.bind(ClassWithBug.class).to(BugFixCode.class)
    ProxyStreamHandle proxyHandler = proxyProvider.get();
    proxyHandler.init(handler, req);
    MDC.put("txId", txId);
    // top level handler...
    try {
        RouterStreamRef streamRef = incomingRequestProtected(req, proxyHandler);
        XFuture<StreamWriter> writer = streamRef.getWriter().thenApply(w -> new TxStreamWriter(txId, w));
        XFuture<StreamWriter> finalWriter = writer.handle((r, t) -> {
            if (t == null)
                return XFuture.completedFuture(r);
            XFuture<StreamWriter> fut = proxyHandler.topLevelFailure(req, t);
            return fut;
        }).thenCompose(Function.identity());
        return new RouterStreamRef("routerSevcTop", finalWriter, streamRef);
    } finally {
        MDC.remove("txId");
    }
}
Also used : RouterCookie(org.webpieces.ctx.api.RouterCookie) Provider(javax.inject.Provider) UriInfo(org.webpieces.ctx.api.UriInfo) LoggerFactory(org.slf4j.LoggerFactory) RouterConfig(org.webpieces.router.api.RouterConfig) HashMap(java.util.HashMap) Random(java.util.Random) Singleton(javax.inject.Singleton) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Function(java.util.function.Function) ContentType(org.webpieces.ctx.api.ContentType) ArrayList(java.util.ArrayList) ParsedContentType(com.webpieces.http2.api.subparsers.ParsedContentType) HashSet(java.util.HashSet) Inject(javax.inject.Inject) RouterRequest(org.webpieces.ctx.api.RouterRequest) Locale(java.util.Locale) AcceptType(com.webpieces.http2.api.subparsers.AcceptType) Map(java.util.Map) Logger(org.slf4j.Logger) RouterStreamHandle(org.webpieces.router.api.RouterStreamHandle) UrlEncodedParser(org.webpieces.util.urlparse.UrlEncodedParser) Set(java.util.Set) RouterService(org.webpieces.router.api.RouterService) Injector(com.google.inject.Injector) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) List(java.util.List) HttpMethod(org.webpieces.ctx.api.HttpMethod) XFuture(org.webpieces.util.futures.XFuture) AcceptMediaType(org.webpieces.ctx.api.AcceptMediaType) StreamRef(com.webpieces.http2.api.streaming.StreamRef) MDC(org.slf4j.MDC) FileMeta(org.webpieces.router.impl.compression.FileMeta) RouterResponseHandler(org.webpieces.router.api.RouterResponseHandler) Entry(java.util.Map.Entry) ObjectStringConverter(org.webpieces.router.api.extensions.ObjectStringConverter) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) HeaderPriorityParser(com.webpieces.http2.api.subparsers.HeaderPriorityParser) Http2HeaderName(com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName) HeaderPriorityParserImpl(com.webpieces.http2.impl.subparsers.HeaderPriorityParserImpl) Arguments(org.webpieces.util.cmdline2.Arguments) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) Http2Headers(com.webpieces.http2.api.dto.highlevel.Http2Headers) XFuture(org.webpieces.util.futures.XFuture) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef)

Example 9 with RouterStreamRef

use of org.webpieces.router.impl.routeinvoker.RouterStreamRef in project webpieces by deanhiller.

the class AbstractRouterService method incomingRequest.

public RouterStreamRef incomingRequest(RouterRequest routerRequest, ProxyStreamHandle handler) {
    try {
        Session session = (Session) cookieTranslator.translateCookieToScope(routerRequest, new SessionImpl(translator));
        FlashSub flash = (FlashSub) cookieTranslator.translateCookieToScope(routerRequest, new FlashImpl(translator));
        Validation validation = (Validation) cookieTranslator.translateCookieToScope(routerRequest, new ValidationImpl(translator));
        ApplicationContext ctx = webInjector.getAppContext();
        RequestContext requestCtx = new RequestContext(validation, flash, session, routerRequest, ctx);
        String user = session.get("userId");
        MDC.put("userId", user);
        // TODO(dhiller): This is request heaaders choke point but need ot also perhaps setup streaming choke point
        // here as well
        Current.setContext(requestCtx);
        try {
            return incomingRequestImpl(requestCtx, handler);
        } finally {
            Current.setContext(null);
        }
    } catch (BadCookieException e) {
        // CHEAT: we know this is syncrhonous exception from the translateCookieToScope
        log.warn("This occurs if secret key changed, or you booted another webapp with different key on same port or someone modified the cookie", e);
        XFuture<StreamWriter> writer = handler.sendRedirectAndClearCookie(routerRequest, e.getCookieName());
        return new RouterStreamRef("cookieFailed", writer, null);
    }
}
Also used : BadCookieException(org.webpieces.router.api.exceptions.BadCookieException) XFuture(org.webpieces.util.futures.XFuture) SessionImpl(org.webpieces.router.impl.ctx.SessionImpl) FlashImpl(org.webpieces.router.impl.ctx.FlashImpl) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) ValidationImpl(org.webpieces.router.impl.ctx.ValidationImpl)

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