| @@ 18-59 (lines=42) @@ | ||
| 15 | import java.util.List; |
|
| 16 | import java.util.stream.Collectors; |
|
| 17 | ||
| 18 | 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 | ||
| @@ 18-59 (lines=42) @@ | ||
| 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 | ||