Example usage for io.vertx.core Future succeededFuture

List of usage examples for io.vertx.core Future succeededFuture

Introduction

In this page you can find the example usage for io.vertx.core Future succeededFuture.

Prototype

static <T> Future<T> succeededFuture(T result) 

Source Link

Document

Created a succeeded future with the specified result.

Usage

From source file:de.braintags.netrelay.controller.persistence.DataAuthorizator.java

License:Open Source License

/**
 * Check if an AuthHandler exists in Context and wether current user has the rights for all actions to be processed
 * //from w w  w  . ja  v  a 2  s .  c  om
 * @param context
 * @param resolvedCaptureCollections
 * @param handler
 *          is getting true, if rights to all actions are granted or if no Authentication handler is active;
 *          false, if right for only one action is not granted
 */
public static void checkAuthorization(RoutingContext context, IAuthenticatable member,
        List<CaptureMap> resolvedCaptureCollections, Handler<AsyncResult<Boolean>> handler) {
    AuthHandler auth = context.get(AuthenticationController.AUTH_HANDLER_PROP);
    if (auth != null && auth instanceof RedirectAuthHandlerBt) {
        if (member == null) {
            // this is an error
            handler.handle(Future.failedFuture(new IllegalArgumentException(
                    "This should not happen, we need an instance of IAuthenticatable here")));
        } else {
            checkAuthorization(resolvedCaptureCollections, auth, member, handler);
        }
    } else {
        handler.handle(Future.succeededFuture(true));
    }
}

From source file:de.braintags.netrelay.controller.persistence.DataAuthorizator.java

License:Open Source License

/**
 * @param resolvedCaptureCollections//from  ww  w  . ja  va 2s.c  om
 * @param auth
 * @param member
 * @param handler
 */
private static void checkAuthorization(List<CaptureMap> resolvedCaptureCollections, AuthHandler auth,
        IAuthenticatable member, Handler<AsyncResult<Boolean>> handler) {
    boolean granted = true;
    for (CaptureMap map : resolvedCaptureCollections) {
        if (!checkAuthorization(auth, member, map)) {
            granted = false;
            break;
        }
    }
    handler.handle(Future.succeededFuture(granted));
}

From source file:de.braintags.netrelay.controller.persistence.PersistenceController.java

License:Open Source License

/**
 * Check wether current user has the rights for all actions to be processed
 * //  w  ww.  j  a  va 2s.co  m
 * @param context
 * @param resolvedCaptureCollections
 * @param handler
 *          is getting true, if rights to all actions are granted or if no Authentication handler is active;
 *          false, if right for only one action is not granted
 */
private void checkAuthorization(RoutingContext context, List<CaptureMap> resolvedCaptureCollections,
        Handler<AsyncResult<Boolean>> handler) {
    AuthHandler auth = context.get(AuthenticationController.AUTH_HANDLER_PROP);
    if (auth != null && auth instanceof RedirectAuthHandlerBt) {
        MemberUtil.getCurrentUser(context, getNetRelay(), result -> {
            if (result.failed()) {
                handler.handle(Future.failedFuture(result.cause()));
            } else {
                IAuthenticatable member = result.result();
                if (member == null) {
                    // this is an error
                    handler.handle(Future.failedFuture(new IllegalArgumentException(
                            "This should not happen, we need an instance of IAuthenticatable here")));
                } else {
                    checkAuthorization(resolvedCaptureCollections, auth, member, handler);
                }
            }
        });
    } else {
        handler.handle(Future.succeededFuture(true));
    }
}

From source file:de.braintags.netrelay.controller.persistence.PersistenceController.java

License:Open Source License

/**
 * @param resolvedCaptureCollections/*from   w w  w  . j  av  a  2s  .c  o  m*/
 * @param auth
 * @param member
 * @param handler
 */
private void checkAuthorization(List<CaptureMap> resolvedCaptureCollections, AuthHandler auth,
        IAuthenticatable member, Handler<AsyncResult<Boolean>> handler) {
    boolean granted = true;
    for (CaptureMap map : resolvedCaptureCollections) {
        if (!checkAuthorization(auth, member, map)) {
            granted = false;
            break;
        }
    }
    handler.handle(Future.succeededFuture(granted));
}

From source file:de.braintags.netrelay.mapping.NetRelayStoreObject.java

License:Open Source License

private void createEntity(Handler<AsyncResult<T>> handler) {
    if (entity != null) {
        handler.handle(Future.succeededFuture(entity));
    } else if (hasProperty(getMapper().getIdField())) {
        Object id = get(getMapper().getIdField());
        IQuery<T> query = netRelay.getDatastore().createQuery(getMapper().getMapperClass());
        query.field(query.getMapper().getIdField().getName()).is(id);
        query.execute(qrr -> {/*from   ww  w.  jav a  2  s .c  o m*/
            if (qrr.failed()) {
                handler.handle(Future.failedFuture(qrr.cause()));
            } else {
                IQueryResult<T> qr = qrr.result();
                if (!qr.iterator().hasNext()) {
                    handler.handle(Future
                            .failedFuture(new NoSuchRecordException("Could not find record with ID " + id)));
                } else {
                    qr.iterator().next(ir -> {
                        if (ir.failed()) {
                            handler.handle(Future.failedFuture(ir.cause()));
                        } else {
                            handler.handle(Future.succeededFuture(ir.result()));
                        }
                    });
                }
            }
        });
    } else {
        T returnObject = getMapper().getObjectFactory().createInstance(getMapper().getMapperClass());
        handler.handle(Future.succeededFuture(returnObject));
    }
}

