use of org.opensearch.rest.RestChannel in project OpenSearch by opensearch-project.
the class NioHttpServerTransportTests method testReadTimeout.
public void testReadTimeout() throws Exception {
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("Should not have received a dispatched request");
}
@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("Should not have received a dispatched request");
}
};
Settings settings = createBuilderWithPort().put(HttpTransportSettings.SETTING_HTTP_READ_TIMEOUT.getKey(), new TimeValue(randomIntBetween(100, 300))).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()) {
NioSocketChannel channel = null;
try {
CountDownLatch channelClosedLatch = new CountDownLatch(1);
channel = client.connect(remoteAddress.address());
channel.addCloseListener((r, t) -> channelClosedLatch.countDown());
assertTrue("Channel should be closed due to read timeout", channelClosedLatch.await(1, TimeUnit.MINUTES));
} finally {
if (channel != null) {
channel.close();
}
}
}
}
}
use of org.opensearch.rest.RestChannel in project OpenSearch by opensearch-project.
the class ActionModuleTests method testPluginCanRegisterRestHandler.
public void testPluginCanRegisterRestHandler() {
class FakeHandler implements RestHandler {
@Override
public List<Route> routes() {
return singletonList(new Route(Method.GET, "/_dummy"));
}
@Override
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
}
}
ActionPlugin registersFakeHandler = new ActionPlugin() {
@Override
public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, Supplier<DiscoveryNodes> nodesInCluster) {
return singletonList(new FakeHandler());
}
};
SettingsModule settings = new SettingsModule(Settings.EMPTY);
ThreadPool threadPool = new TestThreadPool(getTestName());
try {
UsageService usageService = new UsageService();
ActionModule actionModule = new ActionModule(settings.getSettings(), new IndexNameExpressionResolver(threadPool.getThreadContext()), settings.getIndexScopedSettings(), settings.getClusterSettings(), settings.getSettingsFilter(), threadPool, singletonList(registersFakeHandler), null, null, usageService, null);
actionModule.initRestHandlers(null);
// At this point the easiest way to confirm that a handler is loaded is to try to register another one on top of it and to fail
Exception e = expectThrows(IllegalArgumentException.class, () -> actionModule.getRestController().registerHandler(new RestHandler() {
@Override
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
}
@Override
public List<Route> routes() {
return singletonList(new Route(Method.GET, "/_dummy"));
}
}));
assertThat(e.getMessage(), startsWith("Cannot replace existing handler for [/_dummy] for method: GET"));
} finally {
threadPool.shutdown();
}
}
use of org.opensearch.rest.RestChannel in project OpenSearch by opensearch-project.
the class AbstractHttpServerTransportTests method testDispatchDoesNotModifyThreadContext.
public void testDispatchDoesNotModifyThreadContext() {
final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {
@Override
public void dispatchRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext) {
threadContext.putHeader("foo", "bar");
threadContext.putTransient("bar", "baz");
}
@Override
public void dispatchBadRequest(final RestChannel channel, final ThreadContext threadContext, final Throwable cause) {
threadContext.putHeader("foo_bad", "bar");
threadContext.putTransient("bar_bad", "baz");
}
};
try (AbstractHttpServerTransport transport = new AbstractHttpServerTransport(Settings.EMPTY, networkService, bigArrays, threadPool, xContentRegistry(), dispatcher, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)) {
@Override
protected HttpServerChannel bind(InetSocketAddress hostAddress) {
return null;
}
@Override
protected void doStart() {
}
@Override
protected void stopInternal() {
}
@Override
public HttpStats stats() {
return null;
}
}) {
transport.dispatchRequest(null, null, null);
assertNull(threadPool.getThreadContext().getHeader("foo"));
assertNull(threadPool.getThreadContext().getTransient("bar"));
transport.dispatchRequest(null, null, new Exception());
assertNull(threadPool.getThreadContext().getHeader("foo_bad"));
assertNull(threadPool.getThreadContext().getTransient("bar_bad"));
}
}
use of org.opensearch.rest.RestChannel in project OpenSearch by opensearch-project.
the class DefaultRestChannelTests method executeRequest.
private TestHttpResponse executeRequest(final Settings settings, final String originValue, final String host) {
HttpRequest httpRequest = new TestHttpRequest(HttpRequest.HttpVersion.HTTP_1_1, RestRequest.Method.GET, "/");
if (originValue != null) {
httpRequest.getHeaders().put(CorsHandler.ORIGIN, Collections.singletonList(originValue));
}
httpRequest.getHeaders().put(CorsHandler.HOST, Collections.singletonList(host));
final RestRequest request = RestRequest.request(xContentRegistry(), httpRequest, httpChannel);
HttpHandlingSettings httpHandlingSettings = HttpHandlingSettings.fromSettings(settings);
RestChannel channel = new DefaultRestChannel(httpChannel, httpRequest, request, bigArrays, httpHandlingSettings, threadPool.getThreadContext(), new CorsHandler(CorsHandler.buildConfig(settings)), null);
channel.sendResponse(new TestRestResponse());
// get the response
ArgumentCaptor<TestHttpResponse> responseCaptor = ArgumentCaptor.forClass(TestHttpResponse.class);
verify(httpChannel, atLeastOnce()).sendResponse(responseCaptor.capture(), any());
return responseCaptor.getValue();
}
use of org.opensearch.rest.RestChannel in project OpenSearch by opensearch-project.
the class Netty4HttpServerTransportTests 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 (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 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();
}
}
}
}
Aggregations