Search in sources :

Example 1 with ProxyStreamHandle

use of org.webpieces.router.impl.proxyout.ProxyStreamHandle in project webpieces by deanhiller.

the class EchoStreamingClient method stream.

public StreamRef stream(ResponseStreamHandle handle) {
    ProxyStreamHandle h = (ProxyStreamHandle) handle;
    Http2Request req = Current.request().originalRequest;
    Http2Response response = h.createBaseResponse(req, "application/ndjson", 200, "OK");
    try {
        StreamWriter writer = h.process(response).get();
        return new ProxyStreamRef(new EchoWriter(writer));
    } catch (InterruptedException | ExecutionException e) {
        throw SneakyThrow.sneak(e);
    }
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with ProxyStreamHandle

use of org.webpieces.router.impl.proxyout.ProxyStreamHandle in project webpieces by deanhiller.

the class AbstractRouteInvoker method invokeSvc.

private XFuture<Void> invokeSvc(InvokeInfo invokeInfo, Endpoint dynamicInfo, RouteData data, Processor processor) {
    LoadedController loadedController = invokeInfo.getLoadedController();
    ProxyStreamHandle handle = invokeInfo.getHandler();
    RequestContext requestCtx = invokeInfo.getRequestCtx();
    MethodMeta methodMeta = new MethodMeta(loadedController, requestCtx, invokeInfo.getRouteType(), data);
    String i18nBundleName = "";
    return serviceInvoker.invokeSvc(methodMeta, i18nBundleName, dynamicInfo, processor, handle);
}
Also used : MethodMeta(org.webpieces.router.api.routes.MethodMeta) LoadedController(org.webpieces.router.impl.loader.LoadedController) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) RequestContext(org.webpieces.ctx.api.RequestContext)

Example 3 with ProxyStreamHandle

use of org.webpieces.router.impl.proxyout.ProxyStreamHandle in project webpieces by deanhiller.

the class DevRouteInvoker method invokeDevelopmentErrorPage.

private XFuture<Void> invokeDevelopmentErrorPage(InvokeInfo invokeInfo, RouteInfoForInternalError data) {
    RequestContext requestCtx = invokeInfo.getRequestCtx();
    ProxyStreamHandle handler = invokeInfo.getHandler();
    RouterRequest req = requestCtx.getRequest();
    Throwable exception = data.getException();
    Injector webAppInjector = webInjector.getCurrentInjector();
    RouteInfo routeInfo = new RouteInfo(new RouteModuleInfo("", null), "/org/webpieces/devrouter/impl/DevelopmentController.internalError");
    SvcProxyFixedRoutes svcProxy = new SvcProxyFixedRoutes(serviceInvoker, futureUtil);
    LoadedController newLoadedController = controllerFinder.loadGenericController(webAppInjector, routeInfo).getLoadedController();
    Endpoint newInfo = new Endpoint(svcProxy);
    RouterRequest newRequest = new RouterRequest();
    newRequest.putMultipart("url", req.relativePath);
    newRequest.isHttps = req.isHttps;
    newRequest.isBackendRequest = req.isBackendRequest;
    newRequest.originalRequest = req.originalRequest;
    newRequest.requestState.put(DevelopmentController.ORIGINAL_REQUEST, req);
    newRequest.requestState.put(DevelopmentController.EXCEPTION, exception);
    newRequest.requestState.put(DevRouteInvoker.ERROR_KEY, req.requestState.get(DevRouteInvoker.ERROR_KEY));
    ApplicationContext ctx = webInjector.getAppContext();
    RequestContext overridenCtx = new RequestContext(requestCtx.getValidation(), (FlashSub) requestCtx.getFlash(), requestCtx.getSession(), newRequest, ctx);
    InvokeInfo newInvokeInfo = new InvokeInfo(overridenCtx, handler, RouteType.INTERNAL_SERVER_ERROR, newLoadedController, null);
    RequestContext oldContext = Current.getContext();
    Current.setContext(overridenCtx);
    try {
        return super.invokeErrorController(newInvokeInfo, newInfo, data);
    } finally {
        Current.setContext(oldContext);
    }
}
Also used : LoadedController(org.webpieces.router.impl.loader.LoadedController) Endpoint(org.webpieces.router.impl.routers.Endpoint) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) WebInjector(org.webpieces.router.impl.WebInjector) Injector(com.google.inject.Injector) SvcProxyFixedRoutes(org.webpieces.router.impl.services.SvcProxyFixedRoutes) InvokeInfo(org.webpieces.router.impl.routeinvoker.InvokeInfo) RouteInfo(org.webpieces.router.impl.routebldr.RouteInfo) RouteModuleInfo(org.webpieces.router.impl.model.RouteModuleInfo)

Example 4 with ProxyStreamHandle

use of org.webpieces.router.impl.proxyout.ProxyStreamHandle 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 5 with ProxyStreamHandle

use of org.webpieces.router.impl.proxyout.ProxyStreamHandle 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)

Aggregations

ProxyStreamHandle (org.webpieces.router.impl.proxyout.ProxyStreamHandle)10 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)7 XFuture (org.webpieces.util.futures.XFuture)6 Function (java.util.function.Function)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 RequestContext (org.webpieces.ctx.api.RequestContext)5 NotFoundException (org.webpieces.http.exception.NotFoundException)5 RouterStreamRef (org.webpieces.router.impl.routeinvoker.RouterStreamRef)5 List (java.util.List)4 Map (java.util.Map)4 LoadedController (org.webpieces.router.impl.loader.LoadedController)4 FutureHelper (org.webpieces.util.futures.FutureHelper)4 Injector (com.google.inject.Injector)3 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)3 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)3 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)3 Http2HeaderName (com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName)3 SpecificRouterInvokeException (org.webpieces.router.api.exceptions.SpecificRouterInvokeException)3 RouterFutureUtil (org.webpieces.router.impl.RouterFutureUtil)3