Example usage for io.vertx.core.json JsonObject JsonObject

List of usage examples for io.vertx.core.json JsonObject JsonObject

Introduction

In this page you can find the example usage for io.vertx.core.json JsonObject JsonObject.

Prototype

public JsonObject() 

Source Link

Document

Create a new, empty instance

Usage

From source file:com.themonkee.vertx.web.impl.MongoSessionStoreImpl.java

License:Open Source License

public MongoSessionStoreImpl(Vertx vertx, MongoClient mongoClient, JsonObject options,
        Future<MongoSessionStore> resultHandler) {
    this.random = new PRNG(vertx);
    this.vertx = vertx;
    this.mongoClient = mongoClient;
    if (options != null) {
        if (options.containsKey("collection"))
            this.sessionCollection = options.getString("collection");
    }//  w  ww  .ja v  a2  s.  c om

    Future<Void> futCreateColl = Future.future();
    // try to create collection, if it is created or already exists its OK
    this.mongoClient.createCollection(this.sessionCollection, (AsyncResult<Void> res) -> {
        if (res.succeeded() || res.cause().getMessage().contains("collection already exists")) {
            futCreateColl.complete();
        } else {
            futCreateColl.fail(res.cause());
        }
    });

    futCreateColl.compose(v -> {
        // create the session expiry index
        // SessionImpl sets _expire field to Date when session document must be deleted on save
        // so we set expireAfterSeconds to 0 so its deleted when that Date is hit
        // see https://docs.mongodb.com/manual/tutorial/expire-data/
        this.mongoClient.createIndexWithOptions(this.sessionCollection,
                new JsonObject().put(SessionImpl.EXPIRE_FIELD, 1),
                new IndexOptions().expireAfter(0L, TimeUnit.SECONDS), res -> {
                    if (res.succeeded()) {
                        resultHandler.complete(this);
                    } else {
                        resultHandler.fail(res.cause());
                    }
                });
    }, resultHandler);
}

From source file:com.themonkee.vertx.web.impl.MongoSessionStoreImpl.java

License:Open Source License

@Override
public void get(String id, Handler<AsyncResult<Session>> handler) {
    this.mongoClient.findOne(this.sessionCollection, new JsonObject().put("_id", id), null, r -> {
        if (r.result() == null) {
            handler.handle(Future.succeededFuture(null));
        } else {/*from ww w.  j a va  2  s  .  c om*/
            handler.handle(Future.succeededFuture(new SessionImpl(random).fromJsonObject(r.result())));
        }
    });
}

From source file:com.themonkee.vertx.web.impl.MongoSessionStoreImpl.java

License:Open Source License

@Override
public void delete(String id, Handler<AsyncResult<Boolean>> handler) {
    this.mongoClient.removeDocument(this.sessionCollection, new JsonObject().put("_id", id),
            r -> handler.handle(Future.succeededFuture(r.succeeded())));
}

From source file:com.themonkee.vertx.web.impl.MongoSessionStoreImpl.java

License:Open Source License

@Override
public void put(Session session, Handler<AsyncResult<Boolean>> handler) {
    SessionImpl s = (SessionImpl) session;
    this.mongoClient.replaceDocumentsWithOptions(this.sessionCollection, new JsonObject().put("_id", s.id()),
            s.toJsonObject(), new UpdateOptions().setUpsert(true).setMulti(false),
            r -> handler.handle(Future.succeededFuture(r.succeeded())));
}

From source file:com.themonkee.vertx.web.impl.MongoSessionStoreImpl.java

License:Open Source License

@Override
public void clear(Handler<AsyncResult<Boolean>> handler) {
    this.mongoClient.removeDocuments(this.sessionCollection, new JsonObject(),
            c -> handler.handle(Future.succeededFuture(c.succeeded())));
}

From source file:com.waves_rsp.ikb4stream.communication.web.WebCommunication.java

License:Open Source License

/**
 * Starts the server, implemented by vertx.
 *
 * @param databaseReader {@link IDatabaseReader} is the connection to database to get Event
 * @throws NullPointerException if databaseReader is null
 * @see WebCommunication#PROPERTIES_MANAGER
 * @see WebCommunication#server//w  ww .  ja v  a 2s. c  om
 */
@Override
public void start(IDatabaseReader databaseReader) {
    Objects.requireNonNull(databaseReader);
    configureDatabaseReader(databaseReader);
    LOGGER.info("Starting WebCommunication module");
    server = Vertx.vertx();
    DeploymentOptions deploymentOptions = new DeploymentOptions();
    int port = 8081;
    try {
        PROPERTIES_MANAGER.getProperty("communications.web.port");
        port = Integer.parseInt(PROPERTIES_MANAGER.getProperty("communications.web.port"));
        LOGGER.info("WebCommunication Server set on port {}", port);
    } catch (NumberFormatException e) {
        LOGGER.error("Invalid 'communications.web.port' value");
        return;
    } catch (IllegalArgumentException e) {
        LOGGER.info("Property 'communications.web.port' not set. Use default value for score.target");
    }
    JsonObject jsonObject = new JsonObject();
    jsonObject.put("http.port", port);
    deploymentOptions.setConfig(jsonObject);
    server.deployVerticle(VertxServer.class.getName(), deploymentOptions);
}

