Code Duplication    Length = 52-53 lines in 6 locations

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

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

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

@@ 22-73 (lines=52) @@
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(
40
                method.getParameters(),
41
                method.getAnnotation(PutMapping.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(PutMapping.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(PutMapping.class).consumes())
56
                .map(header -> HttpHeaders.CONTENT_TYPE + "=" + header)
57
                .toArray(String[]::new),
58
            Arrays.stream(method.getAnnotation(PutMapping.class).produces())
59
                .map(header -> HttpHeaders.ACCEPT + "=" + header)
60
                .toArray(String[]::new));
61
    }
62
63
    private String getPathFromMethod(Method method) {
64
        PutMapping annotation = method.getAnnotation(PutMapping.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

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

@@ 22-73 (lines=52) @@
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(
40
                method.getParameters(),
41
                method.getAnnotation(PatchMapping.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(PatchMapping.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(PatchMapping.class).consumes())
56
                .map(header -> HttpHeaders.CONTENT_TYPE + "=" + header)
57
                .toArray(String[]::new),
58
            Arrays.stream(method.getAnnotation(PatchMapping.class).produces())
59
                .map(header -> HttpHeaders.ACCEPT + "=" + header)
60
                .toArray(String[]::new));
61
    }
62
63
    private String getPathFromMethod(Method method) {
64
        PatchMapping annotation = method.getAnnotation(PatchMapping.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

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

@@ 22-73 (lines=52) @@
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(
40
                method.getParameters(),
41
                method.getAnnotation(PostMapping.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(PostMapping.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(PostMapping.class).consumes())
56
                .map(header -> HttpHeaders.CONTENT_TYPE + "=" + header)
57
                .toArray(String[]::new),
58
            Arrays.stream(method.getAnnotation(PostMapping.class).produces())
59
                .map(header -> HttpHeaders.ACCEPT + "=" + header)
60
                .toArray(String[]::new));
61
    }
62
63
    private String getPathFromMethod(Method method) {
64
        PostMapping annotation = method.getAnnotation(PostMapping.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

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

@@ 22-73 (lines=52) @@
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(
40
                method.getParameters(),
41
                method.getAnnotation(GetMapping.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(GetMapping.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(GetMapping.class).consumes())
56
                .map(header -> HttpHeaders.CONTENT_TYPE + "=" + header)
57
                .toArray(String[]::new),
58
            Arrays.stream(method.getAnnotation(GetMapping.class).produces())
59
                .map(header -> HttpHeaders.ACCEPT + "=" + header)
60
                .toArray(String[]::new));
61
    }
62
63
    private String getPathFromMethod(Method method) {
64
        GetMapping annotation = method.getAnnotation(GetMapping.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

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

@@ 22-73 (lines=52) @@
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(
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