Search in sources :

Example 1 with BytesRestResponse

use of org.opensearch.rest.BytesRestResponse in project OpenSearch by opensearch-project.

the class RestHighLevelClient method parseResponseException.

/**
 * Converts a {@link ResponseException} obtained from the low level REST client into an {@link OpenSearchException}.
 * If a response body was returned, tries to parse it as an error returned from OpenSearch.
 * If no response body was returned or anything goes wrong while parsing the error, returns a new {@link OpenSearchStatusException}
 * that wraps the original {@link ResponseException}. The potential exception obtained while parsing is added to the returned
 * exception as a suppressed exception. This method is guaranteed to not throw any exception eventually thrown while parsing.
 */
protected final OpenSearchStatusException parseResponseException(ResponseException responseException) {
    Response response = responseException.getResponse();
    HttpEntity entity = response.getEntity();
    OpenSearchStatusException opensearchException;
    RestStatus restStatus = RestStatus.fromCode(response.getStatusLine().getStatusCode());
    if (entity == null) {
        opensearchException = new OpenSearchStatusException(responseException.getMessage(), restStatus, responseException);
    } else {
        try {
            opensearchException = parseEntity(entity, BytesRestResponse::errorFromXContent);
            opensearchException.addSuppressed(responseException);
        } catch (Exception e) {
            opensearchException = new OpenSearchStatusException("Unable to parse response body", restStatus, responseException);
            opensearchException.addSuppressed(e);
        }
    }
    return opensearchException;
}
Also used : MainResponse(org.opensearch.client.core.MainResponse) IndexResponse(org.opensearch.action.index.IndexResponse) MultiTermVectorsResponse(org.opensearch.client.core.MultiTermVectorsResponse) RankEvalResponse(org.opensearch.index.rankeval.RankEvalResponse) BytesRestResponse(org.opensearch.rest.BytesRestResponse) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) UpdateResponse(org.opensearch.action.update.UpdateResponse) FieldCapabilitiesResponse(org.opensearch.action.fieldcaps.FieldCapabilitiesResponse) TermVectorsResponse(org.opensearch.client.core.TermVectorsResponse) DeleteResponse(org.opensearch.action.delete.DeleteResponse) SearchTemplateResponse(org.opensearch.script.mustache.SearchTemplateResponse) MultiSearchTemplateResponse(org.opensearch.script.mustache.MultiSearchTemplateResponse) MultiGetResponse(org.opensearch.action.get.MultiGetResponse) TaskSubmissionResponse(org.opensearch.client.tasks.TaskSubmissionResponse) GetResponse(org.opensearch.action.get.GetResponse) CountResponse(org.opensearch.client.core.CountResponse) GetStoredScriptResponse(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptResponse) GetSourceResponse(org.opensearch.client.core.GetSourceResponse) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) ClearScrollResponse(org.opensearch.action.search.ClearScrollResponse) SearchResponse(org.opensearch.action.search.SearchResponse) ExplainResponse(org.opensearch.action.explain.ExplainResponse) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse) BulkResponse(org.opensearch.action.bulk.BulkResponse) HttpEntity(org.apache.http.HttpEntity) RestStatus(org.opensearch.rest.RestStatus) IOException(java.io.IOException) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) OpenSearchException(org.opensearch.OpenSearchException) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Example 2 with BytesRestResponse

use of org.opensearch.rest.BytesRestResponse in project OpenSearch by opensearch-project.

the class Netty4HttpServerTransportTests method testBadRequest.