From source file:com.zyxist.dirtyplayground.core.database.DatabaseManagerImpl.java

License:Apache License

@Override
public void start() throws InterruptedException, ServiceException {
    CountDownLatch untilStarted = new CountDownLatch(1);
    mongo = MongoClient.createShared(vertx, new JsonObject().put("db_name", "journal"));
    mongo.save("dummy", new JsonObject(), (res) -> {
        connectionSuccessful = res.succeeded();
        untilStarted.countDown();//from  w  ww .  ja v  a  2  s .  c  om
    });
    untilStarted.await();
    if (!connectionSuccessful) {
        mongo.close();
        throw new ServiceException("Cannot start MongoDB client: connection failed.");
    }
}

From source file:de.braintags.netrelay.controller.api.DataTablesController.java

License:Open Source License

private JsonObject createJsonObject(IMapper mapper, List<IStoreObject<?, ?>> selection,
        DataTableLinkDescriptor descr, long completeCount, long tableCount) {
    LOGGER.info("tableCount: " + tableCount + ", completeCount: " + completeCount);
    JsonObject json = new JsonObject();
    json.put("recordsTotal", tableCount);
    json.put("recordsFiltered", completeCount);
    JsonArray resArray = new JsonArray();
    json.put("data", resArray);
    for (IStoreObject<?, ?> ob : selection) {
        resArray.add(handleObject(mapper, ob, descr));
    }/*from  w  w w  . ja  v a2s . c o  m*/
    return json;
}

From source file:de.braintags.netrelay.controller.authentication.AbstractAuthProviderController.java

License:Open Source License

private AuthProvider initDatastoreAuthProvider(String mapper) {
    Class mapperClass = getNetRelay().getSettings().getMappingDefinitions().getMapperClass(mapper);
    if (mapperClass == null) {
        throw new InitException("Could not find defined mapper class for mapper '" + mapper + "'");
    }/*from  w w  w  . j  a  va 2 s .com*/
    JsonObject config = new JsonObject();
    config.put(IDatastoreAuth.PROPERTY_MAPPER_CLASS_NAME, mapperClass.getName());
    return IDatastoreAuth.create(getNetRelay().getDatastore(), config);
}

From source file:de.braintags.netrelay.controller.authentication.AbstractAuthProviderController.java

License:Open Source License

/**
 * Init the Authentication Service/*from  www  .j  a  v  a2s  .c om*/
 */
private AuthProvider initMongoAuthProvider(String mapper) {
    IDataStore store = getNetRelay().getDatastore();
    if (!(store instanceof MongoDataStore)) {
        throw new IllegalArgumentException("MongoAuthProvider expects a MongoDataStore");
    }
    JsonObject config = new JsonObject();
    String saltStyle = readProperty(MongoAuth.PROPERTY_SALT_STYLE, HashSaltStyle.NO_SALT.toString(), false);
    config.put(MongoAuth.PROPERTY_SALT_STYLE, HashSaltStyle.valueOf(saltStyle));
    MongoAuth auth = MongoAuth.create((MongoClient) ((MongoDataStore) store).getClient(), config);

    String passwordFieldName = readProperty(MongoAuth.PROPERTY_PASSWORD_FIELD, null, true);
    Class mapperClass = getNetRelay().getSettings().getMappingDefinitions().getMapperClass(mapper);
    if (mapperClass == null) {
        throw new InitException("Could not find mapper with name " + mapper);
    }
    IMapper mapperDef = getNetRelay().getDatastore().getMapperFactory().getMapper(mapperClass);
    IField pwField = mapperDef.getField(passwordFieldName);
    if (pwField.getEncoder() != null) {
        throw new InitException(
                "MongoAuth does not support the annotation Encoder, please use DatastoreAuth instead");
    }
    auth.setPasswordField(passwordFieldName);
    auth.setUsernameField(readProperty(MongoAuth.PROPERTY_USERNAME_FIELD, null, true));
    auth.setCollectionName(mapper);

    String roleField = readProperty(MongoAuth.PROPERTY_ROLE_FIELD, null, false);
    if (roleField != null) {
        auth.setRoleField(roleField);
    }
    String saltField = readProperty(MongoAuth.PROPERTY_SALT_FIELD, null, false);
    if (saltField != null) {
        auth.setSaltField(saltField);
    }

    return auth;
}