use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class ObjectPath method createFromResponse.
public static ObjectPath createFromResponse(Response response) throws IOException {
byte[] bytes = EntityUtils.toByteArray(response.getEntity());
String contentType = response.getHeader("Content-Type");
XContentType xContentType = XContentType.fromMediaTypeOrFormat(contentType);
return ObjectPath.createFromXContent(xContentType.xContent(), new BytesArray(bytes));
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class ChecksumBlobStoreFormat method writeBlob.
/**
* Writes blob in atomic manner without resolving the blobName using using {@link #blobName} method.
* <p>
* The blob will be compressed and checksum will be written if required.
*
* @param obj object to be serialized
* @param blobContainer blob container
* @param blobName blob name
*/
protected void writeBlob(T obj, BlobContainer blobContainer, String blobName) throws IOException {
BytesReference bytes = write(obj);
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
final String resourceDesc = "ChecksumBlobStoreFormat.writeBlob(blob=\"" + blobName + "\")";
try (OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput(resourceDesc, blobName, byteArrayOutputStream, BUFFER_SIZE)) {
CodecUtil.writeHeader(indexOutput, codec, VERSION);
try (OutputStream indexOutputOutputStream = new IndexOutputOutputStream(indexOutput) {
@Override
public void close() throws IOException {
// this is important since some of the XContentBuilders write bytes on close.
// in order to write the footer we need to prevent closing the actual index input.
}
}) {
bytes.writeTo(indexOutputOutputStream);
}
CodecUtil.writeFooter(indexOutput);
}
BytesArray bytesArray = new BytesArray(byteArrayOutputStream.toByteArray());
try (InputStream stream = bytesArray.streamInput()) {
blobContainer.writeBlob(blobName, stream, bytesArray.length());
}
}
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class RestRequest method contentOrSourceParam.
/**
* Get the content of the request or the contents of the {@code source} param. Prefer {@link #contentOrSourceParamParser()} or
* {@link #withContentOrSourceParamParserOrNull(CheckedConsumer)} if you need a parser.
*/
public final Tuple<XContentType, BytesReference> contentOrSourceParam() {
if (hasContent()) {
if (xContentType.get() == null) {
throw new IllegalStateException("unknown content type");
}
return new Tuple<>(xContentType.get(), content());
}
String source = param("source");
String typeParam = param("source_content_type");
if (source != null && typeParam != null) {
BytesArray bytes = new BytesArray(source);
final XContentType xContentType = parseContentType(Collections.singletonList(typeParam));
if (xContentType == null) {
throw new IllegalStateException("Unknown value for source_content_type [" + typeParam + "]");
}
return new Tuple<>(xContentType, bytes);
}
return new Tuple<>(XContentType.JSON, BytesArray.EMPTY);
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class ShardSearchLocalRequest method cacheKey.
@Override
public BytesReference cacheKey() throws IOException {
BytesStreamOutput out = new BytesStreamOutput();
this.innerWriteTo(out, true);
// do a deep copy
return new BytesArray(out.bytes().toBytesRef(), true);
}
use of org.elasticsearch.common.bytes.BytesArray in project elasticsearch by elastic.
the class TaskTests method testTaskInfoToString.
public void testTaskInfoToString() {
String nodeId = randomAsciiOfLength(10);
long taskId = randomIntBetween(0, 100000);
long startTime = randomNonNegativeLong();
long runningTime = randomNonNegativeLong();
boolean cancellable = randomBoolean();
TaskInfo taskInfo = new TaskInfo(new TaskId(nodeId, taskId), "test_type", "test_action", "test_description", null, startTime, runningTime, cancellable, TaskId.EMPTY_TASK_ID);
String taskInfoString = taskInfo.toString();
Map<String, Object> map = XContentHelper.convertToMap(new BytesArray(taskInfoString.getBytes(StandardCharsets.UTF_8)), true).v2();
assertEquals(((Number) map.get("id")).longValue(), taskId);
assertEquals(map.get("type"), "test_type");
assertEquals(map.get("action"), "test_action");
assertEquals(map.get("description"), "test_description");
assertEquals(((Number) map.get("start_time_in_millis")).longValue(), startTime);
assertEquals(((Number) map.get("running_time_in_nanos")).longValue(), runningTime);
assertEquals(map.get("cancellable"), cancellable);
}
Aggregations