diff --git a/buildSrc/src/main/resources/checkstyle_suppressions.xml b/buildSrc/src/main/resources/checkstyle_suppressions.xml
index eba6dbfc819..dba0529923f 100644
--- a/buildSrc/src/main/resources/checkstyle_suppressions.xml
+++ b/buildSrc/src/main/resources/checkstyle_suppressions.xml
@@ -349,7 +349,6 @@
-
diff --git a/core/src/main/java/org/elasticsearch/ElasticsearchException.java b/core/src/main/java/org/elasticsearch/ElasticsearchException.java
index 97711eed427..7698790a782 100644
--- a/core/src/main/java/org/elasticsearch/ElasticsearchException.java
+++ b/core/src/main/java/org/elasticsearch/ElasticsearchException.java
@@ -44,7 +44,7 @@ import java.util.stream.Collectors;
import static java.util.Collections.unmodifiableMap;
import static org.elasticsearch.cluster.metadata.IndexMetaData.INDEX_UUID_NA_VALUE;
-import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureFieldName;
+import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.elasticsearch.common.xcontent.XContentParserUtils.throwUnknownField;
/**
@@ -357,7 +357,8 @@ public class ElasticsearchException extends RuntimeException implements ToXConte
* instances.
*/
public static ElasticsearchException fromXContent(XContentParser parser) throws IOException {
- XContentParser.Token token = ensureFieldName(parser.nextToken(), parser::getTokenLocation);
+ XContentParser.Token token = parser.nextToken();
+ ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser::getTokenLocation);
String type = null, reason = null, stack = null;
ElasticsearchException cause = null;
diff --git a/core/src/main/java/org/elasticsearch/action/get/GetResponse.java b/core/src/main/java/org/elasticsearch/action/get/GetResponse.java
index 1b347a2d05d..3ba21c447e7 100644
--- a/core/src/main/java/org/elasticsearch/action/get/GetResponse.java
+++ b/core/src/main/java/org/elasticsearch/action/get/GetResponse.java
@@ -27,12 +27,14 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
+import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.get.GetField;
import org.elasticsearch.index.get.GetResult;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
+import java.util.Objects;
/**
* The response of a get action.
@@ -42,7 +44,7 @@ import java.util.Map;
*/
public class GetResponse extends ActionResponse implements Iterable, ToXContent {
- private GetResult getResult;
+ GetResult getResult;
GetResponse() {
}
@@ -156,6 +158,11 @@ public class GetResponse extends ActionResponse implements Iterable, T
return getResult.toXContent(builder, params);
}
+ public static GetResponse fromXContent(XContentParser parser) throws IOException {
+ GetResult getResult = GetResult.fromXContent(parser);
+ return new GetResponse(getResult);
+ }
+
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
@@ -168,6 +175,23 @@ public class GetResponse extends ActionResponse implements Iterable, T
getResult.writeTo(out);
}
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ GetResponse getResponse = (GetResponse) o;
+ return Objects.equals(getResult, getResponse.getResult);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(getResult);
+ }
+
@Override
public String toString() {
return Strings.toString(this, true);
diff --git a/core/src/main/java/org/elasticsearch/action/get/MultiGetResponse.java b/core/src/main/java/org/elasticsearch/action/get/MultiGetResponse.java
index e1fe435fd10..4fc766e2b30 100644
--- a/core/src/main/java/org/elasticsearch/action/get/MultiGetResponse.java
+++ b/core/src/main/java/org/elasticsearch/action/get/MultiGetResponse.java
@@ -140,9 +140,7 @@ public class MultiGetResponse extends ActionResponse implements Iterable {
@Override
public boolean equals(Object obj) {
- if (obj == null) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
return false;
}
return bytes().equals(((Text) obj).bytes());
diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java
index 1625289e528..b288a314ee0 100644
--- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java
+++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java
@@ -349,4 +349,22 @@ public class XContentHelper {
builder.rawField(field, source);
}
}
+
+ /**
+ * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided
+ * {@link XContentType}. Wraps the output into a new anonymous object depending on the value of the wrapInObject argument.
+ */
+ public static BytesReference toXContent(ToXContent toXContent, XContentType xContentType, boolean wrapInObject) throws IOException {
+ try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
+ if (wrapInObject) {
+ builder.startObject();
+ }
+ toXContent.toXContent(builder, ToXContent.EMPTY_PARAMS);
+ if (wrapInObject) {
+ builder.endObject();
+ }
+ return builder.bytes();
+ }
+ }
+
}
diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentParserUtils.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentParserUtils.java
index a2180152444..846582fa5f4 100644
--- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentParserUtils.java
+++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentParserUtils.java
@@ -22,7 +22,6 @@ package org.elasticsearch.common.xcontent;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.xcontent.XContentParser.Token;
-import java.io.IOException;
import java.util.Locale;
import java.util.function.Supplier;
@@ -35,34 +34,6 @@ public final class XContentParserUtils {
private XContentParserUtils() {
}
- /**
- * Makes sure that current token is of type {@link XContentParser.Token#FIELD_NAME}
- *
- * @return the token
- * @throws ParsingException if the token is not of type {@link XContentParser.Token#FIELD_NAME}
- */
- public static Token ensureFieldName(Token token, Supplier location) throws IOException {
- return ensureType(Token.FIELD_NAME, token, location);
- }
-
- /**
- * Makes sure that current token is of type {@link XContentParser.Token#FIELD_NAME} and the the field name is equal to the provided one
- *
- * @return the token
- * @throws ParsingException if the token is not of type {@link XContentParser.Token#FIELD_NAME} or is not equal to the given
- * field name
- */
- public static Token ensureFieldName(XContentParser parser, Token token, String fieldName) throws IOException {
- Token t = ensureType(Token.FIELD_NAME, token, parser::getTokenLocation);
-
- String current = parser.currentName() != null ? parser.currentName() : "";
- if (current.equals(fieldName) == false) {
- String message = "Failed to parse object: expecting field with name [%s] but found [%s]";
- throw new ParsingException(parser.getTokenLocation(), String.format(Locale.ROOT, message, fieldName, current));
- }
- return t;
- }
-
/**
* @throws ParsingException with a "unknown field found" reason
*/
@@ -72,16 +43,14 @@ public final class XContentParserUtils {
}
/**
- * Makes sure that current token is of the expected type
+ * Makes sure that provided token is of the expected type
*
- * @return the token
* @throws ParsingException if the token is not equal to the expected type
*/
- private static Token ensureType(Token expected, Token current, Supplier location) {
- if (current != expected) {
+ public static void ensureExpectedToken(Token expected, Token actual, Supplier location) {
+ if (actual != expected) {
String message = "Failed to parse object: expecting token of type [%s] but found [%s]";
- throw new ParsingException(location.get(), String.format(Locale.ROOT, message, expected, current));
+ throw new ParsingException(location.get(), String.format(Locale.ROOT, message, expected, actual));
}
- return current;
}
}
diff --git a/core/src/main/java/org/elasticsearch/index/get/GetField.java b/core/src/main/java/org/elasticsearch/index/get/GetField.java
index be3b8d6a257..a4169ddfe51 100644
--- a/core/src/main/java/org/elasticsearch/index/get/GetField.java
+++ b/core/src/main/java/org/elasticsearch/index/get/GetField.java
@@ -19,17 +19,25 @@
package org.elasticsearch.index.get;
+import org.elasticsearch.common.ParsingException;
+import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
+import org.elasticsearch.common.xcontent.ToXContent;
+import org.elasticsearch.common.xcontent.XContentBuilder;
+import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.MapperService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
+import java.util.Objects;
-public class GetField implements Streamable, Iterable