|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.client; |
| 21 | + |
| 22 | +import org.elasticsearch.action.ActionListener; |
| 23 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 24 | +import org.elasticsearch.protocol.xpack.security.PutUserRequest; |
| 25 | +import org.elasticsearch.protocol.xpack.security.PutUserResponse; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | + |
| 29 | +import static java.util.Collections.emptySet; |
| 30 | + |
| 31 | +/** |
| 32 | + * A wrapper for the {@link RestHighLevelClient} that provides methods for accessing the Security APIs. |
| 33 | + * <p> |
| 34 | + * See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api.html">Security APIs on elastic.co</a> |
| 35 | + */ |
| 36 | +public final class SecurityClient { |
| 37 | + |
| 38 | + private final RestHighLevelClient restHighLevelClient; |
| 39 | + |
| 40 | + SecurityClient(RestHighLevelClient restHighLevelClient) { |
| 41 | + this.restHighLevelClient = restHighLevelClient; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Create/update a user in the native realm synchronously. |
| 46 | + * See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-users.html"> |
| 47 | + * the docs</a> for more. |
| 48 | + * @param request the request with the user's information |
| 49 | + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized |
| 50 | + * @return the response from the put user call |
| 51 | + * @throws IOException in case there is a problem sending the request or parsing back the response |
| 52 | + */ |
| 53 | + public PutUserResponse putUser(PutUserRequest request, RequestOptions options) throws IOException { |
| 54 | + return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::putUser, options, |
| 55 | + SecurityClient::parsePutUserResponse, emptySet()); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Asynchronously create/update a user in the native realm. |
| 60 | + * See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-users.html"> |
| 61 | + * the docs</a> for more. |
| 62 | + * @param request the request with the user's information |
| 63 | + * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized |
| 64 | + * @param listener the listener to be notified upon request completion |
| 65 | + */ |
| 66 | + public void putUserAsync(PutUserRequest request, RequestOptions options, ActionListener<PutUserResponse> listener) { |
| 67 | + restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::putUser, options, |
| 68 | + SecurityClient::parsePutUserResponse, listener, emptySet()); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Parses the rest response from the put user API. The rest API wraps the XContent of the |
| 73 | + * {@link PutUserResponse} in a field, so this method unwraps this value. |
| 74 | + * |
| 75 | + * @param parser the XContent parser for the response body |
| 76 | + * @return the {@link PutUserResponse} parsed |
| 77 | + * @throws IOException in case there is a problem parsing the response |
| 78 | + */ |
| 79 | + private static PutUserResponse parsePutUserResponse(XContentParser parser) throws IOException { |
| 80 | + XContentParser.Token token; |
| 81 | + String currentFieldName = null; |
| 82 | + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { |
| 83 | + if (token == XContentParser.Token.FIELD_NAME) { |
| 84 | + currentFieldName = parser.currentName(); |
| 85 | + } else if ("user".equals(currentFieldName)) { |
| 86 | + return PutUserResponse.fromXContent(parser); |
| 87 | + } |
| 88 | + } |
| 89 | + throw new IOException("Failed to parse [put_user_response]"); |
| 90 | + } |
| 91 | +} |
0 commit comments