Remove lenient URL parameter parsing

This commit adapts x-plugins for a change in core Elasticsearch that
removes lenient URL parameter parsing.

Relates elastic/elasticsearch#3641

Original commit: elastic/x-pack-elasticsearch@cc0687f32c
This commit is contained in:
Jason Tedor 2016-10-04 12:46:54 -04:00 committed by GitHub
parent ddae0694c9
commit a0e1d44a44
38 changed files with 265 additions and 230 deletions

View File

@ -5,29 +5,22 @@
*/
package org.elasticsearch.license;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.settings.Setting;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.plugins.ActionPlugin;
import org.elasticsearch.rest.RestHandler;
import org.elasticsearch.watcher.ResourceWatcherService;
import org.elasticsearch.xpack.support.clock.Clock;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static java.util.Collections.emptyList;
import static org.elasticsearch.xpack.XPackPlugin.isTribeNode;
import static org.elasticsearch.xpack.XPackPlugin.transportClientMode;
public class Licensing implements ActionPlugin {
public static final String NAME = "license";
@ -69,4 +62,5 @@ public class Licensing implements ActionPlugin {
// TODO convert this wildcard to a real setting
return Collections.singletonList(Setting.groupSetting("license.", Setting.Property.NodeScope));
}
}

View File

@ -7,13 +7,14 @@ package org.elasticsearch.license;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.AcknowledgedRestListener;
import org.elasticsearch.xpack.XPackClient;
import org.elasticsearch.xpack.rest.XPackRestHandler;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.DELETE;
public class RestDeleteLicenseAction extends XPackRestHandler {
@ -33,9 +34,10 @@ public class RestDeleteLicenseAction extends XPackRestHandler {
}
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final XPackClient client) {
client.es().admin().cluster().execute(DeleteLicenseAction.INSTANCE,
public RestChannelConsumer doPrepareRequest(final RestRequest request, final XPackClient client) throws IOException {
return channel -> client.es().admin().cluster().execute(DeleteLicenseAction.INSTANCE,
new DeleteLicenseRequest(),
new AcknowledgedRestListener<>(channel));
}
}

View File

@ -10,7 +10,6 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
@ -18,6 +17,7 @@ import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.xpack.XPackClient;
import org.elasticsearch.xpack.rest.XPackRestHandler;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@ -48,14 +48,14 @@ public class RestGetLicenseAction extends XPackRestHandler {
* The licenses are sorted by latest issue_date
*/
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final XPackClient client) {
public RestChannelConsumer doPrepareRequest(final RestRequest request, final XPackClient client) throws IOException {
final Map<String, String> overrideParams = new HashMap<>(2);
overrideParams.put(License.REST_VIEW_MODE, "true");
overrideParams.put(License.LICENSE_VERSION_MODE, String.valueOf(License.VERSION_CURRENT));
final ToXContent.Params params = new ToXContent.DelegatingMapParams(overrideParams, request);
GetLicenseRequest getLicenseRequest = new GetLicenseRequest();
getLicenseRequest.local(request.paramAsBoolean("local", getLicenseRequest.local()));
client.es().admin().cluster().execute(GetLicenseAction.INSTANCE, getLicenseRequest,
return channel -> client.es().admin().cluster().execute(GetLicenseAction.INSTANCE, getLicenseRequest,
new RestBuilderListener<GetLicenseResponse>(channel) {
@Override
public RestResponse buildResponse(GetLicenseResponse response, XContentBuilder builder) throws Exception {

View File

@ -10,7 +10,6 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
@ -19,6 +18,8 @@ import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.xpack.XPackClient;
import org.elasticsearch.xpack.rest.XPackRestHandler;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestRequest.Method.PUT;
@ -45,11 +46,11 @@ public class RestPutLicenseAction extends XPackRestHandler {
}
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final XPackClient client) {
public RestChannelConsumer doPrepareRequest(final RestRequest request, final XPackClient client) throws IOException {
PutLicenseRequest putLicenseRequest = new PutLicenseRequest();
putLicenseRequest.license(request.content().utf8ToString());
putLicenseRequest.acknowledge(request.paramAsBoolean("acknowledge", false));
client.es().admin().cluster().execute(PutLicenseAction.INSTANCE, putLicenseRequest,
return channel -> client.es().admin().cluster().execute(PutLicenseAction.INSTANCE, putLicenseRequest,
new RestBuilderListener<PutLicenseResponse>(channel) {
@Override
public RestResponse buildResponse(PutLicenseResponse response, XContentBuilder builder) throws Exception {

View File

@ -26,20 +26,17 @@ import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.indices.query.IndicesQueriesRegistry;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestActions;
import org.elasticsearch.rest.action.RestToXContentListener;
import org.elasticsearch.xpack.XPackClient;
import org.elasticsearch.xpack.graph.action.GraphExploreRequest;
import org.elasticsearch.xpack.graph.action.GraphExploreResponse;
import org.elasticsearch.xpack.graph.action.Hop;
import org.elasticsearch.xpack.graph.action.VertexRequest;
import org.elasticsearch.xpack.graph.action.GraphExploreRequest.TermBoost;
import org.elasticsearch.xpack.rest.XPackRestHandler;
/**
* @see GraphExploreRequest
*/
@ -84,7 +81,7 @@ public class RestGraphAction extends XPackRestHandler {
}
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final XPackClient client) throws IOException {
public RestChannelConsumer doPrepareRequest(final RestRequest request, final XPackClient client) throws IOException {
GraphExploreRequest graphRequest = new GraphExploreRequest(Strings.splitStringByCommaToArray(request.param("index")));
graphRequest.indicesOptions(IndicesOptions.fromRequest(request, graphRequest.indicesOptions()));
graphRequest.routing(request.param("routing"));
@ -111,7 +108,7 @@ public class RestGraphAction extends XPackRestHandler {
}
graphRequest.types(Strings.splitStringByCommaToArray(request.param("type")));
client.es().execute(INSTANCE, graphRequest, new RestToXContentListener<GraphExploreResponse>(channel));
return channel -> client.es().execute(INSTANCE, graphRequest, new RestToXContentListener<>(channel));
}
private void parseHop(XContentParser parser, QueryParseContext context, Hop currentHop,

View File

@ -85,7 +85,7 @@ public class MonitoringBulkRequest extends ActionRequest<MonitoringBulkRequest>
* Parses a monitoring bulk request and builds the list of documents to be indexed.
*/
public MonitoringBulkRequest add(BytesReference content, String defaultMonitoringId, String defaultMonitoringApiVersion,
String defaultType) throws Exception {
String defaultType) throws IOException {
// MonitoringBulkRequest accepts a body request that has the same format as the BulkRequest:
// instead of duplicating the parsing logic here we use a new BulkRequest instance to parse the content.
BulkRequest bulkRequest = Requests.bulkRequest().add(content, null, defaultType);

View File

@ -9,6 +9,8 @@ import org.elasticsearch.action.ActionRequestBuilder;
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.common.bytes.BytesReference;
import java.io.IOException;
public class MonitoringBulkRequestBuilder
extends ActionRequestBuilder<MonitoringBulkRequest, MonitoringBulkResponse, MonitoringBulkRequestBuilder> {
@ -22,8 +24,9 @@ public class MonitoringBulkRequestBuilder
}
public MonitoringBulkRequestBuilder add(BytesReference content, String defaultId, String defaultApiVersion, String defaultType)
throws Exception {
throws IOException {
request.add(content, defaultId, defaultApiVersion, defaultType);
return this;
}
}

View File

@ -23,6 +23,7 @@ import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.rest.RestStatus;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@ -52,9 +53,7 @@ public class BackwardsCompatibilityAliasesResource extends HttpResource {
boolean needNewAliases = false;
XContentBuilder request;
try {
Map<String, String> params = parameters();
params.put("filter_path", "*.aliases");
Response response = client.performRequest("GET", "/.marvel-es-1-*", params);
Response response = client.performRequest("GET", "/.marvel-es-1-*", Collections.singletonMap("filter_path", "*.aliases"));
try (XContentParser parser = JsonXContent.jsonXContent.createParser(response.getEntity().getContent())) {
Map<String, Object> indices = parser.map();
request = JsonXContent.contentBuilder();

View File

@ -15,4 +15,5 @@ public abstract class MonitoringRestHandler extends XPackRestHandler {
public MonitoringRestHandler(Settings settings) {
super(settings);
}
}

View File

@ -11,7 +11,6 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
@ -22,6 +21,8 @@ import org.elasticsearch.xpack.monitoring.action.MonitoringBulkRequestBuilder;
import org.elasticsearch.xpack.monitoring.action.MonitoringBulkResponse;
import org.elasticsearch.xpack.monitoring.rest.MonitoringRestHandler;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestRequest.Method.PUT;
@ -41,7 +42,7 @@ public class RestMonitoringBulkAction extends MonitoringRestHandler {
}
@Override
public void handleRequest(RestRequest request, RestChannel channel, XPackClient client) throws Exception {
public RestChannelConsumer doPrepareRequest(RestRequest request, XPackClient client) throws IOException {
String defaultType = request.param("type");
String id = request.param(MONITORING_ID);
@ -64,7 +65,7 @@ public class RestMonitoringBulkAction extends MonitoringRestHandler {
MonitoringBulkRequestBuilder requestBuilder = client.monitoring().prepareMonitoringBulk();
requestBuilder.add(request.content(), id, version, defaultType);
requestBuilder.execute(new RestBuilderListener<MonitoringBulkResponse>(channel) {
return channel -> requestBuilder.execute(new RestBuilderListener<MonitoringBulkResponse>(channel) {
@Override
public RestResponse buildResponse(MonitoringBulkResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
@ -87,4 +88,5 @@ public class RestMonitoringBulkAction extends MonitoringRestHandler {
static final String ERRORS = "errors";
static final String ERROR = "error";
}
}

View File

@ -8,13 +8,11 @@ package org.elasticsearch.xpack.rest;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.xpack.XPackClient;
/**
*
*/
import java.io.IOException;
public abstract class XPackRestHandler extends BaseRestHandler {
protected static String URI_BASE = "/_xpack";
@ -24,9 +22,10 @@ public abstract class XPackRestHandler extends BaseRestHandler {
}
@Override
public final void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
handleRequest(request, channel, new XPackClient(client));
public final RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
return doPrepareRequest(request, new XPackClient(client));
}
protected abstract void handleRequest(RestRequest request, RestChannel channel, XPackClient client) throws Exception;
protected abstract RestChannelConsumer doPrepareRequest(RestRequest request, XPackClient client) throws IOException;
}

View File

@ -5,13 +5,13 @@
*/
package org.elasticsearch.xpack.rest.action;
import java.io.IOException;
import java.util.EnumSet;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
@ -35,14 +35,18 @@ public class RestXPackInfoAction extends XPackRestHandler {
}
@Override
public void handleRequest(RestRequest request, RestChannel restChannel, XPackClient client) throws Exception {
public RestChannelConsumer doPrepareRequest(RestRequest request, XPackClient client) throws IOException {
// we piggyback verbosity on "human" output
boolean verbose = request.paramAsBoolean("human", true);
EnumSet<XPackInfoRequest.Category> categories = XPackInfoRequest.Category
.toSet(request.paramAsStringArray("categories", new String[] { "_all" }));
client.prepareInfo().setVerbose(verbose).setCategories(categories).execute(new RestBuilderListener<XPackInfoResponse>(restChannel) {
return channel ->
client.prepareInfo()
.setVerbose(verbose)
.setCategories(categories)
.execute(new RestBuilderListener<XPackInfoResponse>(channel) {
@Override
public RestResponse buildResponse(XPackInfoResponse infoResponse, XContentBuilder builder) throws Exception {

View File

@ -8,9 +8,9 @@ package org.elasticsearch.xpack.rest.action;
import org.elasticsearch.action.support.master.MasterNodeRequest;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
@ -21,6 +21,8 @@ import org.elasticsearch.xpack.action.XPackUsageRequestBuilder;
import org.elasticsearch.xpack.action.XPackUsageResponse;
import org.elasticsearch.xpack.rest.XPackRestHandler;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.OK;
@ -33,10 +35,11 @@ public class RestXPackUsageAction extends XPackRestHandler {
}
@Override
public void handleRequest(RestRequest request, RestChannel restChannel, XPackClient client) throws Exception {
new XPackUsageRequestBuilder(client.es())
.setMasterNodeTimeout(request.paramAsTime("master_timeout", MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT))
.execute(new RestBuilderListener<XPackUsageResponse>(restChannel) {
public RestChannelConsumer doPrepareRequest(RestRequest request, XPackClient client) throws IOException {
final TimeValue masterTimeout = request.paramAsTime("master_timeout", MasterNodeRequest.DEFAULT_MASTER_NODE_TIMEOUT);
return channel -> new XPackUsageRequestBuilder(client.es())
.setMasterNodeTimeout(masterTimeout)
.execute(new RestBuilderListener<XPackUsageResponse>(channel) {
@Override
public RestResponse buildResponse(XPackUsageResponse response, XContentBuilder builder) throws Exception {
builder.startObject();

View File

@ -13,6 +13,7 @@ import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.xpack.security.authz.RoleDescriptor;
import org.elasticsearch.xpack.security.authz.permission.FieldPermissions;
import java.io.IOException;
import java.util.Map;
/**
@ -29,7 +30,7 @@ public class PutRoleRequestBuilder extends ActionRequestBuilder<PutRoleRequest,
super(client, action, new PutRoleRequest());
}
public PutRoleRequestBuilder source(String name, BytesReference source) throws Exception {
public PutRoleRequestBuilder source(String name, BytesReference source) throws IOException {
// we pass false as last parameter because we want to reject the request if field permissions
// are given in 2.x syntax
RoleDescriptor descriptor = RoleDescriptor.parse(name, source, false);

View File

@ -197,7 +197,7 @@ public class SecurityClient {
return new PutRoleRequestBuilder(client).name(name);
}
public PutRoleRequestBuilder preparePutRole(String name, BytesReference source) throws Exception {
public PutRoleRequestBuilder preparePutRole(String name, BytesReference source) throws IOException {
return new PutRoleRequestBuilder(client).source(name, source);
}

View File

@ -14,7 +14,6 @@ import org.elasticsearch.license.LicenseUtils;
import org.elasticsearch.license.XPackLicenseState;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
@ -22,10 +21,12 @@ import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.xpack.XPackPlugin;
import org.elasticsearch.xpack.security.SecurityContext;
import org.elasticsearch.xpack.security.user.User;
import org.elasticsearch.xpack.security.action.user.AuthenticateAction;
import org.elasticsearch.xpack.security.action.user.AuthenticateRequest;
import org.elasticsearch.xpack.security.action.user.AuthenticateResponse;
import org.elasticsearch.xpack.security.user.User;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.GET;
@ -50,7 +51,7 @@ public class RestAuthenticateAction extends BaseRestHandler {
}
@Override
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
// this API is a special case since we access the user here and we want it to fail with the proper error instead of a request
// validation error
if (licenseState.isAuthAllowed() == false) {
@ -61,7 +62,7 @@ public class RestAuthenticateAction extends BaseRestHandler {
assert user != null;
final String username = user.runAs() == null ? user.principal() : user.runAs().principal();
client.execute(AuthenticateAction.INSTANCE, new AuthenticateRequest(username),
return channel -> client.execute(AuthenticateAction.INSTANCE, new AuthenticateRequest(username),
new RestBuilderListener<AuthenticateResponse>(channel) {
@Override
public RestResponse buildResponse(AuthenticateResponse authenticateResponse, XContentBuilder builder) throws Exception {

View File

@ -9,13 +9,14 @@ import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestActions.NodesResponseRestListener;
import org.elasticsearch.xpack.security.action.realm.ClearRealmCacheRequest;
import org.elasticsearch.xpack.security.client.SecurityClient;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.POST;
public class RestClearRealmCacheAction extends BaseRestHandler {
@ -37,14 +38,14 @@ public class RestClearRealmCacheAction extends BaseRestHandler {
}
@Override
public void handleRequest(RestRequest request, final RestChannel channel, NodeClient client) throws Exception {
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
String[] realms = request.paramAsStringArrayOrEmptyIfAll("realms");
String[] usernames = request.paramAsStringArrayOrEmptyIfAll("usernames");
ClearRealmCacheRequest req = new ClearRealmCacheRequest().realms(realms).usernames(usernames);
new SecurityClient(client).clearRealmCache(req, new NodesResponseRestListener<>(channel));
return channel -> new SecurityClient(client).clearRealmCache(req, new NodesResponseRestListener<>(channel));
}
}

View File

@ -9,13 +9,14 @@ import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.RestActions.NodesResponseRestListener;
import org.elasticsearch.xpack.security.action.role.ClearRolesCacheRequest;
import org.elasticsearch.xpack.security.client.SecurityClient;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.POST;
/**
@ -36,12 +37,12 @@ public class RestClearRolesCacheAction extends BaseRestHandler {
}
@Override
public void handleRequest(RestRequest request, final RestChannel channel, NodeClient client) throws Exception {
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
String[] roles = request.paramAsStringArrayOrEmptyIfAll("name");
ClearRolesCacheRequest req = new ClearRolesCacheRequest().names(roles);
new SecurityClient(client).clearRolesCache(req, new NodesResponseRestListener<>(channel));
return channel -> new SecurityClient(client).clearRolesCache(req, new NodesResponseRestListener<>(channel));
}
}

View File

@ -11,7 +11,6 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
@ -20,6 +19,8 @@ import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.xpack.security.action.role.DeleteRoleResponse;
import org.elasticsearch.xpack.security.client.SecurityClient;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.DELETE;
/**
@ -40,16 +41,18 @@ public class RestDeleteRoleAction extends BaseRestHandler {
}
@Override
public void handleRequest(RestRequest request, final RestChannel channel, NodeClient client) throws Exception {
new SecurityClient(client).prepareDeleteRole(request.param("name"))
.setRefreshPolicy(request.param("refresh"))
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
final String name = request.param("name");
final String refresh = request.param("refresh");
return channel -> new SecurityClient(client).prepareDeleteRole(name)
.setRefreshPolicy(refresh)
.execute(new RestBuilderListener<DeleteRoleResponse>(channel) {
@Override
public RestResponse buildResponse(DeleteRoleResponse response, XContentBuilder builder) throws Exception {
return new BytesRestResponse(response.found() ? RestStatus.OK : RestStatus.NOT_FOUND,
builder.startObject()
.field("found", response.found())
.endObject());
return new BytesRestResponse(
response.found() ? RestStatus.OK : RestStatus.NOT_FOUND,
builder.startObject().field("found", response.found()).endObject());
}
});
}

View File

@ -12,15 +12,16 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.xpack.security.action.role.GetRolesResponse;
import org.elasticsearch.xpack.security.client.SecurityClient;
import org.elasticsearch.xpack.security.authz.RoleDescriptor;
import org.elasticsearch.xpack.security.client.SecurityClient;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.GET;
@ -47,9 +48,9 @@ public class RestGetRolesAction extends BaseRestHandler {
}
@Override
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
final String[] roles = request.paramAsStringArray("name", Strings.EMPTY_ARRAY);
new SecurityClient(client).prepareGetRoles(roles).execute(new RestBuilderListener<GetRolesResponse>(channel) {
return channel -> new SecurityClient(client).prepareGetRoles(roles).execute(new RestBuilderListener<GetRolesResponse>(channel) {
@Override
public RestResponse buildResponse(GetRolesResponse response, XContentBuilder builder) throws Exception {
builder.startObject();

View File

@ -11,7 +11,6 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
@ -21,6 +20,8 @@ import org.elasticsearch.xpack.security.action.role.PutRoleRequestBuilder;
import org.elasticsearch.xpack.security.action.role.PutRoleResponse;
import org.elasticsearch.xpack.security.client.SecurityClient;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestRequest.Method.PUT;
@ -47,16 +48,13 @@ public class RestPutRoleAction extends BaseRestHandler {
}
@Override
public void handleRequest(RestRequest request, final RestChannel channel, NodeClient client) throws Exception {
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
PutRoleRequestBuilder requestBuilder = new SecurityClient(client).preparePutRole(request.param("name"), request.content());
requestBuilder.setRefreshPolicy(request.param("refresh"));
requestBuilder.execute(new RestBuilderListener<PutRoleResponse>(channel) {
return channel -> requestBuilder.execute(new RestBuilderListener<PutRoleResponse>(channel) {
@Override
public RestResponse buildResponse(PutRoleResponse putRoleResponse, XContentBuilder builder) throws Exception {
return new BytesRestResponse(RestStatus.OK,
builder.startObject()
.field("role", putRoleResponse)
.endObject());
return new BytesRestResponse(RestStatus.OK, builder.startObject().field("role", putRoleResponse).endObject());
}
});
}

View File

@ -11,16 +11,17 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.xpack.security.SecurityContext;
import org.elasticsearch.xpack.security.user.User;
import org.elasticsearch.xpack.security.action.user.ChangePasswordResponse;
import org.elasticsearch.xpack.security.client.SecurityClient;
import org.elasticsearch.xpack.security.user.User;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestRequest.Method.PUT;
@ -42,19 +43,25 @@ public class RestChangePasswordAction extends BaseRestHandler {
}
@Override
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
final User user = securityContext.getUser();
String username = request.param("username");
if (username == null) {
final String username;
if (request.param("username") == null) {
username = user.runAs() == null ? user.principal() : user.runAs().principal();
} else {
username = request.param("username");
}
final String refresh = request.param("refresh");
return channel ->
new SecurityClient(client).prepareChangePassword(username, request.content())
.setRefreshPolicy(request.param("refresh"))
.setRefreshPolicy(refresh)
.execute(new RestBuilderListener<ChangePasswordResponse>(channel) {
@Override
public RestResponse buildResponse(ChangePasswordResponse changePasswordResponse, XContentBuilder builder) throws
Exception {
public RestResponse buildResponse(
ChangePasswordResponse changePasswordResponse,
XContentBuilder builder)
throws Exception {
return new BytesRestResponse(RestStatus.OK, channel.newBuilder().startObject().endObject());
}
});

View File

@ -11,7 +11,6 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
@ -20,6 +19,8 @@ import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.xpack.security.action.user.DeleteUserResponse;
import org.elasticsearch.xpack.security.client.SecurityClient;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.DELETE;
/**
@ -40,9 +41,11 @@ public class RestDeleteUserAction extends BaseRestHandler {
}
@Override
public void handleRequest(RestRequest request, final RestChannel channel, NodeClient client) throws Exception {
new SecurityClient(client).prepareDeleteUser(request.param("username"))
.setRefreshPolicy(request.param("refresh"))
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
final String username = request.param("username");
final String refresh = request.param("refresh");
return channel -> new SecurityClient(client).prepareDeleteUser(username)
.setRefreshPolicy(refresh)
.execute(new RestBuilderListener<DeleteUserResponse>(channel) {
@Override
public RestResponse buildResponse(DeleteUserResponse response, XContentBuilder builder) throws Exception {

View File

@ -12,15 +12,16 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.xpack.security.user.User;
import org.elasticsearch.xpack.security.action.user.GetUsersResponse;
import org.elasticsearch.xpack.security.client.SecurityClient;
import org.elasticsearch.xpack.security.user.User;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.GET;
@ -47,10 +48,10 @@ public class RestGetUsersAction extends BaseRestHandler {
}
@Override
public void handleRequest(RestRequest request, final RestChannel channel, NodeClient client) throws Exception {
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
String[] usernames = request.paramAsStringArray("username", Strings.EMPTY_ARRAY);
new SecurityClient(client).prepareGetUsers(usernames).execute(new RestBuilderListener<GetUsersResponse>(channel) {
return channel -> new SecurityClient(client).prepareGetUsers(usernames).execute(new RestBuilderListener<GetUsersResponse>(channel) {
@Override
public RestResponse buildResponse(GetUsersResponse response, XContentBuilder builder) throws Exception {
builder.startObject();

View File

@ -11,7 +11,6 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
@ -21,6 +20,8 @@ import org.elasticsearch.xpack.security.action.user.PutUserRequestBuilder;
import org.elasticsearch.xpack.security.action.user.PutUserResponse;
import org.elasticsearch.xpack.security.client.SecurityClient;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestRequest.Method.PUT;
@ -47,10 +48,10 @@ public class RestPutUserAction extends BaseRestHandler {
}
@Override
public void handleRequest(RestRequest request, final RestChannel channel, NodeClient client) throws Exception {
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
PutUserRequestBuilder requestBuilder = new SecurityClient(client).preparePutUser(request.param("username"), request.content());
requestBuilder.setRefreshPolicy(request.param("refresh"));
requestBuilder.execute(new RestBuilderListener<PutUserResponse>(channel) {
return channel -> requestBuilder.execute(new RestBuilderListener<PutUserResponse>(channel) {
@Override
public RestResponse buildResponse(PutUserResponse putUserResponse, XContentBuilder builder) throws Exception {
return new BytesRestResponse(RestStatus.OK,

View File

@ -11,7 +11,6 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
@ -20,6 +19,8 @@ import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.xpack.security.action.user.SetEnabledResponse;
import org.elasticsearch.xpack.security.client.SecurityClient;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestRequest.Method.PUT;
@ -39,10 +40,11 @@ public class RestSetEnabledAction extends BaseRestHandler {
}
@Override
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
final boolean enabled = request.path().endsWith("_enable");
assert enabled || request.path().endsWith("_disable");
new SecurityClient(client).prepareSetEnabled(request.param("username"), enabled)
final String username = request.param("username");
return channel -> new SecurityClient(client).prepareSetEnabled(username, enabled)
.execute(new RestBuilderListener<SetEnabledResponse>(channel) {
@Override
public RestResponse buildResponse(SetEnabledResponse setEnabledResponse, XContentBuilder builder) throws Exception {

View File

@ -5,17 +5,14 @@
*/
package org.elasticsearch.xpack.watcher.rest;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.xpack.watcher.client.WatcherClient;
/**
*
*/
import java.io.IOException;
public abstract class WatcherRestHandler extends BaseRestHandler {
protected static String URI_BASE = "_xpack/watcher";
@ -25,9 +22,10 @@ public abstract class WatcherRestHandler extends BaseRestHandler {
}
@Override
public final void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
handleRequest(request, channel, new WatcherClient(client));
public final RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
return doPrepareRequest(request, new WatcherClient(client));
}
protected abstract void handleRequest(RestRequest request, RestChannel channel, WatcherClient client) throws Exception;
protected abstract RestChannelConsumer doPrepareRequest(RestRequest request, WatcherClient client) throws IOException;
}

View File

@ -9,7 +9,6 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
@ -22,6 +21,8 @@ import org.elasticsearch.xpack.watcher.transport.actions.ack.AckWatchRequest;
import org.elasticsearch.xpack.watcher.transport.actions.ack.AckWatchResponse;
import org.elasticsearch.xpack.watcher.watch.Watch;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestRequest.Method.PUT;
@ -56,14 +57,14 @@ public class RestAckWatchAction extends WatcherRestHandler {
}
@Override
public void handleRequest(RestRequest request, RestChannel restChannel, WatcherClient client) throws Exception {
public RestChannelConsumer doPrepareRequest(RestRequest request, WatcherClient client) throws IOException {
AckWatchRequest ackWatchRequest = new AckWatchRequest(request.param("id"));
String[] actions = request.paramAsStringArray("actions", null);
if (actions != null) {
ackWatchRequest.setActionIds(actions);
}
ackWatchRequest.masterNodeTimeout(request.paramAsTime("master_timeout", ackWatchRequest.masterNodeTimeout()));
client.ackWatch(ackWatchRequest, new RestBuilderListener<AckWatchResponse>(restChannel) {
return channel -> client.ackWatch(ackWatchRequest, new RestBuilderListener<AckWatchResponse>(channel) {
@Override
public RestResponse buildResponse(AckWatchResponse response, XContentBuilder builder) throws Exception {
return new BytesRestResponse(RestStatus.OK, builder.startObject()

View File

@ -9,7 +9,6 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
@ -22,6 +21,8 @@ import org.elasticsearch.xpack.watcher.transport.actions.activate.ActivateWatchR
import org.elasticsearch.xpack.watcher.transport.actions.activate.ActivateWatchResponse;
import org.elasticsearch.xpack.watcher.watch.Watch;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestRequest.Method.PUT;
@ -48,8 +49,9 @@ public class RestActivateWatchAction extends WatcherRestHandler {
}
@Override
public void handleRequest(RestRequest request, RestChannel channel, WatcherClient client) throws Exception {
public RestChannelConsumer doPrepareRequest(RestRequest request, WatcherClient client) throws IOException {
String watchId = request.param("id");
return channel ->
client.activateWatch(new ActivateWatchRequest(watchId, true), new RestBuilderListener<ActivateWatchResponse>(channel) {
@Override
public RestResponse buildResponse(ActivateWatchResponse response, XContentBuilder builder) throws Exception {
@ -60,15 +62,16 @@ public class RestActivateWatchAction extends WatcherRestHandler {
});
}
static class DeactivateRestHandler extends WatcherRestHandler {
private static class DeactivateRestHandler extends WatcherRestHandler {
public DeactivateRestHandler(Settings settings) {
DeactivateRestHandler(Settings settings) {
super(settings);
}
@Override
public void handleRequest(RestRequest request, RestChannel channel, WatcherClient client) throws Exception {
public RestChannelConsumer doPrepareRequest(RestRequest request, WatcherClient client) throws IOException {
String watchId = request.param("id");
return channel ->
client.activateWatch(new ActivateWatchRequest(watchId, false), new RestBuilderListener<ActivateWatchResponse>(channel) {
@Override
public RestResponse buildResponse(ActivateWatchResponse response, XContentBuilder builder) throws Exception {

View File

@ -9,7 +9,6 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
@ -20,12 +19,12 @@ import org.elasticsearch.xpack.watcher.rest.WatcherRestHandler;
import org.elasticsearch.xpack.watcher.transport.actions.delete.DeleteWatchRequest;
import org.elasticsearch.xpack.watcher.transport.actions.delete.DeleteWatchResponse;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.DELETE;
import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
import static org.elasticsearch.rest.RestStatus.OK;
/**
*/
public class RestDeleteWatchAction extends WatcherRestHandler {
@Inject
@ -37,10 +36,10 @@ public class RestDeleteWatchAction extends WatcherRestHandler {
}
@Override
protected void handleRequest(final RestRequest request, RestChannel channel, WatcherClient client) throws Exception {
protected RestChannelConsumer doPrepareRequest(final RestRequest request, WatcherClient client) throws IOException {
DeleteWatchRequest deleteWatchRequest = new DeleteWatchRequest(request.param("id"));
deleteWatchRequest.masterNodeTimeout(request.paramAsTime("master_timeout", deleteWatchRequest.masterNodeTimeout()));
client.deleteWatch(deleteWatchRequest, new RestBuilderListener<DeleteWatchResponse>(channel) {
return channel -> client.deleteWatch(deleteWatchRequest, new RestBuilderListener<DeleteWatchResponse>(channel) {
@Override
public RestResponse buildResponse(DeleteWatchResponse response, XContentBuilder builder) throws Exception {
builder.startObject()
@ -53,4 +52,5 @@ public class RestDeleteWatchAction extends WatcherRestHandler {
}
});
}
}

View File

@ -14,7 +14,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
@ -59,10 +58,10 @@ public class RestExecuteWatchAction extends WatcherRestHandler {
}
@Override
protected void handleRequest(final RestRequest request, RestChannel channel, WatcherClient client) throws Exception {
protected RestChannelConsumer doPrepareRequest(final RestRequest request, WatcherClient client) throws IOException {
ExecuteWatchRequest executeWatchRequest = parseRequest(request, client);
client.executeWatch(executeWatchRequest, new RestBuilderListener<ExecuteWatchResponse>(channel) {
return channel -> client.executeWatch(executeWatchRequest, new RestBuilderListener<ExecuteWatchResponse>(channel) {
@Override
public RestResponse buildResponse(ExecuteWatchResponse response, XContentBuilder builder) throws Exception {
builder.startObject();

View File

@ -9,7 +9,6 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
@ -22,6 +21,8 @@ import org.elasticsearch.xpack.watcher.transport.actions.get.GetWatchRequest;
import org.elasticsearch.xpack.watcher.transport.actions.get.GetWatchResponse;
import org.elasticsearch.xpack.watcher.watch.WatchStatus;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
import static org.elasticsearch.rest.RestStatus.OK;
@ -38,9 +39,9 @@ public class RestGetWatchAction extends WatcherRestHandler {
}
@Override
protected void handleRequest(final RestRequest request, RestChannel channel, WatcherClient client) throws Exception {
protected RestChannelConsumer doPrepareRequest(final RestRequest request, WatcherClient client) throws IOException {
final GetWatchRequest getWatchRequest = new GetWatchRequest(request.param("id"));
client.getWatch(getWatchRequest, new RestBuilderListener<GetWatchResponse>(channel) {
return channel -> client.getWatch(getWatchRequest, new RestBuilderListener<GetWatchResponse>(channel) {
@Override
public RestResponse buildResponse(GetWatchResponse response, XContentBuilder builder) throws Exception {
builder.startObject()

View File

@ -10,7 +10,6 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
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;
@ -18,13 +17,13 @@ import org.elasticsearch.xpack.watcher.client.WatcherClient;
import org.elasticsearch.xpack.watcher.rest.WatcherRestHandler;
import org.elasticsearch.xpack.watcher.watch.WatchStore;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.DELETE;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestRequest.Method.PUT;
/**
*/
public class RestHijackOperationAction extends WatcherRestHandler {
private static final String ALLOW_DIRECT_ACCESS_TO_WATCH_INDEX_SETTING = "xpack.watcher.index.rest.direct_access";
@ -50,32 +49,40 @@ public class RestHijackOperationAction extends WatcherRestHandler {
}
@Override
public void handleRequest(RestRequest request, RestChannel channel, WatcherClient client) throws Exception {
public RestChannelConsumer doPrepareRequest(RestRequest request, WatcherClient client) throws IOException {
// we have to consume the id parameter lest the request will fail for the wrong reason
if (request.hasParam("id")) {
request.param("id");
}
XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
jsonBuilder.startObject().field("error", "This endpoint is not supported for " +
request.method().name() + " on " + WatchStore.INDEX + " index. Please use " +
request.method().name() + " " + URI_BASE + "/watch/<watch_id> instead");
jsonBuilder.field("status", RestStatus.BAD_REQUEST.getStatus());
jsonBuilder.endObject();
channel.sendResponse(new BytesRestResponse(RestStatus.BAD_REQUEST, jsonBuilder));
return channel -> channel.sendResponse(new BytesRestResponse(RestStatus.BAD_REQUEST, jsonBuilder));
}
public static class UnsupportedHandler extends WatcherRestHandler {
private static class UnsupportedHandler extends WatcherRestHandler {
public UnsupportedHandler(Settings settings) {
private UnsupportedHandler(Settings settings) {
super(settings);
}
@Override
public void handleRequest(RestRequest request, RestChannel channel, WatcherClient client) throws Exception {
request.path();
public RestChannelConsumer doPrepareRequest(RestRequest request, WatcherClient client) throws IOException {
// we have to consume the id parameter lest the request will fail for the wrong reason
if (request.hasParam("id")) {
request.param("id");
}
XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
jsonBuilder.startObject().field("error", "This endpoint is not supported for " +
request.method().name() + " on " + WatchStore.INDEX + " index.");
jsonBuilder.field("status", RestStatus.BAD_REQUEST.getStatus());
jsonBuilder.endObject();
channel.sendResponse(new BytesRestResponse(RestStatus.BAD_REQUEST, jsonBuilder));
}
return channel -> channel.sendResponse(new BytesRestResponse(RestStatus.BAD_REQUEST, jsonBuilder));
}
}
}

View File

@ -9,7 +9,6 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
@ -20,13 +19,13 @@ import org.elasticsearch.xpack.watcher.rest.WatcherRestHandler;
import org.elasticsearch.xpack.watcher.transport.actions.put.PutWatchRequest;
import org.elasticsearch.xpack.watcher.transport.actions.put.PutWatchResponse;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestRequest.Method.PUT;
import static org.elasticsearch.rest.RestStatus.CREATED;
import static org.elasticsearch.rest.RestStatus.OK;
/**
*/
public class RestPutWatchAction extends WatcherRestHandler {
@Inject
@ -41,11 +40,11 @@ public class RestPutWatchAction extends WatcherRestHandler {
}
@Override
protected void handleRequest(final RestRequest request, RestChannel channel, WatcherClient client) throws Exception {
protected RestChannelConsumer doPrepareRequest(final RestRequest request, WatcherClient client) throws IOException {
PutWatchRequest putWatchRequest = new PutWatchRequest(request.param("id"), request.content());
putWatchRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putWatchRequest.masterNodeTimeout()));
putWatchRequest.setActive(request.paramAsBoolean("active", putWatchRequest.isActive()));
client.putWatch(putWatchRequest, new RestBuilderListener<PutWatchResponse>(channel) {
return channel -> client.putWatch(putWatchRequest, new RestBuilderListener<PutWatchResponse>(channel) {
@Override
public RestResponse buildResponse(PutWatchResponse response, XContentBuilder builder) throws Exception {
builder.startObject()
@ -58,4 +57,5 @@ public class RestPutWatchAction extends WatcherRestHandler {
}
});
}
}

View File

@ -7,7 +7,6 @@ package org.elasticsearch.xpack.watcher.rest.action;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.action.AcknowledgedRestListener;
@ -15,11 +14,11 @@ import org.elasticsearch.xpack.watcher.client.WatcherClient;
import org.elasticsearch.xpack.watcher.rest.WatcherRestHandler;
import org.elasticsearch.xpack.watcher.transport.actions.service.WatcherServiceRequest;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestRequest.Method.PUT;
/**
*/
public class RestWatchServiceAction extends WatcherRestHandler {
@Inject
@ -37,31 +36,34 @@ public class RestWatchServiceAction extends WatcherRestHandler {
}
@Override
public void handleRequest(RestRequest request, RestChannel channel, WatcherClient client) throws Exception {
client.watcherService(new WatcherServiceRequest().restart(), new AcknowledgedRestListener<>(channel));
public RestChannelConsumer doPrepareRequest(RestRequest request, WatcherClient client) throws IOException {
return channel -> client.watcherService(new WatcherServiceRequest().restart(), new AcknowledgedRestListener<>(channel));
}
static class StartRestHandler extends WatcherRestHandler {
private static class StartRestHandler extends WatcherRestHandler {
public StartRestHandler(Settings settings) {
StartRestHandler(Settings settings) {
super(settings);
}
@Override
public void handleRequest(RestRequest request, RestChannel channel, WatcherClient client) throws Exception {
client.watcherService(new WatcherServiceRequest().start(), new AcknowledgedRestListener<>(channel));
}
public RestChannelConsumer doPrepareRequest(RestRequest request, WatcherClient client) throws IOException {
return channel -> client.watcherService(new WatcherServiceRequest().start(), new AcknowledgedRestListener<>(channel));
}
static class StopRestHandler extends WatcherRestHandler {
}
public StopRestHandler(Settings settings) {
private static class StopRestHandler extends WatcherRestHandler {
StopRestHandler(Settings settings) {
super(settings);
}
@Override
public void handleRequest(RestRequest request, RestChannel channel, WatcherClient client) throws Exception {
client.watcherService(new WatcherServiceRequest().stop(), new AcknowledgedRestListener<>(channel));
public RestChannelConsumer doPrepareRequest(RestRequest request, WatcherClient client) throws IOException {
return channel -> client.watcherService(new WatcherServiceRequest().stop(), new AcknowledgedRestListener<>(channel));
}
}
}

View File

@ -10,7 +10,6 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
@ -20,6 +19,7 @@ import org.elasticsearch.xpack.watcher.rest.WatcherRestHandler;
import org.elasticsearch.xpack.watcher.transport.actions.stats.WatcherStatsRequest;
import org.elasticsearch.xpack.watcher.transport.actions.stats.WatcherStatsResponse;
import java.io.IOException;
import java.util.Set;
import static org.elasticsearch.rest.RestRequest.Method.GET;
@ -39,7 +39,7 @@ public class RestWatcherStatsAction extends WatcherRestHandler {
}
@Override
protected void handleRequest(final RestRequest restRequest, RestChannel restChannel, WatcherClient client) throws Exception {
protected RestChannelConsumer doPrepareRequest(final RestRequest restRequest, WatcherClient client) throws IOException {
Set<String> metrics = Strings.splitStringByCommaToSet(restRequest.param("metric", ""));
WatcherStatsRequest request = new WatcherStatsRequest();
@ -51,7 +51,7 @@ public class RestWatcherStatsAction extends WatcherRestHandler {
request.includeQueuedWatches(metrics.contains("pending_watches"));
}
client.watcherStats(request, new RestBuilderListener<WatcherStatsResponse>(restChannel) {
return channel -> client.watcherStats(request, new RestBuilderListener<WatcherStatsResponse>(channel) {
@Override
public RestResponse buildResponse(WatcherStatsResponse watcherStatsResponse, XContentBuilder builder) throws Exception {
watcherStatsResponse.toXContent(builder, restRequest);

View File

@ -424,7 +424,6 @@ public class HttpExporterIT extends MonitoringIntegTestCase {
assertThat(request.getMethod(), equalTo("GET"));
assertThat(request.getPath(), startsWith(pathPrefix + "/.marvel-es-1-*"));
assertThat(request.getPath(), containsString("filter_path=*.aliases"));
assertThat(request.getPath(), containsString("master_timeout=30s"));
assertHeaders(request, customHeaders);
if (expectPost) {

View File

@ -41,8 +41,8 @@ public class RestExecuteWatchActionTests extends ESTestCase {
RestExecuteWatchAction restExecuteWatchAction = new RestExecuteWatchAction(Settings.EMPTY, restController,
triggerService);
restExecuteWatchAction.handleRequest(createFakeRestRequest(randomId, recordExecution, ignoreCondition,
debugCondition), restChannel, watcherClient);
restExecuteWatchAction.doPrepareRequest(createFakeRestRequest(randomId, recordExecution, ignoreCondition,
debugCondition), watcherClient);
assertThat(builder.request().getId(), is(randomId));
assertThat(builder.request().isRecordExecution(), is(Boolean.parseBoolean(recordExecution)));