| 1 |  |  | package com.hltech.pact.gen.domain.client.util; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import com.hltech.pact.gen.domain.client.model.Param; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import org.springframework.web.bind.annotation.RequestParam; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import org.springframework.web.bind.annotation.ValueConstants; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | import java.lang.reflect.Parameter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | import java.util.Arrays; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | import java.util.List; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | import java.util.Map; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | import java.util.Optional; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | import java.util.stream.Collectors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | import java.util.stream.Stream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | public final class RequestParametersExtractor { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private RequestParametersExtractor() {} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     public static List<Param> extractAll(Parameter[] parameters, String[] path) { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         return Stream.concat(extractParamsFromMethod(parameters), extractParamsFromAnnotation(path)) | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |             .collect(Collectors.toList()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private static Stream<Param> extractParamsFromAnnotation(String[] path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         if (path.length < 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             return Stream.empty(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         String[] splitPath = path[0].split("\\?"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         if (splitPath.length < 2) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             return Stream.empty(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return Arrays.stream(splitPath[1].split("&")) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             .map(paramString -> Param.builder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 .name(paramString.split("=")[0]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 .defaultValue(paramString.split("=")[1]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 .build()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     private static Stream<Param> extractParamsFromMethod(Parameter[] parameters) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         return Arrays.stream(parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             .filter(param -> param.getAnnotation(RequestParam.class) != null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             .filter(param -> param.getType() != Map.class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             .map(RequestParametersExtractor::extractParam); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     private static Param extractParam(Parameter param) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         Param.ParamBuilder builder = Param.builder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         extractParamDefaultValue(param).ifPresent(builder::defaultValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         List<Class<?>> paramTypes = TypeExtractor.extractParameterTypesFromType(param.getParameterizedType()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         return builder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             .name(extractParamName(param)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             .type(param.getType()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             .genericArgumentType(paramTypes.isEmpty() ? null : paramTypes.get(0)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             .build(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     private static Optional<Object> extractParamDefaultValue(Parameter param) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         RequestParam annotation = param.getAnnotation(RequestParam.class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if (annotation.defaultValue().equals(ValueConstants.DEFAULT_NONE) || annotation.defaultValue().isEmpty()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             return Optional.empty(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return Optional.of(annotation.defaultValue()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     private static String extractParamName(Parameter param) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         RequestParam annotation = param.getAnnotation(RequestParam.class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         if (!annotation.name().isEmpty()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             return annotation.name(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } else if (!annotation.value().isEmpty()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             return annotation.value(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return param.getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |  |