use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.
the class TcpTransport method sendErrorResponse.
/**
* Sends back an error response to the caller via the given channel
* @param nodeVersion the caller node version
* @param channel the channel to send the response to
* @param error the error to return
* @param requestId the request ID this response replies to
* @param action the action this response replies to
*/
public void sendErrorResponse(Version nodeVersion, Channel channel, final Exception error, final long requestId, final String action) throws IOException {
try (BytesStreamOutput stream = new BytesStreamOutput()) {
stream.setVersion(nodeVersion);
RemoteTransportException tx = new RemoteTransportException(nodeName(), new TransportAddress(getLocalAddress(channel)), action, error);
threadPool.getThreadContext().writeTo(stream);
stream.writeException(tx);
byte status = 0;
status = TransportStatus.setResponse(status);
status = TransportStatus.setError(status);
final BytesReference bytes = stream.bytes();
final BytesReference header = buildHeader(requestId, status, nodeVersion, bytes.length());
Runnable onRequestSent = () -> transportServiceAdapter.onResponseSent(requestId, action, error);
sendMessage(channel, new CompositeBytesReference(header, bytes), onRequestSent);
}
}
use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.
the class MainResponseTests method testFromXContent.
public void testFromXContent() throws IOException {
MainResponse mainResponse = createTestItem();
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toXContent(mainResponse, xContentType, humanReadable);
MainResponse parsed;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
parsed = MainResponse.fromXContent(parser);
assertNull(parser.nextToken());
}
assertEquals(mainResponse.getClusterUuid(), parsed.getClusterUuid());
assertEquals(mainResponse.getClusterName(), parsed.getClusterName());
assertEquals(mainResponse.getNodeName(), parsed.getNodeName());
assertEquals(mainResponse.getBuild(), parsed.getBuild());
assertEquals(mainResponse.getVersion(), parsed.getVersion());
// we cannot recreate the "available" flag from xContent, but should be "true" if request came through
assertEquals(true, parsed.isAvailable());
assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.
the class IndexResponseTests method testToAndFromXContent.
public void testToAndFromXContent() throws IOException {
final Tuple<IndexResponse, IndexResponse> tuple = randomIndexResponse();
IndexResponse indexResponse = tuple.v1();
IndexResponse expectedIndexResponse = tuple.v2();
boolean humanReadable = randomBoolean();
XContentType xContentType = randomFrom(XContentType.values());
BytesReference indexResponseBytes = toXContent(indexResponse, xContentType, humanReadable);
// Shuffle the XContent fields
if (randomBoolean()) {
try (XContentParser parser = createParser(xContentType.xContent(), indexResponseBytes)) {
indexResponseBytes = shuffleXContent(parser, randomBoolean()).bytes();
}
}
// Parse the XContent bytes to obtain a parsed
IndexResponse parsedIndexResponse;
try (XContentParser parser = createParser(xContentType.xContent(), indexResponseBytes)) {
parsedIndexResponse = IndexResponse.fromXContent(parser);
assertNull(parser.nextToken());
}
// We can't use equals() to compare the original and the parsed index response
// because the random index response can contain shard failures with exceptions,
// and those exceptions are not parsed back with the same types.
assertDocWriteResponse(expectedIndexResponse, parsedIndexResponse);
}
use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.
the class ShardSearchFailureTests method testFromXContent.
public void testFromXContent() throws IOException {
ShardSearchFailure response = createTestItem();
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toXContent(response, xContentType, humanReadable);
ShardSearchFailure parsed;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
parsed = ShardSearchFailure.fromXContent(parser);
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
assertNull(parser.nextToken());
}
assertEquals(response.index(), parsed.index());
assertEquals(response.shard().getNodeId(), parsed.shard().getNodeId());
assertEquals(response.shardId(), parsed.shardId());
// we cannot compare the cause, because it will be wrapped in an outer ElasticSearchException
// best effort: try to check that the original message appears somewhere in the rendered xContent
String originalMsg = response.getCause().getMessage();
assertEquals(parsed.getCause().getMessage(), "Elasticsearch exception [type=parsing_exception, reason=" + originalMsg + "]");
String nestedMsg = response.getCause().getCause().getMessage();
assertEquals(parsed.getCause().getCause().getMessage(), "Elasticsearch exception [type=illegal_argument_exception, reason=" + nestedMsg + "]");
}
use of org.elasticsearch.common.bytes.BytesReference in project elasticsearch by elastic.
the class ShardSearchFailureTests method testToXContent.
public void testToXContent() throws IOException {
ShardSearchFailure failure = new ShardSearchFailure(new ParsingException(0, 0, "some message", null), new SearchShardTarget("nodeId", new ShardId(new Index("indexName", "indexUuid"), 123)));
BytesReference xContent = toXContent(failure, XContentType.JSON, randomBoolean());
assertEquals("{\"shard\":123," + "\"index\":\"indexName\"," + "\"node\":\"nodeId\"," + "\"reason\":{" + "\"type\":\"parsing_exception\"," + "\"reason\":\"some message\"," + "\"line\":0," + "\"col\":0" + "}" + "}", xContent.utf8ToString());
}
Aggregations