1
|
|
|
package com.hltech.pact.gen.domain.client.annotation.handlers; |
2
|
|
|
|
3
|
|
|
import com.hltech.pact.gen.domain.client.annotation.MappingMethodHandler; |
4
|
|
|
import com.hltech.pact.gen.domain.client.model.Body; |
5
|
|
|
import com.hltech.pact.gen.domain.client.model.Param; |
6
|
|
|
import com.hltech.pact.gen.domain.client.model.RequestRepresentation; |
7
|
|
|
import com.hltech.pact.gen.domain.client.util.TypeExtractor; |
8
|
|
|
import org.springframework.http.HttpMethod; |
9
|
|
|
import org.springframework.util.Assert; |
10
|
|
|
|
11
|
|
|
import javax.ws.rs.GET; |
12
|
|
|
import javax.ws.rs.HeaderParam; |
13
|
|
|
import javax.ws.rs.Path; |
14
|
|
|
import javax.ws.rs.PathParam; |
15
|
|
|
import javax.ws.rs.QueryParam; |
16
|
|
|
import java.lang.annotation.ElementType; |
17
|
|
|
import java.lang.reflect.Method; |
18
|
|
|
import java.lang.reflect.Parameter; |
19
|
|
|
import java.util.Arrays; |
20
|
|
|
import java.util.List; |
21
|
|
|
import java.util.stream.Collectors; |
22
|
|
|
|
23
|
|
|
@MappingMethodHandler |
24
|
|
|
public class JaxRsGetMappingMethodsHandler implements AnnotatedMethodHandler { |
25
|
|
|
|
26
|
|
|
@Override |
27
|
|
|
public boolean isSupported(Method method) { |
28
|
|
|
return method.isAnnotationPresent(GET.class); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
@Override |
32
|
|
|
public RequestRepresentation handleRequest(Method method) { |
33
|
|
|
return RequestRepresentation.builder() |
34
|
|
|
.httpMethod(HttpMethod.GET) |
35
|
|
|
.path(extractPath(method)) |
36
|
|
|
.headers(extractHeaders(method)) |
37
|
|
|
.body(extractBody(method)) |
38
|
|
|
.requestParameters(extractRequestParameters(method)) |
39
|
|
|
.pathParameters(extractPathParameters(method)) |
40
|
|
|
.build(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
@Override |
44
|
|
|
public String[] getResponseMediaHeaders(Method method) { |
45
|
|
|
return new String[0]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private List<Param> extractPathParameters(Method method) { |
49
|
|
|
return Arrays.stream(method.getParameters()) |
50
|
|
|
.filter(param -> param.getAnnotation(PathParam.class) != null) |
51
|
|
|
.map(this::fromPathParam) |
52
|
|
|
.collect(Collectors.toList()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private Param fromPathParam(Parameter parameter) { |
56
|
|
|
return Param.builder() |
57
|
|
|
.name(parameter.getAnnotation(PathParam.class).value()) |
58
|
|
|
.type(parameter.getType()) |
59
|
|
|
.build(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private List<Param> extractRequestParameters(Method method) { |
63
|
|
|
return Arrays.stream(method.getParameters()) |
64
|
|
|
.filter(param -> param.getAnnotation(QueryParam.class) != null) |
65
|
|
|
.map(this::fromQueryParam) |
66
|
|
|
.collect(Collectors.toList()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private Param fromQueryParam(Parameter parameter) { |
70
|
|
|
return Param.builder() |
71
|
|
|
.name(parameter.getAnnotation(QueryParam.class).value()) |
72
|
|
|
.type(parameter.getType()) |
73
|
|
|
.build(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private Body extractBody(Method method) { |
77
|
|
|
Parameter requestBody = Arrays.stream(method.getParameters()) |
78
|
|
|
.filter(parameter -> parameter.getAnnotations().length == 0) |
79
|
|
|
.findFirst() |
80
|
|
|
.orElse(null); |
81
|
|
|
|
82
|
|
|
return Body.builder() |
83
|
|
|
.type(requestBody != null ? requestBody.getType() : null) |
84
|
|
|
.genericArgumentTypes(TypeExtractor.extractParameterTypesFromType( |
85
|
|
|
requestBody != null ? requestBody.getParameterizedType() : null)) |
86
|
|
|
.build(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
//TODO this method supports only headers added via annotation @HeaderParam.class |
90
|
|
|
private List<Param> extractHeaders(Method method) { |
91
|
|
|
return Arrays.stream(method.getParameters()) |
92
|
|
|
.filter(param -> param.getAnnotation(HeaderParam.class) != null) |
93
|
|
|
.map(param -> param.getAnnotation(HeaderParam.class)) |
94
|
|
|
.map(headerParam -> Param |
95
|
|
|
.builder() |
96
|
|
|
.name(headerParam.value()) |
97
|
|
|
.build()) |
98
|
|
|
.collect(Collectors.toList()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private String extractPath(Method method) { |
102
|
|
|
Path annotation = method.getAnnotation(Path.class) != null |
103
|
|
|
? method.getAnnotation(Path.class) |
104
|
|
|
: method.getDeclaringClass().getAnnotation(Path.class); |
105
|
|
|
Assert.notNull(annotation, () -> String.format("Cannot find annotation %s on %s or %s", |
106
|
|
|
Path.class.getName(), ElementType.TYPE, ElementType.METHOD)); |
107
|
|
|
return annotation.value(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|