Skip to content

Commit 4359230

Browse files
authored
HLRC: split watcher request converters (#33442)
In an effort to encapsulate the different clients, the request converters are being shuffled around. This splits the WatcherClient request converters.
1 parent 9d16a7b commit 4359230

File tree

3 files changed

+144
-4
lines changed

3 files changed

+144
-4
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/WatcherClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public final class WatcherClient {
4747
* @throws IOException in case there is a problem sending the request or parsing back the response
4848
*/
4949
public PutWatchResponse putWatch(PutWatchRequest request, RequestOptions options) throws IOException {
50-
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::xPackWatcherPutWatch, options,
50+
return restHighLevelClient.performRequestAndParseEntity(request, WatcherRequestConverters::putWatch, options,
5151
PutWatchResponse::fromXContent, emptySet());
5252
}
5353

@@ -61,7 +61,7 @@ public PutWatchResponse putWatch(PutWatchRequest request, RequestOptions options
6161
*/
6262
public void putWatchAsync(PutWatchRequest request, RequestOptions options,
6363
ActionListener<PutWatchResponse> listener) {
64-
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::xPackWatcherPutWatch, options,
64+
restHighLevelClient.performRequestAsyncAndParseEntity(request, WatcherRequestConverters::putWatch, options,
6565
PutWatchResponse::fromXContent, listener, emptySet());
6666
}
6767

@@ -75,7 +75,7 @@ public void putWatchAsync(PutWatchRequest request, RequestOptions options,
7575
* @throws IOException in case there is a problem sending the request or parsing back the response
7676
*/
7777
public DeleteWatchResponse deleteWatch(DeleteWatchRequest request, RequestOptions options) throws IOException {
78-
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::xPackWatcherDeleteWatch, options,
78+
return restHighLevelClient.performRequestAndParseEntity(request, WatcherRequestConverters::deleteWatch, options,
7979
DeleteWatchResponse::fromXContent, singleton(404));
8080
}
8181

@@ -88,7 +88,7 @@ public DeleteWatchResponse deleteWatch(DeleteWatchRequest request, RequestOption
8888
* @param listener the listener to be notified upon request completion
8989
*/
9090
public void deleteWatchAsync(DeleteWatchRequest request, RequestOptions options, ActionListener<DeleteWatchResponse> listener) {
91-
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::xPackWatcherDeleteWatch, options,
91+
restHighLevelClient.performRequestAsyncAndParseEntity(request, WatcherRequestConverters::deleteWatch, options,
9292
DeleteWatchResponse::fromXContent, listener, singleton(404));
9393
}
9494
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.apache.http.client.methods.HttpDelete;
23+
import org.apache.http.client.methods.HttpPut;
24+
import org.apache.http.entity.ByteArrayEntity;
25+
import org.apache.http.entity.ContentType;
26+
import org.elasticsearch.common.bytes.BytesReference;
27+
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest;
28+
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest;
29+
30+
public class WatcherRequestConverters {
31+
32+
static Request putWatch(PutWatchRequest putWatchRequest) {
33+
String endpoint = new RequestConverters.EndpointBuilder()
34+
.addPathPartAsIs("_xpack")
35+
.addPathPartAsIs("watcher")
36+
.addPathPartAsIs("watch")
37+
.addPathPart(putWatchRequest.getId())
38+
.build();
39+
40+
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
41+
RequestConverters.Params params = new RequestConverters.Params(request).withVersion(putWatchRequest.getVersion());
42+
if (putWatchRequest.isActive() == false) {
43+
params.putParam("active", "false");
44+
}
45+
ContentType contentType = RequestConverters.createContentType(putWatchRequest.xContentType());
46+
BytesReference source = putWatchRequest.getSource();
47+
request.setEntity(new ByteArrayEntity(source.toBytesRef().bytes, 0, source.length(), contentType));
48+
return request;
49+
}
50+
51+
static Request deleteWatch(DeleteWatchRequest deleteWatchRequest) {
52+
String endpoint = new RequestConverters.EndpointBuilder()
53+
.addPathPartAsIs("_xpack")
54+
.addPathPartAsIs("watcher")
55+
.addPathPartAsIs("watch")
56+
.addPathPart(deleteWatchRequest.getId())
57+
.build();
58+
59+
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);
60+
return request;
61+
}
62+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.apache.http.client.methods.HttpDelete;
23+
import org.apache.http.client.methods.HttpPut;
24+
import org.elasticsearch.common.bytes.BytesArray;
25+
import org.elasticsearch.common.xcontent.XContentType;
26+
import org.elasticsearch.protocol.xpack.watcher.DeleteWatchRequest;
27+
import org.elasticsearch.protocol.xpack.watcher.PutWatchRequest;
28+
import org.elasticsearch.test.ESTestCase;
29+
30+
import java.io.ByteArrayOutputStream;
31+
import java.util.HashMap;
32+
import java.util.Map;
33+
34+
import static org.hamcrest.Matchers.is;
35+
import static org.hamcrest.Matchers.nullValue;
36+
37+
public class WatcherRequestConvertersTests extends ESTestCase {
38+
39+
public void testXPackPutWatch() throws Exception {
40+
PutWatchRequest putWatchRequest = new PutWatchRequest();
41+
String watchId = randomAlphaOfLength(10);
42+
putWatchRequest.setId(watchId);
43+
String body = randomAlphaOfLength(20);
44+
putWatchRequest.setSource(new BytesArray(body), XContentType.JSON);
45+
46+
Map<String, String> expectedParams = new HashMap<>();
47+
if (randomBoolean()) {
48+
putWatchRequest.setActive(false);
49+
expectedParams.put("active", "false");
50+
}
51+
52+
if (randomBoolean()) {
53+
long version = randomLongBetween(10, 100);
54+
putWatchRequest.setVersion(version);
55+
expectedParams.put("version", String.valueOf(version));
56+
}
57+
58+
Request request = WatcherRequestConverters.putWatch(putWatchRequest);
59+
assertEquals(HttpPut.METHOD_NAME, request.getMethod());
60+
assertEquals("/_xpack/watcher/watch/" + watchId, request.getEndpoint());
61+
assertEquals(expectedParams, request.getParameters());
62+
assertThat(request.getEntity().getContentType().getValue(), is(XContentType.JSON.mediaTypeWithoutParameters()));
63+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
64+
request.getEntity().writeTo(bos);
65+
assertThat(bos.toString("UTF-8"), is(body));
66+
}
67+
68+
public void testXPackDeleteWatch() {
69+
DeleteWatchRequest deleteWatchRequest = new DeleteWatchRequest();
70+
String watchId = randomAlphaOfLength(10);
71+
deleteWatchRequest.setId(watchId);
72+
73+
Request request = WatcherRequestConverters.deleteWatch(deleteWatchRequest);
74+
assertEquals(HttpDelete.METHOD_NAME, request.getMethod());
75+
assertEquals("/_xpack/watcher/watch/" + watchId, request.getEndpoint());
76+
assertThat(request.getEntity(), nullValue());
77+
}
78+
}

0 commit comments

Comments
 (0)