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.Consumes; |
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.Optional; |
22
|
|
|
import java.util.stream.Collectors; |
23
|
|
|
|
24
|
|
|
@MappingMethodHandler |
25
|
|
|
public class JaxRsGetMappingMethodsHandler implements AnnotatedMethodHandler { |
26
|
|
|
|
27
|
|
|
//TODO Turn on scanning for JAX-RS related annotations when feature is complete |
28
|
|
|
@Override |
29
|
|
|
public boolean isSupported(Method method) { |
30
|
|
|
return false; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
@Override |
34
|
|
|
public RequestRepresentation handleRequest(Method method) { |
35
|
|
|
return RequestRepresentation.builder() |
36
|
|
|
.httpMethod(HttpMethod.GET) |
37
|
|
|
.path(extractPath(method)) |
38
|
|
|
.headers(extractHeaders(method)) |
39
|
|
|
.body(extractBody(method)) |
40
|
|
|
.requestParameters(extractRequestParameters(method)) |
41
|
|
|
.pathParameters(extractPathParameters(method)) |
42
|
|
|
.build(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
//TODO Implement resolving media headers |
46
|
|
|
@Override |
47
|
|
|
public String[] getResponseMediaHeaders(Method method) { |
48
|
|
|
return new String[0]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private List<Param> extractPathParameters(Method method) { |
52
|
|
|
return Arrays.stream(method.getParameters()) |
53
|
|
|
.filter(param -> param.getAnnotation(PathParam.class) != null) |
54
|
|
|
.map(this::fromPathParam) |
55
|
|
|
.collect(Collectors.toList()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private Param fromPathParam(Parameter parameter) { |
59
|
|
|
return Param.builder() |
60
|
|
|
.name(parameter.getAnnotation(PathParam.class).value()) |
61
|
|
|
.type(parameter.getType()) |
62
|
|
|
.build(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private List<Param> extractRequestParameters(Method method) { |
66
|
|
|
return Arrays.stream(method.getParameters()) |
67
|
|
|
.filter(param -> param.getAnnotation(QueryParam.class) != null) |
68
|
|
|
.map(this::fromQueryParam) |
69
|
|
|
.collect(Collectors.toList()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private Param fromQueryParam(Parameter parameter) { |
73
|
|
|
return Param.builder() |
74
|
|
|
.name(parameter.getAnnotation(QueryParam.class).value()) |
75
|
|
|
.type(parameter.getType()) |
76
|
|
|
.build(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private Body extractBody(Method method) { |
80
|
|
|
Optional<Parameter> requestBody = Arrays.stream(method.getParameters()) |
81
|
|
|
.filter(p -> p.getAnnotations().length == 0 || p.getAnnotation(Consumes.class) != null) |
82
|
|
|
.findFirst(); |
83
|
|
|
|
84
|
|
|
Body.BodyBuilder builder = Body.builder(); |
85
|
|
|
requestBody |
86
|
|
|
.map(Parameter::getType) |
87
|
|
|
.ifPresent(builder::type); |
88
|
|
|
requestBody |
89
|
|
|
.map(Parameter::getParameterizedType) |
90
|
|
|
.map(TypeExtractor::extractParameterTypesFromType) |
91
|
|
|
.ifPresent(builder::genericArgumentTypes); |
92
|
|
|
return builder.build(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
//TODO this method supports only headers added via annotation @HeaderParam.class |
96
|
|
|
private List<Param> extractHeaders(Method method) { |
97
|
|
|
return Arrays.stream(method.getParameters()) |
98
|
|
|
.filter(param -> param.getAnnotation(HeaderParam.class) != null) |
99
|
|
|
.map(param -> param.getAnnotation(HeaderParam.class)) |
100
|
|
|
.map(this::fromHeaderParam) |
101
|
|
|
.collect(Collectors.toList()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private Param fromHeaderParam(HeaderParam headerParam) { |
105
|
|
|
return Param.builder() |
106
|
|
|
.name(headerParam.value()) |
107
|
|
|
.build(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
private String extractPath(Method method) { |
111
|
|
|
Path annotation = method.getAnnotation(Path.class) != null |
112
|
|
|
? method.getAnnotation(Path.class) |
113
|
|
|
: method.getDeclaringClass().getAnnotation(Path.class); |
114
|
|
|
Assert.notNull(annotation, () -> String.format("Cannot find annotation %s on %s or %s", |
115
|
|
|
Path.class.getName(), ElementType.TYPE, ElementType.METHOD)); |
116
|
|
|
return annotation.value(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|