|
| 1 | +/* |
| 2 | + * Copyright 2012-2015 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.boot.actuate.endpoint.mvc; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import org.springframework.boot.actuate.endpoint.Endpoint; |
| 28 | +import org.springframework.context.ApplicationContext; |
| 29 | +import org.springframework.util.Assert; |
| 30 | +import org.springframework.util.ObjectUtils; |
| 31 | +import org.springframework.util.StringUtils; |
| 32 | +import org.springframework.web.cors.CorsConfiguration; |
| 33 | +import org.springframework.web.servlet.HandlerMapping; |
| 34 | +import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition; |
| 35 | +import org.springframework.web.servlet.mvc.method.RequestMappingInfo; |
| 36 | +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; |
| 37 | + |
| 38 | +/** |
| 39 | + * {@link HandlerMapping} to map {@link Endpoint}s to URLs via {@link Endpoint#getId()}. |
| 40 | + * The semantics of {@code @RequestMapping} should be identical to a normal |
| 41 | + * {@code @Controller}, but the endpoints should not be annotated as {@code @Controller} |
| 42 | + * (otherwise they will be mapped by the normal MVC mechanisms). |
| 43 | + * <p> |
| 44 | + * One of the aims of the mapping is to support endpoints that work as HTTP endpoints but |
| 45 | + * can still provide useful service interfaces when there is no HTTP server (and no Spring |
| 46 | + * MVC on the classpath). Note that any endpoints having method signatures will break in a |
| 47 | + * non-servlet environment. |
| 48 | + * |
| 49 | + * @param <E> The endpoint type |
| 50 | + * @author Phillip Webb |
| 51 | + * @author Christian Dupuis |
| 52 | + * @author Dave Syer |
| 53 | + * @author Madhura Bhave |
| 54 | + */ |
| 55 | +public class AbstractEndpointHandlerMapping<E extends MvcEndpoint> |
| 56 | + extends RequestMappingHandlerMapping { |
| 57 | + |
| 58 | + private final Set<E> endpoints; |
| 59 | + |
| 60 | + private final CorsConfiguration corsConfiguration; |
| 61 | + |
| 62 | + private String prefix = ""; |
| 63 | + |
| 64 | + private boolean disabled = false; |
| 65 | + |
| 66 | + /** |
| 67 | + * Create a new {@link AbstractEndpointHandlerMapping} instance. All {@link Endpoint}s |
| 68 | + * will be detected from the {@link ApplicationContext}. The endpoints will not accept |
| 69 | + * CORS requests. |
| 70 | + * @param endpoints the endpoints |
| 71 | + */ |
| 72 | + public AbstractEndpointHandlerMapping(Collection<? extends E> endpoints) { |
| 73 | + this(endpoints, null); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Create a new {@link AbstractEndpointHandlerMapping} instance. All {@link Endpoint}s |
| 78 | + * will be detected from the {@link ApplicationContext}. The endpoints will accepts |
| 79 | + * CORS requests based on the given {@code corsConfiguration}. |
| 80 | + * @param endpoints the endpoints |
| 81 | + * @param corsConfiguration the CORS configuration for the endpoints |
| 82 | + * @since 1.3.0 |
| 83 | + */ |
| 84 | + public AbstractEndpointHandlerMapping(Collection<? extends E> endpoints, |
| 85 | + CorsConfiguration corsConfiguration) { |
| 86 | + this.endpoints = new HashSet<E>(endpoints); |
| 87 | + postProcessEndpoints(this.endpoints); |
| 88 | + this.corsConfiguration = corsConfiguration; |
| 89 | + // By default the static resource handler mapping is LOWEST_PRECEDENCE - 1 |
| 90 | + // and the RequestMappingHandlerMapping is 0 (we ideally want to be before both) |
| 91 | + setOrder(-100); |
| 92 | + setUseSuffixPatternMatch(false); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Post process the endpoint setting before they are used. Subclasses can add or |
| 97 | + * modify the endpoints as necessary. |
| 98 | + * @param endpoints the endpoints to post process |
| 99 | + */ |
| 100 | + protected void postProcessEndpoints(Set<E> endpoints) { |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void afterPropertiesSet() { |
| 105 | + super.afterPropertiesSet(); |
| 106 | + if (!this.disabled) { |
| 107 | + for (MvcEndpoint endpoint : this.endpoints) { |
| 108 | + detectHandlerMethods(endpoint); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Since all handler beans are passed into the constructor there is no need to detect |
| 115 | + * anything here. |
| 116 | + */ |
| 117 | + @Override |
| 118 | + protected boolean isHandler(Class<?> beanType) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + @Deprecated |
| 124 | + protected void registerHandlerMethod(Object handler, Method method, |
| 125 | + RequestMappingInfo mapping) { |
| 126 | + if (mapping == null) { |
| 127 | + return; |
| 128 | + } |
| 129 | + String[] patterns = getPatterns(handler, mapping); |
| 130 | + if (!ObjectUtils.isEmpty(patterns)) { |
| 131 | + super.registerHandlerMethod(handler, method, |
| 132 | + withNewPatterns(mapping, patterns)); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private String[] getPatterns(Object handler, RequestMappingInfo mapping) { |
| 137 | + if (handler instanceof String) { |
| 138 | + handler = getApplicationContext().getBean((String) handler); |
| 139 | + } |
| 140 | + Assert.state(handler instanceof MvcEndpoint, "Only MvcEndpoints are supported"); |
| 141 | + String path = getPath((MvcEndpoint) handler); |
| 142 | + return (path == null ? null : getEndpointPatterns(path, mapping)); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Return the path that should be used to map the given {@link MvcEndpoint}. |
| 147 | + * @param endpoint the endpoint to map |
| 148 | + * @return the path to use for the endpoint or {@code null} if no mapping is required |
| 149 | + */ |
| 150 | + protected String getPath(MvcEndpoint endpoint) { |
| 151 | + return endpoint.getPath(); |
| 152 | + } |
| 153 | + |
| 154 | + private String[] getEndpointPatterns(String path, RequestMappingInfo mapping) { |
| 155 | + String patternPrefix = StringUtils.hasText(this.prefix) ? this.prefix + path |
| 156 | + : path; |
| 157 | + Set<String> defaultPatterns = mapping.getPatternsCondition().getPatterns(); |
| 158 | + if (defaultPatterns.isEmpty()) { |
| 159 | + return new String[] { patternPrefix, patternPrefix + ".json" }; |
| 160 | + } |
| 161 | + List<String> patterns = new ArrayList<String>(defaultPatterns); |
| 162 | + for (int i = 0; i < patterns.size(); i++) { |
| 163 | + patterns.set(i, patternPrefix + patterns.get(i)); |
| 164 | + } |
| 165 | + return patterns.toArray(new String[patterns.size()]); |
| 166 | + } |
| 167 | + |
| 168 | + private RequestMappingInfo withNewPatterns(RequestMappingInfo mapping, |
| 169 | + String[] patternStrings) { |
| 170 | + PatternsRequestCondition patterns = new PatternsRequestCondition(patternStrings, |
| 171 | + null, null, useSuffixPatternMatch(), useTrailingSlashMatch(), null); |
| 172 | + return new RequestMappingInfo(patterns, mapping.getMethodsCondition(), |
| 173 | + mapping.getParamsCondition(), mapping.getHeadersCondition(), |
| 174 | + mapping.getConsumesCondition(), mapping.getProducesCondition(), |
| 175 | + mapping.getCustomCondition()); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Set the prefix used in mappings. |
| 180 | + * @param prefix the prefix |
| 181 | + */ |
| 182 | + public void setPrefix(String prefix) { |
| 183 | + Assert.isTrue("".equals(prefix) || StringUtils.startsWithIgnoreCase(prefix, "/"), |
| 184 | + "prefix must start with '/'"); |
| 185 | + this.prefix = prefix; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Get the prefix used in mappings. |
| 190 | + * @return the prefix |
| 191 | + */ |
| 192 | + public String getPrefix() { |
| 193 | + return this.prefix; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Get the path of the endpoint. |
| 198 | + * @param endpoint the endpoint |
| 199 | + * @return the path used in mappings |
| 200 | + */ |
| 201 | + public String getPath(String endpoint) { |
| 202 | + return this.prefix + endpoint; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Sets if this mapping is disabled. |
| 207 | + * @param disabled if the mapping is disabled |
| 208 | + */ |
| 209 | + public void setDisabled(boolean disabled) { |
| 210 | + this.disabled = disabled; |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Returns if this mapping is disabled. |
| 215 | + * @return if the mapping is disabled |
| 216 | + */ |
| 217 | + public boolean isDisabled() { |
| 218 | + return this.disabled; |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Return the endpoints. |
| 223 | + * @return the endpoints |
| 224 | + */ |
| 225 | + public Set<E> getEndpoints() { |
| 226 | + return Collections.unmodifiableSet(this.endpoints); |
| 227 | + } |
| 228 | + |
| 229 | + @Override |
| 230 | + protected CorsConfiguration initCorsConfiguration(Object handler, Method method, |
| 231 | + RequestMappingInfo mappingInfo) { |
| 232 | + return this.corsConfiguration; |
| 233 | + } |
| 234 | + |
| 235 | +} |
0 commit comments