-
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.
Match Rest Path registry with RestController using SDKMethodHandlers (#…
…658) Signed-off-by: Daniel Widdis <widdis@gmail.com>
- Loading branch information
Showing
6 changed files
with
91 additions
and
13 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
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
67 changes: 67 additions & 0 deletions
67
src/main/java/org/opensearch/sdk/rest/SDKMethodHandlers.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,67 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* 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.rest; | ||
|
||
import org.opensearch.common.Nullable; | ||
import org.opensearch.sdk.ExtensionRestHandler; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static org.opensearch.rest.RestRequest.Method; | ||
|
||
/** | ||
* Encapsulate multiple handlers for the same path, allowing different handlers for different HTTP verbs. | ||
* <p> | ||
* Used in SDK to provide identical path-matching functionality as OpenSearch, with Extension-based classes. | ||
*/ | ||
final class SDKMethodHandlers { | ||
|
||
private final String path; | ||
private final Map<Method, ExtensionRestHandler> methodHandlers; | ||
|
||
SDKMethodHandlers(String path, ExtensionRestHandler handler, Method... methods) { | ||
this.path = path; | ||
this.methodHandlers = new HashMap<>(methods.length); | ||
for (Method method : methods) { | ||
methodHandlers.put(method, handler); | ||
} | ||
} | ||
|
||
/** | ||
* Add a handler for an additional array of methods. Note that {@code SDKMethodHandlers} | ||
* does not allow replacing the handler for an already existing method. | ||
*/ | ||
SDKMethodHandlers addMethods(ExtensionRestHandler handler, Method... methods) { | ||
for (Method method : methods) { | ||
ExtensionRestHandler existing = methodHandlers.putIfAbsent(method, handler); | ||
if (existing != null) { | ||
throw new IllegalArgumentException("Cannot replace existing handler for [" + path + "] for method: " + method); | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Returns the handler for the given method or {@code null} if none exists. | ||
*/ | ||
@Nullable | ||
ExtensionRestHandler getHandler(Method method) { | ||
return methodHandlers.get(method); | ||
} | ||
|
||
/** | ||
* Return a set of all valid HTTP methods for the particular path | ||
*/ | ||
Set<Method> getValidMethods() { | ||
return methodHandlers.keySet(); | ||
} | ||
} |
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