|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. 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.apache.skywalking.library.elasticsearch.requests.factory.v7plus; |
| 21 | + |
| 22 | +import static com.google.common.base.Preconditions.checkArgument; |
| 23 | +import static com.google.common.base.Strings.isNullOrEmpty; |
| 24 | +import static com.google.common.collect.Iterables.isEmpty; |
| 25 | +import static java.util.Objects.requireNonNull; |
| 26 | +import java.util.Map; |
| 27 | +import org.apache.skywalking.library.elasticsearch.ElasticSearchVersion; |
| 28 | +import org.apache.skywalking.library.elasticsearch.requests.UpdateRequest; |
| 29 | +import org.apache.skywalking.library.elasticsearch.requests.factory.DocumentFactory; |
| 30 | +import com.google.common.collect.ImmutableMap; |
| 31 | +import com.linecorp.armeria.common.HttpRequest; |
| 32 | +import com.linecorp.armeria.common.HttpRequestBuilder; |
| 33 | +import com.linecorp.armeria.common.MediaType; |
| 34 | +import lombok.SneakyThrows; |
| 35 | +import lombok.experimental.Delegate; |
| 36 | + |
| 37 | +public class V81DocumentFactory implements DocumentFactory { |
| 38 | + private final ElasticSearchVersion version; |
| 39 | + |
| 40 | + @Delegate // Delegate all compatible methods to V7DocumentFactory and just override the incompatible ones. |
| 41 | + private final V7DocumentFactory v7DocumentFactory; |
| 42 | + |
| 43 | + public V81DocumentFactory(ElasticSearchVersion version) { |
| 44 | + this.version = version; |
| 45 | + this.v7DocumentFactory = new V7DocumentFactory(version); |
| 46 | + } |
| 47 | + |
| 48 | + @SneakyThrows |
| 49 | + @Override |
| 50 | + public HttpRequest update(UpdateRequest request, Map<String, ?> params) { |
| 51 | + requireNonNull(request, "request"); |
| 52 | + |
| 53 | + final String index = request.getIndex(); |
| 54 | + final String type = request.getType(); |
| 55 | + final String id = request.getId(); |
| 56 | + final Map<String, Object> doc = request.getDoc(); |
| 57 | + |
| 58 | + checkArgument(!isNullOrEmpty(index), "index cannot be null or empty"); |
| 59 | + checkArgument(!isNullOrEmpty(type), "type cannot be null or empty"); |
| 60 | + checkArgument(!isNullOrEmpty(id), "id cannot be null or empty"); |
| 61 | + checkArgument(doc != null && !isEmpty(doc.entrySet()), "doc cannot be null or empty"); |
| 62 | + |
| 63 | + final HttpRequestBuilder builder = HttpRequest.builder(); |
| 64 | + if (params != null) { |
| 65 | + params.forEach(builder::queryParam); |
| 66 | + } |
| 67 | + final byte[] content = version.codec().encode(ImmutableMap.of("doc", doc)); |
| 68 | + |
| 69 | + builder.post("/{index}/_update/{id}") |
| 70 | + .pathParam("index", index) |
| 71 | + .pathParam("id", id) |
| 72 | + .content(MediaType.JSON, content); |
| 73 | + |
| 74 | + return builder.build(); |
| 75 | + } |
| 76 | +} |
0 commit comments