Search in sources :

Example 46 with StreamWriter

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

the class EScopedRouter method invokeRouteImpl.

public RouterStreamRef invokeRouteImpl(RequestContext ctx, ProxyStreamHandle handler, String subPath) {
    if ("".equals(subPath))
        return findAndInvokeRoute(ctx, handler, subPath);
    else if (!subPath.startsWith("/"))
        throw new IllegalArgumentException("path must start with /");
    String prefix = subPath;
    int index = subPath.indexOf("/", 1);
    if (index == 1) {
        XFuture<StreamWriter> future = new XFuture<>();
        future.completeExceptionally(new NotFoundException("Bad path=" + ctx.getRequest().relativePath + " request=" + ctx.getRequest()));
        return new RouterStreamRef("badPath", future, null);
    } else if (index > 1) {
        prefix = subPath.substring(0, index);
    }
    EScopedRouter routeInfo = getPathPrefixToNextRouter().get(prefix);
    if (routeInfo != null) {
        if (index < 0)
            return routeInfo.invokeRoute(ctx, handler, "");
        String newRelativePath = subPath.substring(index, subPath.length());
        return routeInfo.invokeRoute(ctx, handler, newRelativePath);
    }
    return findAndInvokeRoute(ctx, handler, subPath);
}
Also used : XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) NotFoundException(org.webpieces.http.exception.NotFoundException) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef)

Example 47 with StreamWriter

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

the class EScopedRouter method send403Response.

private void send403Response(ProxyStreamHandle handler, String reason) {
    Http2Response response = new Http2Response();
    response.addHeader(new Http2Header(Http2HeaderName.STATUS, "403"));
    response.addHeader(new Http2Header("Webpieces-Reason", reason));
    XFuture<StreamWriter> process = handler.process(response);
    try {
        process.get(10, TimeUnit.SECONDS);
    } catch (Exception e) {
        throw SneakyThrow.sneak(e);
    }
}
Also used : Http2Response(com.webpieces.http2.api.dto.highlevel.Http2Response) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) NotFoundException(org.webpieces.http.exception.NotFoundException) WebpiecesException(org.webpieces.util.exceptions.WebpiecesException) SpecificRouterInvokeException(org.webpieces.router.api.exceptions.SpecificRouterInvokeException)

Example 48 with StreamWriter

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

the class DScopedRouter method tryRenderWebAppErrorControllerResult.

private XFuture<StreamWriter> tryRenderWebAppErrorControllerResult(RequestContext ctx, ProxyStreamHandle handler, Throwable t) {
    if (ExceptionWrap.isChannelClosed(t)) {
        // if the socket was closed before we responded, do not log a failure
        if (log.isTraceEnabled())
            log.trace("async exception due to socket being closed", t);
        return XFuture.<StreamWriter>completedFuture(new NullStreamWriter());
    }
    String failedRoute = "<Unknown Route>";
    if (t instanceof SpecificRouterInvokeException)
        failedRoute = ((SpecificRouterInvokeException) t).getMatchInfo() + "";
    if (!(t instanceof SimulateInternalError)) {
        log.error("There is three parts to this error message... request, route found, and the exception " + "message.  You should\nread the exception message below  as well as the RouterRequest and RouteMeta.\n\n" + ctx.getRequest() + "\n\n" + failedRoute + ".  \n\nNext, server will try to render apps 5xx page\n\n", t);
        SupressedExceptionLog.log(log, t);
    }
    // page so in that case, fail, and cancel the stream
    if (handler.hasSentResponseAlready()) {
        RstStreamFrame frame = new RstStreamFrame();
        frame.setKnownErrorCode(Http2ErrorCode.CANCEL);
        handler.cancel(frame);
        return XFuture.completedFuture(new NullStreamWriter());
    }
    return invokeWebAppErrorController(t, ctx, handler, failedRoute);
}
Also used : RstStreamFrame(com.webpieces.http2.api.dto.lowlevel.RstStreamFrame) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) SpecificRouterInvokeException(org.webpieces.router.api.exceptions.SpecificRouterInvokeException)

Example 49 with StreamWriter

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

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

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