Search in sources :

Example 41 with XFuture

use of org.webpieces.util.futures.XFuture 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 42 with XFuture

use of org.webpieces.util.futures.XFuture 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 43 with XFuture

use of org.webpieces.util.futures.XFuture 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 44 with XFuture

use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.

the class ConnectedChannels method closeChannels.

public XFuture<Void> closeChannels() {
    // first prevent other threads from calling above functions ever again
    closed = true;
    List<XFuture<Void>> futures = new ArrayList<>();
    for (Channel c : connectedChannels.keySet()) {
        futures.add(c.close());
    }
    @SuppressWarnings("rawtypes") XFuture[] array = futures.toArray(new XFuture[0]);
    return XFuture.allOf(array);
}
Also used : XFuture(org.webpieces.util.futures.XFuture) Channel(org.webpieces.nio.api.channels.Channel) ArrayList(java.util.ArrayList)

Example 45 with XFuture

use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.

the class IntegTestClientNotRead method write.

private void write(Channel channel, String reason, final int counter) {
    log.info("write from client. reason=" + reason);
    byte[] data = new byte[2000];
    ByteBuffer buffer = ByteBuffer.wrap(data);
    XFuture<Void> write = channel.write(buffer);
    final int count = counter + 1;
    if (counter >= 100) {
        write.thenAccept(p -> write(channel, "wrote data from client", count)).whenComplete((r, e) -> finished(r, e));
    } else {
        write.thenAcceptAsync(p -> write(channel, "wrote data async", 0), executor).whenComplete((r, e) -> finished(r, e));
    }
}
Also used : Channel(org.webpieces.nio.api.channels.Channel) DataListener(org.webpieces.nio.api.handlers.DataListener) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Logger(org.slf4j.Logger) Executor(java.util.concurrent.Executor) LoggerFactory(org.slf4j.LoggerFactory) AsyncConfig(org.webpieces.asyncserver.api.AsyncConfig) Timer(java.util.Timer) AsyncServer(org.webpieces.asyncserver.api.AsyncServer) BackpressureConfig(org.webpieces.nio.api.BackpressureConfig) InetSocketAddress(java.net.InetSocketAddress) AsyncServerMgrFactory(org.webpieces.asyncserver.api.AsyncServerMgrFactory) ByteBuffer(java.nio.ByteBuffer) Executors(java.util.concurrent.Executors) ChannelManager(org.webpieces.nio.api.ChannelManager) Metrics(io.micrometer.core.instrument.Metrics) AsyncServerManager(org.webpieces.asyncserver.api.AsyncServerManager) ChannelManagerFactory(org.webpieces.nio.api.ChannelManagerFactory) XFuture(org.webpieces.util.futures.XFuture) TwoPools(org.webpieces.data.api.TwoPools) TimerTask(java.util.TimerTask) TCPChannel(org.webpieces.nio.api.channels.TCPChannel) ByteBuffer(java.nio.ByteBuffer)

Aggregations

XFuture (org.webpieces.util.futures.XFuture)71 Test (org.junit.Test)21 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)20 ByteBuffer (java.nio.ByteBuffer)16 Logger (org.slf4j.Logger)15 LoggerFactory (org.slf4j.LoggerFactory)15 ArrayList (java.util.ArrayList)14 List (java.util.List)13 Map (java.util.Map)12 DataWrapper (org.webpieces.data.api.DataWrapper)12 HttpFullRequest (org.webpieces.httpclient11.api.HttpFullRequest)12 HttpFullResponse (org.webpieces.httpclient11.api.HttpFullResponse)12 NotFoundException (org.webpieces.http.exception.NotFoundException)11 AbstractWebpiecesTest (org.webpieces.webserver.test.AbstractWebpiecesTest)11 ResponseWrapper (org.webpieces.webserver.test.ResponseWrapper)11 Http2Request (com.webpieces.http2.api.dto.highlevel.Http2Request)10 PrivateWebserverForTest (org.webpieces.webserver.PrivateWebserverForTest)10 StreamRef (com.webpieces.http2.api.streaming.StreamRef)9 RequestContext (org.webpieces.ctx.api.RequestContext)9 Http2Response (com.webpieces.http2.api.dto.highlevel.Http2Response)8