Search in sources :

Example 1 with RequestOptions

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));
}
Also used : Response(org.opensearch.client.Response) GzipDecompressingEntity(org.apache.http.client.entity.GzipDecompressingEntity) RequestOptions(org.opensearch.client.RequestOptions) Request(org.opensearch.client.Request) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 2 with RequestOptions

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);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) SSLContext(javax.net.ssl.SSLContext) Arrays(java.util.Arrays) WarningsHandler(org.opensearch.client.WarningsHandler) Collections.unmodifiableList(java.util.Collections.unmodifiableList) HttpStatus(org.apache.http.HttpStatus) SnapshotState(org.opensearch.snapshots.SnapshotState) Version(org.opensearch.Version) KeyStoreException(java.security.KeyStoreException) Request(org.opensearch.client.Request) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Header(org.apache.http.Header) EntityUtils(org.apache.http.util.EntityUtils) Strings(org.opensearch.common.Strings) Collections.sort(java.util.Collections.sort) XContentParser(org.opensearch.common.xcontent.XContentParser) SSLIOSessionStrategy(org.apache.http.nio.conn.ssl.SSLIOSessionStrategy) Matchers.everyItem(org.hamcrest.Matchers.everyItem) SSLContexts(org.apache.http.ssl.SSLContexts) After(org.junit.After) Map(java.util.Map) RestClient(org.opensearch.client.RestClient) RestClientBuilder(org.opensearch.client.RestClientBuilder) Path(java.nio.file.Path) AfterClass(org.junit.AfterClass) TimeValue(org.opensearch.common.unit.TimeValue) Predicate(java.util.function.Predicate) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) ObjectPath(org.opensearch.test.rest.yaml.ObjectPath) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) ReplicationTracker(org.opensearch.index.seqno.ReplicationTracker) KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) RestStatus(org.opensearch.rest.RestStatus) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) List(java.util.List) PutRepositoryRequest(org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Matchers.equalTo(org.hamcrest.Matchers.equalTo) CheckedRunnable(org.opensearch.common.CheckedRunnable) IndexSettings(org.opensearch.index.IndexSettings) XContentType(org.opensearch.common.xcontent.XContentType) Pattern(java.util.regex.Pattern) Matchers.anyOf(org.hamcrest.Matchers.anyOf) Response(org.opensearch.client.Response) Matchers.in(org.hamcrest.Matchers.in) PathUtils(org.opensearch.common.io.PathUtils) Builder(org.opensearch.client.RequestOptions.Builder) RequestOptions(org.opensearch.client.RequestOptions) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LegacyESVersion(org.opensearch.LegacyESVersion) Before(org.junit.Before) SetOnce(org.apache.lucene.util.SetOnce) ListTasksAction(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksAction) Files(java.nio.file.Files) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) InputStreamReader(java.io.InputStreamReader) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentHelper(org.opensearch.common.xcontent.XContentHelper) IOUtils(org.opensearch.core.internal.io.IOUtils) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) HttpPut(org.apache.http.client.methods.HttpPut) ResponseException(org.opensearch.client.ResponseException) BasicHeader(org.apache.http.message.BasicHeader) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) XContentMapValues(org.opensearch.common.xcontent.support.XContentMapValues) Matchers.anEmptyMap(org.hamcrest.Matchers.anEmptyMap) BufferedReader(java.io.BufferedReader) HttpHost(org.apache.http.HttpHost) DeprecationHandler(org.opensearch.common.xcontent.DeprecationHandler) Collections(java.util.Collections) InputStream(java.io.InputStream) RestClientBuilder(org.opensearch.client.RestClientBuilder) Builder(org.opensearch.client.RequestOptions.Builder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 3 with 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);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) SSLContext(javax.net.ssl.SSLContext) Arrays(java.util.Arrays) WarningsHandler(org.opensearch.client.WarningsHandler) Collections.unmodifiableList(java.util.Collections.unmodifiableList) HttpStatus(org.apache.http.HttpStatus) SnapshotState(org.opensearch.snapshots.SnapshotState) Version(org.opensearch.Version) KeyStoreException(java.security.KeyStoreException) Request(org.opensearch.client.Request) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Header(org.apache.http.Header) EntityUtils(org.apache.http.util.EntityUtils) Strings(org.opensearch.common.Strings) Collections.sort(java.util.Collections.sort) XContentParser(org.opensearch.common.xcontent.XContentParser) SSLIOSessionStrategy(org.apache.http.nio.conn.ssl.SSLIOSessionStrategy) Matchers.everyItem(org.hamcrest.Matchers.everyItem) SSLContexts(org.apache.http.ssl.SSLContexts) After(org.junit.After) Map(java.util.Map) RestClient(org.opensearch.client.RestClient) RestClientBuilder(org.opensearch.client.RestClientBuilder) Path(java.nio.file.Path) AfterClass(org.junit.AfterClass) TimeValue(org.opensearch.common.unit.TimeValue) Predicate(java.util.function.Predicate) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) ObjectPath(org.opensearch.test.rest.yaml.ObjectPath) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) ReplicationTracker(org.opensearch.index.seqno.ReplicationTracker) KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) RestStatus(org.opensearch.rest.RestStatus) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) List(java.util.List) PutRepositoryRequest(org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Matchers.equalTo(org.hamcrest.Matchers.equalTo) CheckedRunnable(org.opensearch.common.CheckedRunnable) IndexSettings(org.opensearch.index.IndexSettings) XContentType(org.opensearch.common.xcontent.XContentType) Pattern(java.util.regex.Pattern) Matchers.anyOf(org.hamcrest.Matchers.anyOf) Response(org.opensearch.client.Response) Matchers.in(org.hamcrest.Matchers.in) PathUtils(org.opensearch.common.io.PathUtils) Builder(org.opensearch.client.RequestOptions.Builder) RequestOptions(org.opensearch.client.RequestOptions) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LegacyESVersion(org.opensearch.LegacyESVersion) Before(org.junit.Before) SetOnce(org.apache.lucene.util.SetOnce) ListTasksAction(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksAction) Files(java.nio.file.Files) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) InputStreamReader(java.io.InputStreamReader) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentHelper(org.opensearch.common.xcontent.XContentHelper) IOUtils(org.opensearch.core.internal.io.IOUtils) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) HttpPut(org.apache.http.client.methods.HttpPut) ResponseException(org.opensearch.client.ResponseException) BasicHeader(org.apache.http.message.BasicHeader) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) XContentMapValues(org.opensearch.common.xcontent.support.XContentMapValues) Matchers.anEmptyMap(org.hamcrest.Matchers.anEmptyMap) BufferedReader(java.io.BufferedReader) HttpHost(org.apache.http.HttpHost) DeprecationHandler(org.opensearch.common.xcontent.DeprecationHandler) Collections(java.util.Collections) InputStream(java.io.InputStream) RestClientBuilder(org.opensearch.client.RestClientBuilder) Builder(org.opensearch.client.RequestOptions.Builder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 4 with 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;
}
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)

Aggregations

RequestOptions (org.opensearch.client.RequestOptions)4 Request (org.opensearch.client.Request)3 Response (org.opensearch.client.Response)3 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 KeyManagementException (java.security.KeyManagementException)2 KeyStore (java.security.KeyStore)2 KeyStoreException (java.security.KeyStoreException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 CertificateException (java.security.cert.CertificateException)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Collections.sort (java.util.Collections.sort)2 Collections.unmodifiableList (java.util.Collections.unmodifiableList)2