From 0cdea41bf507eb91c4968ed34c3fa91608bd277d Mon Sep 17 00:00:00 2001 From: Daniel Mitterdorfer Date: Mon, 11 Apr 2016 10:50:00 +0200 Subject: [PATCH] Turn RestChannel into an interface In #17133 we introduce request size limit handling and need a custom channel implementation. In order to ensure we delegate all methods it is better to have this channel implement an interface instead of an abstract base class (so changes on the interface turn into compile errors). Relates #17133 --- .../org/elasticsearch/http/HttpChannel.java | 33 ----- .../org/elasticsearch/http/HttpRequest.java | 29 ----- .../org/elasticsearch/http/HttpServer.java | 5 +- .../elasticsearch/http/HttpServerAdapter.java | 4 +- .../http/netty/NettyHttpChannel.java | 4 +- .../http/netty/NettyHttpRequest.java | 4 +- .../http/netty/NettyHttpServerTransport.java | 6 +- .../rest/AbstractRestChannel.java | 113 ++++++++++++++++++ .../org/elasticsearch/rest/RestChannel.java | 84 ++----------- .../rest/BytesRestResponseTests.java | 4 +- .../rest/RestFilterChainTests.java | 2 +- .../rest/action/support/RestTableTests.java | 4 +- .../DedicatedClusterSnapshotRestoreIT.java | 6 +- 13 files changed, 144 insertions(+), 154 deletions(-) delete mode 100644 core/src/main/java/org/elasticsearch/http/HttpChannel.java delete mode 100644 core/src/main/java/org/elasticsearch/http/HttpRequest.java create mode 100644 core/src/main/java/org/elasticsearch/rest/AbstractRestChannel.java diff --git a/core/src/main/java/org/elasticsearch/http/HttpChannel.java b/core/src/main/java/org/elasticsearch/http/HttpChannel.java deleted file mode 100644 index 0eaacdd1fba..00000000000 --- a/core/src/main/java/org/elasticsearch/http/HttpChannel.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.http; - -import org.elasticsearch.rest.RestChannel; -import org.elasticsearch.rest.RestRequest; - -/** - * - */ -public abstract class HttpChannel extends RestChannel { - - protected HttpChannel(RestRequest request, boolean detailedErrorsEnabled) { - super(request, detailedErrorsEnabled); - } -} diff --git a/core/src/main/java/org/elasticsearch/http/HttpRequest.java b/core/src/main/java/org/elasticsearch/http/HttpRequest.java deleted file mode 100644 index 99456b7d23d..00000000000 --- a/core/src/main/java/org/elasticsearch/http/HttpRequest.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.http; - -import org.elasticsearch.rest.RestRequest; - -/** - * - */ -public abstract class HttpRequest extends RestRequest { - -} diff --git a/core/src/main/java/org/elasticsearch/http/HttpServer.java b/core/src/main/java/org/elasticsearch/http/HttpServer.java index 5b1c0a32f86..5ca565e4c88 100644 --- a/core/src/main/java/org/elasticsearch/http/HttpServer.java +++ b/core/src/main/java/org/elasticsearch/http/HttpServer.java @@ -27,6 +27,7 @@ import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.env.Environment; import org.elasticsearch.node.service.NodeService; import org.elasticsearch.rest.BytesRestResponse; +import org.elasticsearch.rest.RestChannel; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.RestStatus; @@ -93,7 +94,7 @@ public class HttpServer extends AbstractLifecycleComponent implement return transport.stats(); } - public void dispatchRequest(HttpRequest request, HttpChannel channel, ThreadContext threadContext) { + public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) { if (request.rawPath().equals("/favicon.ico")) { handleFavicon(request, channel); return; @@ -101,7 +102,7 @@ public class HttpServer extends AbstractLifecycleComponent implement restController.dispatchRequest(request, channel, threadContext); } - void handleFavicon(HttpRequest request, HttpChannel channel) { + void handleFavicon(RestRequest request, RestChannel channel) { if (request.method() == RestRequest.Method.GET) { try { try (InputStream stream = getClass().getResourceAsStream("/config/favicon.ico")) { diff --git a/core/src/main/java/org/elasticsearch/http/HttpServerAdapter.java b/core/src/main/java/org/elasticsearch/http/HttpServerAdapter.java index c49265cae8c..7fe5562f17a 100644 --- a/core/src/main/java/org/elasticsearch/http/HttpServerAdapter.java +++ b/core/src/main/java/org/elasticsearch/http/HttpServerAdapter.java @@ -20,11 +20,13 @@ package org.elasticsearch.http; import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.rest.RestChannel; +import org.elasticsearch.rest.RestRequest; /** * */ public interface HttpServerAdapter { - void dispatchRequest(HttpRequest request, HttpChannel channel, ThreadContext context); + void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext context); } diff --git a/core/src/main/java/org/elasticsearch/http/netty/NettyHttpChannel.java b/core/src/main/java/org/elasticsearch/http/netty/NettyHttpChannel.java index 0ef5fff11bb..cd45d259293 100644 --- a/core/src/main/java/org/elasticsearch/http/netty/NettyHttpChannel.java +++ b/core/src/main/java/org/elasticsearch/http/netty/NettyHttpChannel.java @@ -24,10 +24,10 @@ import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.ReleasableBytesStreamOutput; import org.elasticsearch.common.lease.Releasable; import org.elasticsearch.common.netty.ReleaseChannelFutureListener; -import org.elasticsearch.http.HttpChannel; import org.elasticsearch.http.netty.cors.CorsHandler; import org.elasticsearch.http.netty.pipelining.OrderedDownstreamChannelEvent; import org.elasticsearch.http.netty.pipelining.OrderedUpstreamMessageEvent; +import org.elasticsearch.rest.AbstractRestChannel; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; import org.jboss.netty.buffer.ChannelBuffer; @@ -53,7 +53,7 @@ import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.CONNECTION; import static org.jboss.netty.handler.codec.http.HttpHeaders.Values.CLOSE; import static org.jboss.netty.handler.codec.http.HttpHeaders.Values.KEEP_ALIVE; -public final class NettyHttpChannel extends HttpChannel { +public final class NettyHttpChannel extends AbstractRestChannel { private final NettyHttpServerTransport transport; private final Channel channel; diff --git a/core/src/main/java/org/elasticsearch/http/netty/NettyHttpRequest.java b/core/src/main/java/org/elasticsearch/http/netty/NettyHttpRequest.java index d06d0957dd6..40ab2d184db 100644 --- a/core/src/main/java/org/elasticsearch/http/netty/NettyHttpRequest.java +++ b/core/src/main/java/org/elasticsearch/http/netty/NettyHttpRequest.java @@ -22,7 +22,7 @@ package org.elasticsearch.http.netty; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.ChannelBufferBytesReference; -import org.elasticsearch.http.HttpRequest; +import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.support.RestUtils; import org.jboss.netty.channel.Channel; import org.jboss.netty.handler.codec.http.HttpMethod; @@ -34,7 +34,7 @@ import java.util.Map; /** * */ -public class NettyHttpRequest extends HttpRequest { +public class NettyHttpRequest extends RestRequest { private final org.jboss.netty.handler.codec.http.HttpRequest request; private final Channel channel; diff --git a/core/src/main/java/org/elasticsearch/http/netty/NettyHttpServerTransport.java b/core/src/main/java/org/elasticsearch/http/netty/NettyHttpServerTransport.java index 4d95065fa16..ccc9a29b629 100644 --- a/core/src/main/java/org/elasticsearch/http/netty/NettyHttpServerTransport.java +++ b/core/src/main/java/org/elasticsearch/http/netty/NettyHttpServerTransport.java @@ -42,9 +42,7 @@ import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.common.util.concurrent.ThreadContext; import org.elasticsearch.http.BindHttpException; -import org.elasticsearch.http.HttpChannel; import org.elasticsearch.http.HttpInfo; -import org.elasticsearch.http.HttpRequest; import org.elasticsearch.http.HttpServerAdapter; import org.elasticsearch.http.HttpServerTransport; import org.elasticsearch.http.HttpStats; @@ -53,6 +51,8 @@ import org.elasticsearch.http.netty.cors.CorsConfigBuilder; import org.elasticsearch.http.netty.cors.CorsHandler; import org.elasticsearch.http.netty.pipelining.HttpPipeliningHandler; import org.elasticsearch.monitor.jvm.JvmInfo; +import org.elasticsearch.rest.RestChannel; +import org.elasticsearch.rest.RestRequest; import org.elasticsearch.rest.support.RestUtils; import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.transport.BindTransportException; @@ -483,7 +483,7 @@ public class NettyHttpServerTransport extends AbstractLifecycleComponent getRepoError = new AtomicReference<>(); - getRepoAction.handleRequest(getRepoRequest, new RestChannel(getRepoRequest, true) { + getRepoAction.handleRequest(getRepoRequest, new AbstractRestChannel(getRepoRequest, true) { @Override public void sendResponse(RestResponse response) { try { @@ -661,7 +661,7 @@ public class DedicatedClusterSnapshotRestoreIT extends AbstractSnapshotIntegTest RestRequest clusterStateRequest = new FakeRestRequest(); final CountDownLatch clusterStateLatch = new CountDownLatch(1); final AtomicReference clusterStateError = new AtomicReference<>(); - clusterStateAction.handleRequest(clusterStateRequest, new RestChannel(clusterStateRequest, true) { + clusterStateAction.handleRequest(clusterStateRequest, new AbstractRestChannel(clusterStateRequest, true) { @Override public void sendResponse(RestResponse response) { try {