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