| 1 |  |  | package com.hltech.pact.gen.domain.client.annotation.handlers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import com.hltech.pact.gen.domain.client.annotation.MappingMethodHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import com.hltech.pact.gen.domain.client.model.Param; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import com.hltech.pact.gen.domain.client.model.RequestRepresentation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | import com.hltech.pact.gen.domain.client.util.PathParametersExtractor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | import com.hltech.pact.gen.domain.client.util.RawHeadersParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | import com.hltech.pact.gen.domain.client.util.RequestBodyExtractor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | import com.hltech.pact.gen.domain.client.util.RequestHeaderParamsExtractor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | import com.hltech.pact.gen.domain.client.util.RequestParametersExtractor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | import org.apache.commons.lang3.ArrayUtils; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | import org.springframework.http.HttpHeaders; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | import org.springframework.http.HttpMethod; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | import org.springframework.web.bind.annotation.DeleteMapping; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | import java.lang.reflect.Method; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | import java.util.Arrays; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | import java.util.List; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | import java.util.stream.Collectors; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | import java.util.stream.Stream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 22 |  | View Code Duplication | @MappingMethodHandler | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | public class DeleteMappingMethodsHandler implements AnnotatedMethodHandler { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     @Override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public boolean isSupported(Method method) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         return method.isAnnotationPresent(DeleteMapping.class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     @Override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public RequestRepresentation handleRequest(Method method) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         return RequestRepresentation.builder() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             .httpMethod(HttpMethod.DELETE) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             .path(getPathFromMethod(method)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             .headers(combineHeaders( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 ArrayUtils.addAll(method.getAnnotation(DeleteMapping.class).headers(), getRequestMediaHeaders(method)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 RequestHeaderParamsExtractor.extractAll(method))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             .body(RequestBodyExtractor.extract(method.getParameters())) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             .requestParameters(RequestParametersExtractor.extractAll( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 method.getParameters(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 method.getAnnotation(DeleteMapping.class).path())) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             .pathParameters(PathParametersExtractor.extractAll(method)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             .build(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     @Override | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     public String[] getResponseMediaHeaders(Method method) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         return Arrays.stream(method.getAnnotation(DeleteMapping.class).produces()) | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             .map(header -> HttpHeaders.CONTENT_TYPE + "=" + header) | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             .toArray(String[]::new); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     private String[] getRequestMediaHeaders(Method method) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         return ArrayUtils.addAll( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             Arrays.stream(method.getAnnotation(DeleteMapping.class).consumes()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 .map(header -> HttpHeaders.CONTENT_TYPE + "=" + header) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 .toArray(String[]::new), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             Arrays.stream(method.getAnnotation(DeleteMapping.class).produces()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 .map(header -> HttpHeaders.ACCEPT + "=" + header) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 .toArray(String[]::new)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     private String getPathFromMethod(Method method) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         DeleteMapping annotation = method.getAnnotation(DeleteMapping.class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         return annotation.path().length == 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             ? annotation.path()[0].split("\\?")[0] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             : annotation.value()[0].split("\\?")[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     private static List<Param> combineHeaders(String[] rawHeaders, List<Param> headers) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return Stream | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             .concat(RawHeadersParser.parseAll(rawHeaders).stream(), headers.stream()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             .collect(Collectors.toList()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |  |