Example usage for io.vertx.core Future future

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

Introduction

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

Prototype

future

Source Link

Usage

From source file:org.folio.auth.permissions_module.impl.MongoPermissionsStore.java

@Override
public Future<Boolean> removePermission(String permission, String tenant) {
    Future<Boolean> future = Future.future();
    JsonObject query = new JsonObject().put("permission_name", permission).put("tenant", tenant);
    mongoClient.find("permissions", query, res -> {
        if (res.failed() || res.result().size() < 1) {
            future.fail("Unable to find permission to delete");
        } else {/*ww w  . j a  va  2 s  .com*/
            //Find all permissions that list this permission as a sub
            /*
            JsonObject subQuery = new JsonObject().put("sub_permissions", new JsonObject()
              .put("$in", new JsonArray().add(permission)));
             */
            JsonObject subUpdate = new JsonObject().put("$pullAll",
                    new JsonObject().put("sub_permissions", new JsonArray().add(permission)));
            mongoClient.updateCollectionWithOptions("permissions", new JsonObject(), subUpdate, updateOptions,
                    res2 -> {
                        if (res2.failed()) {
                            future.fail("Unable to remove sub permissions: " + res2.cause().getMessage());
                        } else {
                            //Now delete the actual permission, since the sub permissions are clean
                            mongoClient.removeDocument("permissions", query, res3 -> {
                                if (res3.failed()) {
                                    future.fail("Unable to delete permission: " + res3.cause().getMessage());
                                } else {
                                    future.complete(true);
                                }
                            });
                        }
                    });
        }
    });
    return future;
}

From source file:org.folio.auth.permissions_module.impl.MongoPermissionsStore.java

@Override
public Future<Boolean> removeSubPermission(String permission, String sub, String tenant) {
    Future<Boolean> future = Future.future();
    JsonObject query = new JsonObject().put("permission_name", permission).put("tenant", tenant);
    JsonObject update = new JsonObject().put("$pull",
            new JsonObject().put("sub_permissions", new JsonObject().put("$in", new JsonArray().add(sub))));
    mongoClient.update("permissions", query, update, res -> {
        if (!res.succeeded()) {
            future.fail("Unable to remove sub permissiion");
        } else {/*  w  w w .  jav a 2 s .  c o m*/
            future.complete(true);
        }
    });
    return future;
}

From source file:org.folio.auth.permissions_module.impl.MongoPermissionsStore.java

@Override
public Future<Boolean> addUser(String user, String tenant) {
    JsonObject query = new JsonObject().put("username", user).put("tenant", tenant);
    Future<Boolean> future = Future.future();
    mongoClient.find("users", query, res -> {
        if (res.result().size() > 0) {
            future.complete(false);//  w  ww.  j av  a  2  s .  c  om
        } else {
            JsonObject insert = new JsonObject().put("username", user).put("tenant", tenant).put("permissions",
                    new JsonArray());
            mongoClient.insert("users", insert, res2 -> {
                if (res2.succeeded()) {
                    future.complete(true);
                } else {
                    future.fail("Unable to insert new user");
                }
            });
        }
    });
    return future;
}

From source file:org.folio.auth.permissions_module.impl.MongoPermissionsStore.java

@Override
public Future<Boolean> removeUser(String user, String tenant) {
    JsonObject query = new JsonObject().put("username", user).put("tenant", tenant);
    Future<Boolean> future = Future.future();
    mongoClient.find("users", query, res -> {
        if (res.result().size() > 0) {
            future.complete(false);//from w  w  w  .  j  a  v  a2 s.c  o m
        } else {
            mongoClient.removeDocument("users", query, res2 -> {
                if (res.failed()) {
                    future.fail("Unable to remove user");
                } else {
                    future.complete(true);
                }
            });
        }
    });
    return future;
}

From source file:org.folio.auth.permissions_module.impl.MongoPermissionsStore.java

