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

The following examples show how to use org.elasticsearch.rest.RestRequest#content() . 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: RequestUtils.java    From openshift-elasticsearch-plugin with Apache License 2.0 5 votes vote down vote up
private BytesReference getContent(final RestRequest request, final OpenshiftRequestContext context) {
    String content = request.content().utf8ToString();
    if(OpenshiftRequestContext.EMPTY != context && content.contains("_index\":\"" + defaultKibanaIndex)) {
        LOGGER.debug("Replacing the content that references the default kibana index");
        String replaced = content.replaceAll("_index\":\"" + defaultKibanaIndex + "\"", "_index\":\"" + context.getKibanaIndex() + "\"");
        return new BytesArray(replaced);
    }
    return request.content();
}
 
Example 2
Source File: RestLangdetectAction.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
private void withContent(RestRequest restRequest, CheckedConsumer<XContentParser, IOException> withParser)
        throws IOException {
    BytesReference content = restRequest.content();
    XContentType xContentType = XContentType.JSON;
    if (content.length() > 0) {
        try (XContentParser parser = xContentType.xContent().createParser(restRequest.getXContentRegistry(),
                DeprecationHandler.THROW_UNSUPPORTED_OPERATION, content.streamInput())) {
            withParser.accept(parser);
        }
    } else {
        withParser.accept(null);
    }
}
 
Example 3
Source File: IndexAuthenticator.java    From elasticsearch-auth with Apache License 2.0 5 votes vote down vote up
@Override
public void login(final RestRequest request,
        final ActionListener<String[]> listener) {
    String username = request.param(usernameKey);
    String password = request.param(passwordKey);
    final BytesReference content = request.content();
    final XContentType xContentType = XContentFactory.xContentType(content);
    XContentParser parser = null;
    try {
        parser = XContentFactory.xContent(xContentType).createParser(
                content);
        final XContentParser.Token t = parser.nextToken();
        if (t != null) {
            final Map<String, Object> contentMap = parser.map();
            username = MapUtil.getAsString(contentMap, usernameKey,
                    username);
            password = MapUtil.getAsString(contentMap, passwordKey,
                    password);
        }
    } catch (final Exception e) {
        listener.onFailure(e);
        return;
    } finally {
        if (parser != null) {
            parser.close();
        }
    }

    if (username == null) {
        listener.onResponse(new String[0]);
        return;
    }

    processLogin(username, password, listener);

}
 
Example 4
Source File: AccountRestAction.java    From elasticsearch-auth with Apache License 2.0 5 votes vote down vote up
@Override
protected void handleRequest(final RestRequest request,
        final RestChannel channel, final Client client) {
    final BytesReference content = request.content();
    final XContentType xContentType = XContentFactory.xContentType(content);
    XContentParser parser = null;
    String authenticator = null;
    String username = null;
    String password = null;
    String[] roles = null;
    try {
        parser = XContentFactory.xContent(xContentType).createParser(
                content);
        final XContentParser.Token t = parser.nextToken();
        if (t != null) {
            final Map<String, Object> contentMap = parser.map();
            authenticator = MapUtil.getAsString(contentMap,
                    "authenticator", null);
            username = MapUtil.getAsString(contentMap, "username", null);
            password = MapUtil.getAsString(contentMap, "password", null);
            roles = MapUtil.getAsArray(contentMap, "roles", new String[0]);
        }
    } catch (final Exception e) {
        logger.error("Could not parse the content.", e);
        ResponseUtil.send(request, channel, RestStatus.BAD_REQUEST,
                "message", "Could not parse the content.");
        return;
    } finally {
        if (parser != null) {
            parser.close();
        }
    }

    processRequest(request, channel, authenticator, username, password,
            roles);

}
 
Example 5
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;
}