// Copyright 2017 The Nomulus Authors. All Rights Reserved.
//
// Licensed 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.
package google.registry.request;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Throwables.throwIfUnchecked;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Ordering;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.function.Function;
/**
* Path prefix request router.
*
*
See the documentation of {@link RequestHandler} for more information.
*
*
Implementation Details
*
*
Request routing is O(logn) because {@link ImmutableSortedMap} performs a binary search over a
* contiguous array, which makes it faster than a {@link TreeMap}. However a prefix trie search in
* generated code would be the ideal approach.
*/
final class Router {
/** Create a new Router for the given component class. */
static Router create(Class> componentClass) {
return new Router(componentClass);
}
private final ImmutableSortedMap routes;
private Router(Class> componentClass) {
this.routes = extractRoutesFromComponent(componentClass);
checkArgument(
!this.routes.isEmpty(), "No routes found for class: %s", componentClass.getCanonicalName());
}
/** Returns the appropriate action route for a request. */
Optional route(String path) {
Map.Entry floor = routes.floorEntry(path);
if (floor != null) {
if (floor.getValue().action().isPrefix()
? path.startsWith(floor.getKey())
: path.equals(floor.getKey())) {
return Optional.of(floor.getValue());
}
}
return Optional.empty();
}
static ImmutableSortedMap extractRoutesFromComponent(Class> componentClass) {
ImmutableSortedMap.Builder routes =
new ImmutableSortedMap.Builder<>(Ordering.natural());
for (Method method : componentClass.getMethods()) {
// Make App Engine's security manager happy.
method.setAccessible(true);
if (!isDaggerInstantiatorOfType(Runnable.class, method)) {
continue;
}
Action action = method.getReturnType().getAnnotation(Action.class);
if (action == null) {
continue;
}
@SuppressWarnings("unchecked") // Safe due to previous checks.
Route route =
Route.create(
action,
(Function