public void testBadRequest() throws InterruptedException {
    final AtomicReference<Throwable> causeReference = new AtomicReference<>();
    final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {

        @Override
        public void dispatchRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext) {
            logger.error("--> Unexpected successful request [{}]", FakeRestRequest.requestToString(request));
            throw new AssertionError();
        }

        @Override
        public void dispatchBadRequest(final RestChannel channel, final ThreadContext threadContext, final Throwable cause) {
            causeReference.set(cause);
            try {
                final OpenSearchException e = new OpenSearchException("you sent a bad request and you should feel bad");
                channel.sendResponse(new BytesRestResponse(channel, BAD_REQUEST, e));
            } catch (final IOException e) {
                throw new AssertionError(e);
            }
        }
    };
    final Settings settings;
    final int maxInitialLineLength;
    final Setting<ByteSizeValue> httpMaxInitialLineLengthSetting = HttpTransportSettings.SETTING_HTTP_MAX_INITIAL_LINE_LENGTH;
    if (randomBoolean()) {
        maxInitialLineLength = httpMaxInitialLineLengthSetting.getDefault(Settings.EMPTY).bytesAsInt();
        settings = createSettings();
    } else {
        maxInitialLineLength = randomIntBetween(1, 8192);
        settings = createBuilderWithPort().put(httpMaxInitialLineLengthSetting.getKey(), maxInitialLineLength + "b").build();
    }
    try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, xContentRegistry(), dispatcher, clusterSettings, new SharedGroupFactory(settings))) {
        transport.start();
        final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
        try (Netty4HttpClient client = new Netty4HttpClient()) {
            final String url = "/" + new String(new byte[maxInitialLineLength], Charset.forName("UTF-8"));
            final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, url);
            final FullHttpResponse response = client.send(remoteAddress.address(), request);
            try {
                assertThat(response.status(), equalTo(HttpResponseStatus.BAD_REQUEST));
                assertThat(new String(response.content().array(), Charset.forName("UTF-8")), containsString("you sent a bad request and you should feel bad"));
            } finally {
                response.release();
            }
        }
    }
    assertNotNull(causeReference.get());
    assertThat(causeReference.get(), instanceOf(TooLongFrameException.class));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) TransportAddress(org.opensearch.common.transport.TransportAddress) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpServerTransport(org.opensearch.http.HttpServerTransport) NullDispatcher(org.opensearch.http.NullDispatcher) BytesRestResponse(org.opensearch.rest.BytesRestResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Settings(org.opensearch.common.settings.Settings) HttpTransportSettings(org.opensearch.http.HttpTransportSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) RestChannel(org.opensearch.rest.RestChannel) SharedGroupFactory(org.opensearch.transport.SharedGroupFactory) IOException(java.io.IOException) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) RestRequest(org.opensearch.rest.RestRequest) OpenSearchException(org.opensearch.OpenSearchException)

Example 3 with BytesRestResponse

use of org.opensearch.rest.BytesRestResponse in project OpenSearch by opensearch-project.

the class Netty4HttpServerTransportTests method testLargeCompressedResponse.

public void testLargeCompressedResponse() throws InterruptedException {
    final String responseString = randomAlphaOfLength(4 * 1024 * 1024);
    final String url = "/thing";
    final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {

        @Override
        public void dispatchRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext) {
            if (url.equals(request.uri())) {
                channel.sendResponse(new BytesRestResponse(OK, responseString));
            } else {
                logger.error("--> Unexpected successful uri [{}]", request.uri());
                throw new AssertionError();
            }
        }

        @Override
        public void dispatchBadRequest(final RestChannel channel, final ThreadContext threadContext, final Throwable cause) {
            logger.error(new ParameterizedMessage("--> Unexpected bad request [{}]", FakeRestRequest.requestToString(channel.request())), cause);
            throw new AssertionError();
        }
    };
    try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(Settings.EMPTY, networkService, bigArrays, threadPool, xContentRegistry(), dispatcher, clusterSettings, new SharedGroupFactory(Settings.EMPTY))) {
        transport.start();
        final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
        try (Netty4HttpClient client = new Netty4HttpClient()) {
            DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, url);
            request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, randomFrom("deflate", "gzip"));
            long numOfHugeAllocations = getHugeAllocationCount();
            final FullHttpResponse response = client.send(remoteAddress.address(), request);
            try {
                assertThat(getHugeAllocationCount(), equalTo(numOfHugeAllocations));
                assertThat(response.status(), equalTo(HttpResponseStatus.OK));
                byte[] bytes = new byte[response.content().readableBytes()];
                response.content().readBytes(bytes);
                assertThat(new String(bytes, StandardCharsets.UTF_8), equalTo(responseString));
            } finally {
                response.release();
            }
        }
    }
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) TransportAddress(org.opensearch.common.transport.TransportAddress) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) RestChannel(org.opensearch.rest.RestChannel) SharedGroupFactory(org.opensearch.transport.SharedGroupFactory) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpServerTransport(org.opensearch.http.HttpServerTransport) NullDispatcher(org.opensearch.http.NullDispatcher) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) RestRequest(org.opensearch.rest.RestRequest) BytesRestResponse(org.opensearch.rest.BytesRestResponse) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse)

Example 4 with BytesRestResponse

use of org.opensearch.rest.BytesRestResponse in project OpenSearch by opensearch-project.

the class NioHttpServerTransportTests method testBadRequest.

public void testBadRequest() throws InterruptedException {
    final AtomicReference<Throwable> causeReference = new AtomicReference<>();
    final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {

        @Override
        public void dispatchRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext) {
            logger.error("--> Unexpected successful request [{}]", FakeRestRequest.requestToString(request));
            throw new AssertionError();
        }

        @Override
        public void dispatchBadRequest(final RestChannel channel, final ThreadContext threadContext, final Throwable cause) {
            causeReference.set(cause);
            try {
                final OpenSearchException e = new OpenSearchException("you sent a bad request and you should feel bad");
                channel.sendResponse(new BytesRestResponse(channel, BAD_REQUEST, e));
            } catch (final IOException e) {
                throw new AssertionError(e);
            }
        }
    };
    final Settings settings;
    final int maxInitialLineLength;
    final Setting<ByteSizeValue> httpMaxInitialLineLengthSetting = HttpTransportSettings.SETTING_HTTP_MAX_INITIAL_LINE_LENGTH;
    if (randomBoolean()) {
        maxInitialLineLength = httpMaxInitialLineLengthSetting.getDefault(Settings.EMPTY).bytesAsInt();
        settings = createSettings();
    } else {
        maxInitialLineLength = randomIntBetween(1, 8192);
        settings = createBuilderWithPort().put(httpMaxInitialLineLengthSetting.getKey(), maxInitialLineLength + "b").build();
    }
    try (NioHttpServerTransport transport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler, threadPool, xContentRegistry(), dispatcher, new NioGroupFactory(settings, logger), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS))) {
        transport.start();
        final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
        try (NioHttpClient client = new NioHttpClient()) {
            final String url = "/" + new String(new byte[maxInitialLineLength], Charset.forName("UTF-8"));
            final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, url);
            final FullHttpResponse response = client.send(remoteAddress.address(), request);
            try {
                assertThat(response.status(), equalTo(HttpResponseStatus.BAD_REQUEST));
                assertThat(new String(response.content().array(), Charset.forName("UTF-8")), containsString("you sent a bad request and you should feel bad"));
            } finally {
                response.release();
            }
        }
    }
    assertNotNull(causeReference.get());
    assertThat(causeReference.get(), instanceOf(TooLongFrameException.class));
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) TransportAddress(org.opensearch.common.transport.TransportAddress) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpServerTransport(org.opensearch.http.HttpServerTransport) NullDispatcher(org.opensearch.http.NullDispatcher) BytesRestResponse(org.opensearch.rest.BytesRestResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) NioGroupFactory(org.opensearch.transport.nio.NioGroupFactory) Settings(org.opensearch.common.settings.Settings) HttpTransportSettings(org.opensearch.http.HttpTransportSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) RestChannel(org.opensearch.rest.RestChannel) IOException(java.io.IOException) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) RestRequest(org.opensearch.rest.RestRequest) OpenSearchException(org.opensearch.OpenSearchException)

Example 5 with BytesRestResponse

use of org.opensearch.rest.BytesRestResponse in project OpenSearch by opensearch-project.

the class NioHttpServerTransportTests method runExpectHeaderTest.

private void runExpectHeaderTest(final Settings settings, final String expectation, final int contentLength, final HttpResponseStatus expectedStatus) throws InterruptedException {
    final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {

        @Override
        public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) {
            channel.sendResponse(new BytesRestResponse(OK, BytesRestResponse.TEXT_CONTENT_TYPE, new BytesArray("done")));
        }

        @Override
        public void dispatchBadRequest(RestChannel channel, ThreadContext threadContext, Throwable cause) {
            logger.error(new ParameterizedMessage("--> Unexpected bad request [{}]", FakeRestRequest.requestToString(channel.request())), cause);
            throw new AssertionError();
        }
    };
    try (NioHttpServerTransport transport = new NioHttpServerTransport(settings, networkService, bigArrays, pageRecycler, threadPool, xContentRegistry(), dispatcher, new NioGroupFactory(settings, logger), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS))) {
        transport.start();
        final TransportAddress remoteAddress = randomFrom(transport.boundAddress().boundAddresses());
        try (NioHttpClient client = new NioHttpClient()) {
            final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
            request.headers().set(HttpHeaderNames.EXPECT, expectation);
            HttpUtil.setContentLength(request, contentLength);
            final FullHttpResponse response = client.send(remoteAddress.address(), request);
            try {
                assertThat(response.status(), equalTo(expectedStatus));
                if (expectedStatus.equals(HttpResponseStatus.CONTINUE)) {
                    final FullHttpRequest continuationRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", Unpooled.EMPTY_BUFFER);
                    final FullHttpResponse continuationResponse = client.send(remoteAddress.address(), continuationRequest);
                    try {
                        assertThat(continuationResponse.status(), is(HttpResponseStatus.OK));
                        assertThat(new String(ByteBufUtil.getBytes(continuationResponse.content()), StandardCharsets.UTF_8), is("done"));
                    } finally {
                        continuationResponse.release();
                    }
                }
            } finally {
                response.release();
            }
        }
    }
}
Also used : BytesArray(org.opensearch.common.bytes.BytesArray) ClusterSettings(org.opensearch.common.settings.ClusterSettings) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) TransportAddress(org.opensearch.common.transport.TransportAddress) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) RestChannel(org.opensearch.rest.RestChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpServerTransport(org.opensearch.http.HttpServerTransport) NullDispatcher(org.opensearch.http.NullDispatcher) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) RestRequest(org.opensearch.rest.RestRequest) BytesRestResponse(org.opensearch.rest.BytesRestResponse) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) NioGroupFactory(org.opensearch.transport.nio.NioGroupFactory)

Aggregations

BytesRestResponse (org.opensearch.rest.BytesRestResponse)33 RestRequest (org.opensearch.rest.RestRequest)26 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)18 IOException (java.io.IOException)17 List (java.util.List)15 RestResponse (org.opensearch.rest.RestResponse)15 NodeClient (org.opensearch.client.node.NodeClient)14 BaseRestHandler (org.opensearch.rest.BaseRestHandler)13 GET (org.opensearch.rest.RestRequest.Method.GET)13 Arrays.asList (java.util.Arrays.asList)12 Collections.unmodifiableList (java.util.Collections.unmodifiableList)12 RestStatus (org.opensearch.rest.RestStatus)12 FakeRestRequest (org.opensearch.test.rest.FakeRestRequest)11 RestBuilderListener (org.opensearch.rest.action.RestBuilderListener)10 Strings (org.opensearch.common.Strings)8 RestChannel (org.opensearch.rest.RestChannel)8 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)7 Matchers.containsString (org.hamcrest.Matchers.containsString)7 TransportAddress (org.opensearch.common.transport.TransportAddress)7 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)7