Search in sources :

Example 41 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class ResponseHeaderPluginIT method testThatSettingHeadersWorks.

public void testThatSettingHeadersWorks() throws IOException {
    ensureGreen();
    try {
        getRestClient().performRequest(new Request("GET", "/_protected"));
        fail("request should have failed");
    } catch (ResponseException e) {
        Response response = e.getResponse();
        assertThat(response.getStatusLine().getStatusCode(), equalTo(401));
        assertThat(response.getHeader("Secret"), equalTo("required"));
    }
    Request request = new Request("GET", "/_protected");
    RequestOptions.Builder options = request.getOptions().toBuilder();
    options.addHeader("Secret", "password");
    request.setOptions(options);
    Response authResponse = getRestClient().performRequest(request);
    assertThat(authResponse.getStatusLine().getStatusCode(), equalTo(200));
    assertThat(authResponse.getHeader("Secret"), equalTo("granted"));
}
Also used : Response(org.opensearch.client.Response) ResponseException(org.opensearch.client.ResponseException) RequestOptions(org.opensearch.client.RequestOptions) Request(org.opensearch.client.Request)

Example 42 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class SearchRestCancellationIT method verifyCancellationDuringQueryPhase.

void verifyCancellationDuringQueryPhase(String searchAction, Request searchRequest) throws Exception {
    Map<String, String> nodeIdToName = readNodesInfo();
    List<ScriptedBlockPlugin> plugins = initBlockFactory();
    indexTestData();
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<Exception> error = new AtomicReference<>();
    Cancellable cancellable = getRestClient().performRequestAsync(searchRequest, new ResponseListener() {

        @Override
        public void onSuccess(Response response) {
            latch.countDown();
        }

        @Override
        public void onFailure(Exception exception) {
            error.set(exception);
            latch.countDown();
        }
    });
    awaitForBlock(plugins);
    cancellable.cancel();
    ensureSearchTaskIsCancelled(searchAction, nodeIdToName::get);
    disableBlocks(plugins);
    latch.await();
    assertThat(error.get(), instanceOf(CancellationException.class));
}
Also used : Cancellable(org.opensearch.client.Cancellable) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResponseListener(org.opensearch.client.ResponseListener) CountDownLatch(java.util.concurrent.CountDownLatch) CancellationException(java.util.concurrent.CancellationException) NodesInfoResponse(org.opensearch.action.admin.cluster.node.info.NodesInfoResponse) Response(org.opensearch.client.Response) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) CancellationException(java.util.concurrent.CancellationException)

Example 43 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class SystemIndexRestIT method testSystemIndexAccessBlockedByDefault.

public void testSystemIndexAccessBlockedByDefault() throws Exception {
    // create index
    {
        Request putDocRequest = new Request("POST", "/_sys_index_test/add_doc/42");
        Response resp = getRestClient().performRequest(putDocRequest);
        assertThat(resp.getStatusLine().getStatusCode(), equalTo(201));
    }
    // make sure the system index now exists
    assertBusy(() -> {
        Request searchRequest = new Request("GET", "/" + SystemIndexTestPlugin.SYSTEM_INDEX_NAME + "/_count");
        searchRequest.setOptions(expectWarnings("this request accesses system indices: [" + SystemIndexTestPlugin.SYSTEM_INDEX_NAME + "], but in a future major version, direct access to system indices will be prevented by default"));
        // Disallow no indices to cause an exception if the flag above doesn't work
        searchRequest.addParameter("allow_no_indices", "false");
        searchRequest.setJsonEntity("{\"query\": {\"match\":  {\"some_field\":  \"some_value\"}}}");
        final Response searchResponse = getRestClient().performRequest(searchRequest);
        assertThat(searchResponse.getStatusLine().getStatusCode(), is(200));
        Map<String, Object> responseMap = entityAsMap(searchResponse);
        assertThat(responseMap, hasKey("count"));
        assertThat(responseMap.get("count"), equalTo(1));
    });
    // And with a partial wildcard
    assertDeprecationWarningOnAccess(".test-*", SystemIndexTestPlugin.SYSTEM_INDEX_NAME);
    // And with a total wildcard
    assertDeprecationWarningOnAccess(randomFrom("*", "_all"), SystemIndexTestPlugin.SYSTEM_INDEX_NAME);
    // Try to index a doc directly
    {
        String expectedWarning = "this request accesses system indices: [" + SystemIndexTestPlugin.SYSTEM_INDEX_NAME + "], but in a " + "future major version, direct access to system indices will be prevented by default";
        Request putDocDirectlyRequest = new Request("PUT", "/" + SystemIndexTestPlugin.SYSTEM_INDEX_NAME + "/_doc/43");
        putDocDirectlyRequest.setJsonEntity("{\"some_field\":  \"some_other_value\"}");
        putDocDirectlyRequest.setOptions(expectWarnings(expectedWarning));
        Response response = getRestClient().performRequest(putDocDirectlyRequest);
        assertThat(response.getStatusLine().getStatusCode(), equalTo(201));
    }
}
Also used : Response(org.opensearch.client.Response) Request(org.opensearch.client.Request) WriteRequest(org.opensearch.action.support.WriteRequest) RestRequest(org.opensearch.rest.RestRequest) IndexRequest(org.opensearch.action.index.IndexRequest)

