|
21 | 21 | import java.util.*; |
22 | 22 |
|
23 | 23 | abstract public class GenericDefectDojoService<T extends DefectDojoModel> { |
24 | | - protected String defectDojoUrl; |
25 | | - protected String defectDojoApiKey; |
| 24 | + protected String defectDojoUrl; |
| 25 | + protected String defectDojoApiKey; |
26 | 26 |
|
27 | | - protected ObjectMapper objectMapper; |
28 | | - protected ObjectMapper searchStringMapper; |
| 27 | + protected ObjectMapper objectMapper; |
| 28 | + protected ObjectMapper searchStringMapper; |
29 | 29 |
|
30 | | - public GenericDefectDojoService(DefectDojoConfig config){ |
31 | | - this.defectDojoUrl = config.getUrl(); |
32 | | - this.defectDojoApiKey = config.getApiKey(); |
| 30 | + public GenericDefectDojoService(DefectDojoConfig config) { |
| 31 | + this.defectDojoUrl = config.getUrl(); |
| 32 | + this.defectDojoApiKey = config.getApiKey(); |
33 | 33 |
|
34 | | - this.objectMapper = new ObjectMapper(); |
35 | | - this.objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 34 | + this.objectMapper = new ObjectMapper(); |
| 35 | + this.objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
36 | 36 |
|
37 | | - this.searchStringMapper = new ObjectMapper(); |
38 | | - this.searchStringMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
39 | | - this.searchStringMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); |
40 | | - } |
| 37 | + this.searchStringMapper = new ObjectMapper(); |
| 38 | + this.searchStringMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 39 | + this.searchStringMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); |
| 40 | + } |
41 | 41 |
|
42 | 42 |
|
| 43 | + protected long DEFECT_DOJO_OBJET_LIMIT = 100L; |
43 | 44 |
|
44 | | - protected long DEFECT_DOJO_OBJET_LIMIT = 100L; |
| 45 | + /** |
| 46 | + * @return The DefectDojo Authentication Header |
| 47 | + */ |
| 48 | + private HttpHeaders getDefectDojoAuthorizationHeaders() { |
| 49 | + HttpHeaders headers = new HttpHeaders(); |
| 50 | + headers.set("Authorization", "Token " + defectDojoApiKey); |
| 51 | + return headers; |
| 52 | + } |
45 | 53 |
|
46 | | - /** |
47 | | - * @return The DefectDojo Authentication Header |
48 | | - */ |
49 | | - private HttpHeaders getDefectDojoAuthorizationHeaders() { |
50 | | - HttpHeaders headers = new HttpHeaders(); |
51 | | - headers.set("Authorization", "Token " + defectDojoApiKey); |
52 | | - return headers; |
53 | | - } |
| 54 | + protected abstract String getUrlPath(); |
54 | 55 |
|
55 | | - protected abstract String getUrlPath(); |
| 56 | + protected abstract Class<T> getModelClass(); |
56 | 57 |
|
57 | | - protected abstract Class<T> getModelClass(); |
| 58 | + protected abstract DefectDojoResponse<T> deserializeList(String response) throws JsonProcessingException; |
58 | 59 |
|
59 | | - protected abstract DefectDojoResponse<T> deserializeList(String response) throws JsonProcessingException; |
| 60 | + public T get(long id) { |
| 61 | + RestTemplate restTemplate = new RestTemplate(); |
| 62 | + HttpEntity<String> payload = new HttpEntity<>(getDefectDojoAuthorizationHeaders()); |
60 | 63 |
|
61 | | - public T get(long id) { |
62 | | - RestTemplate restTemplate = new RestTemplate(); |
63 | | - HttpEntity<String> payload = new HttpEntity<>(getDefectDojoAuthorizationHeaders()); |
| 64 | + ResponseEntity<T> response = restTemplate.exchange( |
| 65 | + defectDojoUrl + "/api/v2/" + this.getUrlPath() + "/" + id, |
| 66 | + HttpMethod.GET, |
| 67 | + payload, |
| 68 | + getModelClass() |
| 69 | + ); |
64 | 70 |
|
65 | | - ResponseEntity<T> response = restTemplate.exchange( |
66 | | - defectDojoUrl + "/api/v2/" + this.getUrlPath() + "/" + id, |
67 | | - HttpMethod.GET, |
68 | | - payload, |
69 | | - getModelClass() |
70 | | - ); |
| 71 | + return response.getBody(); |
| 72 | + } |
71 | 73 |
|
72 | | - return response.getBody(); |
73 | | - } |
| 74 | + protected DefectDojoResponse<T> internalSearch(Map<String, Object> queryParams, long limit, long offset) throws JsonProcessingException, URISyntaxException { |
| 75 | + RestTemplate restTemplate = new RestTemplate(); |
| 76 | + HttpEntity<String> payload = new HttpEntity<>(getDefectDojoAuthorizationHeaders()); |
74 | 77 |
|
75 | | - protected DefectDojoResponse<T> internalSearch(Map<String, Object> queryParams, long limit, long offset) throws JsonProcessingException, URISyntaxException { |
76 | | - RestTemplate restTemplate = new RestTemplate(); |
77 | | - HttpEntity<String> payload = new HttpEntity<>(getDefectDojoAuthorizationHeaders()); |
| 78 | + var mutableQueryParams = new HashMap<String, Object>(queryParams); |
78 | 79 |
|
79 | | - var mutableQueryParams = new HashMap<String, Object>(queryParams); |
| 80 | + mutableQueryParams.put("limit", String.valueOf(limit)); |
| 81 | + mutableQueryParams.put("offset", String.valueOf(offset)); |
80 | 82 |
|
81 | | - mutableQueryParams.put("limit", String.valueOf(limit)); |
82 | | - mutableQueryParams.put("offset", String.valueOf(offset)); |
| 83 | + var multiValueMap = new LinkedMultiValueMap<String, String>(); |
| 84 | + for (var entry : mutableQueryParams.entrySet()) { |
| 85 | + multiValueMap.set(entry.getKey(), String.valueOf(entry.getValue())); |
| 86 | + } |
83 | 87 |
|
84 | | - var multiValueMap = new LinkedMultiValueMap<String, String>(); |
85 | | - for (var entry : mutableQueryParams.entrySet()) { |
86 | | - multiValueMap.set(entry.getKey(), String.valueOf(entry.getValue())); |
87 | | - } |
| 88 | + var url = new URI(defectDojoUrl + "/api/v2/" + this.getUrlPath() + "/"); |
| 89 | + var uriBuilder = UriComponentsBuilder.fromUri(url).queryParams(multiValueMap); |
88 | 90 |
|
89 | | - var url = new URI(defectDojoUrl + "/api/v2/" + this.getUrlPath() + "/"); |
90 | | - var uriBuilder = UriComponentsBuilder.fromUri(url).queryParams(multiValueMap); |
| 91 | + ResponseEntity<String> responseString = restTemplate.exchange( |
| 92 | + uriBuilder.build(mutableQueryParams), |
| 93 | + HttpMethod.GET, |
| 94 | + payload, |
| 95 | + String.class |
| 96 | + ); |
91 | 97 |
|
92 | | - ResponseEntity<String> responseString = restTemplate.exchange( |
93 | | - uriBuilder.build(mutableQueryParams), |
94 | | - HttpMethod.GET, |
95 | | - payload, |
96 | | - String.class |
97 | | - ); |
98 | | - |
99 | | - return deserializeList(responseString.getBody()); |
100 | | - } |
| 98 | + return deserializeList(responseString.getBody()); |
| 99 | + } |
101 | 100 |
|
102 | | - public List<T> search(Map<String, Object> queryParams) throws URISyntaxException, JsonProcessingException { |
103 | | - List<T> objects = new LinkedList<>(); |
| 101 | + public List<T> search(Map<String, Object> queryParams) throws URISyntaxException, JsonProcessingException { |
| 102 | + List<T> objects = new LinkedList<>(); |
104 | 103 |
|
105 | | - boolean hasNext = false; |
106 | | - long page = 0; |
107 | | - do { |
108 | | - var response = internalSearch(queryParams, DEFECT_DOJO_OBJET_LIMIT, DEFECT_DOJO_OBJET_LIMIT * page++); |
109 | | - objects.addAll(response.getResults()); |
| 104 | + boolean hasNext = false; |
| 105 | + long page = 0; |
| 106 | + do { |
| 107 | + var response = internalSearch(queryParams, DEFECT_DOJO_OBJET_LIMIT, DEFECT_DOJO_OBJET_LIMIT * page++); |
| 108 | + objects.addAll(response.getResults()); |
110 | 109 |
|
111 | | - hasNext = response.getNext() != null; |
112 | | - if (page > 100) { |
113 | | - throw new DefectDojoLoopException("Found too many response object. Quitting after " + page + " paginated API pages of " + DEFECT_DOJO_OBJET_LIMIT + " each."); |
114 | | - } |
115 | | - } while (hasNext); |
| 110 | + hasNext = response.getNext() != null; |
| 111 | + if (page > 100) { |
| 112 | + throw new DefectDojoLoopException("Found too many response object. Quitting after " + page + " paginated API pages of " + DEFECT_DOJO_OBJET_LIMIT + " each."); |
| 113 | + } |
| 114 | + } while (hasNext); |
116 | 115 |
|
117 | | - return objects; |
118 | | - } |
| 116 | + return objects; |
| 117 | + } |
119 | 118 |
|
120 | | - public List<T> search() throws URISyntaxException, JsonProcessingException { |
121 | | - return search(new LinkedHashMap<>()); |
122 | | - } |
| 119 | + public List<T> search() throws URISyntaxException, JsonProcessingException { |
| 120 | + return search(new LinkedHashMap<>()); |
| 121 | + } |
123 | 122 |
|
124 | | - @SuppressWarnings("unchecked") |
125 | | - public Optional<T> searchUnique(T searchObject) throws URISyntaxException, JsonProcessingException { |
126 | | - Map<String, Object> queryParams = searchStringMapper.convertValue(searchObject, Map.class); |
| 123 | + @SuppressWarnings("unchecked") |
| 124 | + public Optional<T> searchUnique(T searchObject) throws URISyntaxException, JsonProcessingException { |
| 125 | + Map<String, Object> queryParams = searchStringMapper.convertValue(searchObject, Map.class); |
127 | 126 |
|
128 | | - var objects = search(queryParams); |
| 127 | + var objects = search(queryParams); |
129 | 128 |
|
130 | | - return objects.stream() |
131 | | - .filter((object) -> object != null && object.equalsQueryString(queryParams)) |
132 | | - .findFirst(); |
133 | | - } |
| 129 | + return objects.stream() |
| 130 | + .filter((object) -> object != null && object.equalsQueryString(queryParams)) |
| 131 | + .findFirst(); |
| 132 | + } |
134 | 133 |
|
135 | | - public Optional<T> searchUnique(Map<String, Object> queryParams) throws URISyntaxException, JsonProcessingException { |
136 | | - var objects = search(queryParams); |
| 134 | + public Optional<T> searchUnique(Map<String, Object> queryParams) throws URISyntaxException, JsonProcessingException { |
| 135 | + var objects = search(queryParams); |
137 | 136 |
|
138 | | - return objects.stream() |
139 | | - .filter((object) -> object.equalsQueryString(queryParams)) |
140 | | - .findFirst(); |
141 | | - } |
| 137 | + return objects.stream() |
| 138 | + .filter((object) -> object.equalsQueryString(queryParams)) |
| 139 | + .findFirst(); |
| 140 | + } |
142 | 141 |
|
143 | | - public T create(T object) { |
144 | | - RestTemplate restTemplate = new RestTemplate(); |
145 | | - HttpEntity<T> payload = new HttpEntity<T>(object, getDefectDojoAuthorizationHeaders()); |
| 142 | + public T create(T object) { |
| 143 | + RestTemplate restTemplate = new RestTemplate(); |
| 144 | + HttpEntity<T> payload = new HttpEntity<T>(object, getDefectDojoAuthorizationHeaders()); |
146 | 145 |
|
147 | | - ResponseEntity<T> response = restTemplate.exchange(defectDojoUrl + "/api/v2/" + getUrlPath() + "/", HttpMethod.POST, payload, getModelClass()); |
148 | | - return response.getBody(); |
149 | | - } |
| 146 | + ResponseEntity<T> response = restTemplate.exchange(defectDojoUrl + "/api/v2/" + getUrlPath() + "/", HttpMethod.POST, payload, getModelClass()); |
| 147 | + return response.getBody(); |
| 148 | + } |
150 | 149 |
|
151 | | - public void delete(long id) { |
152 | | - RestTemplate restTemplate = new RestTemplate(); |
153 | | - HttpEntity<String> payload = new HttpEntity<>(getDefectDojoAuthorizationHeaders()); |
| 150 | + public void delete(long id) { |
| 151 | + RestTemplate restTemplate = new RestTemplate(); |
| 152 | + HttpEntity<String> payload = new HttpEntity<>(getDefectDojoAuthorizationHeaders()); |
154 | 153 |
|
155 | | - restTemplate.exchange(defectDojoUrl + "/api/v2/" + getUrlPath() + "/" + id + "/", HttpMethod.DELETE, payload, String.class); |
156 | | - } |
| 154 | + restTemplate.exchange(defectDojoUrl + "/api/v2/" + getUrlPath() + "/" + id + "/", HttpMethod.DELETE, payload, String.class); |
| 155 | + } |
157 | 156 |
|
158 | | - public T update(T object, long objectId) { |
159 | | - RestTemplate restTemplate = new RestTemplate(); |
160 | | - HttpEntity<T> payload = new HttpEntity<T>(object, getDefectDojoAuthorizationHeaders()); |
| 157 | + public T update(T object, long objectId) { |
| 158 | + RestTemplate restTemplate = new RestTemplate(); |
| 159 | + HttpEntity<T> payload = new HttpEntity<T>(object, getDefectDojoAuthorizationHeaders()); |
161 | 160 |
|
162 | | - ResponseEntity<T> response = restTemplate.exchange(defectDojoUrl + "/api/v2/" + getUrlPath() + "/" + objectId + "/", HttpMethod.PUT, payload, getModelClass()); |
163 | | - return response.getBody(); |
164 | | - } |
| 161 | + ResponseEntity<T> response = restTemplate.exchange(defectDojoUrl + "/api/v2/" + getUrlPath() + "/" + objectId + "/", HttpMethod.PUT, payload, getModelClass()); |
| 162 | + return response.getBody(); |
| 163 | + } |
165 | 164 | } |
0 commit comments