2010-02-08 15:30:06 +02:00
|
|
|
/*
|
2014-01-06 22:48:02 +01:00
|
|
|
* Licensed to Elasticsearch under one or more contributor
|
|
|
|
* license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright
|
|
|
|
* ownership. Elasticsearch licenses this file to you 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
|
2010-02-08 15:30:06 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-05-28 18:43:29 +03:00
|
|
|
package org.elasticsearch.rest;
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2014-03-31 03:24:14 +02:00
|
|
|
import org.elasticsearch.ElasticsearchException;
|
|
|
|
import org.elasticsearch.common.bytes.BytesArray;
|
|
|
|
import org.elasticsearch.common.bytes.BytesReference;
|
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
|
|
2011-05-28 18:43:29 +03:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2014-03-31 03:24:14 +02:00
|
|
|
import static org.elasticsearch.ExceptionsHelper.detailedMessage;
|
|
|
|
|
|
|
|
public class BytesRestResponse extends RestResponse {
|
2011-05-28 18:43:29 +03:00
|
|
|
|
2014-03-31 03:24:14 +02:00
|
|
|
public static final String TEXT_CONTENT_TYPE = "text/plain; charset=UTF-8";
|
2011-05-28 18:43:29 +03:00
|
|
|
|
2014-03-31 03:24:14 +02:00
|
|
|
private final RestStatus status;
|
|
|
|
private final BytesReference content;
|
|
|
|
private final boolean contentThreadSafe;
|
2011-05-28 18:43:29 +03:00
|
|
|
private final String contentType;
|
|
|
|
|
2014-03-31 03:24:14 +02:00
|
|
|
public BytesRestResponse(RestStatus status) {
|
|
|
|
this(status, TEXT_CONTENT_TYPE, BytesArray.EMPTY, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new response based on {@link XContentBuilder}.
|
|
|
|
*/
|
|
|
|
public BytesRestResponse(RestStatus status, XContentBuilder builder) {
|
|
|
|
this(status, builder.contentType().restContentType(), builder.bytes(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new plain text response.
|
|
|
|
*/
|
|
|
|
public BytesRestResponse(RestStatus status, String content) {
|
|
|
|
this(status, TEXT_CONTENT_TYPE, new BytesArray(content), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new plain text response.
|
|
|
|
*/
|
|
|
|
public BytesRestResponse(RestStatus status, String contentType, String content) {
|
|
|
|
this(status, contentType, new BytesArray(content), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a binary response.
|
|
|
|
*/
|
|
|
|
public BytesRestResponse(RestStatus status, String contentType, byte[] content) {
|
|
|
|
this(status, contentType, new BytesArray(content), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a binary response.
|
|
|
|
*/
|
|
|
|
public BytesRestResponse(RestStatus status, String contentType, BytesReference content, boolean contentThreadSafe) {
|
|
|
|
this.status = status;
|
|
|
|
this.content = content;
|
|
|
|
this.contentThreadSafe = contentThreadSafe;
|
2011-05-28 18:43:29 +03:00
|
|
|
this.contentType = contentType;
|
|
|
|
}
|
|
|
|
|
2014-04-06 23:39:51 +02:00
|
|
|
public BytesRestResponse(RestChannel channel, Throwable t) throws IOException {
|
|
|
|
this(channel, ((t instanceof ElasticsearchException) ? ((ElasticsearchException) t).status() : RestStatus.INTERNAL_SERVER_ERROR), t);
|
2011-05-28 18:43:29 +03:00
|
|
|
}
|
|
|
|
|
2014-04-06 23:39:51 +02:00
|
|
|
public BytesRestResponse(RestChannel channel, RestStatus status, Throwable t) throws IOException {
|
|
|
|
this.status = status;
|
|
|
|
if (channel.request().method() == RestRequest.Method.HEAD) {
|
|
|
|
this.content = BytesArray.EMPTY;
|
|
|
|
this.contentType = TEXT_CONTENT_TYPE;
|
|
|
|
} else {
|
|
|
|
XContentBuilder builder = convert(channel, status, t);
|
|
|
|
this.content = builder.bytes();
|
|
|
|
this.contentType = builder.contentType().restContentType();
|
|
|
|
}
|
2014-08-14 02:55:09 +02:00
|
|
|
if (t instanceof HasRestHeaders) {
|
|
|
|
addHeaders(((HasRestHeaders) t).getHeaders());
|
|
|
|
}
|
2014-04-06 23:39:51 +02:00
|
|
|
this.contentThreadSafe = true;
|
2011-05-28 18:43:29 +03:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
2014-03-31 03:24:14 +02:00
|
|
|
public String contentType() {
|
|
|
|
return this.contentType;
|
2011-05-28 18:43:29 +03:00
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
2014-03-31 03:24:14 +02:00
|
|
|
public boolean contentThreadSafe() {
|
|
|
|
return this.contentThreadSafe;
|
2011-05-28 18:43:29 +03:00
|
|
|
}
|
2010-02-08 15:30:06 +02:00
|
|
|
|
2012-10-27 20:34:32 -04:00
|
|
|
@Override
|
2014-03-31 03:24:14 +02:00
|
|
|
public BytesReference content() {
|
|
|
|
return this.content;
|
2012-10-27 20:34:32 -04:00
|
|
|
}
|
|
|
|
|
2011-12-06 02:42:25 +02:00
|
|
|
@Override
|
|
|
|
public RestStatus status() {
|
2014-03-31 03:24:14 +02:00
|
|
|
return this.status;
|
|
|
|
}
|
|
|
|
|
2014-04-06 23:39:51 +02:00
|
|
|
private static XContentBuilder convert(RestChannel channel, RestStatus status, Throwable t) throws IOException {
|
2015-03-16 17:09:13 -07:00
|
|
|
XContentBuilder builder = channel.newBuilder().startObject();
|
|
|
|
if (t == null) {
|
|
|
|
builder.field("error", "Unknown");
|
|
|
|
} else if (channel.detailedErrorsEnabled()) {
|
|
|
|
builder.field("error", detailedMessage(t));
|
|
|
|
if (channel.request().paramAsBoolean("error_trace", false)) {
|
|
|
|
buildErrorTrace(t, builder);
|
2014-03-31 03:24:14 +02:00
|
|
|
}
|
2015-03-16 17:09:13 -07:00
|
|
|
} else {
|
|
|
|
builder.field("error", simpleMessage(t));
|
2014-03-31 03:24:14 +02:00
|
|
|
}
|
2015-03-16 17:09:13 -07:00
|
|
|
builder.field("status", status.getStatus());
|
2014-03-31 03:24:14 +02:00
|
|
|
builder.endObject();
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
2015-03-16 17:09:13 -07:00
|
|
|
private static void buildErrorTrace(Throwable t, XContentBuilder builder) throws IOException {
|
|
|
|
builder.startObject("error_trace");
|
|
|
|
boolean first = true;
|
|
|
|
int counter = 0;
|
|
|
|
while (t != null) {
|
|
|
|
// bail if there are more than 10 levels, becomes useless really...
|
|
|
|
if (counter++ > 10) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!first) {
|
|
|
|
builder.startObject("cause");
|
|
|
|
}
|
|
|
|
buildThrowable(t, builder);
|
|
|
|
if (!first) {
|
|
|
|
builder.endObject();
|
|
|
|
}
|
|
|
|
t = t.getCause();
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
builder.endObject();
|
|
|
|
}
|
|
|
|
|
2014-03-31 03:24:14 +02:00
|
|
|
private static void buildThrowable(Throwable t, XContentBuilder builder) throws IOException {
|
|
|
|
builder.field("message", t.getMessage());
|
|
|
|
for (StackTraceElement stElement : t.getStackTrace()) {
|
|
|
|
builder.startObject("at")
|
|
|
|
.field("class", stElement.getClassName())
|
|
|
|
.field("method", stElement.getMethodName());
|
|
|
|
if (stElement.getFileName() != null) {
|
|
|
|
builder.field("file", stElement.getFileName());
|
|
|
|
}
|
|
|
|
if (stElement.getLineNumber() >= 0) {
|
|
|
|
builder.field("line", stElement.getLineNumber());
|
|
|
|
}
|
|
|
|
builder.endObject();
|
|
|
|
}
|
2011-05-28 18:43:29 +03:00
|
|
|
}
|
2015-03-16 17:09:13 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Builds a simple error string from the message of the first ElasticsearchException
|
|
|
|
*/
|
|
|
|
private static String simpleMessage(Throwable t) throws IOException {
|
|
|
|
int counter = 0;
|
|
|
|
Throwable next = t;
|
|
|
|
while (next != null && counter++ < 10) {
|
|
|
|
if (t instanceof ElasticsearchException) {
|
|
|
|
return next.getClass().getSimpleName() + "[" + next.getMessage() + "]";
|
|
|
|
}
|
|
|
|
next = next.getCause();
|
|
|
|
}
|
|
|
|
|
|
|
|
return "No ElasticsearchException found";
|
|
|
|
}
|
|
|
|
}
|