1
|
|
|
package com.hltech.pact.gen.domain.client.util; |
2
|
|
|
|
3
|
|
|
import com.hltech.pact.gen.domain.client.model.Param; |
4
|
|
|
import org.springframework.http.HttpHeaders; |
5
|
|
|
import org.springframework.util.MultiValueMap; |
6
|
|
|
import org.springframework.web.bind.annotation.RequestHeader; |
7
|
|
|
import org.springframework.web.bind.annotation.ValueConstants; |
8
|
|
|
|
9
|
|
|
import java.lang.reflect.Method; |
10
|
|
|
import java.lang.reflect.Parameter; |
11
|
|
|
import java.util.Arrays; |
12
|
|
|
import java.util.List; |
13
|
|
|
import java.util.Map; |
14
|
|
|
import java.util.Optional; |
15
|
|
|
import java.util.stream.Collectors; |
16
|
|
|
|
17
|
|
|
public final class RequestHeaderParamsExtractor { |
18
|
|
|
|
19
|
|
|
private RequestHeaderParamsExtractor() {} |
20
|
|
|
|
21
|
|
|
public static List<Param> extractAll(Method feignClientMethod) { |
22
|
|
|
return Arrays.stream(feignClientMethod.getParameters()) |
23
|
|
|
.filter(param -> param.getAnnotation(RequestHeader.class) != null) |
24
|
|
|
.filter(param -> param.getType() != Map.class |
25
|
|
|
&& param.getType() != MultiValueMap.class |
26
|
|
|
&& param.getType() != HttpHeaders.class) |
27
|
|
|
.map(RequestHeaderParamsExtractor::extract) |
28
|
|
|
.collect(Collectors.toList()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private static Param extract(Parameter param) { |
32
|
|
|
Param.ParamBuilder builder = Param.builder(); |
33
|
|
|
|
34
|
|
|
extractHeaderDefaultValue(param).ifPresent(builder::defaultValue); |
35
|
|
|
|
36
|
|
|
List<Class<?>> paramTypes = TypeExtractor.extractParameterTypesFromType(param.getParameterizedType()); |
37
|
|
|
|
38
|
|
|
return builder |
39
|
|
|
.name(extractHeaderName(param)) |
40
|
|
|
.type(param.getType()) |
41
|
|
|
.genericArgumentType(paramTypes.isEmpty() ? null : paramTypes.get(0)) |
42
|
|
|
.build(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private static Optional<Object> extractHeaderDefaultValue(Parameter param) { |
46
|
|
|
RequestHeader annotation = param.getAnnotation(RequestHeader.class); |
47
|
|
|
|
48
|
|
|
if (annotation.defaultValue().equals(ValueConstants.DEFAULT_NONE) || annotation.defaultValue().isEmpty()) { |
49
|
|
|
return Optional.empty(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return Optional.of(annotation.defaultValue()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private static String extractHeaderName(Parameter param) { |
56
|
|
|
RequestHeader annotation = param.getAnnotation(RequestHeader.class); |
57
|
|
|
|
58
|
|
|
if (!annotation.name().isEmpty()) { |
59
|
|
|
return annotation.name(); |
60
|
|
|
} else if (!annotation.value().isEmpty()) { |
61
|
|
|
return annotation.value(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return param.getName(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|