Introduce replaceRoutes() method and 2 new constructors to RestHandler.java (#947)

* Add addRoutesPrefix() method to RestHandler.java

Signed-off-by: Azar Fazel <azar.fazel@gmail.com>
This commit is contained in:
afazel 2021-07-15 19:14:15 -07:00 committed by GitHub
parent 854967ff92
commit 23d604571c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 0 deletions

View File

@ -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<ReplacedRoute> replaceRoutes(List<Route> routes, final String prefix, final String deprecatedPrefix){
return routes.stream()
.map(route -> new ReplacedRoute(route, prefix, deprecatedPrefix))
.collect(Collectors.toList());
}
}

View File

@ -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<Route> routes = Arrays.asList(new Route(Method.GET, "/path/test"), new Route(Method.PUT, "/path2/test"));
List<ReplacedRoute> 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());
}
}
}