Skip to content

Commit 22c0c7a

Browse files
committed
address review
1 parent 425e95f commit 22c0c7a

File tree

14 files changed

+367
-360
lines changed

14 files changed

+367
-360
lines changed

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

+8-12
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,19 @@ public class GetWatchResponse {
3535
private final WatchStatus status;
3636

3737
private final BytesReference source;
38-
private final XContentType xContentType;
3938

4039
/**
4140
* Ctor for missing watch
4241
*/
4342
public GetWatchResponse(String id) {
44-
this(id, Versions.NOT_FOUND, null, null, null);
43+
this(id, Versions.NOT_FOUND, null, null);
4544
}
4645

47-
public GetWatchResponse(String id, long version, WatchStatus status, BytesReference source, XContentType xContentType) {
46+
public GetWatchResponse(String id, long version, WatchStatus status, BytesReference source) {
4847
this.id = id;
4948
this.version = version;
5049
this.status = status;
5150
this.source = source;
52-
this.xContentType = xContentType;
5351
}
5452

5553
public String getId() {
@@ -73,7 +71,7 @@ public BytesReference getSource() {
7371
}
7472

7573
public XContentType getContentType() {
76-
return xContentType;
74+
return XContentType.JSON;
7775
}
7876

7977
@Override
@@ -84,13 +82,12 @@ public boolean equals(Object o) {
8482
return version == that.version &&
8583
Objects.equals(id, that.id) &&
8684
Objects.equals(status, that.status) &&
87-
Objects.equals(source, that.source) &&
88-
xContentType == that.xContentType;
85+
Objects.equals(source, that.source);
8986
}
9087

9188
@Override
9289
public int hashCode() {
93-
return Objects.hash(id, status, source, xContentType, version);
90+
return Objects.hash(id, status, source, version);
9491
}
9592

9693
private static final ParseField ID_FIELD = new ParseField("_id");
@@ -104,9 +101,8 @@ public int hashCode() {
104101
a -> {
105102
boolean isFound = (boolean) a[1];
106103
if (isFound) {
107-
XContentBuilder builder = (XContentBuilder) a[4];
108-
BytesReference source = BytesReference.bytes(builder);
109-
return new GetWatchResponse((String) a[0], (long) a[2], (WatchStatus) a[3], source, builder.contentType());
104+
BytesReference source = (BytesReference) a[4];
105+
return new GetWatchResponse((String) a[0], (long) a[2], (WatchStatus) a[3], source);
110106
} else {
111107
return new GetWatchResponse((String) a[0]);
112108
}
@@ -122,7 +118,7 @@ public int hashCode() {
122118
(parser, context) -> {
123119
try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) {
124120
builder.copyCurrentStructure(parser);
125-
return builder;
121+
return BytesReference.bytes(builder);
126122
}
127123
}, WATCH_FIELD);
128124
}

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

+13-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.client.watcher;
2121

2222
import org.elasticsearch.ElasticsearchParseException;
23+
import org.elasticsearch.common.Nullable;
2324
import org.elasticsearch.common.ParseField;
2425
import org.elasticsearch.common.xcontent.XContentParser;
2526
import org.joda.time.DateTime;
@@ -44,19 +45,22 @@ public class WatchStatus {
4445
private final DateTime lastMetCondition;
4546
private final long version;
4647
private final Map<String, ActionStatus> actions;
48+
@Nullable private Map<String, String> headers;
4749

4850
public WatchStatus(long version,
4951
State state,
5052
ExecutionState executionState,
5153
DateTime lastChecked,
5254
DateTime lastMetCondition,
53-
Map<String, ActionStatus> actions) {
55+
Map<String, ActionStatus> actions,
56+
Map<String, String> headers) {
5457
this.version = version;
5558
this.lastChecked = lastChecked;
5659
this.lastMetCondition = lastMetCondition;
5760
this.actions = actions;
5861
this.state = state;
5962
this.executionState = executionState;
63+
this.headers = headers;
6064
}
6165

6266
public State state() {
@@ -79,7 +83,7 @@ public ActionStatus actionStatus(String actionId) {
7983
return actions.get(actionId);
8084
}
8185

82-
Map<String, ActionStatus> getActions() {
86+
public Map<String, ActionStatus> getActions() {
8387
return actions;
8488
}
8589

@@ -116,6 +120,7 @@ public static WatchStatus parse(XContentParser parser) throws IOException {
116120
DateTime lastChecked = null;
117121
DateTime lastMetCondition = null;
118122
Map<String, ActionStatus> actions = null;
123+
Map<String, String> headers = null;
119124
long version = -1;
120125

121126
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);
@@ -176,13 +181,17 @@ public static WatchStatus parse(XContentParser parser) throws IOException {
176181
throw new ElasticsearchParseException("could not parse watch status. expecting field [{}] to be an object, " +
177182
"found [{}] instead", currentFieldName, token);
178183
}
184+
} else if (Field.HEADERS.match(currentFieldName, parser.getDeprecationHandler())) {
185+
if (token == XContentParser.Token.START_OBJECT) {
186+
headers = parser.mapStrings();
187+
}
179188
} else {
180189
parser.skipChildren();
181190
}
182191
}
183192

184193
actions = actions == null ? emptyMap() : unmodifiableMap(actions);
185-
return new WatchStatus(version, state, executionState, lastChecked, lastMetCondition, actions);
194+
return new WatchStatus(version, state, executionState, lastChecked, lastMetCondition, actions, headers);
186195
}
187196

188197
public static class State {
@@ -233,5 +242,6 @@ public interface Field {
233242
ParseField ACTIONS = new ParseField("actions");
234243
ParseField VERSION = new ParseField("version");
235244
ParseField EXECUTION_STATE = new ParseField("execution_state");
245+
ParseField HEADERS = new ParseField("headers");
236246
}
237247
}

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/WatcherDocumentationIT.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -200,23 +200,26 @@ public void onFailure(Exception e) {
200200
}
201201

202202
{
203-
//tag::x-pack-get-watch-execute
203+
//tag::get-watch-request
204204
GetWatchRequest request = new GetWatchRequest("my_watch_id");
205+
//end::get-watch-request
206+
207+
//tag::ack-watch-execute
205208
GetWatchResponse response = client.watcher().getWatch(request, RequestOptions.DEFAULT);
206-
//end::x-pack-get-watch-execute
209+
//end::get-watch-request
207210

208-
//tag::x-pack-get-watch-response
211+
//tag::get-watch-response
209212
String watchId = response.getId(); // <1>
210213
boolean found = response.isFound(); // <2>
211214
long version = response.getVersion(); // <3>
212215
WatchStatus status = response.getStatus(); // <4>
213216
BytesReference source = response.getSource(); // <5>
214-
//end::x-pack-get-watch-response
217+
//end::get-watch-response
215218
}
216219

217220
{
218221
GetWatchRequest request = new GetWatchRequest("my_other_watch_id");
219-
// tag::x-pack-get-watch-execute-listener
222+
// tag::get-watch-execute-listener
220223
ActionListener<GetWatchResponse> listener = new ActionListener<GetWatchResponse>() {
221224
@Override
222225
public void onResponse(GetWatchResponse response) {
@@ -228,15 +231,15 @@ public void onFailure(Exception e) {
228231
// <2>
229232
}
230233
};
231-
// end::x-pack-get-watch-execute-listener
234+
// end::get-watch-execute-listener
232235

233236
// Replace the empty listener by a blocking listener in test
234237
final CountDownLatch latch = new CountDownLatch(1);
235238
listener = new LatchedActionListener<>(listener, latch);
236239

237-
// tag::x-pack-get-watch-execute-async
240+
// tag::get-watch-execute-async
238241
client.watcher().getWatchAsync(request, RequestOptions.DEFAULT, listener); // <1>
239-
// end::x-pack-get-watch-execute-async
242+
// end::get-watch-execute-async
240243

241244
assertTrue(latch.await(30L, TimeUnit.SECONDS));
242245
}

0 commit comments

Comments
 (0)