From 96dabb31d30a93b51e3802d620a4f5a92b4d0438 Mon Sep 17 00:00:00 2001 From: Chang Liu Date: Thu, 22 Jul 2021 11:26:16 -0700 Subject: [PATCH] Introduce replaceRoutes() method and 2 new constructors to RestHandler.java (#947) (#998) * Add addRoutesPrefix() method to RestHandler.java Signed-off-by: Azar Fazel Signed-off-by: cliu123 Co-authored-by: afazel --- .../java/org/opensearch/rest/RestHandler.java | 42 +++++++++++++++++++ .../opensearch/rest/BaseRestHandlerTests.java | 17 ++++++++ 2 files changed, 59 insertions(+) diff --git a/server/src/main/java/org/opensearch/rest/RestHandler.java b/server/src/main/java/org/opensearch/rest/RestHandler.java index 6b610b43379..6229dba84fb 100644 --- a/server/src/main/java/org/opensearch/rest/RestHandler.java +++ b/server/src/main/java/org/opensearch/rest/RestHandler.java @@ -38,6 +38,7 @@ import org.opensearch.rest.RestRequest.Method; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; /** * Handler for REST requests @@ -157,12 +158,40 @@ public interface RestHandler { private final String deprecatedPath; private final Method deprecatedMethod; + /** + * Construct replaced routes using new and deprocated methods and new and deprecated paths + * @param method route method + * @param path new route path + * @param deprecatedMethod deprecated method + * @param deprecatedPath deprecated path + */ public ReplacedRoute(Method method, String path, Method deprecatedMethod, String deprecatedPath) { super(method, path); this.deprecatedMethod = deprecatedMethod; this.deprecatedPath = deprecatedPath; } + /** + * Construct replaced routes using route method, new and deprecated paths + * This constructor can be used when both new and deprecated paths use the same method + * @param method route method + * @param path new route path + * @param deprecatedPath deprecated path + */ + public ReplacedRoute(Method method, String path, String deprecatedPath) { + this(method, path, method, deprecatedPath); + } + + /** + * Construct replaced routes using route, new and deprecated prefixes + * @param route route + * @param prefix new route prefix + * @param deprecatedPrefix deprecated prefix + */ + public ReplacedRoute(Route route, String prefix, String deprecatedPrefix) { + this(route.getMethod(), prefix + route.getPath(), deprecatedPrefix + route.getPath()); + } + public String getDeprecatedPath() { return deprecatedPath; } @@ -171,4 +200,17 @@ public interface RestHandler { return deprecatedMethod; } } + + /** + * Construct replaced routes using routes template and prefixes for new and deprecated paths + * @param routes routes + * @param prefix new prefix + * @param deprecatedPrefix deprecated prefix + * @return new list of API routes prefixed with the prefix string + */ + static List replaceRoutes(List routes, final String prefix, final String deprecatedPrefix){ + return routes.stream() + .map(route -> new ReplacedRoute(route, prefix, deprecatedPrefix)) + .collect(Collectors.toList()); + } } diff --git a/server/src/test/java/org/opensearch/rest/BaseRestHandlerTests.java b/server/src/test/java/org/opensearch/rest/BaseRestHandlerTests.java index d4640a80ce4..df1b700267a 100644 --- a/server/src/test/java/org/opensearch/rest/BaseRestHandlerTests.java +++ b/server/src/test/java/org/opensearch/rest/BaseRestHandlerTests.java @@ -45,10 +45,16 @@ import org.opensearch.test.rest.FakeRestChannel; import org.opensearch.test.rest.FakeRestRequest; import org.opensearch.threadpool.TestThreadPool; import org.opensearch.threadpool.ThreadPool; +import org.opensearch.rest.RestRequest.Method; +import org.opensearch.rest.RestHandler.Route; +import org.opensearch.rest.RestHandler.ReplacedRoute; +import org.opensearch.rest.RestHandler; import java.io.IOException; import java.util.Collections; import java.util.HashMap; +import java.util.List; +import java.util.Arrays; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; @@ -337,4 +343,15 @@ public class BaseRestHandlerTests extends OpenSearchTestCase { } } + public void testReplaceRoutesMethod() throws Exception { + List routes = Arrays.asList(new Route(Method.GET, "/path/test"), new Route(Method.PUT, "/path2/test")); + List replacedRoutes = RestHandler.replaceRoutes(routes, "/prefix", "/deprecatedPrefix"); + + for(int i = 0; i < routes.size(); i++) { + assertEquals("/prefix" + routes.get(i).getPath(), replacedRoutes.get(i).getPath()); + assertEquals(routes.get(i).getMethod(), replacedRoutes.get(i).getMethod()); + assertEquals("/deprecatedPrefix" + routes.get(i).getPath(), replacedRoutes.get(i).getDeprecatedPath()); + } + } + }