|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 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 | + * 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 org.springframework.web.servlet.function.support; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import javax.servlet.http.HttpServletRequest; |
| 24 | + |
| 25 | +import org.jetbrains.annotations.NotNull; |
| 26 | + |
| 27 | +import org.springframework.beans.factory.BeanFactoryUtils; |
| 28 | +import org.springframework.beans.factory.InitializingBean; |
| 29 | +import org.springframework.context.ApplicationContext; |
| 30 | +import org.springframework.http.converter.ByteArrayHttpMessageConverter; |
| 31 | +import org.springframework.http.converter.HttpMessageConverter; |
| 32 | +import org.springframework.http.converter.StringHttpMessageConverter; |
| 33 | +import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter; |
| 34 | +import org.springframework.http.converter.xml.SourceHttpMessageConverter; |
| 35 | +import org.springframework.lang.Nullable; |
| 36 | +import org.springframework.util.CollectionUtils; |
| 37 | +import org.springframework.web.servlet.function.RouterFunction; |
| 38 | +import org.springframework.web.servlet.function.RouterFunctions; |
| 39 | +import org.springframework.web.servlet.function.ServerRequest; |
| 40 | +import org.springframework.web.servlet.handler.AbstractHandlerMapping; |
| 41 | + |
| 42 | +/** |
| 43 | + * {@code HandlerMapping} implementation that supports {@link RouterFunction}s. |
| 44 | + * <p>If no {@link RouterFunction} is provided at |
| 45 | + * {@linkplain #RouterFunctionMapping(RouterFunction) construction time}, this mapping will detect |
| 46 | + * all router functions in the application context, and consult them in |
| 47 | + * {@linkplain org.springframework.core.annotation.Order order}. |
| 48 | + * |
| 49 | + * @author Arjen Poutsma |
| 50 | + * @since 5.2 |
| 51 | + */ |
| 52 | +public class RouterFunctionMapping extends AbstractHandlerMapping implements InitializingBean { |
| 53 | + |
| 54 | + @Nullable |
| 55 | + private RouterFunction<?> routerFunction; |
| 56 | + |
| 57 | + private List<HttpMessageConverter<?>> messageConverters = Collections.emptyList(); |
| 58 | + |
| 59 | + private boolean detectHandlerFunctionsInAncestorContexts = false; |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + /** |
| 64 | + * Create an empty {@code RouterFunctionMapping}. |
| 65 | + * <p>If this constructor is used, this mapping will detect all {@link RouterFunction} instances |
| 66 | + * available in the application context. |
| 67 | + */ |
| 68 | + public RouterFunctionMapping() { |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Create a {@code RouterFunctionMapping} with the given {@link RouterFunction}. |
| 73 | + * <p>If this constructor is used, no application context detection will occur. |
| 74 | + * @param routerFunction the router function to use for mapping |
| 75 | + */ |
| 76 | + public RouterFunctionMapping(RouterFunction<?> routerFunction) { |
| 77 | + this.routerFunction = routerFunction; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Set the router function to map to. |
| 82 | + * <p>If this property is used, no application context detection will occur. |
| 83 | + */ |
| 84 | + public void setRouterFunction(@Nullable RouterFunction<?> routerFunction) { |
| 85 | + this.routerFunction = routerFunction; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Return the configured {@link RouterFunction}. |
| 90 | + * <p><strong>Note:</strong> When router functions are detected from the |
| 91 | + * ApplicationContext, this method may return {@code null} if invoked |
| 92 | + * prior to {@link #afterPropertiesSet()}. |
| 93 | + * @return the router function or {@code null} |
| 94 | + */ |
| 95 | + @Nullable |
| 96 | + public RouterFunction<?> getRouterFunction() { |
| 97 | + return this.routerFunction; |
| 98 | + } |
| 99 | + |
| 100 | + public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters) { |
| 101 | + this.messageConverters = messageConverters; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Set whether to detect handler functions in ancestor ApplicationContexts. |
| 106 | + * <p>Default is "false": Only handler functions in the current ApplicationContext |
| 107 | + * will be detected, i.e. only in the context that this HandlerMapping itself |
| 108 | + * is defined in (typically the current DispatcherServlet's context). |
| 109 | + * <p>Switch this flag on to detect handler beans in ancestor contexts |
| 110 | + * (typically the Spring root WebApplicationContext) as well. |
| 111 | + */ |
| 112 | + public void setDetectHandlerFunctionsInAncestorContexts(boolean detectHandlerFunctionsInAncestorContexts) { |
| 113 | + this.detectHandlerFunctionsInAncestorContexts = detectHandlerFunctionsInAncestorContexts; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void afterPropertiesSet() throws Exception { |
| 118 | + if (this.routerFunction == null) { |
| 119 | + initRouterFunction(); |
| 120 | + } |
| 121 | + if (CollectionUtils.isEmpty(this.messageConverters)) { |
| 122 | + initMessageConverters(); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Detect a all {@linkplain RouterFunction router functions} in the |
| 128 | + * current application context. |
| 129 | + */ |
| 130 | + @SuppressWarnings({"unchecked", "rawtypes"}) |
| 131 | + private void initRouterFunction() { |
| 132 | + ApplicationContext applicationContext = obtainApplicationContext(); |
| 133 | + Map<String, RouterFunction> beans = |
| 134 | + (this.detectHandlerFunctionsInAncestorContexts ? |
| 135 | + BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, RouterFunction.class) : |
| 136 | + applicationContext.getBeansOfType(RouterFunction.class)); |
| 137 | + |
| 138 | + List<RouterFunction> routerFunctions = new ArrayList<>(beans.values()); |
| 139 | + if (!CollectionUtils.isEmpty(routerFunctions) && logger.isInfoEnabled()) { |
| 140 | + routerFunctions.forEach(routerFunction -> logger.info("Mapped " + routerFunction)); |
| 141 | + } |
| 142 | + this.routerFunction = routerFunctions.stream() |
| 143 | + .reduce(RouterFunction::andOther) |
| 144 | + .orElse(null); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Initializes a default set of {@linkplain HttpMessageConverter message converters}. |
| 149 | + */ |
| 150 | + private void initMessageConverters() { |
| 151 | + List<HttpMessageConverter<?>> messageConverters = new ArrayList<>(4); |
| 152 | + |
| 153 | + messageConverters.add(new ByteArrayHttpMessageConverter()); |
| 154 | + |
| 155 | + StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(); |
| 156 | + stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316 |
| 157 | + messageConverters.add(stringHttpMessageConverter); |
| 158 | + |
| 159 | + try { |
| 160 | + messageConverters.add(new SourceHttpMessageConverter<>()); |
| 161 | + } |
| 162 | + catch (Error err) { |
| 163 | + // Ignore when no TransformerFactory implementation is available |
| 164 | + } |
| 165 | + messageConverters.add(new AllEncompassingFormHttpMessageConverter()); |
| 166 | + |
| 167 | + this.messageConverters = messageConverters; |
| 168 | + } |
| 169 | + |
| 170 | + @Nullable |
| 171 | + @Override |
| 172 | + protected Object getHandlerInternal(@NotNull HttpServletRequest servletRequest) throws Exception { |
| 173 | + if (this.routerFunction != null) { |
| 174 | + ServerRequest request = ServerRequest.create(servletRequest, this.messageConverters); |
| 175 | + servletRequest.setAttribute(RouterFunctions.REQUEST_ATTRIBUTE, request); |
| 176 | + return this.routerFunction.route(request).orElse(null); |
| 177 | + } |
| 178 | + else { |
| 179 | + return null; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments