Search in sources :

Example 11 with HttpServerTransport

use of org.opensearch.http.HttpServerTransport in project OpenSearch by opensearch-project.

the class Netty4BadRequestTests method testBadParameterEncoding.

public void testBadParameterEncoding() throws Exception {
    final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {

        @Override
        public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) {
            fail();
        }

        @Override
        public void dispatchBadRequest(RestChannel channel, ThreadContext threadContext, Throwable cause) {
            try {
                final Exception e = cause instanceof Exception ? (Exception) cause : new OpenSearchException(cause);
                channel.sendResponse(new BytesRestResponse(channel, RestStatus.BAD_REQUEST, e));
            } catch (final IOException e) {
                throw new UncheckedIOException(e);
            }
        }
    };
    Settings settings = Settings.builder().put(HttpTransportSettings.SETTING_HTTP_PORT.getKey(), getPortRange()).build();
    try (HttpServerTransport httpServerTransport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, xContentRegistry(), dispatcher, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), new SharedGroupFactory(Settings.EMPTY))) {
        httpServerTransport.start();
        final TransportAddress transportAddress = randomFrom(httpServerTransport.boundAddress().boundAddresses());
        try (Netty4HttpClient nettyHttpClient = new Netty4HttpClient()) {
            final Collection<FullHttpResponse> responses = nettyHttpClient.get(transportAddress.address(), "/_cluster/settings?pretty=%");
            try {
                assertThat(responses, hasSize(1));
                assertThat(responses.iterator().next().status().code(), equalTo(400));
                final Collection<String> responseBodies = Netty4HttpClient.returnHttpResponseBodies(responses);
                assertThat(responseBodies, hasSize(1));
                assertThat(responseBodies.iterator().next(), containsString("\"type\":\"bad_parameter_exception\""));
                assertThat(responseBodies.iterator().next(), containsString("\"reason\":\"java.lang.IllegalArgumentException: unterminated escape sequence at end of string: %\""));
            } finally {
                responses.forEach(ReferenceCounted::release);
            }
        }
    }
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) TransportAddress(org.opensearch.common.transport.TransportAddress) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) RestChannel(org.opensearch.rest.RestChannel) UncheckedIOException(java.io.UncheckedIOException) SharedGroupFactory(org.opensearch.transport.SharedGroupFactory) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpServerTransport(org.opensearch.http.HttpServerTransport) OpenSearchException(org.opensearch.OpenSearchException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) RestRequest(org.opensearch.rest.RestRequest) BytesRestResponse(org.opensearch.rest.BytesRestResponse) OpenSearchException(org.opensearch.OpenSearchException) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpTransportSettings(org.opensearch.http.HttpTransportSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings) ReferenceCounted(io.netty.util.ReferenceCounted)

Example 12 with HttpServerTransport

use of org.opensearch.http.HttpServerTransport in project OpenSearch by opensearch-project.

the class NioPipeliningIT method testThatNioHttpServerSupportsPipelining.

public void testThatNioHttpServerSupportsPipelining() throws Exception {
    String[] requests = new String[] { "/", "/_nodes/stats", "/", "/_cluster/state", "/" };
    HttpServerTransport httpServerTransport = internalCluster().getInstance(HttpServerTransport.class);
    TransportAddress[] boundAddresses = httpServerTransport.boundAddress().boundAddresses();
    TransportAddress transportAddress = randomFrom(boundAddresses);
    try (NioHttpClient nettyHttpClient = new NioHttpClient()) {
        Collection<FullHttpResponse> responses = nettyHttpClient.get(transportAddress.address(), requests);
        assertThat(responses, hasSize(5));
        Collection<String> opaqueIds = NioHttpClient.returnOpaqueIds(responses);
        assertOpaqueIdsInOrder(opaqueIds);
    }
}
Also used : TransportAddress(org.opensearch.common.transport.TransportAddress) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpServerTransport(org.opensearch.http.HttpServerTransport)

Aggregations

HttpServerTransport (org.opensearch.http.HttpServerTransport)12 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)6 ClusterSettings (org.opensearch.common.settings.ClusterSettings)6 TransportAddress (org.opensearch.common.transport.TransportAddress)6 ReferenceCounted (io.netty.util.ReferenceCounted)5 Settings (org.opensearch.common.settings.Settings)5 ThreadPool (org.opensearch.threadpool.ThreadPool)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 BigArrays (org.opensearch.common.util.BigArrays)3 PageCacheRecycler (org.opensearch.common.util.PageCacheRecycler)3 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)3 CircuitBreakerService (org.opensearch.indices.breaker.CircuitBreakerService)3 NetworkPlugin (org.opensearch.plugins.NetworkPlugin)3 TestThreadPool (org.opensearch.threadpool.TestThreadPool)3 IOException (java.io.IOException)2 Tuple (org.opensearch.common.collect.Tuple)2 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)2 NoOpNodeClient (org.opensearch.test.client.NoOpNodeClient)2