|
| 1 | +/* |
| 2 | + * Copyright 2017-2019 original authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.micronaut.http.bind.binders; |
| 18 | + |
| 19 | +import io.micronaut.core.annotation.AnnotationMetadata; |
| 20 | +import io.micronaut.core.bind.annotation.AbstractAnnotatedArgumentBinder; |
| 21 | +import io.micronaut.core.convert.ArgumentConversionContext; |
| 22 | +import io.micronaut.core.convert.ConversionService; |
| 23 | +import io.micronaut.core.convert.value.ConvertibleMultiValues; |
| 24 | +import io.micronaut.core.convert.value.ConvertibleValues; |
| 25 | +import io.micronaut.core.type.Argument; |
| 26 | +import io.micronaut.http.HttpAttributes; |
| 27 | +import io.micronaut.http.HttpMethod; |
| 28 | +import io.micronaut.http.HttpRequest; |
| 29 | +import io.micronaut.http.annotation.PathVariable; |
| 30 | +import io.micronaut.http.uri.UriMatchInfo; |
| 31 | +import io.micronaut.http.uri.UriMatchVariable; |
| 32 | + |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.Optional; |
| 35 | + |
| 36 | +/** |
| 37 | + * Used for binding a parameter exclusively from a path variable. |
| 38 | + * |
| 39 | + * @author graemerocher |
| 40 | + * @since 1.0.3 |
| 41 | + * @see PathVariable |
| 42 | + * @param <T> |
| 43 | + */ |
| 44 | +public class PathVariableAnnotationBinder<T> extends AbstractAnnotatedArgumentBinder<PathVariable, T, HttpRequest<?>> implements AnnotatedRequestArgumentBinder<PathVariable, T> { |
| 45 | + |
| 46 | + /** |
| 47 | + * @param conversionService The conversion service |
| 48 | + */ |
| 49 | + public PathVariableAnnotationBinder(ConversionService<?> conversionService) { |
| 50 | + super(conversionService); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public Class<PathVariable> getAnnotationType() { |
| 55 | + return PathVariable.class; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public BindingResult<T> bind(ArgumentConversionContext<T> context, HttpRequest<?> source) { |
| 60 | + ConvertibleMultiValues<String> parameters = source.getParameters(); |
| 61 | + Argument<T> argument = context.getArgument(); |
| 62 | + HttpMethod httpMethod = source.getMethod(); |
| 63 | + |
| 64 | + AnnotationMetadata annotationMetadata = argument.getAnnotationMetadata(); |
| 65 | + boolean hasAnnotation = annotationMetadata.hasAnnotation(PathVariable.class); |
| 66 | + String parameterName = annotationMetadata.getValue(PathVariable.class, String.class).orElse(argument.getName()); |
| 67 | + // If we need to bind all request params to command object |
| 68 | + // checks if the variable is defined with modifier char * |
| 69 | + // eg. ?pojo* |
| 70 | + final Optional<UriMatchInfo> matchInfo = source.getAttribute(HttpAttributes.ROUTE_MATCH, UriMatchInfo.class); |
| 71 | + boolean bindAll = matchInfo |
| 72 | + .flatMap(umi -> umi.getVariables() |
| 73 | + .stream() |
| 74 | + .filter(v -> v.getName().equals(parameterName)) |
| 75 | + .findFirst() |
| 76 | + .map(UriMatchVariable::isExploded)).orElse(false); |
| 77 | + |
| 78 | + |
| 79 | + BindingResult<T> result; |
| 80 | + // if the annotation is present or the HTTP method doesn't allow a request body |
| 81 | + // attempt to bind from request parameters. This avoids allowing the request URI to |
| 82 | + // be manipulated to override POST or JSON variables |
| 83 | + if (hasAnnotation && matchInfo.isPresent()) { |
| 84 | + final ConvertibleValues<Object> variableValues = ConvertibleValues.of(matchInfo.get().getVariableValues()); |
| 85 | + if (bindAll) { |
| 86 | + Object value; |
| 87 | + // Only maps and POJOs will "bindAll", lists work like normal |
| 88 | + if (Iterable.class.isAssignableFrom(argument.getType())) { |
| 89 | + value = doResolve(context, variableValues, parameterName); |
| 90 | + if (value == null) { |
| 91 | + value = Collections.emptyList(); |
| 92 | + } |
| 93 | + } else { |
| 94 | + value = parameters.asMap(); |
| 95 | + } |
| 96 | + result = doConvert(value, context); |
| 97 | + } else { |
| 98 | + result = doBind(context, variableValues, parameterName); |
| 99 | + } |
| 100 | + } else { |
| 101 | + //noinspection unchecked |
| 102 | + result = BindingResult.EMPTY; |
| 103 | + } |
| 104 | + |
| 105 | + return result; |
| 106 | + } |
| 107 | +} |
0 commit comments