Java Code Examples for org.elasticsearch.rest.RestRequest#method()

The following examples show how to use org.elasticsearch.rest.RestRequest#method() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JobRestHandler.java    From elasticsearch-rest-command with The Unlicense 6 votes vote down vote up
private String getCommandStringFromRestRequest(final RestRequest request) throws CommandException{
	String command = "";
	if(request.method() == RestRequest.Method.GET)
		command = request.param("q", "");
	else{
		HashMap<String, String> post = new HashMap<String, String>();
		RestUtils.decodeQueryString(request.content().toUtf8(), 0, post);
		if(post.containsKey("q")){
			command = post.get("q");
		}
	}
	
	if (command.length() == 0) {
		throw new CommandException("命令为空");
		
	}else{
		if( ! command.startsWith(Search.PREFIX_SEARCH_STRING))
			command = Search.PREFIX_SEARCH_STRING+" "+command;				
	}
	
	logger.info(command);
	
	return command;
}
 
Example 2
Source File: AbstractConfigurationValidator.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
public AbstractConfigurationValidator(final RestRequest request, final BytesReference ref, final Settings esSettings, Object... param) {
    this.content = ref;
    this.method = request.method();
    this.esSettings = esSettings;
    this.request = request;
    this.param = param;
}
 
Example 3
Source File: PermissionsInfoAction.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
	switch (request.method()) {
	case GET:
		return handleGet(request, client);
	default:
		throw new IllegalArgumentException(request.method() + " not supported");
	}
}
 
Example 4
Source File: PatchableResourceApiAction.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Override
protected void handleApiRequest(RestChannel channel, final RestRequest request, final Client client)
        throws IOException {

    if (request.method() == Method.PATCH) {
        handlePatch(channel, request, client);
    } else {
        super.handleApiRequest(channel, request, client);
    }
}
 
Example 5
Source File: AbstractApiAction.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
protected void handleApiRequest(final RestChannel channel, final RestRequest request, final Client client) throws IOException {

		try {
			// validate additional settings, if any
			AbstractConfigurationValidator validator = getValidator(request, request.content());
			if (!validator.validate()) {
				request.params().clear();
				badRequestResponse(channel, validator);
				return;
			}
			switch (request.method()) {
				case DELETE:
					handleDelete(channel,request, client, validator.getContentAsNode()); break;
				case POST:
					handlePost(channel,request, client, validator.getContentAsNode());break;
				case PUT:
					handlePut(channel,request, client, validator.getContentAsNode());break;
				case GET:
					handleGet(channel,request, client, validator.getContentAsNode());break;
				default:
					throw new IllegalArgumentException(request.method() + " not supported");
			}
		} catch (JsonMappingException jme) {
			throw jme;
			//TODO strip source
			//if(jme.getLocation() == null || jme.getLocation().getSourceRef() == null) {
			//    throw jme;
			//} else throw new JsonMappingException(null, jme.getMessage());
		}
	}
 
Example 6
Source File: OpenDistroSecurityConfigAction.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
@Override
protected void handleApiRequest(RestChannel channel, RestRequest request, Client client) throws IOException {
    if (request.method() == Method.PATCH && !allowPutOrPatch) {
        notImplemented(channel, Method.PATCH);
    } else {
        super.handleApiRequest(channel, request, client);
    }
}
 
Example 7
Source File: SetupAction.java    From zentity with Apache License 2.0 5 votes vote down vote up
@Override
protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) {

    // Parse request
    Boolean pretty = restRequest.paramAsBoolean("pretty", false);
    int numberOfShards = restRequest.paramAsInt("number_of_shards", 1);
    int numberOfReplicas = restRequest.paramAsInt("number_of_replicas", 1);
    Method method = restRequest.method();

    return channel -> {
        try {
            if (method == POST) {

                createIndex(client, numberOfShards, numberOfReplicas);
                XContentBuilder content = XContentFactory.jsonBuilder();
                if (pretty)
                    content.prettyPrint();
                content.startObject().field("acknowledged", true).endObject();
                channel.sendResponse(new BytesRestResponse(RestStatus.OK, content));

            } else {
                throw new NotImplementedException("Method and endpoint not implemented.");
            }
        } catch (NotImplementedException e) {
            channel.sendResponse(new BytesRestResponse(channel, RestStatus.NOT_IMPLEMENTED, e));
        }
    };
}
 
Example 8
Source File: RestFeatureStoreCaches.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
    if (request.method() == RestRequest.Method.POST) {
        return clearCache(request, client);
    } else {
        return getStats(client);
    }
}
 
Example 9
Source File: RestSimpleFeatureStore.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    String indexName = indexName(request);
    if (request.method() == RestRequest.Method.DELETE) {
        return delete(client, type, indexName, request);
    } else if (request.method() == RestRequest.Method.HEAD || request.method() == RestRequest.Method.GET) {
        return get(client, type, indexName, request);
    } else {
        return addOrUpdate(client, type, indexName, request);
    }
}
 
Example 10
Source File: RequestUtils.java    From openshift-elasticsearch-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Modify the request of needed
 * @param request the original request
 * @param context the Openshift context
 * @param channel the channel that is processing the request
 * 
 * @return The modified request
 */
public RestRequest modifyRequest(final RestRequest request, OpenshiftRequestContext context, RestChannel channel) {
    
    final String uri = getUri(request, context);
    final BytesReference content = getContent(request, context);
    if(!getUser(request).equals(context.getUser()) || !uri.equals(request.uri()) || content != request.content()) {
        LOGGER.debug("Modifying header '{}' to be '{}'", proxyUserHeader, context.getUser());
        final Map<String, List<String>> modifiedHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
        modifiedHeaders.putAll(request.getHeaders());
        modifiedHeaders.put(proxyUserHeader, Arrays.asList(context.getUser()));
        if(request.header("Content-Type") != null && request.header("Content-Type").toLowerCase().endsWith("json")){
            modifiedHeaders.put("Content-Type", Arrays.asList("application/json"));
        }
        RestRequest modified = new RestRequest(request.getXContentRegistry(), uri, modifiedHeaders) {

            @Override
            public Method method() {
                return request.method();
            }

            @Override
            public String uri() {
                return uri;
            }

            @Override
            public boolean hasContent() {
                return content.length() > 0;
            }

            @Override
            public BytesReference content() {
                return content;
            }
            
            @Override
            public SocketAddress getRemoteAddress() {
                return request.getRemoteAddress();
            }

            /**
             * Returns the local address where this request channel is bound to.  The returned
             * {@link SocketAddress} is supposed to be down-cast into more concrete
             * type such as {@link java.net.InetSocketAddress} to retrieve the detailed
             * information.
             */
            @Override
            public SocketAddress getLocalAddress() {
                return request.getRemoteAddress();
            }

            @SuppressWarnings("unused")
            public Channel getChannel() {
                return (Channel) channel;
            }
            
        };
        modified.params().putAll(request.params());
        //HACK - only need to do if we modify the kibana index
        if (uri.contains(defaultKibanaIndex)) {
            modified.params().put("index", context.getKibanaIndex());
        }

        return modified;
    }
    return request;
}
 
Example 11
Source File: LogoutFilter.java    From elasticsearch-auth with Apache License 2.0 4 votes vote down vote up
@Override
public void process(final RestRequest request, final RestChannel channel,
        final RestFilterChain filterChain) {
    final String rawPath = request.rawPath();
    if (rawPath.equals(logoutPath)) {
        for (final Method method : methods) {
            if (method == request.method()) {
                final String token = authService.getToken(request);
                if (token != null) {
                    authService.deleteToken(token,
                            new ActionListener<Void>() {
                                @Override
                                public void onResponse(final Void response) {
                                    ResponseUtil.send(request, channel,
                                            RestStatus.OK);
                                }

                                @Override
                                public void onFailure(final Throwable e) {
                                    logger.error(
                                            "Failed to delete the token.",
                                            e);
                                    if (e instanceof AuthException) {
                                        ResponseUtil.send(request, channel,
                                                (AuthException) e);
                                    } else {
                                        ResponseUtil
                                                .send(request,
                                                        channel,
                                                        RestStatus.INTERNAL_SERVER_ERROR,
                                                        "message",
                                                        "Failed to delete the token.");
                                    }
                                }
                            });
                } else {
                    ResponseUtil.send(request, channel,
                            RestStatus.BAD_REQUEST, "message",
                            "Invalid token.");
                }
                return;
            }
        }
        ResponseUtil.send(request, channel, RestStatus.BAD_REQUEST,
                "message",
                "Unsupported HTTP method for the logout process.");
        return;
    }
    filterChain.continueProcessing(request, channel);
}