From source file:de.braintags.netrelay.mapping.NetRelayStoreObjectFactory.java

License:Open Source License

@Override
public <T> void createStoreObject(IMapper<T> mapper, T entity,
        Handler<AsyncResult<IStoreObject<T, ?>>> handler) {
    NetRelayStoreObject<T> storeObject = new NetRelayStoreObject<>(mapper, entity);
    storeObject.initFromEntity(initResult -> {
        if (initResult.failed()) {
            handler.handle(Future.failedFuture(initResult.cause()));
        } else {//ww w  .ja  v a  2 s.  com
            handler.handle(Future.succeededFuture(storeObject));
        }
    });
}

From source file:de.braintags.netrelay.mapping.NetRelayStoreObjectFactory.java

License:Open Source License

/**
 * This method is called in cases, where a subobject needs to be updated, which can't be loaded from the datastore
 * //from  w  ww.j  a v a 2 s. com
 * @param storedObject
 * @param entity
 * @param mapper
 * @param handler
 */
public <T> void createStoreObject(Map<String, String> storedObject, T entity, IMapper<T> mapper,
        Handler<AsyncResult<IStoreObject<T, ?>>> handler) {
    NetRelayStoreObject<T> storeObject = new NetRelayStoreObject<>(storedObject, entity, mapper, netRelay);
    storeObject.initToEntity(result -> {
        if (result.failed()) {
            handler.handle(Future.failedFuture(result.cause()));
        } else {
            handler.handle(Future.succeededFuture(storeObject));
        }
    });
}

From source file:de.braintags.netrelay.MemberUtil.java

License:Open Source License

/**
 * If a user is logged in, this method fetches the fitting instance of {@link IAuthenticatable} from the datastore,
 * stores it in the context, so that it can be fetched again by this method or by the method
 * {@link #getCurrentUser(RoutingContext)}. Additionally the IAuthenticatable is stored as result in the result
 * handler//  w w w  . j av  a 2s .co m
 * 
 * @param context
 *          the current context
 * @param netRelay
 *          the instance of NetRelay
 * @param resultHandler
 *          the result habndler, which is getting a found instance of IAuthenticatable or null, if no user is logged
 *          in
 */
public static final void getCurrentUser(RoutingContext context, NetRelay netRelay,
        Handler<AsyncResult<IAuthenticatable>> resultHandler) {
    IAuthenticatable member = getCurrentUser(context);
    if (member != null) {
        resultHandler.handle(Future.succeededFuture(member));
    } else {
        recoverContextUser(context);
        User user = context.user();
        if (user == null) {
            resultHandler.handle(Future.succeededFuture(null));
        } else {
            readUser(context, netRelay, user, resultHandler);
        }
    }
}

From source file:de.braintags.netrelay.MemberUtil.java

License:Open Source License

/**
 * Fetch the instance of IAuthenticatable from the datastore and store it into the context
 * // w w  w.java  2 s .c  o  m
 * @param context
 * @param netRelay
 * @param user
 * @param resultHandler
 */
private static void readUser(RoutingContext context, NetRelay netRelay, User user,
        Handler<AsyncResult<IAuthenticatable>> resultHandler) {
    Class<? extends IAuthenticatable> mapperClass = getMapperClass(context, netRelay);
    if (user instanceof MongoUser) {
        readMongoUser(context, netRelay, (MongoUser) user, resultHandler, mapperClass);
    } else if (user instanceof DatastoreUser) {
        resultHandler.handle(Future.succeededFuture(((DatastoreUser) user).getAuthenticatable()));
    } else {
        Future<IAuthenticatable> future = Future.failedFuture(
                new UnsupportedOperationException("user type not supported: " + user.getClass().getName()));
        resultHandler.handle(future);
    }
}

From source file:de.braintags.netrelay.MemberUtil.java

License:Open Source License

/**
 * @param context/*from   w  w  w. jav  a 2 s  . com*/
 * @param netRelay
 * @param user
 * @param resultHandler
 * @param mapperClass
 */
private static void readMongoUser(RoutingContext context, NetRelay netRelay, MongoUser user,
        Handler<AsyncResult<IAuthenticatable>> resultHandler, Class<? extends IAuthenticatable> mapperClass) {
    String id = user.principal().getString("_id");
    IQuery<? extends IAuthenticatable> query = netRelay.getDatastore().createQuery(mapperClass);
    query.field(query.getMapper().getIdField().getName()).is(id);
    query.execute(qr -> {
        if (qr.failed()) {
            resultHandler.handle(Future.failedFuture(qr.cause()));
        } else {
            if (qr.result().size() <= 0) {
                resultHandler.handle(
                        Future.failedFuture(new NoSuchRecordException("no record found for principal with id "
                                + id + " in mapper " + mapperClass.getName())));
            }
            qr.result().iterator().next(ir -> {
                if (ir.failed()) {
                    resultHandler.handle(Future.failedFuture(ir.cause()));
                } else {
                    IAuthenticatable auth = ir.result();
                    setCurrentUser(auth, context);
                    resultHandler.handle(Future.succeededFuture(auth));
                }
            });
        }
    });
}