summaryrefslogtreecommitdiff
path: root/core/src/main/java/org/elasticsearch/rest/AbstractRestChannel.java
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/main/java/org/elasticsearch/rest/AbstractRestChannel.java')
-rw-r--r--core/src/main/java/org/elasticsearch/rest/AbstractRestChannel.java37
1 files changed, 22 insertions, 15 deletions
diff --git a/core/src/main/java/org/elasticsearch/rest/AbstractRestChannel.java b/core/src/main/java/org/elasticsearch/rest/AbstractRestChannel.java
index 4b3505e97e..bdc78c82dd 100644
--- a/core/src/main/java/org/elasticsearch/rest/AbstractRestChannel.java
+++ b/core/src/main/java/org/elasticsearch/rest/AbstractRestChannel.java
@@ -20,7 +20,6 @@ package org.elasticsearch.rest;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
-import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
@@ -58,28 +57,37 @@ public abstract class AbstractRestChannel implements RestChannel {
@Override
public XContentBuilder newBuilder() throws IOException {
- return newBuilder(request.hasContent() ? request.content() : null, true);
+ return newBuilder(request.getXContentType(), true);
}
@Override
public XContentBuilder newErrorBuilder() throws IOException {
// Disable filtering when building error responses
- return newBuilder(request.hasContent() ? request.content() : null, false);
+ return newBuilder(request.getXContentType(), false);
}
+ /**
+ * Creates a new {@link XContentBuilder} for a response to be sent using this channel. The builder's type is determined by the following
+ * logic. If the request has a format parameter that will be used to attempt to map to an {@link XContentType}. If there is no format
+ * parameter, the HTTP Accept header is checked to see if it can be matched to a {@link XContentType}. If this first attempt to map
+ * fails, the request content type will be used if the value is not {@code null}; if the value is {@code null} the output format falls
+ * back to JSON.
+ */
@Override
- public XContentBuilder newBuilder(@Nullable BytesReference autoDetectSource, boolean useFiltering) throws IOException {
- XContentType contentType = XContentType.fromMediaTypeOrFormat(format);
- if (contentType == null) {
- // try and guess it from the auto detect source
- if (autoDetectSource != null) {
- contentType = XContentFactory.xContentType(autoDetectSource);
+ public XContentBuilder newBuilder(@Nullable XContentType requestContentType, boolean useFiltering) throws IOException {
+ // try to determine the response content type from the media type or the format query string parameter, with the format parameter
+ // taking precedence over the Accept header
+ XContentType responseContentType = XContentType.fromMediaTypeOrFormat(format);
+ if (responseContentType == null) {
+ if (requestContentType != null) {
+ // if there was a parsed content-type for the incoming request use that since no format was specified using the query
+ // string parameter or the HTTP Accept header
+ responseContentType = requestContentType;
+ } else {
+ // default to JSON output when all else fails
+ responseContentType = XContentType.JSON;
}
}
- if (contentType == null) {
- // default to JSON
- contentType = XContentType.JSON;
- }
Set<String> includes = Collections.emptySet();
Set<String> excludes = Collections.emptySet();
@@ -89,7 +97,7 @@ public abstract class AbstractRestChannel implements RestChannel {
excludes = filters.stream().filter(EXCLUDE_FILTER).map(f -> f.substring(1)).collect(toSet());
}
- XContentBuilder builder = new XContentBuilder(XContentFactory.xContent(contentType), bytesOutput(), includes, excludes);
+ XContentBuilder builder = new XContentBuilder(XContentFactory.xContent(responseContentType), bytesOutput(), includes, excludes);
if (pretty) {
builder.prettyPrint().lfAtEnd();
}
@@ -125,5 +133,4 @@ public abstract class AbstractRestChannel implements RestChannel {
public boolean detailedErrorsEnabled() {
return detailedErrorsEnabled;
}
-
}