Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove @Inject from rest handlers #15687

Closed
wants to merge 12 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public TransportClient build() {
}
modules.add(new PluginsModule(pluginsService));
modules.add(new SettingsModule(this.settings, settingsFilter ));
modules.add(new NetworkModule(networkService, this.settings, true));
modules.add(new NetworkModule(networkService, this.settings, settingsFilter, true, version));
modules.add(new ClusterNameModule(this.settings));
modules.add(new ThreadPoolModule(threadPool));
modules.add(new SearchModule() {
Expand Down
430 changes: 274 additions & 156 deletions core/src/main/java/org/elasticsearch/common/network/NetworkModule.java

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion core/src/main/java/org/elasticsearch/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ protected Node(Environment tmpEnv, Version version, Collection<Class<? extends P
final NetworkService networkService = new NetworkService(settings);
final SettingsFilter settingsFilter = new SettingsFilter(settings);
final ThreadPool threadPool = new ThreadPool(settings);
final NetworkModule networkModule = new NetworkModule(networkService, settings, settingsFilter, false, version);
boolean success = false;
try {
final MonitorService monitorService = new MonitorService(settings, nodeEnvironment, threadPool);
Expand All @@ -178,7 +179,7 @@ protected Node(Environment tmpEnv, Version version, Collection<Class<? extends P
modules.add(new SettingsModule(this.settings, settingsFilter));
modules.add(new EnvironmentModule(environment));
modules.add(new NodeModule(this, monitorService));
modules.add(new NetworkModule(networkService, settings, false));
modules.add(networkModule);
modules.add(new ScriptModule(this.settings));
modules.add(new NodeEnvironmentModule(nodeEnvironment));
modules.add(new ClusterNameModule(this.settings));
Expand All @@ -202,6 +203,14 @@ protected Node(Environment tmpEnv, Version version, Collection<Class<? extends P

client = injector.getInstance(Client.class);
threadPool.setClusterSettings(injector.getInstance(ClusterSettings.class));

/*
* This is our shim to handle REST dependencies not being non-guiced.
* Remove this when the dependencies can be passed to the constructor.
*/
injector.injectMembers(networkModule);

networkModule.setupRestActions();
success = true;
} catch (IOException ex) {
throw new ElasticsearchException("failed to bind service", ex);
Expand Down
51 changes: 6 additions & 45 deletions core/src/main/java/org/elasticsearch/rest/BaseRestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,9 @@

package org.elasticsearch.rest;

import org.elasticsearch.action.Action;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestBuilder;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.FilterClient;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.settings.Settings;

import java.util.Set;

/**
* Base handler for REST requests.
Expand All @@ -42,49 +33,19 @@
*/
public abstract class BaseRestHandler extends AbstractComponent implements RestHandler {

private final RestController controller;
private final Client client;
private final RestGlobalContext context;
protected final ParseFieldMatcher parseFieldMatcher;

protected BaseRestHandler(Settings settings, RestController controller, Client client) {
super(settings);
this.controller = controller;
this.client = client;
this.parseFieldMatcher = new ParseFieldMatcher(settings);
protected BaseRestHandler(RestGlobalContext context) {
super(context.getSettings());
this.context = context;
this.parseFieldMatcher = new ParseFieldMatcher(context.getSettings());
}

@Override
public final void handleRequest(RestRequest request, RestChannel channel) throws Exception {
handleRequest(request, channel, new HeadersAndContextCopyClient(client, request, controller.relevantHeaders()));
handleRequest(request, channel, context.createClient(request));
}

protected abstract void handleRequest(RestRequest request, RestChannel channel, Client client) throws Exception;

static final class HeadersAndContextCopyClient extends FilterClient {

private final RestRequest restRequest;
private final Set<String> headers;

HeadersAndContextCopyClient(Client in, RestRequest restRequest, Set<String> headers) {
super(in);
this.restRequest = restRequest;
this.headers = headers;
}

private static void copyHeadersAndContext(ActionRequest actionRequest, RestRequest restRequest, Set<String> headers) {
for (String usefulHeader : headers) {
String headerValue = restRequest.header(usefulHeader);
if (headerValue != null) {
actionRequest.putHeader(usefulHeader, headerValue);
}
}
actionRequest.copyContextFrom(restRequest);
}

@Override
protected <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder>> void doExecute(Action<Request, Response, RequestBuilder> action, Request request, ActionListener<Response> listener) {
copyHeadersAndContext(request, restRequest, headers);
super.doExecute(action, request, listener);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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.rest;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.rest.RestRequest.Method;

import static java.util.Collections.unmodifiableList;

import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestRequest.Method.PUT;

/**
* Helper subclass for describing REST handlers that want to register in standard ways.
*/
public abstract class BaseStandardRegistrationsRestHandler extends BaseRestHandler {
/**
* Many controllers use these methods.
*/
protected static final Method[] PUT_AND_POST = new Method[] {PUT, POST};

private final Method[] methods;
private final String[] paths;

/**
* Constructor for when registering for a single method.
*/
public BaseStandardRegistrationsRestHandler(RestGlobalContext context, Method method, String... paths) {
this(context, new Method[] {method}, paths);
}

/**
* Constructor for when registering for multiple methods on the same paths.
*/
public BaseStandardRegistrationsRestHandler(RestGlobalContext context, Method[] methods, String... paths) {
super(context);
this.methods = methods;
this.paths = paths;
}

protected String[] paths() {
return paths;
}

protected Method[] methods() {
return methods;
}

@Override
public final Collection<Tuple<Method, String>> registrations() {
List<Tuple<Method, String>> registrations = new ArrayList<>(paths.length * methods.length);
for (String path : paths) {
for (Method method : methods) {
registrations.add(new Tuple<>(method, path));
}
}
return unmodifiableList(registrations);
}
}
2 changes: 0 additions & 2 deletions core/src/main/java/org/elasticsearch/rest/RestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.path.PathTrie;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand Down Expand Up @@ -58,7 +57,6 @@ public class RestController extends AbstractLifecycleComponent<RestController> {
// non volatile since the assumption is that pre processors are registered on startup
private RestFilter[] filters = new RestFilter[0];

@Inject
public RestController(Settings settings) {
super(settings);
}
Expand Down
95 changes: 95 additions & 0 deletions core/src/main/java/org/elasticsearch/rest/RestGlobalContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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.rest;

import java.util.Set;

import org.elasticsearch.action.Action;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestBuilder;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.FilterClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.indices.query.IndicesQueriesRegistry;

/**
* Context passed to all Rest actions on construction. Think carefully before
* adding things to this because they can be seen by all rest actions.
*/
public class RestGlobalContext {
private final Settings settings;
private final RestController controller;
private final Client client;
private final IndicesQueriesRegistry indicesQueriesRegistry;

public RestGlobalContext(Settings settings, RestController controller, Client client, IndicesQueriesRegistry indicesQueriesRegistry) {
this.settings = settings;
this.controller = controller;
this.client = client;
this.indicesQueriesRegistry = indicesQueriesRegistry;
}

public Settings getSettings() {
return settings;
}

/**
* Create the client to be used to process a request. This client will copy
* headers from the rest request into the internal requests but otherwise
* simply wraps a globally shared client.
*/
public Client createClient(RestRequest request) {
return new HeadersAndContextCopyClient(client, request, controller.relevantHeaders());
}

public IndicesQueriesRegistry getIndicesQueriesRegistry() {
return indicesQueriesRegistry;
}

static final class HeadersAndContextCopyClient extends FilterClient {

private final RestRequest restRequest;
private final Set<String> headers;

HeadersAndContextCopyClient(Client in, RestRequest restRequest, Set<String> headers) {
super(in);
this.restRequest = restRequest;
this.headers = headers;
}

private static void copyHeadersAndContext(ActionRequest<?> actionRequest, RestRequest restRequest, Set<String> headers) {
for (String usefulHeader : headers) {
String headerValue = restRequest.header(usefulHeader);
if (headerValue != null) {
actionRequest.putHeader(usefulHeader, headerValue);
}
}
actionRequest.copyContextFrom(restRequest);
}

@Override
protected <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder>> void doExecute(Action<Request, Response, RequestBuilder> action, Request request, ActionListener<Response> listener) {
copyHeadersAndContext(request, restRequest, headers);
super.doExecute(action, request, listener);
}
}
}
10 changes: 9 additions & 1 deletion core/src/main/java/org/elasticsearch/rest/RestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@

package org.elasticsearch.rest;

import java.util.Collection;

import org.elasticsearch.common.collect.Tuple;

/**
* Handler for REST requests
*/
public interface RestHandler {

void handleRequest(RestRequest request, RestChannel channel) throws Exception;

/**
* REST methods for which this handler should be registered.
*/
Collection<Tuple<RestRequest.Method, String>> registrations();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,22 @@
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BaseStandardRegistrationsRestHandler;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestGlobalContext;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.support.RestStatusToXContentListener;

import java.util.Locale;

import static org.elasticsearch.client.Requests.clusterHealthRequest;

import static org.elasticsearch.rest.RestRequest.Method.GET;
/**
*
*/
public class RestClusterHealthAction extends BaseRestHandler {

@Inject
public RestClusterHealthAction(Settings settings, RestController controller, Client client) {
super(settings, controller, client);

controller.registerHandler(RestRequest.Method.GET, "/_cluster/health", this);
controller.registerHandler(RestRequest.Method.GET, "/_cluster/health/{index}", this);
public class RestClusterHealthAction extends BaseStandardRegistrationsRestHandler {
public RestClusterHealthAction(RestGlobalContext context) {
super(context, GET, "/_cluster/health", "/_cluster/health/{index}");
}

@Override
Expand Down
Loading