@Override
public Future<Boolean> addPermissionToUser(String user, String permission, String tenant) {
    logger.debug("Permissions> Adding permission '" + permission + "' to user '" + user + "'");
    JsonObject query = new JsonObject().put("username", user).put("tenant", tenant);
    Future<Boolean> future = Future.future();
    mongoClient.find("users", query, res -> {
        if (res.failed()) {
            future.fail(res.cause().getMessage());
        } else {/*w w  w. jav  a2  s.  c o  m*/
            logger.debug("Permissions> Found user successfully");
            JsonArray currentPermissions = res.result().get(0).getJsonArray("permissions");
            if (res.result().size() < 0) {
                future.complete(false);
            } else {
                this.getPermissionsForUser(user, tenant, false).setHandler(res2 -> {
                    if (res2.failed()) {
                        future.fail("Error querying current permissions: " + res2.cause().getMessage());
                    } else {
                        JsonArray currentPerms = res2.result();
                        if (currentPerms.contains(permission)) {
                            future.fail("Permission already exists in user");
                        } else {
                            JsonObject push = new JsonObject().put("$push",
                                    new JsonObject().put("permissions", permission));
                            logger.debug("Permissions> Using user query '" + query.encode()
                                    + "' and update query '" + push.encode() + "'");
                            mongoClient.updateCollection("users", query, push, res3 -> {
                                if (res3.failed()) {
                                    future.fail("Unable to add permission:" + res3.cause().getMessage());
                                } else {
                                    logger.debug("Permissions> Permission '" + permission + "' added to user '"
                                            + user + "'");
                                    future.complete(true);
                                }
                            });
                        }
                    }
                });
            }
        }
    });
    return future;
}

From source file:org.folio.auth.permissions_module.impl.MongoPermissionsStore.java

@Override
public Future<Boolean> removePermissionFromUser(String user, String permission, String tenant) {
    JsonObject query = new JsonObject().put("username", user).put("tenant", tenant);
    Future<Boolean> future = Future.future();
    mongoClient.find("users", query, res -> {
        if (res.failed() || res.result().size() < 1) {
            future.complete(false);/*from w  w w .ja va 2s  . c om*/
        } else {
            getPermissionsForUser(user, tenant, false).setHandler(res2 -> {
                if (res2.failed()) {
                    future.fail("Unable to retrieve initial permissions: " + res2.cause().getMessage());
                } else {
                    JsonArray permissions = res2.result();
                    logger.debug("PERMISSIONS: " + permissions.encode());
                    if (!permissions.contains(permission)) {
                        future.complete(true);
                    } else {
                        permissions.remove(permission);
                        JsonObject update = new JsonObject().put("$set",
                                new JsonObject().put("permissions", permissions));
                        mongoClient.updateCollection("users", query, update, res3 -> {
                            if (res3.failed()) {
                                future.fail("Unable to remove permission:" + res3.cause().getMessage());
                            } else {
                                logger.debug("Permissions> Permission '" + permission + "' removed");
                                future.complete(true);
                            }
                        });
                    }
                }
            });
        }
    });
    return future;
}

From source file:org.folio.auth.permissions_module.impl.MongoPermissionsStore.java

public Future<JsonArray> getPermissionsForUser(String user, String tenant, Boolean expand) {
    JsonObject query = new JsonObject().put("username", user).put("tenant", tenant);
    Future<JsonArray> future = Future.future();
    mongoClient.find("users", query, (AsyncResult<List<JsonObject>> res) -> {
        if (res.result().size() < 1) {
            future.fail("No such user");
        } else {//  ww w.  j  a v  a 2s. c  om
            JsonObject userObject = res.result().get(0);
            logger.debug("Permissions> Permissions for user " + user + ": " + userObject.encode());
            JsonArray permissions = userObject.getJsonArray("permissions");
            if (expand) {
                ArrayList<Future> futureList = new ArrayList<>();
                for (Object o : permissions) {
                    String permissionName = (String) o;
                    Future<JsonArray> expandPermissionFuture = this.getExpandedPermissions(permissionName,
                            tenant);
                    futureList.add(expandPermissionFuture);
                }
                logger.debug("Permissions> Assembling CompositeFuture of " + futureList.size()
                        + " permissions to expand");
                CompositeFuture compositeFuture = CompositeFuture.all(futureList);
                compositeFuture.setHandler(res2 -> {
                    if (res2.failed()) {
                        future.fail(res2.cause());
                    } else {
                        JsonArray allPermissions = new JsonArray();
                        for (Future f : futureList) {
                            JsonArray arr = (JsonArray) f.result();
                            for (Object o : arr) {
                                String perm = (String) o;
                                if (!allPermissions.contains(perm)) {
                                    allPermissions.add(perm);
                                }
                            }
                        }
                        logger.debug(
                                "Permissions> Returning list of " + allPermissions.size() + " permissions");
                        future.complete(allPermissions);
                    }
                });
            } else {
                future.complete(permissions);
            }
        }
    });
    return future;
}

From source file:org.folio.auth.permissions_module.impl.MongoPermissionsStore.java

@Override
public Future<JsonObject> getPermission(String permission, String tenant) {
    Future<JsonObject> future = Future.future();
    JsonObject query = new JsonObject().put("permission_name", permission).put("tenant", tenant);
    mongoClient.find("permissions", query, res -> {
        if (res.failed()) {
            future.fail(res.cause().getMessage());
        } else if (res.result().size() < 1) {
            future.fail("No results for that name");
        } else {//from  ww w  . j a  v  a 2 s.c  om
            future.complete(res.result().get(0));
        }
    });
    return future;//To change body of generated methods, choose Tools | Templates.
}

From source file:org.folio.auth.permissions_module.impl.MongoPermissionsStore.java

@Override
public Future<JsonObject> getUser(String username, String tenant) {
    Future<JsonObject> future = Future.future();
    JsonObject query = new JsonObject().put("username", username).put("tenant", tenant);
    mongoClient.find("users", query, res -> {
        if (res.failed()) {
            future.fail(res.cause().getMessage());
        } else if (res.result().size() < 1) {
            future.fail("No results for that name");
        } else {/*from   w ww .  java2 s  .co  m*/
            future.complete(res.result().get(0));
        }
    });
    return future;
}

From source file:org.sfs.SfsServer.java

License:Apache License

@Override
public void start(Future<Void> startedResult) {
    SfsServer _this = this;
    LOGGER.info("Starting verticle " + _this);
    SfsSingletonServer candidate = new SfsSingletonServer();
    if (setDelegate(candidate)) {
        candidate.init(vertx, vertx.getOrCreateContext());
        candidate.start(Future.future());
    }//from   ww w .  j  av a  2 s.  com
    vertx.setPeriodic(100, event -> {
        SfsSingletonServer server = getDelegate();
        if (server != null && server.isStarted()) {
            Throwable startException = server.getStartException();
            vertx.cancelTimer(event);
            if (startException == null) {
                vertxContext = new VertxContext<Server>(this);
                httpsClient = server.createHttpClient(vertx, true);
                httpClient = server.createHttpClient(vertx, false);
                server.initRxSchedulers(vertx);
                server.initHttpListeners(vertxContext, true)
                        .doOnNext(httpServers1 -> httpServers.addAll(httpServers1)).map(new ToVoid<>())
                        .subscribe(new Subscriber<Void>() {
                            @Override
                            public void onCompleted() {
                                LOGGER.info("Started verticle " + _this);
                                startedResult.complete();
                            }

                            @Override
                            public void onError(Throwable e) {
                                LOGGER.error("Failed to start verticle " + _this, e);
                                startedResult.fail(e);
                            }

                            @Override
                            public void onNext(Void aVoid) {

                            }
                        });
            } else {
                LOGGER.error("Failed to start verticle " + _this, startException);
                startedResult.fail(startException);
            }
        }
    });
}