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

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

Introduction

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

Prototype

public String encodePrettily() 

Source Link

Document

Encode this JSON object a a string, with whitespace to make the object easier to read by a human, or other sentient organism.

Usage

From source file:org.pac4j.vertx.Pac4jAuthHandlerIntegrationTestBase.java

License:Apache License

private Handler<RoutingContext> loginSuccessHandler() {
    // Just write out the routing context's user principal, we can then validate against this
    return rc -> {
        final User user = rc.user();
        final JsonObject json = user != null ? user.principal() : new JsonObject();
        rc.response().end(json.encodePrettily());
    };/*from w  w  w .j a  v a 2s .c o m*/
}

From source file:org.sfs.elasticsearch.AbstractBulkUpdateEndableWriteStream.java

License:Apache License

protected Observable<Void> write0(SearchHit data) {
    count++;//from   w w w .j  a va2 s  . c o m
    return Defer.aVoid().flatMap(aVoid -> {
        String index = data.getIndex();
        String type = data.getType();
        String id = data.getId();
        long version = data.getVersion();
        JsonObject jsonObject = new JsonObject(data.getSourceAsString());
        return transform(jsonObject, id, version).map(oUpdatedJsonObject -> {
            if (oUpdatedJsonObject.isPresent()) {
                JsonObject updatedJsonObject = oUpdatedJsonObject.get();

                if (!equal(updatedJsonObject, jsonObject)) {

                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(format("Index Request {%s,%s,%s,%d} = %s, original was %s", index, type,
                                id, data.getVersion(), updatedJsonObject.encodePrettily(),
                                jsonObject.encodePrettily()));
                    }

                    IndexRequestBuilder request = elasticsearch.get().prepareIndex(index, type, id)
                            .setSource(updatedJsonObject.encode())
                            .setTimeout(timeValueMillis(elasticsearch.getDefaultIndexTimeout() - 10));
                    if (version >= 0) {
                        request = request.setVersion(version);
                    }

                    if (bulkRequest == null) {
                        bulkRequest = elasticsearch.get().prepareBulk().setTimeout(timeValueMillis(
                                (elasticsearch.getDefaultIndexTimeout() * writeQueueMaxSize) - 10));
                    }
                    bulkRequest.add(request);

                }
            } else {
                DeleteRequestBuilder request = elasticsearch.get().prepareDelete(index, type, id)
                        .setTimeout(timeValueMillis(elasticsearch.getDefaultDeleteTimeout() - 10));
                if (version >= 0) {
                    request = request.setVersion(version);
                }

                if (bulkRequest == null) {
                    bulkRequest = elasticsearch.get().prepareBulk().setTimeout(
                            timeValueMillis((elasticsearch.getDefaultIndexTimeout() * writeQueueMaxSize) - 10));
                }
                bulkRequest.add(request);
            }
            return (Void) null;
        }).onErrorResumeNext(throwable -> {
            LOGGER.warn("Handling Error", throwable);
            return just(null);
        }).singleOrDefault(null);
    });
}

From source file:org.sfs.elasticsearch.account.PersistAccount.java

License:Apache License

@Override
public Observable<Optional<PersistentAccount>> call(final TransientAccount transientAccount) {

    final Elasticsearch elasticSearch = vertxContext.verticle().elasticsearch();

    final JsonObject source = transientAccount.toJsonObject();

    String encoded;//w w w .  ja  va  2 s.co  m

    if (LOGGER.isDebugEnabled()) {
        encoded = source.encodePrettily();
        LOGGER.debug(format("Index Request {%s,%s,%s} = %s", elasticSearch.defaultType(),
                elasticSearch.accountIndex(), transientAccount.getId(), encoded));
    } else {
        encoded = source.encode();
    }

    IndexRequestBuilder request = elasticSearch.get()
            .prepareIndex(elasticSearch.accountIndex(), elasticSearch.defaultType(), transientAccount.getId())
            .setCreate(true).setTimeout(timeValueMillis(elasticSearch.getDefaultIndexTimeout() - 10))
            .setSource(encoded);

    return elasticSearch.execute(vertxContext, request, elasticSearch.getDefaultIndexTimeout())
            .map(indexResponse -> {
                if (indexResponse.isPresent()) {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(format("Index Response {%s,%s,%s} = %s", indexResponse.get().getType(),
                                indexResponse.get().getIndex(), transientAccount.getId(),
                                Jsonify.toString(indexResponse.get())));
                    }
                    return of(fromIndexResponse(indexResponse.get(), source));
                } else {
                    LOGGER.debug(format("Index Response {%s,%s,%s} = %s", elasticSearch.defaultType(),
                            elasticSearch.accountIndex(), transientAccount.getId(), "null"));
                    return absent();
                }
            });
}

