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.Param; |
5
|
|
|
import com.hltech.pact.gen.domain.client.model.RequestRepresentation; |
6
|
|
|
import com.hltech.pact.gen.domain.client.util.PathParametersExtractor; |
7
|
|
|
import com.hltech.pact.gen.domain.client.util.RawHeadersParser; |
8
|
|
|
import com.hltech.pact.gen.domain.client.util.RequestBodyExtractor; |
9
|
|
|
import com.hltech.pact.gen.domain.client.util.RequestHeaderParamsExtractor; |
10
|
|
|
import com.hltech.pact.gen.domain.client.util.RequestParametersExtractor; |
11
|
|
|
import org.apache.commons.lang3.ArrayUtils; |
12
|
|
|
import org.springframework.http.HttpHeaders; |
13
|
|
|
import org.springframework.http.HttpMethod; |
14
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
15
|
|
|
|
16
|
|
|
import java.lang.reflect.Method; |
17
|
|
|
import java.util.Arrays; |
18
|
|
|
import java.util.List; |
19
|
|
|
import java.util.stream.Collectors; |
20
|
|
|
import java.util.stream.Stream; |
21
|
|
|
|
22
|
|
View Code Duplication |
@MappingMethodHandler |
|
|
|
|
23
|
|
|
public class RequestMappingMethodsHandler implements AnnotatedMethodHandler { |
24
|
|
|
|
25
|
|
|
@Override |
26
|
|
|
public boolean isSupported(Method method) { |
27
|
|
|
return method.isAnnotationPresent(RequestMapping.class); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
@Override |
31
|
|
|
public RequestRepresentation handleRequest(Method method) { |
32
|
|
|
return RequestRepresentation.builder() |
33
|
|
|
.httpMethod(HttpMethod.resolve(method.getAnnotation(RequestMapping.class) |
34
|
|
|
.method()[0].name().toUpperCase())) |
35
|
|
|
.path(getPathFromMethod(method)) |
36
|
|
|
.headers(combineHeaders( |
37
|
|
|
ArrayUtils.addAll(method.getAnnotation(RequestMapping.class).headers(), getRequestMediaHeaders(method)), |
38
|
|
|
RequestHeaderParamsExtractor.extractAll(method))) |
39
|
|
|
.body(RequestBodyExtractor.extract(method.getParameters())) |
40
|
|
|
.requestParameters(RequestParametersExtractor.extractAll( |
41
|
|
|
method.getParameters(), |
42
|
|
|
method.getAnnotation(RequestMapping.class).path())) |
43
|
|
|
.pathParameters(PathParametersExtractor.extractAll(method)) |
44
|
|
|
.build(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
@Override |
48
|
|
|
public String[] getResponseMediaHeaders(Method method) { |
49
|
|
|
return Arrays.stream(method.getAnnotation(RequestMapping.class).produces()) |
50
|
|
|
.map(header -> HttpHeaders.CONTENT_TYPE + "=" + header) |
51
|
|
|
.toArray(String[]::new); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private String[] getRequestMediaHeaders(Method method) { |
55
|
|
|
return ArrayUtils.addAll( |
56
|
|
|
Arrays.stream(method.getAnnotation(RequestMapping.class).consumes()) |
57
|
|
|
.map(header -> HttpHeaders.CONTENT_TYPE + "=" + header) |
58
|
|
|
.toArray(String[]::new), |
59
|
|
|
Arrays.stream(method.getAnnotation(RequestMapping.class).produces()) |
60
|
|
|
.map(header -> HttpHeaders.ACCEPT + "=" + header) |
61
|
|
|
.toArray(String[]::new)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private String getPathFromMethod(Method method) { |
65
|
|
|
RequestMapping annotation = method.getAnnotation(RequestMapping.class); |
66
|
|
|
return annotation.path().length == 1 |
67
|
|
|
? annotation.path()[0].split("\\?")[0] |
68
|
|
|
: annotation.value()[0].split("\\?")[0]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private static List<Param> combineHeaders(String[] rawHeaders, List<Param> headers) { |
72
|
|
|
return Stream |
73
|
|
|
.concat(RawHeadersParser.parseAll(rawHeaders).stream(), headers.stream()) |
74
|
|
|
.collect(Collectors.toList()); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|