Search in sources :

Example 21 with StreamRef

use of com.webpieces.http2.api.streaming.StreamRef in project webpieces by deanhiller.

the class ProxyHttpStream method incomingRequest.

@Override
public StreamRef incomingRequest(Http2Request request, ResponseStream stream) {
    String expect = request.getSingleHeaderValue("Expect");
    XFuture<StreamWriter> future = XFuture.completedFuture(null);
    if (expect != null && "100-continue".equals(expect.toLowerCase())) {
        Http2Response continueResponse = new Http2Response();
        continueResponse.setEndOfStream(false);
        continueResponse.addHeader(new Http2Header(Http2HeaderName.STATUS, "100"));
        future = stream.process(continueResponse);
    }
    // This is only for streaming to backpressure clients IF we responded OR cancelled so we don't
    // waste CPU on a client stream coming in
    XFuture<ProxyWriter> futureWriter = new XFuture<>();
    ProxyResponseStream proxy = new ProxyResponseStream(request, stream, futureWriter);
    StreamRef streamRef = openStream.incomingRequest(request, proxy);
    XFuture<StreamWriter> writer = future.thenCompose(w -> {
        return createProxy(streamRef.getWriter(), futureWriter);
    });
    return new ProxyStreamRef(writer, streamRef);
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) StreamRef(com.webpieces.http2.api.streaming.StreamRef) XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)

Example 22 with StreamRef

use of com.webpieces.http2.api.streaming.StreamRef 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 23 with StreamRef

use of com.webpieces.http2.api.streaming.StreamRef 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)

Example 24 with StreamRef

use of com.webpieces.http2.api.streaming.StreamRef 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 25 with StreamRef

use of com.webpieces.http2.api.streaming.StreamRef in project webpieces by deanhiller.

the class TestCMaxConcurrentSetting method sendTwoRequests.

private RequestsSent sendTwoRequests() {
    Http2Request request1 = Requests.createRequest();
    Http2Request request2 = Requests.createRequest();
    MockStreamWriter writer1 = new MockStreamWriter();
    MockStreamWriter writer2 = new MockStreamWriter();
    MockResponseListener listener1 = new MockResponseListener();
    listener1.setIncomingRespDefault(XFuture.completedFuture(writer1));
    // do not set default incoming response as we want to delay the resolution of the future
    MockResponseListener listener2 = new MockResponseListener();
    StreamRef streamRef1 = httpSocket.openStream().process(request1, listener1);
    XFuture<StreamWriter> future = streamRef1.getWriter();
    StreamRef streamRef2 = httpSocket.openStream().process(request2, listener2);
    XFuture<StreamWriter> future2 = streamRef2.getWriter();
    RequestHolder r1 = new RequestHolder(request1, listener1, writer1, future);
    RequestHolder r2 = new RequestHolder(request2, listener2, writer2, future2);
    RequestsSent requests = new RequestsSent(r1, r2);
    Http2Msg req = mockChannel.getFrameAndClear();
    Assert.assertEquals(1, req.getStreamId());
    Assert.assertEquals(request1, req);
    Assert.assertTrue(future.isDone());
    Assert.assertFalse(future2.isDone());
    return requests;
}
Also used : StreamRef(com.webpieces.http2.api.streaming.StreamRef) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) RequestsSent(org.webpieces.http2client.util.RequestsSent) MockResponseListener(org.webpieces.http2client.mock.MockResponseListener) RequestHolder(org.webpieces.http2client.util.RequestHolder) Http2Msg(com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg) MockStreamWriter(org.webpieces.http2client.mock.MockStreamWriter)

Aggregations

StreamRef (com.webpieces.http2.api.streaming.StreamRef)34 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)30 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)27 Test (org.junit.Test)22 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)17 XFuture (org.webpieces.util.futures.XFuture)12 RequestStreamHandle (com.webpieces.http2.api.streaming.RequestStreamHandle)11 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)8 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)8 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)6 DataWrapper (org.webpieces.data.api.DataWrapper)6 RouterService (org.webpieces.router.api.RouterService)6 ProxyStreamHandle (org.webpieces.router.impl.proxyout.ProxyStreamHandle)6 RstStreamFrame (com.webpieces.http2.api.dto.lowlevel.RstStreamFrame)5 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)5 Function (java.util.function.Function)5 RequestContext (org.webpieces.ctx.api.RequestContext)5 MockResponseListener (org.webpieces.httpclient.api.mocks.MockResponseListener)5 GoAwayFrame (com.webpieces.http2.api.dto.lowlevel.GoAwayFrame)4 ConnectionClosedException (com.webpieces.http2engine.api.error.ConnectionClosedException)4