3838import java .util .*;
3939
4040abstract public class GenericDefectDojoService <T extends DefectDojoModel > {
41- protected String defectDojoUrl ;
42- protected String defectDojoApiKey ;
41+ protected DefectDojoConfig defectDojoConfig ;
4342
4443 protected ObjectMapper objectMapper ;
4544 protected ObjectMapper searchStringMapper ;
4645
4746 public GenericDefectDojoService (DefectDojoConfig config ) {
48- this .defectDojoUrl = config .getUrl ();
49- this .defectDojoApiKey = config .getApiKey ();
47+ this .defectDojoConfig = config ;
5048
5149 this .objectMapper = new ObjectMapper ();
5250 this .objectMapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
@@ -65,7 +63,7 @@ public GenericDefectDojoService(DefectDojoConfig config) {
6563 */
6664 private HttpHeaders getDefectDojoAuthorizationHeaders () {
6765 HttpHeaders headers = new HttpHeaders ();
68- headers .set ("Authorization" , "Token " + defectDojoApiKey );
66+ headers .set ("Authorization" , "Token " + this . defectDojoConfig . getApiKey () );
6967 return headers ;
7068 }
7169
@@ -80,7 +78,7 @@ public T get(long id) {
8078 HttpEntity <String > payload = new HttpEntity <>(getDefectDojoAuthorizationHeaders ());
8179
8280 ResponseEntity <T > response = restTemplate .exchange (
83- defectDojoUrl + "/api/v2/" + this .getUrlPath () + "/" + id ,
81+ this . defectDojoConfig . getUrl () + "/api/v2/" + this .getUrlPath () + "/" + id ,
8482 HttpMethod .GET ,
8583 payload ,
8684 getModelClass ()
@@ -103,7 +101,7 @@ protected DefectDojoResponse<T> internalSearch(Map<String, Object> queryParams,
103101 multiValueMap .set (entry .getKey (), String .valueOf (entry .getValue ()));
104102 }
105103
106- var url = new URI (defectDojoUrl + "/api/v2/" + this .getUrlPath () + "/" );
104+ var url = new URI (this . defectDojoConfig . getUrl () + "/api/v2/" + this .getUrlPath () + "/" );
107105 var uriBuilder = UriComponentsBuilder .fromUri (url ).queryParams (multiValueMap );
108106
109107 ResponseEntity <String > responseString = restTemplate .exchange (
@@ -126,8 +124,8 @@ public List<T> search(Map<String, Object> queryParams) throws URISyntaxException
126124 objects .addAll (response .getResults ());
127125
128126 hasNext = response .getNext () != null ;
129- if (page > 100 ) {
130- throw new DefectDojoLoopException ("Found too many response object. Quitting after " + page + " paginated API pages of " + DEFECT_DOJO_OBJET_LIMIT + " each." );
127+ if (page > this . defectDojoConfig . getMaxPageCountForGets () ) {
128+ throw new DefectDojoLoopException ("Found too many response object. Quitting after " + ( page - 1 ) + " paginated API pages of " + DEFECT_DOJO_OBJET_LIMIT + " each." );
131129 }
132130 } while (hasNext );
133131
@@ -161,22 +159,22 @@ public T create(T object) {
161159 RestTemplate restTemplate = new RestTemplate ();
162160 HttpEntity <T > payload = new HttpEntity <T >(object , getDefectDojoAuthorizationHeaders ());
163161
164- ResponseEntity <T > response = restTemplate .exchange (defectDojoUrl + "/api/v2/" + getUrlPath () + "/" , HttpMethod .POST , payload , getModelClass ());
162+ ResponseEntity <T > response = restTemplate .exchange (this . defectDojoConfig . getUrl () + "/api/v2/" + getUrlPath () + "/" , HttpMethod .POST , payload , getModelClass ());
165163 return response .getBody ();
166164 }
167165
168166 public void delete (long id ) {
169167 RestTemplate restTemplate = new RestTemplate ();
170168 HttpEntity <String > payload = new HttpEntity <>(getDefectDojoAuthorizationHeaders ());
171169
172- restTemplate .exchange (defectDojoUrl + "/api/v2/" + getUrlPath () + "/" + id + "/" , HttpMethod .DELETE , payload , String .class );
170+ restTemplate .exchange (this . defectDojoConfig . getUrl () + "/api/v2/" + getUrlPath () + "/" + id + "/" , HttpMethod .DELETE , payload , String .class );
173171 }
174172
175173 public T update (T object , long objectId ) {
176174 RestTemplate restTemplate = new RestTemplate ();
177175 HttpEntity <T > payload = new HttpEntity <T >(object , getDefectDojoAuthorizationHeaders ());
178176
179- ResponseEntity <T > response = restTemplate .exchange (defectDojoUrl + "/api/v2/" + getUrlPath () + "/" + objectId + "/" , HttpMethod .PUT , payload , getModelClass ());
177+ ResponseEntity <T > response = restTemplate .exchange (this . defectDojoConfig . getUrl () + "/api/v2/" + getUrlPath () + "/" + objectId + "/" , HttpMethod .PUT , payload , getModelClass ());
180178 return response .getBody ();
181179 }
182180}
0 commit comments