use of org.opensearch.client.RequestOptions in project OpenSearch by opensearch-project.
the class HttpCompressionIT method testCompressesResponseIfRequested.
public void testCompressesResponseIfRequested() throws IOException {
Request request = new Request("POST", "/company/_doc/2");
request.setJsonEntity(SAMPLE_DOCUMENT);
Response response = client().performRequest(request);
assertEquals(201, response.getStatusLine().getStatusCode());
assertNull(response.getHeader(HttpHeaders.CONTENT_ENCODING));
assertThat(response.getEntity(), is(not(instanceOf(GzipDecompressingEntity.class))));
request = new Request("GET", "/company/_doc/2");
RequestOptions requestOptions = RequestOptions.DEFAULT.toBuilder().addHeader(HttpHeaders.ACCEPT_ENCODING, GZIP_ENCODING).build();
request.setOptions(requestOptions);
response = client().performRequest(request);
assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals(GZIP_ENCODING, response.getHeader(HttpHeaders.CONTENT_ENCODING));
assertThat(response.getEntity(), instanceOf(GzipDecompressingEntity.class));
String body = EntityUtils.toString(response.getEntity());
assertThat(body, containsString(SAMPLE_DOCUMENT));
}
use of org.opensearch.client.RequestOptions in project OpenSearch by opensearch-project.
the class OpenSearchRestTestCase method expectSoftDeletesWarning.
protected static void expectSoftDeletesWarning(Request request, String indexName) {
final List<String> esExpectedWarnings = Collections.singletonList("Creating indices with soft-deletes disabled is deprecated and will be removed in future Elasticsearch versions. " + "Please do not specify value for setting [index.soft_deletes.enabled] of index [" + indexName + "].");
final List<String> opensearchExpectedWarnings = Collections.singletonList("Creating indices with soft-deletes disabled is deprecated and will be removed in future OpenSearch versions. " + "Please do not specify value for setting [index.soft_deletes.enabled] of index [" + indexName + "].");
final Builder requestOptions = RequestOptions.DEFAULT.toBuilder();
if (nodeVersions.stream().allMatch(version -> version.onOrAfter(LegacyESVersion.V_7_6_0) && version.before(Version.V_1_0_0))) {
requestOptions.setWarningsHandler(warnings -> warnings.equals(esExpectedWarnings) == false);
request.setOptions(requestOptions);
} else if (nodeVersions.stream().anyMatch(version -> version.onOrAfter(LegacyESVersion.V_7_6_0) && version.before(Version.V_1_0_0))) {
requestOptions.setWarningsHandler(warnings -> warnings.isEmpty() == false && warnings.equals(esExpectedWarnings) == false);
request.setOptions(requestOptions);
}
if (nodeVersions.stream().allMatch(version -> version.onOrAfter(Version.V_1_0_0))) {
requestOptions.setWarningsHandler(warnings -> warnings.equals(opensearchExpectedWarnings) == false);
request.setOptions(requestOptions);
} else if (nodeVersions.stream().anyMatch(version -> version.onOrAfter(Version.V_1_0_0))) {
requestOptions.setWarningsHandler(warnings -> warnings.isEmpty() == false && warnings.equals(opensearchExpectedWarnings) == false);
request.setOptions(requestOptions);
}
}
use of org.opensearch.client.RequestOptions in project OpenSearch by opensearch-project.
the class OpenSearchRestTestCase method expectTranslogRetentionWarning.
protected static void expectTranslogRetentionWarning(Request request) {
final List<String> expectedWarnings = Collections.singletonList("Translog retention settings [index.translog.retention.age] " + "and [index.translog.retention.size] are deprecated and effectively ignored. They will be removed in a future version.");
final Builder requestOptions = RequestOptions.DEFAULT.toBuilder();
if (nodeVersions.stream().allMatch(version -> version.onOrAfter(LegacyESVersion.V_7_7_0))) {
requestOptions.setWarningsHandler(warnings -> warnings.equals(expectedWarnings) == false);
request.setOptions(requestOptions);
} else if (nodeVersions.stream().anyMatch(version -> version.onOrAfter(LegacyESVersion.V_7_7_0))) {
requestOptions.setWarningsHandler(warnings -> warnings.isEmpty() == false && warnings.equals(expectedWarnings) == false);
request.setOptions(requestOptions);
}
}
use of org.opensearch.client.RequestOptions 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;
}
Aggregations