Example 44 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class SystemIndexRestIT method assertDeprecationWarningOnAccess.

private void assertDeprecationWarningOnAccess(String queryPattern, String warningIndexName) throws IOException {
    String expectedWarning = "this request accesses system indices: [" + warningIndexName + "], but in a " + "future major version, direct access to system indices will be prevented by default";
    Request searchRequest = new Request("GET", "/" + queryPattern + randomFrom("/_count", "/_search"));
    searchRequest.setJsonEntity("{\"query\": {\"match\":  {\"some_field\":  \"some_value\"}}}");
    // Disallow no indices to cause an exception if this resolves to zero indices, so that we're sure it resolved the index
    searchRequest.addParameter("allow_no_indices", "false");
    if (!assertedWarnings.contains(expectedWarning)) {
        searchRequest.setOptions(expectWarnings(expectedWarning));
        assertedWarnings.add(expectedWarning);
    }
    Response response = getRestClient().performRequest(searchRequest);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
Also used : Response(org.opensearch.client.Response) Request(org.opensearch.client.Request) WriteRequest(org.opensearch.action.support.WriteRequest) RestRequest(org.opensearch.rest.RestRequest) IndexRequest(org.opensearch.action.index.IndexRequest)

Example 45 with Response

use of org.opensearch.client.Response in project OpenSearch by opensearch-project.

the class VerifyVersionConstantsIT method testLuceneVersionConstant.

public void testLuceneVersionConstant() throws IOException, ParseException {
    final Response response = client().performRequest(new Request("GET", "/"));
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
    final ObjectPath objectPath = ObjectPath.createFromResponse(response);
    final String opensearchVersionString = objectPath.evaluate("version.number").toString();
    final Version opensearchVersion = Version.fromString(opensearchVersionString.replace("-SNAPSHOT", ""));
    final String luceneVersionString = objectPath.evaluate("version.lucene_version").toString();
    final org.apache.lucene.util.Version luceneVersion = org.apache.lucene.util.Version.parse(luceneVersionString);
    assertThat(opensearchVersion.luceneVersion, equalTo(luceneVersion));
}
Also used : Response(org.opensearch.client.Response) ObjectPath(org.opensearch.test.rest.yaml.ObjectPath) Version(org.opensearch.Version) Request(org.opensearch.client.Request)

Aggregations

Response (org.opensearch.client.Response)134 Request (org.opensearch.client.Request)113 ResponseException (org.opensearch.client.ResponseException)24 Map (java.util.Map)23 Matchers.containsString (org.hamcrest.Matchers.containsString)20 ArrayList (java.util.ArrayList)17 PutRepositoryRequest (org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest)16 RestClient (org.opensearch.client.RestClient)15 HashMap (java.util.HashMap)14 RequestOptions (org.opensearch.client.RequestOptions)12 ObjectPath (org.opensearch.test.rest.yaml.ObjectPath)12 IndexRequest (org.opensearch.action.index.IndexRequest)11 IOException (java.io.IOException)10 List (java.util.List)10 CountDownLatch (java.util.concurrent.CountDownLatch)8 Version (org.opensearch.Version)8 WriteRequest (org.opensearch.action.support.WriteRequest)8 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)7 IndexResponse (org.opensearch.action.index.IndexResponse)7 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)7