|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or 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 | + * https://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 | +package org.springframework.shell.command.annotation.support; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.List; |
| 20 | +import java.util.stream.Collectors; |
| 21 | + |
| 22 | +import org.springframework.beans.factory.config.ConfigurableBeanFactory; |
| 23 | +import org.springframework.core.MethodParameter; |
| 24 | +import org.springframework.core.convert.ConversionService; |
| 25 | +import org.springframework.lang.Nullable; |
| 26 | +import org.springframework.messaging.Message; |
| 27 | +import org.springframework.messaging.MessageHandlingException; |
| 28 | +import org.springframework.shell.command.annotation.Option; |
| 29 | +import org.springframework.shell.support.AbstractArgumentMethodArgumentResolver; |
| 30 | +import org.springframework.util.Assert; |
| 31 | +import org.springframework.util.StringUtils; |
| 32 | + |
| 33 | +/** |
| 34 | + * Resolver for {@link Option @Option} arguments. |
| 35 | + * |
| 36 | + * @author Janne Valkealahti |
| 37 | + */ |
| 38 | +public class OptionMethodArgumentResolver extends AbstractArgumentMethodArgumentResolver { |
| 39 | + |
| 40 | + public OptionMethodArgumentResolver(ConversionService conversionService, |
| 41 | + @Nullable ConfigurableBeanFactory beanFactory) { |
| 42 | + super(conversionService, beanFactory); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public boolean supportsParameter(MethodParameter parameter) { |
| 47 | + return parameter.hasParameterAnnotation(Option.class); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + protected NamedValueInfo createNamedValueInfo(MethodParameter parameter) { |
| 52 | + Option annot = parameter.getParameterAnnotation(Option.class); |
| 53 | + Assert.state(annot != null, "No Option annotation"); |
| 54 | + List<String> names = Arrays.stream(annot != null ? annot.longNames() : new String[0]) |
| 55 | + .collect(Collectors.toList()); |
| 56 | + return new HeaderNamedValueInfo(annot, names); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + @Nullable |
| 61 | + protected Object resolveArgumentInternal(MethodParameter parameter, Message<?> message, List<String> names) |
| 62 | + throws Exception { |
| 63 | + for (String name : names) { |
| 64 | + if (message.getHeaders().containsKey(ARGUMENT_PREFIX + name)) { |
| 65 | + return message.getHeaders().get(ARGUMENT_PREFIX + name); |
| 66 | + } |
| 67 | + } |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + protected void handleMissingValue(List<String> headerName, MethodParameter parameter, Message<?> message) { |
| 73 | + throw new MessageHandlingException(message, |
| 74 | + "Missing headers '" + StringUtils.collectionToCommaDelimitedString(headerName) |
| 75 | + + "' for method parameter type [" + parameter.getParameterType() + "]"); |
| 76 | + } |
| 77 | + |
| 78 | + private static final class HeaderNamedValueInfo extends NamedValueInfo { |
| 79 | + |
| 80 | + private HeaderNamedValueInfo(Option annotation, List<String> names) { |
| 81 | + super(names, false, null); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments