Search in sources :

Example 66 with XFuture

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

the class HibernateAsyncController method display.

public XFuture<Render> display(Integer id) {
    EntityManager mgr = Em.get();
    XFuture<Integer> future = new XFuture<Integer>();
    kickOffAsyncResponse(future);
    return future.thenApply(intVal -> runDisplay(mgr, id));
}
Also used : EntityManager(javax.persistence.EntityManager) XFuture(org.webpieces.util.futures.XFuture)

Example 67 with XFuture

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

the class TestPermitQueue method testAddPermits.

@Test
public void testAddPermits() throws InterruptedException {
    PermitQueue queue = new PermitQueue(1);
    XFuture<Long> future1 = new XFuture<Long>();
    svc.addToReturn(future1);
    svc.addToReturn(future1);
    svc.addToReturn(future1);
    svc.addToReturn(future1);
    queue.runRequest(() -> svc.runFunction(1));
    queue.runRequest(() -> svc.runFunction(2));
    queue.runRequest(() -> svc.runFunction(3));
    queue.runRequest(() -> svc.runFunction(4));
    List<Integer> results = svc.getAndClear();
    Assert.assertEquals(1, results.size());
    Assert.assertEquals(1, results.get(0).intValue());
    queue.modifyPermitPoolSize(2);
    List<Integer> results2 = svc.getAndClear();
    Assert.assertEquals(2, results2.size());
    Assert.assertEquals(2, results2.get(0).intValue());
    Assert.assertEquals(3, results2.get(1).intValue());
    queue.releasePermit();
    List<Integer> results3 = svc.getAndClear();
    Assert.assertEquals(1, results3.size());
    Assert.assertEquals(4, results3.get(0).intValue());
}
Also used : FuturePermitQueue(org.webpieces.util.locking.FuturePermitQueue) PermitQueue(org.webpieces.util.locking.PermitQueue) XFuture(org.webpieces.util.futures.XFuture) Test(org.junit.Test)

Example 68 with XFuture

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

the class MyStreamRef method cancel.

@Override
public XFuture<Void> cancel(CancelReason reason) {
    // swap out writer for a cancelled one
    XFuture<StreamWriter> writer = new XFuture<StreamWriter>();
    writer.completeExceptionally(new CancellationException("Cancelled.  reason=" + reason));
    ref.set(writer);
    return XFuture.completedFuture(null);
}
Also used : XFuture(org.webpieces.util.futures.XFuture) CancellationException(java.util.concurrent.CancellationException)

Example 69 with XFuture

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

the class RouterServiceImpl method incomingRequest.

