diff --git a/src/main/java/io/securecodebox/persistence/defectdojo/service/DefaultImportScanService.java b/src/main/java/io/securecodebox/persistence/defectdojo/service/DefaultImportScanService.java index 00bb63b..b17b012 100644 --- a/src/main/java/io/securecodebox/persistence/defectdojo/service/DefaultImportScanService.java +++ b/src/main/java/io/securecodebox/persistence/defectdojo/service/DefaultImportScanService.java @@ -31,7 +31,7 @@ import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; -import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import java.nio.charset.StandardCharsets; @@ -124,7 +124,7 @@ public String getFilename() { final var payload = new HttpEntity>(body, headers); return exchangeRequest(endpoint, payload); - } catch (HttpClientErrorException e) { + } catch (RestClientException e) { log.error("Exception while attaching findings to engagement: {}", e.getMessage()); throw new PersistenceException("Failed to attach findings to engagement.", e); } diff --git a/src/main/java/io/securecodebox/persistence/defectdojo/service/GenericDefectDojoService.java b/src/main/java/io/securecodebox/persistence/defectdojo/service/GenericDefectDojoService.java index 2119c61..0531669 100644 --- a/src/main/java/io/securecodebox/persistence/defectdojo/service/GenericDefectDojoService.java +++ b/src/main/java/io/securecodebox/persistence/defectdojo/service/GenericDefectDojoService.java @@ -12,6 +12,7 @@ import com.fasterxml.jackson.databind.cfg.CoercionInputShape; import io.securecodebox.persistence.defectdojo.config.Config; import io.securecodebox.persistence.defectdojo.exception.LoopException; +import io.securecodebox.persistence.defectdojo.exception.PersistenceException; import io.securecodebox.persistence.defectdojo.http.Foo; import io.securecodebox.persistence.defectdojo.http.ProxyConfigFactory; import io.securecodebox.persistence.defectdojo.model.Engagement; @@ -29,6 +30,7 @@ import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.util.LinkedMultiValueMap; +import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; @@ -73,14 +75,20 @@ public final T get(long id) { final var url = this.config.getUrl() + API_PREFIX + this.getUrlPath() + "/" + id; log.debug("Requesting URL: {}", url); - ResponseEntity response = restTemplate.exchange( - url, - HttpMethod.GET, - payload, - getModelClass() - ); - - return response.getBody(); + + try { + ResponseEntity response = restTemplate.exchange( + url, + HttpMethod.GET, + payload, + getModelClass() + ); + + return response.getBody(); + } catch (RestClientException e) { + log.error("Exception while doing a GET request to DefectDojo API: {}", e.getMessage()); + throw new PersistenceException("Failed to do a GET to DefectDojo API!", e); + } } @Override @@ -133,8 +141,17 @@ public final T create(@NonNull T object) { var restTemplate = this.getRestTemplate(); HttpEntity payload = new HttpEntity<>(object, getDefectDojoAuthorizationHeaders()); - ResponseEntity response = restTemplate.exchange(this.config.getUrl() + API_PREFIX + getUrlPath() + "/", HttpMethod.POST, payload, getModelClass()); - return response.getBody(); + try { + ResponseEntity response = restTemplate.exchange( + this.config.getUrl() + API_PREFIX + getUrlPath() + "/", + HttpMethod.POST, + payload, + getModelClass()); + return response.getBody(); + } catch (RestClientException e) { + log.error("Exception while doing a POST request to DefectDojo API: {}", e.getMessage()); + throw new PersistenceException("Failed to do a POST to DefectDojo API!", e); + } } @Override @@ -142,7 +159,16 @@ public final void delete(long id) { var restTemplate = this.getRestTemplate(); HttpEntity payload = new HttpEntity<>(getDefectDojoAuthorizationHeaders()); - restTemplate.exchange(this.config.getUrl() + API_PREFIX + getUrlPath() + "/" + id + "/", HttpMethod.DELETE, payload, String.class); + try { + restTemplate.exchange( + this.config.getUrl() + API_PREFIX + getUrlPath() + "/" + id + "/", + HttpMethod.DELETE, + payload, + String.class); + } catch (RestClientException e) { + log.error("Exception while doing a DELETE request to DefectDojo API: {}", e.getMessage()); + throw new PersistenceException("Failed to do a DELETE to DefectDojo API!", e); + } } @Override @@ -150,10 +176,18 @@ public final T update(@NonNull T object, long id) { var restTemplate = this.getRestTemplate(); HttpEntity payload = new HttpEntity<>(object, getDefectDojoAuthorizationHeaders()); - ResponseEntity response = restTemplate.exchange(this.config.getUrl() + API_PREFIX + getUrlPath() + "/" + id + "/", HttpMethod.PUT, payload, getModelClass()); - return response.getBody(); + try { + ResponseEntity response = restTemplate.exchange(this.config.getUrl() + API_PREFIX + getUrlPath() + "/" + id + "/", + HttpMethod.PUT, + payload, + getModelClass()); + return response.getBody(); + } catch (RestClientException e) { + log.error("Exception while doing a PUT request to DefectDojo API: {}", e.getMessage()); + throw new PersistenceException("Failed to do a PUT to DefectDojo API!", e); + } } - + /** * Get the URL path for the REST endpoint relative to {@link #API_PREFIX} * @@ -212,16 +246,21 @@ protected PaginatedResult internalSearch(Map queryParams, lon } var url = new URI(this.config.getUrl() + API_PREFIX + this.getUrlPath() + "/"); - log.debug("Requesting URL: " + url); + log.debug("Requesting URL: {}", url); var uriBuilder = UriComponentsBuilder.fromUri(url).queryParams(multiValueMap); - ResponseEntity responseString = restTemplate.exchange( - uriBuilder.build(mutableQueryParams), - HttpMethod.GET, - payload, - String.class - ); - - return deserializeList(responseString.getBody()); + try { + ResponseEntity responseString = restTemplate.exchange( + uriBuilder.build(mutableQueryParams), + HttpMethod.GET, + payload, + String.class + ); + + return deserializeList(responseString.getBody()); + } catch (RestClientException e) { + log.error("Exception while doing a GET request to DefectDojo API: {}", e.getMessage()); + throw new PersistenceException("Failed to do a GET to DefectDojo API!", e); + } } } diff --git a/src/main/java/io/securecodebox/persistence/defectdojo/service/ImportScanService2.java b/src/main/java/io/securecodebox/persistence/defectdojo/service/ImportScanService2.java index 28d809b..0df013f 100644 --- a/src/main/java/io/securecodebox/persistence/defectdojo/service/ImportScanService2.java +++ b/src/main/java/io/securecodebox/persistence/defectdojo/service/ImportScanService2.java @@ -22,7 +22,7 @@ import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; -import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import java.nio.charset.StandardCharsets; @@ -94,8 +94,8 @@ public String getFilename() { var payload = new HttpEntity<>(mvn, headers); return restTemplate.exchange(config.getUrl() + "/api/v2/" + endpoint + "/", HttpMethod.POST, payload, ImportScanResponse.class).getBody(); - } catch (HttpClientErrorException e) { - throw new PersistenceException("Failed to attach findings to engagement."); + } catch (RestClientException e) { + throw new PersistenceException("Failed to attach findings to engagement.", e); } }