Code Duplication    Length = 48-49 lines in 6 locations

src/main/java/com/hltech/pact/gen/domain/client/annotation/handlers/RequestMappingMethodsHandler.java 1 location

@@ 22-70 (lines=49) @@
19
import java.util.stream.Collectors;
20
import java.util.stream.Stream;
21
22
@MappingMethodHandler
23
public class RequestMappingMethodsHandler implements AnnotatedMethodHandler {
24
25
    @Override
26
    public boolean isSupported(Method method) {
27
        return method.isAnnotationPresent(RequestMapping.class);
28
    }
29
30
    @Override
31
    public RequestRepresentation handleRequest(Method method) {
32
        return RequestRepresentation.builder()
33
            .httpMethod(HttpMethod.resolve(method.getAnnotation(RequestMapping.class)
34
                .method()[0].name().toUpperCase()))
35
            .path(getPathFromMethod(method))
36
            .headers(combineHeaders(
37
                ArrayUtils.addAll(method.getAnnotation(RequestMapping.class).headers(), getRequestMediaHeaders(method)),
38
                RequestHeaderParamsExtractor.extractAll(method)))
39
            .body(RequestBodyExtractor.extract(method.getParameters()))
40
            .requestParameters(RequestParametersExtractor.extractAll(method))
41
            .pathParameters(PathParametersExtractor.extractAll(method))
42
            .build();
43
    }
44
45
    @Override
46
    public String[] getResponseMediaHeaders(Method method) {
47
        return Arrays.stream(method.getAnnotation(RequestMapping.class).produces())
48
            .map(header -> HttpHeaders.CONTENT_TYPE + "=" + header)
49
            .toArray(String[]::new);
50
    }
51
52
    private String[] getRequestMediaHeaders(Method method) {
53
        return ArrayUtils.addAll(
54
            Arrays.stream(method.getAnnotation(RequestMapping.class).consumes())
55
                .map(header -> HttpHeaders.CONTENT_TYPE + "=" + header)
56
                .toArray(String[]::new),
57
            Arrays.stream(method.getAnnotation(RequestMapping.class).produces())
58
                .map(header -> HttpHeaders.ACCEPT + "=" + header)
59
                .toArray(String[]::new));
60
    }
61
62
    private String getPathFromMethod(Method method) {
63
        RequestMapping annotation = method.getAnnotation(RequestMapping.class);
64
        return annotation.path().length == 1 ? annotation.path()[0] : annotation.value()[0];
65
    }
66
67
    private static List<Param> combineHeaders(String[] rawHeaders, List<Param> headers) {
68
        return Stream
69
            .concat(RawHeadersParser.parseAll(rawHeaders).stream(), headers.stream())
70
            .collect(Collectors.toList());
71
    }
72
}
73

src/main/java/com/hltech/pact/gen/domain/client/annotation/handlers/PostMappingMethodsHandler.java 1 location

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

src/main/java/com/hltech/pact/gen/domain/client/annotation/handlers/PutMappingMethodsHandler.java 1 location

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

src/main/java/com/hltech/pact/gen/domain/client/annotation/handlers/PatchMappingMethodsHandler.java 1 location

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

src/main/java/com/hltech/pact/gen/domain/client/annotation/handlers/GetMappingMethodsHandler.java 1 location

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

src/main/java/com/hltech/pact/gen/domain/client/annotation/handlers/DeleteMappingMethodsHandler.java 1 location

@@ 22-69 (lines=48) @@
19
import java.util.stream.Collectors;
20
import java.util.stream.Stream;
21
22
@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(method))
40
            .pathParameters(PathParametersExtractor.extractAll(method))
41
            .build();
42
    }
43
44
    @Override
45
    public String[] getResponseMediaHeaders(Method method) {
46
        return Arrays.stream(method.getAnnotation(DeleteMapping.class).produces())
47
            .map(header -> HttpHeaders.CONTENT_TYPE + "=" + header)
48
            .toArray(String[]::new);
49
    }
50
51
    private String[] getRequestMediaHeaders(Method method) {
52
        return ArrayUtils.addAll(
53
            Arrays.stream(method.getAnnotation(DeleteMapping.class).consumes())
54
                .map(header -> HttpHeaders.CONTENT_TYPE + "=" + header)
55
                .toArray(String[]::new),
56
            Arrays.stream(method.getAnnotation(DeleteMapping.class).produces())
57
                .map(header -> HttpHeaders.ACCEPT + "=" + header)
58
                .toArray(String[]::new));
59
    }
60
61
    private String getPathFromMethod(Method method) {
62
        DeleteMapping annotation = method.getAnnotation(DeleteMapping.class);
63
        return annotation.path().length == 1 ? annotation.path()[0] : annotation.value()[0];
64
    }
65
66
    private static List<Param> combineHeaders(String[] rawHeaders, List<Param> headers) {
67
        return Stream
68
            .concat(RawHeadersParser.parseAll(rawHeaders).stream(), headers.stream())
69
            .collect(Collectors.toList());
70
    }
71
}
72