Skip to content

Commit

Permalink
Extract MethodHandlers interface.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Jan 18, 2024
1 parent af14293 commit c1ea9cc
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 81 deletions.
77 changes: 6 additions & 71 deletions server/src/main/java/org/opensearch/rest/MethodHandlers.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,89 +6,24 @@
* compatible open source license.
*/

/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.rest;

import org.opensearch.common.Nullable;
import org.opensearch.common.annotation.PublicApi;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* Encapsulate multiple handlers for the same path, allowing different handlers for different HTTP verbs.
*
* @opensearch.api
* A collection of REST method handlers.
*/
public final class MethodHandlers {

private final String path;
private final Map<RestRequest.Method, RestHandler> methodHandlers;

MethodHandlers(String path, RestHandler handler, RestRequest.Method... methods) {
this.path = path;
this.methodHandlers = new HashMap<>(methods.length);
for (RestRequest.Method method : methods) {
methodHandlers.put(method, handler);
}
}

/**
* Add a handler for an additional array of methods. Note that {@code MethodHandlers}
* does not allow replacing the handler for an already existing method.
*/
MethodHandlers addMethods(RestHandler handler, RestRequest.Method... methods) {
for (RestRequest.Method method : methods) {
RestHandler 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
RestHandler getHandler(RestRequest.Method method) {
return methodHandlers.get(method);
}

@PublicApi(since = "2.12.0")
public interface MethodHandlers {
/**
* Return a set of all valid HTTP methods for the particular path.
*/
public Set<RestRequest.Method> getValidMethods() {
return methodHandlers.keySet();
}
Set<RestRequest.Method> getValidMethods();

/**
* Returns the relative HTTP path of the set of method handlers.
*/
public String getPath() {
return path;
}
String getPath();
}
17 changes: 10 additions & 7 deletions server/src/main/java/org/opensearch/rest/RestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -107,7 +108,7 @@ public class RestController implements HttpServerTransport.Dispatcher {
}
}

private final PathTrie<MethodHandlers> handlers = new PathTrie<>(RestUtils.REST_DECODER);
private final PathTrie<RestMethodHandlers> handlers = new PathTrie<>(RestUtils.REST_DECODER);

private final UnaryOperator<RestHandler> handlerWrapper;

Expand Down Expand Up @@ -149,7 +150,9 @@ public RestController(
* @return {@link Iterator} of {@link MethodHandlers}
*/
public Iterator<MethodHandlers> getAllHandlers() {
return handlers.retrieveAll();
List<MethodHandlers> methodHandlers = new ArrayList<>();
handlers.retrieveAll().forEachRemaining(methodHandlers::add);
return methodHandlers.iterator();
}

/**
Expand Down Expand Up @@ -229,7 +232,7 @@ protected void registerHandler(RestRequest.Method method, String path, RestHandl
private void registerHandlerNoWrap(RestRequest.Method method, String path, RestHandler maybeWrappedHandler) {
handlers.insertOrUpdate(
path,
new MethodHandlers(path, maybeWrappedHandler, method),
new RestMethodHandlers(path, maybeWrappedHandler, method),
(mHandlers, newMHandler) -> mHandlers.addMethods(maybeWrappedHandler, method)
);
}
Expand Down Expand Up @@ -400,10 +403,10 @@ private void tryAllHandlers(final RestRequest request, final RestChannel channel
// Resolves the HTTP method and fails if the method is invalid
requestMethod = request.method();
// Loop through all possible handlers, attempting to dispatch the request
Iterator<MethodHandlers> allHandlers = getAllHandlers(request.params(), rawPath);
Iterator<RestMethodHandlers> allHandlers = getAllRestMethodHandlers(request.params(), rawPath);
while (allHandlers.hasNext()) {
final RestHandler handler;
final MethodHandlers handlers = allHandlers.next();
final RestMethodHandlers handlers = allHandlers.next();
if (handlers == null) {
handler = null;
} else {
Expand Down Expand Up @@ -431,7 +434,7 @@ private void tryAllHandlers(final RestRequest request, final RestChannel channel
handleBadRequest(uri, requestMethod, channel);
}

Iterator<MethodHandlers> getAllHandlers(@Nullable Map<String, String> requestParamsRef, String rawPath) {
Iterator<RestMethodHandlers> getAllRestMethodHandlers(@Nullable Map<String, String> requestParamsRef, String rawPath) {
final Supplier<Map<String, String>> paramsSupplier;
if (requestParamsRef == null) {
paramsSupplier = () -> null;
Expand Down Expand Up @@ -569,7 +572,7 @@ private boolean handleAuthenticateUser(final RestRequest request, final RestChan
*/
private Set<RestRequest.Method> getValidHandlerMethodSet(String rawPath) {
Set<RestRequest.Method> validMethods = new HashSet<>();
Iterator<MethodHandlers> allHandlers = getAllHandlers(null, rawPath);
Iterator<RestMethodHandlers> allHandlers = getAllRestMethodHandlers(null, rawPath);
while (allHandlers.hasNext()) {
final MethodHandlers methodHandlers = allHandlers.next();
if (methodHandlers != null) {
Expand Down
92 changes: 92 additions & 0 deletions server/src/main/java/org/opensearch/rest/RestMethodHandlers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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.
*/

/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.rest;

import org.opensearch.common.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* Encapsulate multiple handlers for the same path, allowing different handlers for different HTTP verbs.
*/
final class RestMethodHandlers implements MethodHandlers {

private final String path;
private final Map<RestRequest.Method, RestHandler> methodHandlers;

RestMethodHandlers(String path, RestHandler handler, RestRequest.Method... methods) {
this.path = path;
this.methodHandlers = new HashMap<>(methods.length);
for (RestRequest.Method method : methods) {
methodHandlers.put(method, handler);
}
}

/**
* Add a handler for an additional array of methods. Note that {@code MethodHandlers}
* does not allow replacing the handler for an already existing method.
*/
public RestMethodHandlers addMethods(RestHandler handler, RestRequest.Method... methods) {
for (RestRequest.Method method : methods) {
RestHandler 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
public RestHandler getHandler(RestRequest.Method method) {
return methodHandlers.get(method);
}

/**
* Return a set of all valid HTTP methods for the particular path.
*/
public Set<RestRequest.Method> getValidMethods() {
return methodHandlers.keySet();
}

/**
* Returns the relative HTTP path of the set of method handlers.
*/
public String getPath() {
return path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ public void testApplyRelevantHeaders() throws Exception {
restHeaders.put("header.3", Collections.singletonList("false"));
RestRequest fakeRequest = new FakeRestRequest.Builder(xContentRegistry()).withHeaders(restHeaders).build();
final RestController spyRestController = spy(restController);
when(spyRestController.getAllHandlers(null, fakeRequest.rawPath())).thenReturn(new Iterator<MethodHandlers>() {
when(spyRestController.getAllRestMethodHandlers(null, fakeRequest.rawPath())).thenReturn(new Iterator<RestMethodHandlers>() {
@Override
public boolean hasNext() {
return false;
}

@Override
public MethodHandlers next() {
return new MethodHandlers("/", (RestRequest request, RestChannel channel, NodeClient client) -> {
public RestMethodHandlers next() {
return new RestMethodHandlers("/", (RestRequest request, RestChannel channel, NodeClient client) -> {
assertEquals("true", threadContext.getHeader("header.1"));
assertEquals("true", threadContext.getHeader("header.2"));
assertNull(threadContext.getHeader("header.3"));
Expand Down

0 comments on commit c1ea9cc

Please sign in to comment.