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);
}
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);
}
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);
}
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);
}
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;
}
Aggregations