-
Notifications
You must be signed in to change notification settings - Fork 58
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
Handle named wildcards (REST path parameters) #123
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ab2e0dc
Register REST paths including named wildcards
dbwiddis fea6ebc
Move HelloWorld extension to subpackage
dbwiddis a2c127f
Update Hello World example with named wildcard example
dbwiddis 0b6a19b
Pass consumed params in RestResponse header
dbwiddis 67e5b3a
Linelint hates me and web tool exports
dbwiddis 73de199
Code Review tweaks
dbwiddis 0f0485e
Update DESIGN.md
dbwiddis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
70 changes: 70 additions & 0 deletions
70
src/main/java/org/opensearch/sdk/ExtensionRestPathRegistry.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,70 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
package org.opensearch.sdk; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.opensearch.common.path.PathTrie; | ||
import org.opensearch.rest.RestUtils; | ||
import org.opensearch.rest.RestRequest.Method; | ||
|
||
/** | ||
* This class registers REST paths from extension Rest Handlers. | ||
*/ | ||
public class ExtensionRestPathRegistry { | ||
|
||
// PathTrie to match paths to handlers | ||
private PathTrie<ExtensionRestHandler> pathTrie = new PathTrie<>(RestUtils.REST_DECODER); | ||
// List to return registered handlers | ||
private List<String> registeredPaths = new ArrayList<>(); | ||
|
||
/** | ||
* Register a REST handler to handle a method and route in this extension's path registry. | ||
* | ||
* @param method The method to register. | ||
* @param uri The URI to register. May include named wildcards. | ||
* @param extensionRestHandler The RestHandler to handle this route | ||
*/ | ||
public void registerHandler(Method method, String uri, ExtensionRestHandler extensionRestHandler) { | ||
String restPath = restPathToString(method, uri); | ||
pathTrie.insert(restPath, extensionRestHandler); | ||
registeredPaths.add(restPath); | ||
} | ||
|
||
/** | ||
* Get the registered REST handler for the specified method and URI. | ||
* | ||
* @param method the registered method. | ||
* @param uri the registered URI. | ||
* @return The REST handler registered to handle this method and URI combination if found, null otherwise. | ||
*/ | ||
public ExtensionRestHandler getHandler(Method method, String uri) { | ||
return pathTrie.retrieve(restPathToString(method, uri)); | ||
} | ||
|
||
/** | ||
* List the registered routes. | ||
* | ||
* @return A list of strings identifying the registered routes. | ||
*/ | ||
public List<String> getRegisteredPaths() { | ||
return registeredPaths; | ||
} | ||
|
||
/** | ||
* Converts a REST method and URI to a string. | ||
* | ||
* @param method the method. | ||
* @param uri the URI. | ||
* @return A string appending the method and URI. | ||
*/ | ||
public static String restPathToString(Method method, String uri) { | ||
return method.name() + " " + uri; | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
src/main/java/org/opensearch/sdk/ExtensionRestResponse.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,93 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
package org.opensearch.sdk; | ||
|
||
import java.util.List; | ||
|
||
import org.opensearch.common.bytes.BytesReference; | ||
import org.opensearch.common.xcontent.XContentBuilder; | ||
import org.opensearch.rest.BytesRestResponse; | ||
import org.opensearch.rest.RestStatus; | ||
|
||
/** | ||
* A subclass of {@link BytesRestResponse} which processes the consumed parameters into a custom header. | ||
*/ | ||
public class ExtensionRestResponse extends BytesRestResponse { | ||
|
||
/** | ||
* Key passed in {@link BytesRestResponse} headers to identify parameters consumed by the handler. For internal use. | ||
*/ | ||
static final String CONSUMED_PARAMS_KEY = "extension.consumed.parameters"; | ||
|
||
/** | ||
* Creates a new response based on {@link XContentBuilder}. | ||
* | ||
* @param status The REST status. | ||
* @param builder The builder for the response. | ||
* @param consumedParams Parameters consumed by the handler. | ||
*/ | ||
public ExtensionRestResponse(RestStatus status, XContentBuilder builder, List<String> consumedParams) { | ||
super(status, builder); | ||
addConsumedParamHeader(consumedParams); | ||
} | ||
|
||
/** | ||
* Creates a new plain text response. | ||
* | ||
* @param status The REST status. | ||
* @param content A plain text response string. | ||
* @param consumedParams Parameters consumed by the handler. | ||
*/ | ||
public ExtensionRestResponse(RestStatus status, String content, List<String> consumedParams) { | ||
super(status, content); | ||
addConsumedParamHeader(consumedParams); | ||
} | ||
|
||
/** | ||
* Creates a new plain text response. | ||
* | ||
* @param status The REST status. | ||
* @param contentType The content type of the response string. | ||
* @param content A response string. | ||
* @param consumedParams Parameters consumed by the handler. | ||
*/ | ||
public ExtensionRestResponse(RestStatus status, String contentType, String content, List<String> consumedParams) { | ||
super(status, contentType, content); | ||
addConsumedParamHeader(consumedParams); | ||
} | ||
|
||
/** | ||
* Creates a binary response. | ||
* | ||
* @param status The REST status. | ||
* @param contentType The content type of the response bytes. | ||
* @param content Response bytes. | ||
* @param consumedParams Parameters consumed by the handler. | ||
*/ | ||
public ExtensionRestResponse(RestStatus status, String contentType, byte[] content, List<String> consumedParams) { | ||
super(status, contentType, content); | ||
addConsumedParamHeader(consumedParams); | ||
} | ||
|
||
/** | ||
* Creates a binary response. | ||
* | ||
* @param status The REST status. | ||
* @param contentType The content type of the response bytes. | ||
* @param content Response bytes. | ||
* @param consumedParams Parameters consumed by the handler. | ||
*/ | ||
public ExtensionRestResponse(RestStatus status, String contentType, BytesReference content, List<String> consumedParams) { | ||
super(status, contentType, content); | ||
addConsumedParamHeader(consumedParams); | ||
} | ||
|
||
private void addConsumedParamHeader(List<String> consumedParams) { | ||
consumedParams.stream().forEach(p -> addHeader(CONSUMED_PARAMS_KEY, p)); | ||
} | ||
} |
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.
This gives off a code smell where this class is additional functionality to the data structure, can we add
getKeys()
to the PathTrie implementation?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.
@peternied I tried that route. Unfortunately the
PathTrie
does not have functionality to list keys. You can insert, but the retrieve functionality requires you to give it a path (and possibly parameters to match). There is no way to generate a list of paths (short of a brute force dictionary attack of all possible strings to see which ones work).This lack of functionality on the PathTrie is also a challenge in testing (no way to validate/confirm that a path has been inserted, other than an IT) and might be a reasonable feature request to OpenSearch. But for now, for the purposes of deserializing the extension REST API to pass it over, this slightly smelly code is really the only reasonable option.