From source file:org.sfs.elasticsearch.account.UpdateAccount.java

License:Apache License

@Override
public Observable<Optional<PersistentAccount>> call(final PersistentAccount persistentAccount) {

    final Elasticsearch elasticSearch = vertxContext.verticle().elasticsearch();

    final JsonObject source = persistentAccount.toJsonObject();

    String encoded;//www .j a v  a 2  s  .co  m

    if (LOGGER.isDebugEnabled()) {
        encoded = source.encodePrettily();
        LOGGER.debug(format("Index Request {%s,%s,%s,%d} = %s", elasticSearch.defaultType(),
                elasticSearch.accountIndex(), persistentAccount.getId(),
                persistentAccount.getPersistentVersion(), encoded));
    } else {
        encoded = source.encode();
    }

    IndexRequestBuilder request = elasticSearch.get()
            .prepareIndex(elasticSearch.accountIndex(), elasticSearch.defaultType(), persistentAccount.getId())
            .setVersion(persistentAccount.getPersistentVersion())
            .setTimeout(timeValueMillis(elasticSearch.getDefaultIndexTimeout() - 10)).setSource(encoded);

    return elasticSearch.execute(vertxContext, request, elasticSearch.getDefaultIndexTimeout())
            .map(indexResponse -> {
                if (indexResponse.isPresent()) {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(format("Index Response {%s,%s,%s,%d} = %s", elasticSearch.defaultType(),
                                elasticSearch.accountIndex(), persistentAccount.getId(),
                                persistentAccount.getPersistentVersion(),
                                Jsonify.toString(indexResponse.get())));
                    }
                    return of(fromIndexResponse(indexResponse.get(), source));
                } else {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(format("Index Response {%s,%s,%s,%d} = %s", elasticSearch.defaultType(),
                                elasticSearch.accountIndex(), persistentAccount.getId(),
                                persistentAccount.getPersistentVersion(), "null"));
                    }
                    return absent();
                }
            });
}

From source file:org.sfs.elasticsearch.container.ListContainerKeys.java

License:Apache License

@Override
public Observable<PersistentContainerKey> call(PersistentContainer persistentContainer) {

    final Elasticsearch elasticSearch = vertxContext.verticle().elasticsearch();

    final JsonObject source = persistentContainer.toJsonObject();

    String encoded;/*from   w  ww  .ja v a 2  s .com*/

    if (LOGGER.isDebugEnabled()) {
        encoded = source.encodePrettily();
        LOGGER.debug(format("Search Request {%s,%s,%s,%d} = %s", elasticSearch.defaultType(),
                elasticSearch.containerKeyIndex(), persistentContainer.getId(),
                persistentContainer.getPersistentVersion(), encoded));
    } else {
        encoded = source.encode();
    }

    SearchRequestBuilder request = elasticSearch.get().prepareSearch(elasticSearch.containerKeyIndex())
            .setTypes(elasticSearch.defaultType()).setVersion(true)
            .setQuery(termQuery("container_id", persistentContainer.getId()))
            .setTimeout(timeValueMillis(elasticSearch.getDefaultIndexTimeout() - 10)).setSource(encoded);

    return elasticSearch.execute(vertxContext, request, elasticSearch.getDefaultIndexTimeout())
            .flatMap(oSearchResponse -> {
                if (oSearchResponse.isPresent()) {
                    SearchResponse searchResponse = oSearchResponse.get();
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(format("Search Response {%s,%s,%s,%d} = %s", elasticSearch.defaultType(),
                                elasticSearch.containerIndex(), persistentContainer.getId(),
                                persistentContainer.getPersistentVersion(), Jsonify.toString(searchResponse)));
                    }
                    return from(searchResponse.getHits());
                } else {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(format("Index Response {%s,%s,%s,%d} = %s", elasticSearch.defaultType(),
                                elasticSearch.containerIndex(), persistentContainer.getId(),
                                persistentContainer.getPersistentVersion(), "null"));
                    }
                    return empty();
                }
            }).map(searchHitFields -> fromSearchHit(persistentContainer, searchHitFields));
}

From source file:org.sfs.elasticsearch.container.PersistContainer.java

License:Apache License

