| 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.Body; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | import com.hltech.pact.gen.domain.client.model.ClientMethodRepresentation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | import com.hltech.pact.gen.domain.client.model.Param; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | import com.hltech.pact.gen.domain.client.model.RequestRepresentation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | import com.hltech.pact.gen.domain.client.model.ResponseRepresentation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | import com.hltech.pact.gen.domain.client.util.RawHeadersParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | import com.hltech.pact.gen.domain.client.util.TypeExtractor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | import org.springframework.http.HttpStatus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | import java.lang.reflect.Method; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | import java.util.Arrays; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | import java.util.Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | import java.util.List; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | import java.util.stream.Collectors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | public class FeignMethodRepresentationExtractor implements ClientMethodRepresentationExtractor { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private final Collection<AnnotatedMethodHandler> annotatedMethodHandlers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public FeignMethodRepresentationExtractor(Collection<AnnotatedMethodHandler> annotatedMethodHandlers) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         this.annotatedMethodHandlers = annotatedMethodHandlers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     @Override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public ClientMethodRepresentation extractClientMethodRepresentation(Method clientMethod) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         return ClientMethodRepresentation.builder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             .requestRepresentation(extractRequestProperties(clientMethod)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             .responseRepresentationList(extractResponseProperties(clientMethod)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             .build(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     private RequestRepresentation extractRequestProperties(Method feignClientMethod) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         return this.annotatedMethodHandlers.stream() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             .filter(annotationHandler -> annotationHandler.isSupported(feignClientMethod)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             .findFirst().orElseThrow(() -> new IllegalArgumentException("Unknown HTTP method")) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             .handle(feignClientMethod); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     private static List<ResponseRepresentation> extractResponseProperties(Method feignClientMethod) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         feignClientMethod.getGenericReturnType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         List<ResponseRepresentation> results = Arrays | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             .stream(feignClientMethod.getDeclaredAnnotationsByType(InteractionInfo.class)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             .map(annotation -> populateResponse( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 annotation.responseStatus(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 RawHeadersParser.parseAll(annotation.responseHeaders()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 feignClientMethod, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 annotation.description())) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             .collect(Collectors.toList()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return !results.isEmpty() ? results : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             Lists.newArrayList(populateResponse(HttpStatus.OK, Lists.newArrayList(), feignClientMethod, "")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     private static ResponseRepresentation populateResponse( | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         HttpStatus status, List<Param> headers, Method feignClientMethod, String description) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         return ResponseRepresentation.builder() | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             .status(status) | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             .headers(headers) | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             .body(Body.builder() | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                 .type(feignClientMethod.getReturnType()) | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |                 .genericArgumentTypes( | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |                     TypeExtractor.extractParameterTypesFromType(feignClientMethod.getGenericReturnType())) | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |                 .build()) | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             .description(description) | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             .build(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |  |