use of org.elasticsearch.rest.BytesRestResponse in project elasticsearch by elastic.
the class RestGetSourceAction method prepareRequest.
@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
final GetRequest getRequest = new GetRequest(request.param("index"), request.param("type"), request.param("id"));
getRequest.operationThreaded(true);
getRequest.refresh(request.paramAsBoolean("refresh", getRequest.refresh()));
getRequest.routing(request.param("routing"));
getRequest.parent(request.param("parent"));
getRequest.preference(request.param("preference"));
getRequest.realtime(request.paramAsBoolean("realtime", getRequest.realtime()));
getRequest.fetchSourceContext(FetchSourceContext.parseFromRestRequest(request));
return channel -> {
if (getRequest.fetchSourceContext() != null && !getRequest.fetchSourceContext().fetchSource()) {
final ActionRequestValidationException validationError = new ActionRequestValidationException();
validationError.addValidationError("fetching source can not be disabled");
channel.sendResponse(new BytesRestResponse(channel, validationError));
} else {
client.get(getRequest, new RestResponseListener<GetResponse>(channel) {
@Override
public RestResponse buildResponse(final GetResponse response) throws Exception {
final XContentBuilder builder = channel.newBuilder(request.getXContentType(), false);
if (response.isSourceEmpty()) {
return new BytesRestResponse(NOT_FOUND, builder);
} else {
builder.rawValue(response.getSourceInternal());
return new BytesRestResponse(OK, builder);
}
}
});
}
};
}
use of org.elasticsearch.rest.BytesRestResponse in project elasticsearch by elastic.
the class RestTable method buildTextPlainResponse.
public static RestResponse buildTextPlainResponse(Table table, RestChannel channel) throws IOException {
RestRequest request = channel.request();
boolean verbose = request.paramAsBoolean("v", false);
List<DisplayHeader> headers = buildDisplayHeaders(table, request);
int[] width = buildWidths(table, request, verbose, headers);
BytesStreamOutput bytesOut = channel.bytesOutput();
UTF8StreamWriter out = new UTF8StreamWriter().setOutput(bytesOut);
int lastHeader = headers.size() - 1;
if (verbose) {
for (int col = 0; col < headers.size(); col++) {
DisplayHeader header = headers.get(col);
boolean isLastColumn = col == lastHeader;
pad(new Table.Cell(header.display, table.findHeaderByName(header.name)), width[col], request, out, isLastColumn);
if (!isLastColumn) {
out.append(" ");
}
}
out.append("\n");
}
List<Integer> rowOrder = getRowOrder(table, request);
for (Integer row : rowOrder) {
for (int col = 0; col < headers.size(); col++) {
DisplayHeader header = headers.get(col);
boolean isLastColumn = col == lastHeader;
pad(table.getAsMap().get(header.name).get(row), width[col], request, out, isLastColumn);
if (!isLastColumn) {
out.append(" ");
}
}
out.append("\n");
}
out.close();
return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, bytesOut.bytes());
}
use of org.elasticsearch.rest.BytesRestResponse in project elasticsearch by elastic.
the class RestRecoveryAction method prepareRequest.
@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
final RecoveryRequest recoveryRequest = new RecoveryRequest(Strings.splitStringByCommaToArray(request.param("index")));
recoveryRequest.detailed(request.paramAsBoolean("detailed", false));
recoveryRequest.activeOnly(request.paramAsBoolean("active_only", false));
recoveryRequest.indicesOptions(IndicesOptions.fromRequest(request, recoveryRequest.indicesOptions()));
return channel -> client.admin().indices().recoveries(recoveryRequest, new RestBuilderListener<RecoveryResponse>(channel) {
@Override
public RestResponse buildResponse(RecoveryResponse response, XContentBuilder builder) throws Exception {
response.detailed(recoveryRequest.detailed());
builder.startObject();
response.toXContent(builder, request);
builder.endObject();
return new BytesRestResponse(OK, builder);
}
});
}
use of org.elasticsearch.rest.BytesRestResponse in project elasticsearch by elastic.
the class TestDeprecationHeaderRestAction method prepareRequest.
// List<String> casts
@SuppressWarnings("unchecked")
@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
final List<String> settings;
try (XContentParser parser = request.contentParser()) {
final Map<String, Object> source = parser.map();
if (source.containsKey("deprecated_settings")) {
deprecationLogger.deprecated(DEPRECATED_USAGE);
settings = (List<String>) source.get("deprecated_settings");
} else {
settings = (List<String>) source.get("settings");
}
}
return channel -> {
final XContentBuilder builder = channel.newBuilder();
builder.startObject().startArray("settings");
for (String setting : settings) {
builder.startObject().field(setting, SETTINGS.get(setting).getRaw(this.settings)).endObject();
}
builder.endArray().endObject();
channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder));
};
}
use of org.elasticsearch.rest.BytesRestResponse in project elasticsearch by elastic.
the class TestResponseHeaderRestAction method prepareRequest.
@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
if ("password".equals(request.header("Secret"))) {
RestResponse response = new BytesRestResponse(RestStatus.OK, "Access granted");
response.addHeader("Secret", "granted");
return channel -> channel.sendResponse(response);
} else {
RestResponse response = new BytesRestResponse(RestStatus.UNAUTHORIZED, "Access denied");
response.addHeader("Secret", "required");
return channel -> channel.sendResponse(response);
}
}
Aggregations