New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HLRC: Add get watch API #35531
Merged
Merged
HLRC: Add get watch API #35531
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4a74bfa
HLRC: Add get watch API
jimczi 6c23b72
Merge branch 'master' into hlrc_get_watch
jimczi 05dcebe
Merge branch 'master' into hlrc_get_watch
jimczi af60cec
iter
jimczi 425e95f
Merge branch 'master' into hlrc_get_watch
jimczi 22c0c7a
address review
jimczi 569aefa
fix checkstyle
jimczi 0228652
add a method to get the watch as a map
jimczi 1b27b2f
fix map conversion
jimczi a046649
checkstyle
jimczi a23bbc2
remove deprecation handler that uses log4j
jimczi 78dab6e
Merge branch 'master' into hlrc_get_watch
jimczi c9c9cb5
iter
jimczi d10e976
cleanup
jimczi File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/GetWatchRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.client.watcher; | ||
|
||
import org.elasticsearch.client.Validatable; | ||
import org.elasticsearch.client.ValidationException; | ||
|
||
/** | ||
* The request to get the watch by name (id) | ||
*/ | ||
public final class GetWatchRequest implements Validatable { | ||
|
||
private final String id; | ||
|
||
public GetWatchRequest(String watchId) { | ||
validateId(watchId); | ||
this.id = watchId; | ||
} | ||
|
||
private void validateId(String id) { | ||
ValidationException exception = new ValidationException(); | ||
if (id == null) { | ||
exception.addValidationError("watch id is missing"); | ||
} else if (PutWatchRequest.isValidId(id) == false) { | ||
exception.addValidationError("watch id contains whitespace"); | ||
} | ||
if (exception.validationErrors().isEmpty() == false) { | ||
throw exception; | ||
} | ||
} | ||
|
||
/** | ||
* @return The name of the watch to retrieve | ||
*/ | ||
public String getId() { | ||
return id; | ||
} | ||
} |
148 changes: 148 additions & 0 deletions
148
client/rest-high-level/src/main/java/org/elasticsearch/client/watcher/GetWatchResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.client.watcher; | ||
|
||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.bytes.BytesReference; | ||
import org.elasticsearch.common.lucene.uid.Versions; | ||
import org.elasticsearch.common.xcontent.ConstructingObjectParser; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentHelper; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class GetWatchResponse { | ||
private final String id; | ||
private final long version; | ||
private final WatchStatus status; | ||
|
||
private final BytesReference source; | ||
private final XContentType xContentType; | ||
|
||
/** | ||
* Ctor for missing watch | ||
*/ | ||
public GetWatchResponse(String id) { | ||
this(id, Versions.NOT_FOUND, null, null, null); | ||
} | ||
|
||
public GetWatchResponse(String id, long version, WatchStatus status, BytesReference source, XContentType xContentType) { | ||
this.id = id; | ||
this.version = version; | ||
this.status = status; | ||
this.source = source; | ||
this.xContentType = xContentType; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public long getVersion() { | ||
return version; | ||
} | ||
|
||
public boolean isFound() { | ||
return version != Versions.NOT_FOUND; | ||
} | ||
|
||
public WatchStatus getStatus() { | ||
return status; | ||
} | ||
|
||
/** | ||
* Returns the {@link XContentType} of the source | ||
*/ | ||
public XContentType getContentType() { | ||
return xContentType; | ||
} | ||
|
||
/** | ||
* Returns the serialized watch | ||
*/ | ||
public BytesReference getSource() { | ||
return source; | ||
} | ||
|
||
/** | ||
* Returns the source as a map | ||
*/ | ||
public Map<String, Object> getSourceAsMap() { | ||
return source == null ? null : XContentHelper.convertToMap(source, false, getContentType()).v2(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
GetWatchResponse that = (GetWatchResponse) o; | ||
return version == that.version && | ||
Objects.equals(id, that.id) && | ||
Objects.equals(status, that.status) && | ||
Objects.equals(xContentType, that.xContentType) && | ||
Objects.equals(source, that.source); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id, status, source, version); | ||
} | ||
|
||
private static final ParseField ID_FIELD = new ParseField("_id"); | ||
private static final ParseField FOUND_FIELD = new ParseField("found"); | ||
private static final ParseField VERSION_FIELD = new ParseField("_version"); | ||
private static final ParseField STATUS_FIELD = new ParseField("status"); | ||
private static final ParseField WATCH_FIELD = new ParseField("watch"); | ||
|
||
private static ConstructingObjectParser<GetWatchResponse, Void> PARSER = | ||
new ConstructingObjectParser<>("get_watch_response", true, | ||
a -> { | ||
boolean isFound = (boolean) a[1]; | ||
if (isFound) { | ||
XContentBuilder builder = (XContentBuilder) a[4]; | ||
BytesReference source = BytesReference.bytes(builder); | ||
return new GetWatchResponse((String) a[0], (long) a[2], (WatchStatus) a[3], source, builder.contentType()); | ||
} else { | ||
return new GetWatchResponse((String) a[0]); | ||
} | ||
}); | ||
|
||
static { | ||
PARSER.declareString(ConstructingObjectParser.constructorArg(), ID_FIELD); | ||
PARSER.declareBoolean(ConstructingObjectParser.constructorArg(), FOUND_FIELD); | ||
PARSER.declareLong(ConstructingObjectParser.optionalConstructorArg(), VERSION_FIELD); | ||
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), | ||
(parser, context) -> WatchStatus.parse(parser), STATUS_FIELD); | ||
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), | ||
(parser, context) -> { | ||
try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) { | ||
builder.copyCurrentStructure(parser); | ||
return builder; | ||
} | ||
}, WATCH_FIELD); | ||
} | ||
|
||
public static GetWatchResponse fromXContent(XContentParser parser) throws IOException { | ||
return PARSER.parse(parser, null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small comment: it would be nice to add a comment to this constructor explaining that it is for a 'missing watch', as opposed to the one below.