summaryrefslogtreecommitdiffstats
path: root/gerrit-elasticsearch/src/main/java/com/google/gerrit/elasticsearch/builders/XContentBuilder.java
blob: 06427f1079651173a9c40554ab37d8168ae15dca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (C) 2018 The Android Open Source Project, 2009-2015 Elasticsearch
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.elasticsearch.builders;

import static java.time.format.DateTimeFormatter.ISO_INSTANT;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.google.common.base.Charsets;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.util.Date;

/** A trimmed down and modified version of org.elasticsearch.common.xcontent.XContentBuilder. */
public final class XContentBuilder implements Closeable {

  private final JsonGenerator generator;

  private final ByteArrayOutputStream bos = new ByteArrayOutputStream();

  /**
   * Constructs a new builder. Make sure to call {@link #close()} when the builder is done with.
   * Inspired from org.elasticsearch.common.xcontent.json.JsonXContent static block.
   */
  public XContentBuilder() throws IOException {
    JsonFactory jsonFactory = new JsonFactory();
    jsonFactory.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    jsonFactory.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, true);
    jsonFactory.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
    jsonFactory.configure(
        JsonFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW,
        false); // this trips on many mappings now...
    this.generator = jsonFactory.createGenerator(bos, JsonEncoding.UTF8);
  }

  public XContentBuilder startObject(String name) throws IOException {
    field(name);
    startObject();
    return this;
  }

  public XContentBuilder startObject() throws IOException {
    generator.writeStartObject();
    return this;
  }

  public XContentBuilder endObject() throws IOException {
    generator.writeEndObject();
    return this;
  }

  public void startArray(String name) throws IOException {
    field(name);
    startArray();
  }

  private void startArray() throws IOException {
    generator.writeStartArray();
  }

  public void endArray() throws IOException {
    generator.writeEndArray();
  }

  public XContentBuilder field(String name) throws IOException {
    generator.writeFieldName(name);
    return this;
  }

  public XContentBuilder field(String name, String value) throws IOException {
    field(name);
    generator.writeString(value);
    return this;
  }

  public XContentBuilder field(String name, int value) throws IOException {
    field(name);
    generator.writeNumber(value);
    return this;
  }

  public XContentBuilder field(String name, Iterable<?> value) throws IOException {
    startArray(name);
    for (Object o : value) {
      value(o);
    }
    endArray();
    return this;
  }

  public XContentBuilder field(String name, Object value) throws IOException {
    field(name);
    writeValue(value);
    return this;
  }

  public XContentBuilder value(Object value) throws IOException {
    writeValue(value);
    return this;
  }

  public XContentBuilder field(String name, boolean value) throws IOException {
    field(name);
    generator.writeBoolean(value);
    return this;
  }

  public XContentBuilder value(String value) throws IOException {
    generator.writeString(value);
    return this;
  }

  @Override
  public void close() {
    try {
      generator.close();
    } catch (IOException e) {
      // ignore
    }
  }

  /** Returns a string representation of the builder (only applicable for text based xcontent). */
  public String string() {
    close();
    byte[] bytesArray = bos.toByteArray();
    return new String(bytesArray, Charsets.UTF_8);
  }

  private void writeValue(Object value) throws IOException {
    if (value == null) {
      generator.writeNull();
      return;
    }
    Class<?> type = value.getClass();
    if (type == String.class) {
      generator.writeString((String) value);
    } else if (type == Integer.class) {
      generator.writeNumber(((Integer) value));
    } else if (type == byte[].class) {
      generator.writeBinary((byte[]) value);
    } else if (value instanceof Date) {
      generator.writeString(ISO_INSTANT.format(((Date) value).toInstant()));
    } else {
      // if this is a "value" object, like enum, DistanceUnit, ..., just toString it
      // yea, it can be misleading when toString a Java class, but really, jackson should be used in
      // that case
      generator.writeString(value.toString());
      // throw new ElasticsearchIllegalArgumentException("type not supported for generic value
      // conversion: " + type);
    }
  }
}