|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2016 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.data.rest; |
| 20 | + |
| 21 | +import com.fasterxml.classmate.MemberResolver; |
| 22 | +import com.fasterxml.classmate.ResolvedType; |
| 23 | +import com.fasterxml.classmate.ResolvedTypeWithMembers; |
| 24 | +import com.fasterxml.classmate.TypeResolver; |
| 25 | +import com.fasterxml.classmate.members.ResolvedMethod; |
| 26 | +import com.google.common.base.Optional; |
| 27 | +import org.springframework.core.annotation.AnnotationUtils; |
| 28 | +import org.springframework.data.rest.core.mapping.ResourceMetadata; |
| 29 | +import org.springframework.http.MediaType; |
| 30 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 31 | +import org.springframework.web.method.HandlerMethod; |
| 32 | +import org.springframework.web.servlet.mvc.condition.NameValueExpression; |
| 33 | +import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition; |
| 34 | +import org.springframework.web.servlet.mvc.method.RequestMappingInfo; |
| 35 | +import springfox.documentation.RequestHandler; |
| 36 | +import springfox.documentation.RequestHandlerKey; |
| 37 | +import springfox.documentation.service.ResolvedMethodParameter; |
| 38 | + |
| 39 | +import java.lang.annotation.Annotation; |
| 40 | +import java.util.List; |
| 41 | +import java.util.Set; |
| 42 | + |
| 43 | +import static com.google.common.collect.Lists.*; |
| 44 | +import static com.google.common.collect.Sets.*; |
| 45 | + |
| 46 | +class EntitySchemaHandler implements RequestHandler { |
| 47 | + private final TypeResolver resolver; |
| 48 | + private final ResourceMetadata resource; |
| 49 | + private final RequestMappingInfo requestMapping; |
| 50 | + private final HandlerMethod handlerMethod; |
| 51 | + |
| 52 | + EntitySchemaHandler( |
| 53 | + TypeResolver resolver, |
| 54 | + ResourceMetadata resource, |
| 55 | + RequestMappingInfo requestMapping, |
| 56 | + HandlerMethod handlerMethod) { |
| 57 | + this.resolver = resolver; |
| 58 | + this.resource = resource; |
| 59 | + this.requestMapping = requestMapping; |
| 60 | + this.handlerMethod = handlerMethod; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Class<?> declaringClass() { |
| 65 | + return handlerMethod.getMethod().getDeclaringClass(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean isAnnotatedWith(Class<? extends Annotation> annotation) { |
| 70 | + return null != AnnotationUtils.findAnnotation(handlerMethod.getMethod(), annotation); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public PatternsRequestCondition getPatternsCondition() { |
| 75 | + Set<String> patterns = newHashSet(); |
| 76 | + for (String each : requestMapping.getPatternsCondition().getPatterns()) { |
| 77 | + String replaced = each |
| 78 | + .replace("/{repository}", resource.getPath().toString()); |
| 79 | + patterns.add(replaced); |
| 80 | + } |
| 81 | + return new PatternsRequestCondition(patterns.toArray(new String[patterns.size()])); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public String groupName() { |
| 86 | + return "Entity Schemas"; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public String getName() { |
| 91 | + return handlerMethod.getMethod().getName(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public Set<RequestMethod> supportedMethods() { |
| 96 | + return requestMapping.getMethodsCondition().getMethods(); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public Set<? extends MediaType> produces() { |
| 101 | + return requestMapping.getProducesCondition().getProducibleMediaTypes(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public Set<? extends MediaType> consumes() { |
| 106 | + return requestMapping.getConsumesCondition().getConsumableMediaTypes(); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public Set<NameValueExpression<String>> headers() { |
| 111 | + return requestMapping.getHeadersCondition().getExpressions(); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public Set<NameValueExpression<String>> params() { |
| 116 | + return requestMapping.getParamsCondition().getExpressions(); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public <T extends Annotation> Optional<T> findAnnotation(Class<T> annotation) { |
| 121 | + return Optional.fromNullable(AnnotationUtils.findAnnotation(handlerMethod.getMethod(), annotation)); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public RequestHandlerKey key() { |
| 126 | + return new RequestHandlerKey( |
| 127 | + getPatternsCondition().getPatterns(), |
| 128 | + supportedMethods(), |
| 129 | + produces(), |
| 130 | + consumes()); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public List<ResolvedMethodParameter> getParameters() { |
| 135 | + return newArrayList(); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public ResolvedType getReturnType() { |
| 140 | + MemberResolver memberResolver = new MemberResolver(resolver); |
| 141 | + ResolvedTypeWithMembers members = memberResolver.resolve( |
| 142 | + resolver.resolve(handlerMethod.getMethod().getDeclaringClass()), null, null); |
| 143 | + for (ResolvedMethod resolvedMethod : members.getMemberMethods()) { |
| 144 | + if (resolvedMethod.getRawMember().equals(handlerMethod.getMethod())) { |
| 145 | + return resolvedMethod.getReturnType(); |
| 146 | + } |
| 147 | + } |
| 148 | + return resolver.resolve(Void.TYPE); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public <T extends Annotation> Optional<T> findControllerAnnotation(Class<T> annotation) { |
| 153 | + return Optional.fromNullable(AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), annotation)); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public RequestMappingInfo getRequestMapping() { |
| 158 | + return requestMapping; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public HandlerMethod getHandlerMethod() { |
| 163 | + return handlerMethod; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public RequestHandler combine(RequestHandler other) { |
| 168 | + return this; |
| 169 | + } |
| 170 | +} |
0 commit comments