-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle named wildcards (REST path parameters) (#123)
* Register REST paths including named wildcards Signed-off-by: Daniel Widdis <[email protected]> * Move HelloWorld extension to subpackage Signed-off-by: Daniel Widdis <[email protected]> * Update Hello World example with named wildcard example Signed-off-by: Daniel Widdis <[email protected]> * Pass consumed params in RestResponse header Signed-off-by: Daniel Widdis <[email protected]> * Linelint hates me and web tool exports Signed-off-by: Daniel Widdis <[email protected]> * Code Review tweaks Signed-off-by: Daniel Widdis <[email protected]> * Update DESIGN.md Co-authored-by: Sarat Vemulapalli <[email protected]> Signed-off-by: Daniel Widdis <[email protected]> Signed-off-by: Daniel Widdis <[email protected]> Co-authored-by: Sarat Vemulapalli <[email protected]>
- Loading branch information
1 parent
e0144c2
commit 27612c8
Showing
19 changed files
with
659 additions
and
97 deletions.
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.