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

Added an option to add arbitrary headers to the client requests #7127

Closed
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Added an option to add arbitrary headers to the client requests

The headers are key/value pairs defined in the settings under the `request.headers` namespace.
  • Loading branch information...
uboness committed Aug 1, 2014
commit 0ec0edb5e43e9d798c3b52e44fdff24600ab65e6
@@ -25,6 +25,7 @@
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.support.AbstractClient;
import org.elasticsearch.client.support.Headers;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
@@ -44,11 +45,14 @@

private final ImmutableMap<ClientAction, TransportAction> actions;

private final Headers headers;

@Inject
public NodeClient(Settings settings, ThreadPool threadPool, NodeAdminClient admin, Map<GenericAction, TransportAction> actions) {
public NodeClient(Settings settings, ThreadPool threadPool, NodeAdminClient admin, Map<GenericAction, TransportAction> actions, Headers headers) {
this.settings = settings;
this.threadPool = threadPool;
this.admin = admin;
this.headers = headers;
MapBuilder<ClientAction, TransportAction> actionsBuilder = new MapBuilder<>();
for (Map.Entry<GenericAction, TransportAction> entry : actions.entrySet()) {
if (entry.getKey() instanceof ClientAction) {
@@ -81,13 +85,15 @@ public AdminClient admin() {
@SuppressWarnings("unchecked")
@Override
public <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder, Client>> ActionFuture<Response> execute(Action<Request, Response, RequestBuilder, Client> action, Request request) {
headers.applyTo(request);
TransportAction<Request, Response> transportAction = actions.get((ClientAction)action);
return transportAction.execute(request);
}

@SuppressWarnings("unchecked")
@Override
public <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder, Client>> void execute(Action<Request, Response, RequestBuilder, Client> action, Request request, ActionListener<Response> listener) {
headers.applyTo(request);
TransportAction<Request, Response> transportAction = actions.get((ClientAction)action);
transportAction.execute(request, listener);
}
@@ -23,6 +23,7 @@
import org.elasticsearch.client.Client;
import org.elasticsearch.client.ClusterAdminClient;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.support.Headers;
import org.elasticsearch.common.inject.AbstractModule;

/**
@@ -32,6 +33,7 @@

@Override
protected void configure() {
bind(Headers.class).asEagerSingleton();
bind(ClusterAdminClient.class).to(NodeClusterAdminClient.class).asEagerSingleton();
bind(IndicesAdminClient.class).to(NodeIndicesAdminClient.class).asEagerSingleton();
bind(AdminClient.class).to(NodeAdminClient.class).asEagerSingleton();
@@ -25,6 +25,7 @@
import org.elasticsearch.action.support.TransportAction;
import org.elasticsearch.client.ClusterAdminClient;
import org.elasticsearch.client.support.AbstractClusterAdminClient;
import org.elasticsearch.client.support.Headers;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.threadpool.ThreadPool;
@@ -40,9 +41,12 @@

private final ImmutableMap<ClusterAction, TransportAction> actions;

private final Headers headers;

@Inject
public NodeClusterAdminClient(ThreadPool threadPool, Map<GenericAction, TransportAction> actions) {
public NodeClusterAdminClient(ThreadPool threadPool, Map<GenericAction, TransportAction> actions, Headers headers) {
this.threadPool = threadPool;
this.headers = headers;
MapBuilder<ClusterAction, TransportAction> actionsBuilder = new MapBuilder<>();
for (Map.Entry<GenericAction, TransportAction> entry : actions.entrySet()) {
if (entry.getKey() instanceof ClusterAction) {
@@ -60,13 +64,15 @@ public ThreadPool threadPool() {
@SuppressWarnings("unchecked")
@Override
public <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder, ClusterAdminClient>> ActionFuture<Response> execute(Action<Request, Response, RequestBuilder, ClusterAdminClient> action, Request request) {
headers.applyTo(request);
TransportAction<Request, Response> transportAction = actions.get((ClusterAction)action);
return transportAction.execute(request);
}

@SuppressWarnings("unchecked")
@Override
public <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder, ClusterAdminClient>> void execute(Action<Request, Response, RequestBuilder, ClusterAdminClient> action, Request request, ActionListener<Response> listener) {
headers.applyTo(request);
TransportAction<Request, Response> transportAction = actions.get((ClusterAction)action);
transportAction.execute(request, listener);
}
@@ -25,6 +25,7 @@
import org.elasticsearch.action.support.TransportAction;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.support.AbstractIndicesAdminClient;
import org.elasticsearch.client.support.Headers;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.threadpool.ThreadPool;
@@ -40,9 +41,12 @@

private final ImmutableMap<IndicesAction, TransportAction> actions;

private final Headers headers;

@Inject
public NodeIndicesAdminClient(ThreadPool threadPool, Map<GenericAction, TransportAction> actions) {
public NodeIndicesAdminClient(ThreadPool threadPool, Map<GenericAction, TransportAction> actions, Headers headers) {
this.threadPool = threadPool;
this.headers = headers;
MapBuilder<IndicesAction, TransportAction> actionsBuilder = new MapBuilder<>();
for (Map.Entry<GenericAction, TransportAction> entry : actions.entrySet()) {
if (entry.getKey() instanceof IndicesAction) {
@@ -60,13 +64,15 @@ public ThreadPool threadPool() {
@SuppressWarnings("unchecked")
@Override
public <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder, IndicesAdminClient>> ActionFuture<Response> execute(Action<Request, Response, RequestBuilder, IndicesAdminClient> action, Request request) {
headers.applyTo(request);
TransportAction<Request, Response> transportAction = actions.get((IndicesAction)action);
return transportAction.execute(request);
}

@SuppressWarnings("unchecked")
@Override
public <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder, IndicesAdminClient>> void execute(Action<Request, Response, RequestBuilder, IndicesAdminClient> action, Request request, ActionListener<Response> listener) {
headers.applyTo(request);
TransportAction<Request, Response> transportAction = actions.get((IndicesAction)action);
transportAction.execute(request, listener);
}
@@ -0,0 +1,59 @@
/*
* 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.client.support;

import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;

/**
* Client request headers picked up from the client settings. Applied to every
* request sent by the client (both transport and node clients)
*/
public class Headers {

public static final String PREFIX = "request.headers";

public static final Headers EMPTY = new Headers(ImmutableSettings.EMPTY) {
@Override
public void applyTo(ActionRequest request) {
}
};

private final Settings headers;

@Inject
public Headers(Settings settings) {
headers = resolveHeaders(settings);
}

public void applyTo(ActionRequest request) {

This comment has been minimized.

Copy link
@javanna

javanna Aug 3, 2014

Member

I like applyTo better than set! Out of curiosity, any specific reason why you don't want to move this logic to the ActionRequest?

This comment has been minimized.

Copy link
@uboness

uboness Aug 3, 2014

Author Contributor

not sure where you want to put it... the headers are injected on the clients (it's a client only feature)

This comment has been minimized.

Copy link
@javanna

javanna Aug 4, 2014

Member

I see your point, fair enough

for (String key : headers.names()) {
request.putHeader(key, headers.get(key));
}
}

static Settings resolveHeaders(Settings settings) {
Settings headers = settings.getAsSettings(PREFIX);
return headers != null ? headers : ImmutableSettings.EMPTY;
}

}
@@ -19,6 +19,7 @@

package org.elasticsearch.client.transport;

import org.elasticsearch.client.support.Headers;
import org.elasticsearch.client.transport.support.InternalTransportAdminClient;
import org.elasticsearch.client.transport.support.InternalTransportClient;
import org.elasticsearch.client.transport.support.InternalTransportClusterAdminClient;
@@ -32,6 +33,7 @@

@Override
protected void configure() {
bind(Headers.class).asEagerSingleton();
bind(InternalTransportClient.class).asEagerSingleton();
bind(InternalTransportAdminClient.class).asEagerSingleton();
bind(InternalTransportIndicesAdminClient.class).asEagerSingleton();
@@ -25,6 +25,7 @@
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.support.AbstractClient;
import org.elasticsearch.client.support.Headers;
import org.elasticsearch.client.transport.TransportClientNodesService;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.collect.MapBuilder;
@@ -49,14 +50,17 @@

private final ImmutableMap<Action, TransportActionNodeProxy> actions;

private final Headers headers;

@Inject
public InternalTransportClient(Settings settings, ThreadPool threadPool, TransportService transportService,
TransportClientNodesService nodesService, InternalTransportAdminClient adminClient,
Map<String, GenericAction> actions) {
Map<String, GenericAction> actions, Headers headers) {
this.settings = settings;
this.threadPool = threadPool;
this.nodesService = nodesService;
this.adminClient = adminClient;
this.headers = headers;
MapBuilder<Action, TransportActionNodeProxy> actionsBuilder = new MapBuilder<>();
for (GenericAction action : actions.values()) {
if (action instanceof Action) {
@@ -97,6 +101,7 @@ public AdminClient admin() {
@SuppressWarnings("unchecked")
@Override
public <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder, Client>> void execute(final Action<Request, Response, RequestBuilder, Client> action, final Request request, ActionListener<Response> listener) {
headers.applyTo(request);
final TransportActionNodeProxy<Request, Response> proxy = actions.get(action);
nodesService.execute(new TransportClientNodesService.NodeListenerCallback<Response>() {
@Override
@@ -25,6 +25,7 @@
import org.elasticsearch.action.support.PlainActionFuture;
import org.elasticsearch.client.ClusterAdminClient;
import org.elasticsearch.client.support.AbstractClusterAdminClient;
import org.elasticsearch.client.support.Headers;
import org.elasticsearch.client.transport.TransportClientNodesService;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.collect.MapBuilder;
@@ -47,11 +48,14 @@

private final ImmutableMap<ClusterAction, TransportActionNodeProxy> actions;

private final Headers headers;

@Inject
public InternalTransportClusterAdminClient(Settings settings, TransportClientNodesService nodesService, ThreadPool threadPool, TransportService transportService,
Map<String, GenericAction> actions) {
Map<String, GenericAction> actions, Headers headers) {
this.nodesService = nodesService;
this.threadPool = threadPool;
this.headers = headers;
MapBuilder<ClusterAction, TransportActionNodeProxy> actionsBuilder = new MapBuilder<>();
for (GenericAction action : actions.values()) {
if (action instanceof ClusterAction) {
@@ -77,6 +81,7 @@ public ThreadPool threadPool() {
@SuppressWarnings("unchecked")
@Override
public <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder, ClusterAdminClient>> void execute(final Action<Request, Response, RequestBuilder, ClusterAdminClient> action, final Request request, final ActionListener<Response> listener) {
headers.applyTo(request);
final TransportActionNodeProxy<Request, Response> proxy = actions.get(action);
nodesService.execute(new TransportClientNodesService.NodeListenerCallback<Response>() {
@Override
@@ -25,6 +25,7 @@
import org.elasticsearch.action.support.PlainActionFuture;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.support.AbstractIndicesAdminClient;
import org.elasticsearch.client.support.Headers;
import org.elasticsearch.client.transport.TransportClientNodesService;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.collect.MapBuilder;
@@ -47,11 +48,14 @@

private final ImmutableMap<Action, TransportActionNodeProxy> actions;

private final Headers headers;

@Inject
public InternalTransportIndicesAdminClient(Settings settings, TransportClientNodesService nodesService, TransportService transportService, ThreadPool threadPool,
Map<String, GenericAction> actions) {
Map<String, GenericAction> actions, Headers headers) {
this.nodesService = nodesService;
this.threadPool = threadPool;
this.headers = headers;
MapBuilder<Action, TransportActionNodeProxy> actionsBuilder = new MapBuilder<>();
for (GenericAction action : actions.values()) {
if (action instanceof IndicesAction) {
@@ -77,6 +81,7 @@ public ThreadPool threadPool() {
@SuppressWarnings("unchecked")
@Override
public <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder, IndicesAdminClient>> void execute(final Action<Request, Response, RequestBuilder, IndicesAdminClient> action, final Request request, ActionListener<Response> listener) {
headers.applyTo(request);
final TransportActionNodeProxy<Request, Response> proxy = actions.get(action);
nodesService.execute(new TransportClientNodesService.NodeListenerCallback<Response>() {
@Override
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.