1
|
|
|
package com.hltech.pact.gen.domain.client.feign; |
2
|
|
|
|
3
|
|
|
import com.google.common.collect.Lists; |
4
|
|
|
import com.hltech.pact.gen.domain.client.ClientMethodRepresentationExtractor; |
5
|
|
|
import com.hltech.pact.gen.domain.client.annotation.handlers.AnnotatedMethodHandler; |
6
|
|
|
import com.hltech.pact.gen.domain.client.model.ClientMethodRepresentation; |
7
|
|
|
import com.hltech.pact.gen.domain.client.model.RequestRepresentation; |
8
|
|
|
import com.hltech.pact.gen.domain.client.model.ResponseRepresentation; |
9
|
|
|
import com.hltech.pact.gen.domain.client.util.RawHeadersParser; |
10
|
|
|
import org.apache.commons.lang3.ArrayUtils; |
11
|
|
|
|
12
|
|
|
import java.lang.reflect.Method; |
13
|
|
|
import java.util.Arrays; |
14
|
|
|
import java.util.Collection; |
15
|
|
|
import java.util.List; |
16
|
|
|
import java.util.stream.Collectors; |
17
|
|
|
|
18
|
|
|
public class FeignMethodRepresentationExtractor implements ClientMethodRepresentationExtractor { |
19
|
|
|
|
20
|
|
|
private final Collection<AnnotatedMethodHandler> annotatedMethodHandlers; |
21
|
|
|
|
22
|
|
|
public FeignMethodRepresentationExtractor(Collection<AnnotatedMethodHandler> annotatedMethodHandlers) { |
23
|
|
|
this.annotatedMethodHandlers = annotatedMethodHandlers; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
@Override |
27
|
|
|
public ClientMethodRepresentation extractClientMethodRepresentation(Method method) { |
28
|
|
|
return ClientMethodRepresentation.builder() |
29
|
|
|
.requestRepresentation(extractRequestProperties(method)) |
30
|
|
|
.responseRepresentationList(extractResponseProperties(method)) |
31
|
|
|
.build(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private RequestRepresentation extractRequestProperties(Method method) { |
35
|
|
|
return this.annotatedMethodHandlers.stream() |
36
|
|
|
.filter(annotationHandler -> annotationHandler.isSupported(method)) |
37
|
|
|
.findFirst().orElseThrow(() -> new IllegalArgumentException("Unknown HTTP method")) |
38
|
|
|
.handleRequest(method); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private List<ResponseRepresentation> extractResponseProperties(Method method) { |
42
|
|
|
String[] responseHeaders = this.annotatedMethodHandlers.stream() |
43
|
|
|
.filter(annotationHandler -> annotationHandler.isSupported(method)) |
44
|
|
|
.findFirst().orElseThrow(() -> new IllegalArgumentException("Unknown HTTP method")) |
45
|
|
|
.getResponseMediaHeaders(method); |
46
|
|
|
|
47
|
|
|
List<ResponseRepresentation> results = Arrays |
48
|
|
|
.stream(method.getDeclaredAnnotationsByType(InteractionInfo.class)) |
49
|
|
|
.map(annotation -> ResponseRepresentation.from( |
50
|
|
|
annotation.responseStatus(), |
51
|
|
|
RawHeadersParser.parseAll(ArrayUtils.addAll(annotation.responseHeaders(), responseHeaders)), |
52
|
|
|
method, |
53
|
|
|
annotation.description(), |
54
|
|
|
annotation.emptyBodyExpected())) |
55
|
|
|
.collect(Collectors.toList()); |
56
|
|
|
|
57
|
|
|
return !results.isEmpty() |
58
|
|
|
? results |
59
|
|
|
: Lists.newArrayList(ResponseRepresentation.getDefaultForMethod(method, responseHeaders)); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|