@Override
public Observable<Optional<PersistentContainer>> call(final TransientContainer transientContainer) {

    final Elasticsearch elasticSearch = vertxContext.verticle().elasticsearch();

    final JsonObject source = transientContainer.toJsonObject();

    String encoded;//from   ww  w .ja v  a 2  s . c om

    if (LOGGER.isDebugEnabled()) {
        encoded = source.encodePrettily();
        LOGGER.debug(format("Index Request {%s,%s,%s} = %s", elasticSearch.defaultType(),
                elasticSearch.containerIndex(), transientContainer.getId(), encoded));
    } else {
        encoded = source.encode();
    }

    IndexRequestBuilder request = elasticSearch.get()
            .prepareIndex(elasticSearch.containerIndex(), elasticSearch.defaultType(),
                    transientContainer.getId())
            .setCreate(true).setTimeout(timeValueMillis(elasticSearch.getDefaultIndexTimeout() - 10))
            .setSource(encoded);

    return elasticSearch.execute(vertxContext, request, elasticSearch.getDefaultIndexTimeout())
            .map(indexResponse -> {
                if (indexResponse.isPresent()) {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(format("Index Response {%s,%s,%s} = %s", elasticSearch.defaultType(),
                                elasticSearch.containerIndex(), transientContainer.getId(),
                                Jsonify.toString(indexResponse.get())));
                    }
                    return of(fromIndexResponse(transientContainer.getParent(), indexResponse.get(), source));
                } else {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(format("Index Response {%s,%s,%s} = %s", elasticSearch.defaultType(),
                                elasticSearch.containerIndex(), transientContainer.getId(), "null"));
                    }
                    return absent();
                }
            });
}

From source file:org.sfs.elasticsearch.container.UpdateContainer.java

License:Apache License

@Override
public Observable<Optional<PersistentContainer>> call(final PersistentContainer persistentContainer) {

    final Elasticsearch elasticSearch = vertxContext.verticle().elasticsearch();

    final JsonObject source = persistentContainer.toJsonObject();

    String encoded;//from   w w  w . ja v  a  2  s . c  o m

    if (LOGGER.isDebugEnabled()) {
        encoded = source.encodePrettily();
        LOGGER.debug(format("Index Request {%s,%s,%s,%d} = %s", elasticSearch.defaultType(),
                elasticSearch.containerIndex(), persistentContainer.getId(),
                persistentContainer.getPersistentVersion(), encoded));
    } else {
        encoded = source.encode();
    }

    IndexRequestBuilder request = elasticSearch.get()
            .prepareIndex(elasticSearch.containerIndex(), elasticSearch.defaultType(),
                    persistentContainer.getId())
            .setVersion(persistentContainer.getPersistentVersion())
            .setTimeout(timeValueMillis(elasticSearch.getDefaultIndexTimeout() - 10)).setSource(encoded);

    return elasticSearch.execute(vertxContext, request, elasticSearch.getDefaultIndexTimeout())
            .map(indexResponse -> {
                if (indexResponse.isPresent()) {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(format("Index Response {%s,%s,%s,%d} = %s", elasticSearch.defaultType(),
                                elasticSearch.containerIndex(), persistentContainer.getId(),
                                persistentContainer.getPersistentVersion(),
                                Jsonify.toString(indexResponse.get())));
                    }
                    return of(fromIndexResponse(persistentContainer.getParent(), indexResponse.get(), source));
                } else {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(format("Index Response {%s,%s,%s,%d} = %s", elasticSearch.defaultType(),
                                elasticSearch.containerIndex(), persistentContainer.getId(),
                                persistentContainer.getPersistentVersion(), "null"));
                    }
                    return absent();
                }
            });
}

From source file:org.sfs.elasticsearch.containerkey.PersistContainerKey.java

License:Apache License

@Override
public Observable<Holder2<TransientContainerKey, Optional<PersistentContainerKey>>> call(
        final TransientContainerKey transientContainerKey) {

    final Elasticsearch elasticSearch = vertxContext.verticle().elasticsearch();

    final JsonObject document = transientContainerKey.toJsonObject();

    String encoded;//from  w  w w .  jav  a 2s . co m

    if (LOGGER.isDebugEnabled()) {
        encoded = document.encodePrettily();
        LOGGER.debug(format("Index Request {%s,%s,%s} = %s", elasticSearch.defaultType(),
                elasticSearch.containerKeyIndex(), transientContainerKey.getId(), encoded));
    } else {
        encoded = document.encode();
    }

    IndexRequestBuilder request = elasticSearch.get()
            .prepareIndex(elasticSearch.containerKeyIndex(), elasticSearch.defaultType(),
                    transientContainerKey.getId())
            .setCreate(true) // put only if absent
            .setTimeout(timeValueMillis(elasticSearch.getDefaultIndexTimeout() - 10)).setSource(encoded);

    return elasticSearch.execute(vertxContext, request, elasticSearch.getDefaultIndexTimeout())
            .map(indexResponse -> {
                Holder2<TransientContainerKey, Optional<PersistentContainerKey>> output = new Holder2<>();
                output.value0 = transientContainerKey;
                if (indexResponse.isPresent()) {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(format("Index Response {%s,%s,%s} = %s", elasticSearch.defaultType(),
                                elasticSearch.containerKeyIndex(), transientContainerKey.getId(),
                                Jsonify.toString(indexResponse.get())));
                    }
                    output.value1 = of(fromIndexResponse(transientContainerKey.getPersistentContainer(),
                            indexResponse.get(), document));
                } else {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(format("Index Response {%s,%s,%s} = %s", elasticSearch.defaultType(),
                                elasticSearch.containerKeyIndex(), transientContainerKey.getId(), "null"));
                    }
                    output.value1 = absent();
                }
                return output;
            });
}

From source file:org.sfs.elasticsearch.containerkey.UpdateContainerKey.java

License:Apache License

@Override
public Observable<Holder2<PersistentContainerKey, Optional<PersistentContainerKey>>> call(
        final PersistentContainerKey persistentContainerKey) {
    final JsonObject source = persistentContainerKey.toJsonObject();

    final Elasticsearch elasticSearch = vertxContext.verticle().elasticsearch();

    String encoded;/*from  w w  w  .  j  a va 2  s  .  com*/

    if (LOGGER.isDebugEnabled()) {
        encoded = source.encodePrettily();
        LOGGER.debug(format("Index Request {%s,%s,%s,%d} = %s", elasticSearch.defaultType(),
                elasticSearch.containerKeyIndex(), persistentContainerKey.getId(),
                persistentContainerKey.getPersistentVersion(), encoded));
    } else {
        encoded = source.encode();
    }

    IndexRequestBuilder request = elasticSearch.get()
            .prepareIndex(elasticSearch.containerKeyIndex(), elasticSearch.defaultType(),
                    persistentContainerKey.getId())
            .setVersion(persistentContainerKey.getPersistentVersion())
            .setTimeout(timeValueMillis(elasticSearch.getDefaultIndexTimeout() - 10)).setSource(encoded);

    return elasticSearch.execute(vertxContext, request, elasticSearch.getDefaultIndexTimeout())
            .map(indexResponse -> {
                Holder2<PersistentContainerKey, Optional<PersistentContainerKey>> output = new Holder2<>();
                output.value0 = persistentContainerKey;
                if (indexResponse.isPresent()) {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(format("Index Response {%s,%s,%s,%d} = %s", elasticSearch.defaultType(),
                                elasticSearch.containerKeyIndex(), persistentContainerKey.getId(),
                                persistentContainerKey.getPersistentVersion(),
                                Jsonify.toString(indexResponse.get())));
                    }
                    output.value1 = of(fromIndexResponse(persistentContainerKey.getPersistentContainer(),
                            indexResponse.get(), source));
                } else {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug(format("Index Response {%s,%s,%s,%d} = %s", elasticSearch.defaultType(),
                                elasticSearch.containerKeyIndex(), persistentContainerKey.getId(),
                                persistentContainerKey.getPersistentVersion(), "null"));
                    }
                    output.value1 = absent();
                }
                return output;
            });
}

From source file:org.sfs.elasticsearch.nodes.PersistServiceDef.java

License:Apache License

@Override
public Observable<Optional<PersistentServiceDef>> call(final TransientServiceDef transientServiceDef) {

    final Elasticsearch elasticSearch = vertxContext.verticle().elasticsearch();

    final String id = transientServiceDef.getId();

    final JsonObject source = transientServiceDef.toJsonObject();

    String encoded;//from ww w .ja va 2  s .c  o m

    if (LOGGER.isDebugEnabled()) {
        encoded = source.encodePrettily();
        LOGGER.debug(format("Request {%s,%s,%s} = %s", elasticSearch.defaultType(),
                elasticSearch.serviceDefTypeIndex(), id, encoded));
    } else {
        encoded = source.encode();
    }

    IndexRequestBuilder request = elasticSearch.get()
            .prepareIndex(elasticSearch.serviceDefTypeIndex(), elasticSearch.defaultType(), id)
            .setSource(encoded).setTTL(ttl).setCreate(true)
            .setTimeout(timeValueMillis(elasticSearch.getDefaultIndexTimeout() - 10));

    return elasticSearch.execute(vertxContext, request, elasticSearch.getDefaultIndexTimeout())
            .map(new Func1<Optional<IndexResponse>, Optional<PersistentServiceDef>>() {
                @Override
                public Optional<PersistentServiceDef> call(Optional<IndexResponse> oIndexResponse) {
                    if (oIndexResponse.isPresent()) {
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug(format("Response {%s,%s,%s} = %s", elasticSearch.defaultType(),
                                    elasticSearch.serviceDefTypeIndex(), id,
                                    Jsonify.toString(oIndexResponse.get())));
                        }
                        return fromNullable(fromIndexResponse(oIndexResponse.get(), source));
                    } else {
                        LOGGER.debug(format("Response {%s,%s,%s} = %s", elasticSearch.defaultType(),
                                elasticSearch.serviceDefTypeIndex(), id, "null"));
                        return absent();
                    }
                }
            });
}