@Override
public StreamRef incomingRequest(Http2Request req, RouterResponseHandler handler) {
    // ******************************************************************************************
    // DO NOT ADD CODE HERE OR ABOVE THIS METHOD in RouterService.  This is our CATCH-ALL point so
    // ANY code above that is not protected from our catch and respond to clients
    // ******************************************************************************************
    String txId = generate();
    // I do NOT like doing Guice creation on the request path(Dagger creation would probably be ok) BUT this
    // is very worth it AND customers can swap out these critical classes if they need to quickly temporarily fix a bug while
    // we work on the bug.  We can easily give customers bug fixes like add binder.bind(ClassWithBug.class).to(BugFixCode.class)
    ProxyStreamHandle proxyHandler = proxyProvider.get();
    proxyHandler.init(handler, req);
    MDC.put("txId", txId);
    // top level handler...
    try {
        RouterStreamRef streamRef = incomingRequestProtected(req, proxyHandler);
        XFuture<StreamWriter> writer = streamRef.getWriter().thenApply(w -> new TxStreamWriter(txId, w));
        XFuture<StreamWriter> finalWriter = writer.handle((r, t) -> {
            if (t == null)
                return XFuture.completedFuture(r);
            XFuture<StreamWriter> fut = proxyHandler.topLevelFailure(req, t);
            return fut;
        }).thenCompose(Function.identity());
        return new RouterStreamRef("routerSevcTop", finalWriter, streamRef);
    } finally {
        MDC.remove("txId");
    }
}
Also used : RouterCookie(org.webpieces.ctx.api.RouterCookie) Provider(javax.inject.Provider) UriInfo(org.webpieces.ctx.api.UriInfo) LoggerFactory(org.slf4j.LoggerFactory) RouterConfig(org.webpieces.router.api.RouterConfig) HashMap(java.util.HashMap) Random(java.util.Random) Singleton(javax.inject.Singleton) Http2Request(com.webpieces.http2.api.dto.highlevel.Http2Request) Function(java.util.function.Function) ContentType(org.webpieces.ctx.api.ContentType) ArrayList(java.util.ArrayList) ParsedContentType(com.webpieces.http2.api.subparsers.ParsedContentType) HashSet(java.util.HashSet) Inject(javax.inject.Inject) RouterRequest(org.webpieces.ctx.api.RouterRequest) Locale(java.util.Locale) AcceptType(com.webpieces.http2.api.subparsers.AcceptType) Map(java.util.Map) Logger(org.slf4j.Logger) RouterStreamHandle(org.webpieces.router.api.RouterStreamHandle) UrlEncodedParser(org.webpieces.util.urlparse.UrlEncodedParser) Set(java.util.Set) RouterService(org.webpieces.router.api.RouterService) Injector(com.google.inject.Injector) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) List(java.util.List) HttpMethod(org.webpieces.ctx.api.HttpMethod) XFuture(org.webpieces.util.futures.XFuture) AcceptMediaType(org.webpieces.ctx.api.AcceptMediaType) StreamRef(com.webpieces.http2.api.streaming.StreamRef) MDC(org.slf4j.MDC) FileMeta(org.webpieces.router.impl.compression.FileMeta) RouterResponseHandler(org.webpieces.router.api.RouterResponseHandler) Entry(java.util.Map.Entry) ObjectStringConverter(org.webpieces.router.api.extensions.ObjectStringConverter) Http2Header(com.webpieces.http2.api.dto.lowlevel.lib.Http2Header) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) HeaderPriorityParser(com.webpieces.http2.api.subparsers.HeaderPriorityParser) Http2HeaderName(com.webpieces.http2.api.dto.lowlevel.lib.Http2HeaderName) HeaderPriorityParserImpl(com.webpieces.http2.impl.subparsers.HeaderPriorityParserImpl) Arguments(org.webpieces.util.cmdline2.Arguments) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) Http2Headers(com.webpieces.http2.api.dto.highlevel.Http2Headers) XFuture(org.webpieces.util.futures.XFuture) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef)

Example 70 with XFuture

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

the class AbstractRouterService method incomingRequest.

public RouterStreamRef incomingRequest(RouterRequest routerRequest, ProxyStreamHandle handler) {
    try {
        Session session = (Session) cookieTranslator.translateCookieToScope(routerRequest, new SessionImpl(translator));
        FlashSub flash = (FlashSub) cookieTranslator.translateCookieToScope(routerRequest, new FlashImpl(translator));
        Validation validation = (Validation) cookieTranslator.translateCookieToScope(routerRequest, new ValidationImpl(translator));
        ApplicationContext ctx = webInjector.getAppContext();
        RequestContext requestCtx = new RequestContext(validation, flash, session, routerRequest, ctx);
        String user = session.get("userId");
        MDC.put("userId", user);
        // TODO(dhiller): This is request heaaders choke point but need ot also perhaps setup streaming choke point
        // here as well
        Current.setContext(requestCtx);
        try {
            return incomingRequestImpl(requestCtx, handler);
        } finally {
            Current.setContext(null);
        }
    } catch (BadCookieException e) {
        // CHEAT: we know this is syncrhonous exception from the translateCookieToScope
        log.warn("This occurs if secret key changed, or you booted another webapp with different key on same port or someone modified the cookie", e);
        XFuture<StreamWriter> writer = handler.sendRedirectAndClearCookie(routerRequest, e.getCookieName());
        return new RouterStreamRef("cookieFailed", writer, null);
    }
}
Also used : BadCookieException(org.webpieces.router.api.exceptions.BadCookieException) XFuture(org.webpieces.util.futures.XFuture) SessionImpl(org.webpieces.router.impl.ctx.SessionImpl) FlashImpl(org.webpieces.router.impl.ctx.FlashImpl) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) ValidationImpl(org.webpieces.router.impl.ctx.ValidationImpl)

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