Skip to content

Commit

Permalink
Replaces NamedRouteHandler and update logic to map route handlers for…
Browse files Browse the repository at this point in the history
… Rest request

Signed-off-by: Darshit Chanpura <[email protected]>
  • Loading branch information
DarshitChanpura committed Jun 30, 2023
1 parent 8ac2f16 commit 49fa657
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.opensearch.rest.NamedRoute;
import org.opensearch.rest.RestHandler.DeprecatedRoute;
import org.opensearch.rest.RestHandler.ReplacedRoute;
import org.opensearch.rest.RestHandler.Route;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestStatus;

Expand All @@ -46,18 +45,9 @@ public abstract class BaseExtensionRestHandler implements ExtensionRestHandler {
*/
public static final String JSON_CONTENT_TYPE = APPLICATION_JSON.withCharset(StandardCharsets.UTF_8).toString();

/**
* Defines a list of methods which handle each rest {@link NamedRoute}. Override this in a subclass to use the functional syntax.
*
* @return a list of {@link NamedRouteHandler} with corresponding methods to each route.
*/
protected List<NamedRouteHandler> namedRouteHandlers() {
return Collections.emptyList();
}

@Override
public List<Route> routes() {
return List.copyOf(namedRouteHandlers());
public List<NamedRoute> routes() {
return Collections.emptyList();
}

/**
Expand Down Expand Up @@ -103,12 +93,12 @@ public List<ReplacedRoute> replacedRoutes() {

@Override
public ExtensionRestResponse handleRequest(RestRequest request) {
Optional<NamedRouteHandler> handler = namedRouteHandlers().stream()
Optional<NamedRoute> route = routes().stream()
.filter(rh -> rh.getMethod().equals(request.method()))
.filter(rh -> restPathMatches(request.path(), rh.getPath()))
.findFirst();
if (handler.isPresent()) {
return handler.get().handleRequest(request);
if (route.isPresent() && route.get().handler() != null) {
return (ExtensionRestResponse) route.get().handler().apply(request);
}
Optional<DeprecatedNamedRouteHandler> deprecatedHandler = deprecatedNamedRouteHandlers().stream()
.filter(rh -> rh.getMethod().equals(request.method()))
Expand Down Expand Up @@ -138,7 +128,7 @@ public ExtensionRestResponse handleRequest(RestRequest request) {
* Determines if the request's path is a match for the configured handler path.
*
* @param requestPath The path from the {@link RestRequest}
* @param handlerPath The path from the {@link NamedRouteHandler} or {@link DeprecatedNamedRouteHandler} or {@link ReplacedNamedRouteHandler}
* @param handlerPath The path from the {@link NamedRoute} or {@link DeprecatedNamedRouteHandler} or {@link ReplacedNamedRouteHandler}
* @return true if the request path matches the route
*/
private boolean restPathMatches(String requestPath, String handlerPath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.opensearch.extensions.rest.ExtensionRestResponse;
import org.opensearch.rest.BaseRestHandler;
import org.opensearch.rest.NamedRoute;
import org.opensearch.rest.RestHandler;
import org.opensearch.rest.RestHandler.DeprecatedRoute;
import org.opensearch.rest.RestHandler.ReplacedRoute;
Expand Down Expand Up @@ -42,7 +43,7 @@ public interface ExtensionRestHandler {
*
* @return The routes this handler will handle.
*/
default List<Route> routes() {
default List<NamedRoute> routes() {
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ public class ExtensionRestPathRegistry {
*/
public void registerHandler(ExtensionRestHandler restHandler) {
restHandler.routes().forEach(route -> {
NamedRouteHandler namedRouteHandler = ((NamedRouteHandler) route);
String routeActionName = namedRouteHandler.name();
String routeActionName = route.name();
if (routeActionName == null) {
throw new IllegalArgumentException("Route handler must have a name associated with it.");
}
Set<String> associatedActions = namedRouteHandler.actionNames();
Set<String> associatedActions = route.actionNames();
registerHandler(route.getMethod(), route.getPath(), routeActionName, associatedActions, restHandler);
});
restHandler.deprecatedRoutes().forEach(route -> {
Expand Down
59 changes: 0 additions & 59 deletions src/main/java/org/opensearch/sdk/rest/NamedRouteHandler.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.extensions.rest.ExtensionRestResponse;
import org.opensearch.rest.NamedRoute;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestResponse;
import org.opensearch.sdk.rest.BaseExtensionRestHandler;
import org.opensearch.sdk.rest.ExtensionRestHandler;
import org.opensearch.sdk.rest.NamedRouteHandler;

import java.io.IOException;
import java.net.URLDecoder;
Expand Down Expand Up @@ -60,29 +61,43 @@ public class RestHelloAction extends BaseExtensionRestHandler {
public RestHelloAction() {}

@Override
public List<NamedRouteHandler> namedRouteHandlers() {
public List<NamedRoute> routes() {
return List.of(
new NamedRouteHandler(GET, "/hello", handleGetRequest, routePrefix("greet"), Set.of("cluster:admin/opensearch/hw/greet")),
new NamedRouteHandler(POST, "/hello", handlePostRequest, routePrefix("greet_with_adjective"), Collections.emptySet()),
new NamedRouteHandler(
PUT,
"/hello/{name}",
handlePutRequest,
routePrefix("greet_with_name"),
Set.of("cluster:admin/opensearch/hw/greet_with_name")
),
new NamedRouteHandler(DELETE, "/goodbye", handleDeleteRequest, routePrefix("goodbye"), Collections.emptySet())
new NamedRoute.Builder().method(GET)
.path("/hello")
.handler(handleGetRequest)
.uniqueName(routePrefix("greet"))
.legacyActionNames(Set.of("cluster:admin/opensearch/hw/greet"))
.build(),
new NamedRoute.Builder().method(POST)
.path("/hello")
.handler(handlePostRequest)
.uniqueName(routePrefix("greet_with_adjective"))
.legacyActionNames(Collections.emptySet())
.build(),
new NamedRoute.Builder().method(PUT)
.path("/hello/{name}")
.handler(handlePutRequest)
.uniqueName(routePrefix("greet_with_name"))
.legacyActionNames(Set.of("cluster:admin/opensearch/hw/greet_with_name"))
.build(),
new NamedRoute.Builder().method(DELETE)
.path("/goodbye")
.handler(handleDeleteRequest)
.uniqueName(routePrefix("goodbye"))
.legacyActionNames(Collections.emptySet())
.build()
);
}

private Function<RestRequest, ExtensionRestResponse> handleGetRequest = (request) -> {
private Function<RestRequest, RestResponse> handleGetRequest = (request) -> {
String worldNameWithRandomAdjective = worldAdjectives.isEmpty()
? worldName
: String.join(" ", worldAdjectives.get(rand.nextInt(worldAdjectives.size())), worldName);
return new ExtensionRestResponse(request, OK, String.format(GREETING, worldNameWithRandomAdjective));
};

private Function<RestRequest, ExtensionRestResponse> handlePostRequest = (request) -> {
private Function<RestRequest, RestResponse> handlePostRequest = (request) -> {
if (request.hasContent()) {
String adjective = "";
XContentType contentType = request.getXContentType();
Expand Down Expand Up @@ -130,7 +145,7 @@ public List<NamedRouteHandler> namedRouteHandlers() {
return new ExtensionRestResponse(request, BAD_REQUEST, TEXT_CONTENT_TYPE, noContent);
};

private Function<RestRequest, ExtensionRestResponse> handlePutRequest = (request) -> {
private Function<RestRequest, RestResponse> handlePutRequest = (request) -> {
String name = request.param("name");
try {
worldName = URLDecoder.decode(name, StandardCharsets.UTF_8);
Expand All @@ -140,7 +155,7 @@ public List<NamedRouteHandler> namedRouteHandlers() {
return new ExtensionRestResponse(request, OK, "Updated the world's name to " + worldName);
};

private Function<RestRequest, ExtensionRestResponse> handleDeleteRequest = (request) -> {
private Function<RestRequest, RestResponse> handleDeleteRequest = (request) -> {
this.worldName = DEFAULT_NAME;
this.worldAdjectives.clear();
return new ExtensionRestResponse(request, OK, "Goodbye, cruel world! Restored default values.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
import org.opensearch.extensions.ExtensionsManager;
import org.opensearch.extensions.action.RemoteExtensionActionResponse;
import org.opensearch.extensions.rest.ExtensionRestResponse;
import org.opensearch.rest.NamedRoute;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestResponse;
import org.opensearch.sdk.ExtensionsRunner;
import org.opensearch.sdk.SDKClient;
import org.opensearch.sdk.action.RemoteExtensionAction;
import org.opensearch.sdk.action.RemoteExtensionActionRequest;
import org.opensearch.sdk.rest.BaseExtensionRestHandler;
import org.opensearch.sdk.rest.NamedRouteHandler;
import org.opensearch.sdk.sample.helloworld.transport.SampleAction;
import org.opensearch.sdk.sample.helloworld.transport.SampleRequest;
import org.opensearch.sdk.sample.helloworld.transport.SampleResponse;
Expand Down Expand Up @@ -51,19 +52,19 @@ public RestRemoteHelloAction(ExtensionsRunner runner) {
}

@Override
public List<NamedRouteHandler> namedRouteHandlers() {
public List<NamedRoute> routes() {
return List.of(
new NamedRouteHandler(
GET,
"/hello/{name}",
handleRemoteGetRequest,
routePrefix("remote_greet_with_name"),
Collections.emptySet()
)

new NamedRoute.Builder().method(GET)
.path("/hello/{name}")
.handler(handleRemoteGetRequest)
.uniqueName(routePrefix("remote_greet_with_name"))
.legacyActionNames(Collections.emptySet())
.build()
);
}

private Function<RestRequest, ExtensionRestResponse> handleRemoteGetRequest = (request) -> {
private Function<RestRequest, RestResponse> handleRemoteGetRequest = (request) -> {
SDKClient client = extensionsRunner.getSdkClient();

String name = request.param("name");
Expand Down
Loading

0 comments on commit 49fa657

Please sign in to comment.