Search in sources :

Example 41 with StreamWriter

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

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

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

the class XFileReader method sendHttpChunk.

private XFuture<Void> sendHttpChunk(StreamWriter writer, BufferPool pool, ByteBuffer buf, boolean isEos) {
    DataWrapper data = wrapperFactory.wrapByteBuffer(buf);
    int len = data.getReadableSize();
    if (log.isTraceEnabled())
        log.trace("SENDING data to=" + writer + " len=" + len + " isEnd=" + isEos + " content=" + data.createStringFromUtf8(0, len));
    DataFrame frame = new DataFrame();
    frame.setEndOfStream(isEos);
    frame.setData(data);
    return writer.processPiece(frame).thenApply(w -> {
        // at this point, the buffer was consumed
        // after process, release the buffer for re-use
        buf.position(buf.limit());
        pool.releaseBuffer(buf);
        return null;
    });
}
Also used : DataWrapper(org.webpieces.data.api.DataWrapper) DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame)

Example 44 with StreamWriter

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

the class ProxyStreamHandle method createFrame.

private CompletionStage<Void> createFrame(byte[] bytes, StreamWriter writer) {
    DataFrame frame = new DataFrame();
    frame.setEndOfStream(true);
    frame.setData(dataGen.wrapByteArray(bytes));
    return writer.processPiece(frame);
}
Also used : DataFrame(com.webpieces.http2.api.dto.lowlevel.DataFrame)

Example 45 with StreamWriter

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

Aggregations

StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)60 Test (org.junit.Test)43 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)37 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)32 StreamRef (com.webpieces.http2.api.streaming.StreamRef)25 DataFrame (com.webpieces.http2.api.dto.lowlevel.DataFrame)24 DataWrapper (org.webpieces.data.api.DataWrapper)19 XFuture (org.webpieces.util.futures.XFuture)17 MockResponseListener (org.webpieces.http2client.mock.MockResponseListener)15 PassedIn (org.webpieces.httpfrontend2.api.mock2.MockHttp2RequestListener.PassedIn)13 MockStreamWriter (org.webpieces.http2client.mock.MockStreamWriter)11 GoAwayFrame (com.webpieces.http2.api.dto.lowlevel.GoAwayFrame)9 Http2Msg (com.webpieces.http2.api.dto.lowlevel.lib.Http2Msg)9 MockStreamRef (org.webpieces.httpfrontend2.api.mock2.MockStreamRef)8 Header (org.webpieces.httpparser.api.common.Header)8 HttpRequest (org.webpieces.httpparser.api.dto.HttpRequest)8 Http2Header (com.webpieces.http2.api.dto.lowlevel.lib.Http2Header)7 ByteBuffer (java.nio.ByteBuffer)7 HttpResponse (org.webpieces.httpparser.api.dto.HttpResponse)7 RouterStreamRef (org.webpieces.router.impl.routeinvoker.RouterStreamRef)7