Completed
Pull Request — master (#66)
by Filip
26:34 queued 06:50
created

com.hltech.pact.gen.domain.client.annotation.handlers.RequestMappingMethodsHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 100 %

Importance

Changes 0
Metric Value
eloc 38
dl 49
loc 49
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isSupported(Method) 3 3 1
A getResponseMediaHeaders(Method) 5 5 1
A handleRequest(Method) 13 13 1
A getRequestMediaHeaders(Method) 8 8 1
A getPathFromMethod(Method) 3 3 2
A combineHeaders(String[],List) 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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.RequestMapping;
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
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
22
public class RequestMappingMethodsHandler implements AnnotatedMethodHandler {
23
24
    @Override
25
    public boolean isSupported(Method method) {
26
        return method.isAnnotationPresent(RequestMapping.class);
27
    }
28
29
    @Override
30
    public RequestRepresentation handleRequest(Method method) {
31
        return RequestRepresentation.builder()
32
            .httpMethod(HttpMethod.resolve(method.getAnnotation(RequestMapping.class)
33
                .method()[0].name().toUpperCase()))
34
            .path(getPathFromMethod(method))
35
            .headers(combineHeaders(
36
                ArrayUtils.addAll(method.getAnnotation(RequestMapping.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(RequestMapping.class).produces())
47
            .map(header -> "Content-Type=" + header)
48
            .toArray(String[]::new);
49
    }
50
51
    private String[] getRequestMediaHeaders(Method method) {
52
        return ArrayUtils.addAll(
53
            Arrays.stream(method.getAnnotation(RequestMapping.class).consumes())
54
                .map(header -> "Content-Type=" + header)
55
                .toArray(String[]::new),
56
            Arrays.stream(method.getAnnotation(RequestMapping.class).produces())
57
                .map(header -> "Accept=" + header)
58
                .toArray(String[]::new));
59
    }
60
61
    private String getPathFromMethod(Method method) {
62
        RequestMapping annotation = method.getAnnotation(RequestMapping.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