Search in sources :

Example 1 with NdJsonpSerializable

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;
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) RequestOptions(org.opensearch.client.RequestOptions) NdJsonpSerializable(org.opensearch.client.json.NdJsonpSerializable) JsonGenerator(jakarta.json.stream.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with NdJsonpSerializable

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');
        }
    }
}
Also used : NdJsonpSerializable(org.opensearch.client.json.NdJsonpSerializable) JsonGenerator(jakarta.json.stream.JsonGenerator)

Aggregations

JsonGenerator (jakarta.json.stream.JsonGenerator)2 NdJsonpSerializable (org.opensearch.client.json.NdJsonpSerializable)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)1 RequestOptions (org.opensearch.client.RequestOptions)1