use of org.opensearch.client.json.NdJsonpSerializable in project opensearch-java by opensearch-project.
the class RestClientTransport method prepareLowLevelRequest.
private <RequestT> org.opensearch.client.Request prepareLowLevelRequest(RequestT request, Endpoint<RequestT, ?, ?> endpoint, @Nullable TransportOptions options) {
String method = endpoint.method(request);
String path = endpoint.requestUrl(request);
Map<String, String> params = endpoint.queryParameters(request);
org.opensearch.client.Request clientReq = new org.opensearch.client.Request(method, path);
RequestOptions restOptions = options == null ? transportOptions.restClientRequestOptions() : RestClientOptions.of(options).restClientRequestOptions();
if (restOptions != null) {
clientReq.setOptions(restOptions);
}
clientReq.addParameters(params);
if (endpoint.hasRequestBody()) {
// Request has a body and must implement JsonpSerializable or NdJsonpSerializable
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (request instanceof NdJsonpSerializable) {
writeNdJson((NdJsonpSerializable) request, baos);
} else {
JsonGenerator generator = mapper.jsonProvider().createGenerator(baos);
mapper.serialize(request, generator);
generator.close();
}
clientReq.setEntity(new ByteArrayEntity(baos.toByteArray(), JsonContentType));
}
// Request parameter intercepted by LLRC
clientReq.addParameter("ignore", "400,401,403,404,405");
return clientReq;
}
use of org.opensearch.client.json.NdJsonpSerializable in project opensearch-java by opensearch-project.
the class RestClientTransport method writeNdJson.
/**
* Write an nd-json value by serializing each of its items on a separate line, recursing if its items themselves implement
* {@link NdJsonpSerializable} to flattening nested structures.
*/
private void writeNdJson(NdJsonpSerializable value, ByteArrayOutputStream baos) {
Iterator<?> values = value._serializables();
while (values.hasNext()) {
Object item = values.next();
if (item instanceof NdJsonpSerializable && item != value) {
// do not recurse on the item itself
writeNdJson((NdJsonpSerializable) item, baos);
} else {
JsonGenerator generator = mapper.jsonProvider().createGenerator(baos);
mapper.serialize(item, generator);
generator.close();
baos.write('\n');
}
}
}
Aggregations