|
1
|
|
|
package com.hltech.pact.gen.domain.client.jaxrs; |
|
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.feign.InteractionInfo; |
|
7
|
|
|
import com.hltech.pact.gen.domain.client.model.ClientMethodRepresentation; |
|
8
|
|
|
import com.hltech.pact.gen.domain.client.model.RequestRepresentation; |
|
9
|
|
|
import com.hltech.pact.gen.domain.client.model.ResponseRepresentation; |
|
10
|
|
|
import com.hltech.pact.gen.domain.client.util.RawHeadersParser; |
|
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
|
|
View Code Duplication |
public class JaxRsMethodRepresentationExtractor implements ClientMethodRepresentationExtractor { |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
private final Collection<AnnotatedMethodHandler> annotatedMethodHandlers; |
|
21
|
|
|
|
|
22
|
|
|
public JaxRsMethodRepresentationExtractor(Collection<AnnotatedMethodHandler> annotatedMethodHandlers) { |
|
23
|
|
|
this.annotatedMethodHandlers = annotatedMethodHandlers; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
@Override |
|
27
|
|
|
public ClientMethodRepresentation extractClientMethodRepresentation(Method clientMethod) { |
|
28
|
|
|
return ClientMethodRepresentation.builder() |
|
29
|
|
|
.requestRepresentation(extractRequestProperties(clientMethod)) |
|
30
|
|
|
.responseRepresentationList(extractResponseProperties(clientMethod)) |
|
31
|
|
|
.build(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
private RequestRepresentation extractRequestProperties(Method clientMethod) { |
|
35
|
|
|
return this.annotatedMethodHandlers.stream() |
|
36
|
|
|
.filter(annotationHandler -> annotationHandler.isSupported(clientMethod)) |
|
37
|
|
|
.findFirst().orElseThrow(() -> new IllegalArgumentException("Unknown HTTP method")) |
|
38
|
|
|
.handleRequest(clientMethod); |
|
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(annotation.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
|
|
|
|