View Javadoc
1   package cn.home1.oss.lib.swagger;
2   
3   import static com.google.common.collect.Sets.newHashSet;
4   import static lombok.AccessLevel.PRIVATE;
5   
6   import com.google.common.base.Optional;
7   
8   import cn.home1.oss.lib.swagger.model.ApiOperationInfo;
9   
10  import com.fasterxml.classmate.ResolvedType;
11  
12  import lombok.AllArgsConstructor;
13  import lombok.Builder;
14  import lombok.Getter;
15  
16  import org.springframework.http.MediaType;
17  import org.springframework.web.bind.annotation.RequestMethod;
18  import org.springframework.web.method.HandlerMethod;
19  import org.springframework.web.servlet.mvc.condition.ConsumesRequestCondition;
20  import org.springframework.web.servlet.mvc.condition.HeadersRequestCondition;
21  import org.springframework.web.servlet.mvc.condition.NameValueExpression;
22  import org.springframework.web.servlet.mvc.condition.ParamsRequestCondition;
23  import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
24  import org.springframework.web.servlet.mvc.condition.ProducesRequestCondition;
25  import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
26  import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
27  
28  import springfox.documentation.RequestHandler;
29  import springfox.documentation.RequestHandlerKey;
30  import springfox.documentation.service.ResolvedMethodParameter;
31  
32  import java.lang.annotation.Annotation;
33  import java.util.List;
34  import java.util.Set;
35  
36  @Builder(builderMethodName = "requestHandlerBuilder")
37  @AllArgsConstructor(access = PRIVATE)
38  public class ManualRequestHandler implements RequestHandler {
39  
40    @Getter
41    private final ApiOperationInfo apiOperationInfo;
42  
43    private final ConsumesRequestCondition consumes;
44    private final Class<?> declaringClass;
45    private final String groupName;
46    private final HeadersRequestCondition headers;
47    /**
48     * method parameters.
49     */
50    private final List<ResolvedMethodParameter> parameters;
51    /**
52     * params condition expression.
53     */
54    private final ParamsRequestCondition params;
55    /**
56     * path exporession.
57     */
58    private final PatternsRequestCondition patternsCondition;
59    private final ProducesRequestCondition produces;
60    private final ResolvedType returnType;
61    private final RequestMethodsRequestCondition supportedMethods;
62  
63    @Override
64    public Class<?> declaringClass() {
65      return this.declaringClass;
66    }
67  
68    @Override
69    public boolean isAnnotatedWith(final Class<? extends Annotation> clazz) {
70      return false;
71    }
72  
73    @Override
74    public PatternsRequestCondition getPatternsCondition() {
75      return this.patternsCondition;
76    }
77  
78    @Override
79    public String groupName() {
80      return this.groupName;
81    }
82  
83    @Override
84    public String getName() {
85      return this.getClass().getSimpleName();
86    }
87  
88    @Override
89    public Set<RequestMethod> supportedMethods() {
90      return this.supportedMethods != null ? this.supportedMethods.getMethods() : newHashSet();
91    }
92  
93    @Override
94    public Set<? extends MediaType> produces() {
95      return this.produces != null ? this.produces.getProducibleMediaTypes() : newHashSet();
96    }
97  
98    @Override
99    public Set<? extends MediaType> consumes() {
100     return this.consumes != null ? this.consumes.getConsumableMediaTypes() : newHashSet();
101   }
102 
103   @Override
104   public Set<NameValueExpression<String>> headers() {
105     return this.headers != null ? this.headers.getExpressions() : newHashSet();
106   }
107 
108   @Override
109   public Set<NameValueExpression<String>> params() {
110     return this.params != null ? this.params.getExpressions() : newHashSet();
111   }
112 
113   @Override
114   public <T extends Annotation> Optional<T> findAnnotation(final Class<T> clazz) {
115     return Optional.absent();
116   }
117 
118   @Override
119   public RequestHandlerKey key() {
120     return new RequestHandlerKey(
121       this.getPatternsCondition().getPatterns(),
122       this.supportedMethods(),
123       this.consumes(),
124       this.produces());
125   }
126 
127   @Override
128   public List<ResolvedMethodParameter> getParameters() {
129     return this.parameters;
130   }
131 
132   @Override
133   public ResolvedType getReturnType() {
134     return this.returnType;
135   }
136 
137   @Override
138   public <T extends Annotation> Optional<T> findControllerAnnotation(final Class<T> clazz) {
139     return Optional.absent();
140   }
141 
142   @Deprecated
143   @Override
144   public RequestMappingInfo getRequestMapping() {
145     return new RequestMappingInfo(
146       this.patternsCondition,
147       this.supportedMethods,
148       this.params,
149       this.headers,
150       this.consumes,
151       this.produces,
152       null
153     );
154   }
155 
156   @Deprecated
157   @Override
158   public HandlerMethod getHandlerMethod() {
159     return null;
160   }
161 
162   @Override
163   public RequestHandler combine(final RequestHandler requestHandler) {
164     return this;
165   }
166 }