Java Code Examples for org.elasticsearch.common.xcontent.XContentBuilder#prettyPrint()

The following examples show how to use org.elasticsearch.common.xcontent.XContentBuilder#prettyPrint() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HomeAction.java    From zentity with Apache License 2.0 6 votes vote down vote up
@Override
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) {

    Properties props = ZentityPlugin.properties();
    Boolean pretty = restRequest.paramAsBoolean("pretty", false);

    return channel -> {
        XContentBuilder content = XContentFactory.jsonBuilder();
        if (pretty)
            content.prettyPrint();
        content.startObject();
        content.field("name", props.getProperty("name"));
        content.field("description", props.getProperty("description"));
        content.field("website", props.getProperty("zentity.website"));
        content.startObject("version");
        content.field("zentity", props.getProperty("zentity.version"));
        content.field("elasticsearch", props.getProperty("elasticsearch.version"));
        content.endObject();
        content.endObject();
        channel.sendResponse(new BytesRestResponse(RestStatus.OK, content));
    };
}
 
Example 2
Source File: ESTestCase.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Randomly shuffles the fields inside objects parsed using the {@link XContentParser} passed in.
 * Recursively goes through inner objects and also shuffles them. Exceptions for this
 * recursive shuffling behavior can be made by passing in the names of fields which
 * internally should stay untouched.
 */
public static XContentBuilder shuffleXContent(XContentParser parser, boolean prettyPrint, String... exceptFieldNames)
        throws IOException {
    XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType());
    if (prettyPrint) {
        xContentBuilder.prettyPrint();
    }
    Token token = parser.currentToken() == null ? parser.nextToken() : parser.currentToken();
    if (token == Token.START_ARRAY) {
        List<Object> shuffledList = shuffleList(parser.listOrderedMap(), new HashSet<>(Arrays.asList(exceptFieldNames)));
        return xContentBuilder.value(shuffledList);
    }
    //we need a sorted map for reproducibility, as we are going to shuffle its keys and write XContent back
    Map<String, Object> shuffledMap = shuffleMap((LinkedHashMap<String, Object>)parser.mapOrdered(),
        new HashSet<>(Arrays.asList(exceptFieldNames)));
    return xContentBuilder.map(shuffledMap);
}
 
Example 3
Source File: SetupAction.java    From zentity with Apache License 2.0 5 votes vote down vote up
@Override
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) {

    // Parse request
    Boolean pretty = restRequest.paramAsBoolean("pretty", false);
    int numberOfShards = restRequest.paramAsInt("number_of_shards", 1);
    int numberOfReplicas = restRequest.paramAsInt("number_of_replicas", 1);
    Method method = restRequest.method();

    return channel -> {
        try {
            if (method == POST) {

                createIndex(client, numberOfShards, numberOfReplicas);
                XContentBuilder content = XContentFactory.jsonBuilder();
                if (pretty)
                    content.prettyPrint();
                content.startObject().field("acknowledged", true).endObject();
                channel.sendResponse(new BytesRestResponse(RestStatus.OK, content));

            } else {
                throw new NotImplementedException("Method and endpoint not implemented.");
            }
        } catch (NotImplementedException e) {
            channel.sendResponse(new BytesRestResponse(channel, RestStatus.NOT_IMPLEMENTED, e));
        }
    };
}
 
Example 4
Source File: SortBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.prettyPrint();
        toXContent(builder, EMPTY_PARAMS);
        return builder.string();
    } catch (Exception e) {
        throw new ElasticsearchException("Failed to build query", e);
    }
}
 
Example 5
Source File: ToXContentToBytes.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public final String toString() {
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.prettyPrint();
        toXContent(builder, EMPTY_PARAMS);
        return builder.string();
    } catch (Exception e) {
        return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}";
    }
}
 
Example 6
Source File: ElasticSearchHelper.java    From camunda-bpm-elasticsearch with Apache License 2.0 5 votes vote down vote up
public static String convertRequestToJson(ActionRequest request) throws IOException {
    BytesStreamOutput bytesStreamOutput = new BytesStreamOutput();
    request.writeTo(bytesStreamOutput);

    XContentBuilder builder = XContentFactory.jsonBuilder(bytesStreamOutput);
    builder.prettyPrint();

//    builder.startObject();
//    builder.endObject();
    BytesArray bytesArray = builder.bytes().toBytesArray();
    return new String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length());
  }
 
Example 7
Source File: ElasticSearchHelper.java    From camunda-bpm-elasticsearch with Apache License 2.0 5 votes vote down vote up
public static String convertResponseToJson(ActionResponse response) throws IOException {
    BytesStreamOutput bytesStreamOutput = new BytesStreamOutput();
    response.writeTo(bytesStreamOutput);

    XContentBuilder builder = XContentFactory.jsonBuilder(bytesStreamOutput);
    builder.prettyPrint();

//    builder.startObject();
//    builder.endObject();
    return builder.bytes().toUtf8();
  }
 
Example 8
Source File: Strings.java    From crate with Apache License 2.0 5 votes vote down vote up
private static XContentBuilder createBuilder(boolean pretty, boolean human) throws IOException {
    XContentBuilder builder = JsonXContent.contentBuilder();
    if (pretty) {
        builder.prettyPrint();
    }
    if (human) {
        builder.humanReadable(true);
    }
    return builder;
}
 
Example 9
Source File: ShardStateMetaData.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected XContentBuilder newXContentBuilder(XContentType type, OutputStream stream) throws IOException {
    XContentBuilder xContentBuilder = super.newXContentBuilder(type, stream);
    xContentBuilder.prettyPrint();
    return xContentBuilder;
}
 
Example 10
Source File: NodeMetaData.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
protected XContentBuilder newXContentBuilder(XContentType type, OutputStream stream) throws IOException {
    XContentBuilder xContentBuilder = super.newXContentBuilder(type, stream);
    xContentBuilder.prettyPrint();
    return xContentBuilder;
}
 
Example 11
Source File: ShardStateMetaData.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
protected XContentBuilder newXContentBuilder(XContentType type, OutputStream stream) throws IOException {
    XContentBuilder xContentBuilder = super.newXContentBuilder(type, stream);
    xContentBuilder.prettyPrint();
    return xContentBuilder;
}