mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-17 02:14:54 +00:00
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:
parent
854967ff92
commit
23d604571c
@ -38,6 +38,7 @@ import org.opensearch.rest.RestRequest.Method;
|
|||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler for REST requests
|
* Handler for REST requests
|
||||||
@ -157,12 +158,40 @@ public interface RestHandler {
|
|||||||
private final String deprecatedPath;
|
private final String deprecatedPath;
|
||||||
private final Method deprecatedMethod;
|
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) {
|
public ReplacedRoute(Method method, String path, Method deprecatedMethod, String deprecatedPath) {
|
||||||
super(method, path);
|
super(method, path);
|
||||||
this.deprecatedMethod = deprecatedMethod;
|
this.deprecatedMethod = deprecatedMethod;
|
||||||
this.deprecatedPath = deprecatedPath;
|
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() {
|
public String getDeprecatedPath() {
|
||||||
return deprecatedPath;
|
return deprecatedPath;
|
||||||
}
|
}
|
||||||
@ -171,4 +200,17 @@ public interface RestHandler {
|
|||||||
return deprecatedMethod;
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,10 +45,16 @@ import org.opensearch.test.rest.FakeRestChannel;
|
|||||||
import org.opensearch.test.rest.FakeRestRequest;
|
import org.opensearch.test.rest.FakeRestRequest;
|
||||||
import org.opensearch.threadpool.TestThreadPool;
|
import org.opensearch.threadpool.TestThreadPool;
|
||||||
import org.opensearch.threadpool.ThreadPool;
|
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.io.IOException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user