
io.micronaut.web.router.AbstractRouteMatch Maven / Gradle / Ivy
/*
* Copyright 2017-2020 original authors
*
* 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
*
* https://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 io.micronaut.web.router;
import edu.umd.cs.findbugs.annotations.Nullable;
import io.micronaut.core.annotation.AnnotationMetadata;
import io.micronaut.core.bind.ArgumentBinder;
import io.micronaut.core.bind.annotation.Bindable;
import io.micronaut.core.convert.ArgumentConversionContext;
import io.micronaut.core.convert.ConversionContext;
import io.micronaut.core.convert.ConversionError;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.convert.exceptions.ConversionErrorException;
import io.micronaut.core.type.Argument;
import io.micronaut.core.type.ReturnType;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MediaType;
import io.micronaut.http.sse.Event;
import io.micronaut.inject.ExecutableMethod;
import io.micronaut.inject.MethodExecutionHandle;
import io.micronaut.web.router.exceptions.UnsatisfiedRouteException;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.lang.reflect.Method;
import java.util.*;
import java.util.function.Predicate;
/**
* Abstract implementation of the {@link RouteMatch} interface.
*
* @param The target type
* @param Route Match
* @author Graeme Rocher
* @since 1.0
*/
abstract class AbstractRouteMatch implements MethodBasedRouteMatch {
protected final MethodExecutionHandle executableMethod;
protected final ConversionService> conversionService;
protected final Map requiredInputs;
protected final DefaultRouteBuilder.AbstractRoute abstractRoute;
protected final List consumedMediaTypes;
protected final List producedMediaTypes;
/**
* Constructor.
*
* @param abstractRoute The abstract route builder
* @param conversionService The conversion service
*/
protected AbstractRouteMatch(DefaultRouteBuilder.AbstractRoute abstractRoute, ConversionService> conversionService) {
this.abstractRoute = abstractRoute;
//noinspection unchecked
this.executableMethod = (MethodExecutionHandle) abstractRoute.targetMethod;
this.conversionService = conversionService;
Argument[] requiredArguments = executableMethod.getArguments();
this.requiredInputs = new LinkedHashMap<>(requiredArguments.length);
for (Argument requiredArgument : requiredArguments) {
String inputName = resolveInputName(requiredArgument);
requiredInputs.put(inputName, requiredArgument);
}
this.consumedMediaTypes = abstractRoute.getConsumes();
this.producedMediaTypes = abstractRoute.getProduces();
}
@Override
public final boolean isSuspended() {
return this.abstractRoute.isSuspended();
}
@Override
public final boolean isReactive() {
return this.abstractRoute.isReactive();
}
@Override
public final boolean isSingleResult() {
return this.abstractRoute.isSingleResult();
}
@Override
public final boolean isSpecifiedSingle() {
return this.abstractRoute.isSpecifiedSingle();
}
@Override
public final boolean isAsync() {
return this.abstractRoute.isAsync();
}
@Override
public final boolean isVoid() {
return this.abstractRoute.isVoid();
}
@Override
public T getTarget() {
return executableMethod.getTarget();
}
@NonNull
@Override
public ExecutableMethod, R> getExecutableMethod() {
return executableMethod.getExecutableMethod();
}
@Override
public List getProduces() {
Optional> firstTypeVariable = executableMethod.getReturnType().getFirstTypeVariable();
if (firstTypeVariable.isPresent() && Event.class.isAssignableFrom(firstTypeVariable.get().getType())) {
return Collections.singletonList(MediaType.TEXT_EVENT_STREAM_TYPE);
} else {
return abstractRoute.getProduces();
}
}
@Override
public AnnotationMetadata getAnnotationMetadata() {
return executableMethod.getAnnotationMetadata();
}
@SuppressWarnings("unchecked")
@Override
public Optional> getBodyArgument() {
Argument> arg = abstractRoute.bodyArgument;
if (arg != null) {
return Optional.of(arg);
}
String bodyArgument = abstractRoute.bodyArgumentName;
if (bodyArgument != null) {
return Optional.ofNullable(requiredInputs.get(bodyArgument));
}
return Optional.empty();
}
@Override
public boolean isRequiredInput(String name) {
return requiredInputs.containsKey(name);
}
@SuppressWarnings("unchecked")
@Override
public Optional> getRequiredInput(String name) {
return Optional.ofNullable(requiredInputs.get(name));
}
@Override
public boolean isExecutable() {
Map variables = getVariableValues();
for (Map.Entry entry : requiredInputs.entrySet()) {
Object value = variables.get(entry.getKey());
if (value == null || value instanceof UnresolvedArgument) {
return false;
}
}
Optional> bodyArgument = getBodyArgument();
if (bodyArgument.isPresent()) {
Object value = variables.get(bodyArgument.get().getName());
return value != null && !(value instanceof UnresolvedArgument);
}
return true;
}
@Override
public Method getTargetMethod() {
return executableMethod.getTargetMethod();
}
@Override
public String getMethodName() {
return this.executableMethod.getMethodName();
}
@Override
public Class getDeclaringType() {
return executableMethod.getDeclaringType();
}
@Override
public Argument[] getArguments() {
return executableMethod.getArguments();
}
@Override
public boolean test(HttpRequest request) {
for (Predicate> condition : abstractRoute.conditions) {
if (!condition.test(request)) {
return false;
}
}
return true;
}
@Override
public ReturnType getReturnType() {
return executableMethod.getReturnType();
}
@Override
public R invoke(Object... arguments) {
ConversionService> conversionService = this.conversionService;
Argument[] targetArguments = getArguments();
if (targetArguments.length == 0) {
return executableMethod.invoke();
} else {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy