|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2017-2018 the original author or authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + * |
| 18 | + */ |
| 19 | +package springfox.documentation.spring.web; |
| 20 | + |
| 21 | +import com.google.common.base.Function; |
| 22 | +import com.google.common.base.Optional; |
| 23 | +import org.springframework.core.Ordered; |
| 24 | +import org.springframework.core.annotation.AnnotationUtils; |
| 25 | +import org.springframework.core.env.Environment; |
| 26 | +import org.springframework.stereotype.Controller; |
| 27 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 28 | +import org.springframework.web.method.HandlerMethod; |
| 29 | +import org.springframework.web.servlet.HandlerMapping; |
| 30 | +import org.springframework.web.servlet.mvc.method.RequestMappingInfo; |
| 31 | +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; |
| 32 | +import org.springframework.web.util.UriTemplate; |
| 33 | + |
| 34 | +import javax.servlet.http.HttpServletRequest; |
| 35 | +import java.lang.reflect.Method; |
| 36 | +import java.util.LinkedHashMap; |
| 37 | +import java.util.Map; |
| 38 | + |
| 39 | +public class PropertySourcedRequestMappingHandlerMapping extends RequestMappingHandlerMapping { |
| 40 | + |
| 41 | + private final Map<String, HandlerMethod> handlerMethods = new LinkedHashMap<String, HandlerMethod>(); |
| 42 | + private final Environment environment; |
| 43 | + private final Object handler; |
| 44 | + |
| 45 | + public PropertySourcedRequestMappingHandlerMapping( |
| 46 | + Environment environment, |
| 47 | + Object handler) { |
| 48 | + this.environment = environment; |
| 49 | + this.handler = handler; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected void initHandlerMethods() { |
| 54 | + logger.debug("initialising the handler methods"); |
| 55 | + setOrder(Ordered.HIGHEST_PRECEDENCE + 1000); |
| 56 | + Class<?> clazz = handler.getClass(); |
| 57 | + if (isHandler(clazz)) { |
| 58 | + for (Method method : clazz.getMethods()) { |
| 59 | + PropertySourcedMapping mapper = AnnotationUtils.getAnnotation(method, PropertySourcedMapping.class); |
| 60 | + if (mapper != null) { |
| 61 | + RequestMappingInfo mapping = getMappingForMethod(method, clazz); |
| 62 | + HandlerMethod handlerMethod = createHandlerMethod(handler, method); |
| 63 | + String mappingPath = mappingPath(mapper); |
| 64 | + if (mappingPath != null) { |
| 65 | + logger.info(String.format("Mapped URL path [%s] onto method [%s]", mappingPath, handlerMethod.toString())); |
| 66 | + handlerMethods.put(mappingPath, handlerMethod); |
| 67 | + } else { |
| 68 | + for (String path : mapping.getPatternsCondition().getPatterns()) { |
| 69 | + logger.info(String.format("Mapped URL path [%s] onto method [%s]", path, handlerMethod.toString())); |
| 70 | + handlerMethods.put(path, handlerMethod); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private String mappingPath(final PropertySourcedMapping mapper) { |
| 79 | + final String key = mapper.value(); |
| 80 | + return Optional.fromNullable(environment.getProperty(key)) |
| 81 | + .transform(new Function<String, String>() { |
| 82 | + @Override |
| 83 | + public String apply(String input) { |
| 84 | + return input.replace(String.format("${%s}", key), input); |
| 85 | + } |
| 86 | + }) |
| 87 | + .orNull(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + protected boolean isHandler(Class<?> beanType) { |
| 92 | + return ((AnnotationUtils.findAnnotation(beanType, Controller.class) != null) || |
| 93 | + (AnnotationUtils.findAnnotation(beanType, RequestMapping.class) != null)); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * The lookup handler method, maps the SEOMapper method to the request URL. |
| 98 | + * <p>If no mapping is found, or if the URL is disabled, it will simply drop throug |
| 99 | + * to the standard 404 handling.</p> |
| 100 | + * |
| 101 | + * @param urlPath the path to match. |
| 102 | + * @param request the http servlet request. |
| 103 | + * @return The HandlerMethod if one was found. |
| 104 | + * @throws Exception |
| 105 | + */ |
| 106 | + @Override |
| 107 | + protected HandlerMethod lookupHandlerMethod(String urlPath, HttpServletRequest request) throws Exception { |
| 108 | + logger.debug("looking up handler for path: " + urlPath); |
| 109 | + HandlerMethod handlerMethod = handlerMethods.get(urlPath); |
| 110 | + if (handlerMethod != null) { |
| 111 | + return handlerMethod; |
| 112 | + } |
| 113 | + for (String path : handlerMethods.keySet()) { |
| 114 | + UriTemplate template = new UriTemplate(path); |
| 115 | + if (template.matches(urlPath)) { |
| 116 | + request.setAttribute( |
| 117 | + HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, |
| 118 | + template.match(urlPath)); |
| 119 | + return handlerMethods.get(path); |
| 120 | + } |
| 121 | + } |
| 122 | + return null; |
| 123 | + } |
| 124 | +} |
0 commit comments