@@ -39,159 +39,159 @@ public abstract class GenericDefectDojoService<T extends Model> {
3939 private static final String API_PREFIX = "/api/v2/" ;
4040 protected Config config ;
4141
42- protected ObjectMapper objectMapper ;
43- protected ObjectMapper searchStringMapper ;
42+ protected ObjectMapper objectMapper ;
43+ protected ObjectMapper searchStringMapper ;
4444
45- @ Getter
46- protected RestTemplate restTemplate ;
45+ @ Getter
46+ protected RestTemplate restTemplate ;
4747
48- public GenericDefectDojoService (Config config ) {
49- this .config = config ;
48+ public GenericDefectDojoService (Config config ) {
49+ this .config = config ;
5050
51- this .objectMapper = new ObjectMapper ();
52- this .objectMapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
53- this .objectMapper .coercionConfigFor (Engagement .Status .class ).setCoercion (CoercionInputShape .EmptyString , CoercionAction .AsNull );
54- this .objectMapper .findAndRegisterModules ();
51+ this .objectMapper = new ObjectMapper ();
52+ this .objectMapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
53+ this .objectMapper .coercionConfigFor (Engagement .Status .class ).setCoercion (CoercionInputShape .EmptyString , CoercionAction .AsNull );
54+ this .objectMapper .findAndRegisterModules ();
5555
56- this .searchStringMapper = new ObjectMapper ();
57- this .searchStringMapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
58- this .searchStringMapper .coercionConfigFor (Engagement .Status .class ).setCoercion (CoercionInputShape .EmptyString , CoercionAction .AsNull );
59- this .searchStringMapper .setSerializationInclusion (JsonInclude .Include .NON_DEFAULT );
56+ this .searchStringMapper = new ObjectMapper ();
57+ this .searchStringMapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
58+ this .searchStringMapper .coercionConfigFor (Engagement .Status .class ).setCoercion (CoercionInputShape .EmptyString , CoercionAction .AsNull );
59+ this .searchStringMapper .setSerializationInclusion (JsonInclude .Include .NON_DEFAULT );
6060
61- this .restTemplate = this .setupRestTemplate ();
62- }
61+ this .restTemplate = this .setupRestTemplate ();
62+ }
6363
6464
65- protected long DEFECT_DOJO_OBJET_LIMIT = 100L ;
65+ protected long DEFECT_DOJO_OBJET_LIMIT = 100L ;
6666
67- /**
68- * @return The DefectDojo Authentication Header
69- */
70- private HttpHeaders getDefectDojoAuthorizationHeaders () {
71- return new Foo (config , new ProxyConfigFactory ().create ()).generateAuthorizationHeaders ();
72- }
67+ /**
68+ * @return The DefectDojo Authentication Header
69+ */
70+ private HttpHeaders getDefectDojoAuthorizationHeaders () {
71+ return new Foo (config , new ProxyConfigFactory ().create ()).generateAuthorizationHeaders ();
72+ }
7373
74- private RestTemplate setupRestTemplate () {
75- RestTemplate restTemplate = new Foo (config , new ProxyConfigFactory ().create ()).createRestTemplate ();
76- MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter ();
77- converter .setObjectMapper (this .objectMapper );
78- restTemplate .setMessageConverters (List .of (
79- new FormHttpMessageConverter (),
80- new ResourceHttpMessageConverter (),
81- new StringHttpMessageConverter (),
82- converter
83- ));
84- return restTemplate ;
85- }
74+ private RestTemplate setupRestTemplate () {
75+ RestTemplate restTemplate = new Foo (config , new ProxyConfigFactory ().create ()).createRestTemplate ();
76+ MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter ();
77+ converter .setObjectMapper (this .objectMapper );
78+ restTemplate .setMessageConverters (List .of (
79+ new FormHttpMessageConverter (),
80+ new ResourceHttpMessageConverter (),
81+ new StringHttpMessageConverter (),
82+ converter
83+ ));
84+ return restTemplate ;
85+ }
8686
87- protected abstract String getUrlPath ();
87+ protected abstract String getUrlPath ();
8888
89- protected abstract Class <T > getModelClass ();
89+ protected abstract Class <T > getModelClass ();
9090
91- protected abstract Response <T > deserializeList (String response ) throws JsonProcessingException ;
91+ protected abstract Response <T > deserializeList (String response ) throws JsonProcessingException ;
9292
93- public T get (long id ) {
94- var restTemplate = this .getRestTemplate ();
95- HttpEntity <String > payload = new HttpEntity <>(getDefectDojoAuthorizationHeaders ());
93+ public T get (long id ) {
94+ var restTemplate = this .getRestTemplate ();
95+ HttpEntity <String > payload = new HttpEntity <>(getDefectDojoAuthorizationHeaders ());
9696
97- ResponseEntity <T > response = restTemplate .exchange (
98- this .config .getUrl () + API_PREFIX + this .getUrlPath () + "/" + id ,
99- HttpMethod .GET ,
100- payload ,
101- getModelClass ()
102- );
97+ ResponseEntity <T > response = restTemplate .exchange (
98+ this .config .getUrl () + API_PREFIX + this .getUrlPath () + "/" + id ,
99+ HttpMethod .GET ,
100+ payload ,
101+ getModelClass ()
102+ );
103103
104- return response .getBody ();
105- }
104+ return response .getBody ();
105+ }
106106
107- protected Response <T > internalSearch (Map <String , Object > queryParams , long limit , long offset ) throws JsonProcessingException , URISyntaxException {
108- var restTemplate = this .getRestTemplate ();
109- HttpEntity <String > payload = new HttpEntity <>(getDefectDojoAuthorizationHeaders ());
107+ protected Response <T > internalSearch (Map <String , Object > queryParams , long limit , long offset ) throws JsonProcessingException , URISyntaxException {
108+ var restTemplate = this .getRestTemplate ();
109+ HttpEntity <String > payload = new HttpEntity <>(getDefectDojoAuthorizationHeaders ());
110110
111- var mutableQueryParams = new HashMap <String , Object >(queryParams );
111+ var mutableQueryParams = new HashMap <String , Object >(queryParams );
112112
113- mutableQueryParams .put ("limit" , String .valueOf (limit ));
114- mutableQueryParams .put ("offset" , String .valueOf (offset ));
113+ mutableQueryParams .put ("limit" , String .valueOf (limit ));
114+ mutableQueryParams .put ("offset" , String .valueOf (offset ));
115115
116- var multiValueMap = new LinkedMultiValueMap <String , String >();
117- for (var entry : mutableQueryParams .entrySet ()) {
118- multiValueMap .set (entry .getKey (), String .valueOf (entry .getValue ()));
119- }
116+ var multiValueMap = new LinkedMultiValueMap <String , String >();
117+ for (var entry : mutableQueryParams .entrySet ()) {
118+ multiValueMap .set (entry .getKey (), String .valueOf (entry .getValue ()));
119+ }
120120
121- var url = new URI (this .config .getUrl () + API_PREFIX + this .getUrlPath () + "/" );
122- var uriBuilder = UriComponentsBuilder .fromUri (url ).queryParams (multiValueMap );
121+ var url = new URI (this .config .getUrl () + API_PREFIX + this .getUrlPath () + "/" );
122+ var uriBuilder = UriComponentsBuilder .fromUri (url ).queryParams (multiValueMap );
123123
124- ResponseEntity <String > responseString = restTemplate .exchange (
125- uriBuilder .build (mutableQueryParams ),
126- HttpMethod .GET ,
127- payload ,
128- String .class
129- );
124+ ResponseEntity <String > responseString = restTemplate .exchange (
125+ uriBuilder .build (mutableQueryParams ),
126+ HttpMethod .GET ,
127+ payload ,
128+ String .class
129+ );
130130
131- return deserializeList (responseString .getBody ());
132- }
131+ return deserializeList (responseString .getBody ());
132+ }
133133
134- public List <T > search (Map <String , Object > queryParams ) throws URISyntaxException , JsonProcessingException {
135- List <T > objects = new LinkedList <>();
134+ public List <T > search (Map <String , Object > queryParams ) throws URISyntaxException , JsonProcessingException {
135+ List <T > objects = new LinkedList <>();
136136
137- boolean hasNext = false ;
138- long page = 0 ;
139- do {
140- var response = internalSearch (queryParams , DEFECT_DOJO_OBJET_LIMIT , DEFECT_DOJO_OBJET_LIMIT * page ++);
141- objects .addAll (response .getResults ());
137+ boolean hasNext = false ;
138+ long page = 0 ;
139+ do {
140+ var response = internalSearch (queryParams , DEFECT_DOJO_OBJET_LIMIT , DEFECT_DOJO_OBJET_LIMIT * page ++);
141+ objects .addAll (response .getResults ());
142142
143- hasNext = response .getNext () != null ;
144- if (page > this .config .getMaxPageCountForGets ()) {
145- throw new LoopException ("Found too many response object. Quitting after " + (page - 1 ) + " paginated API pages of " + DEFECT_DOJO_OBJET_LIMIT + " each." );
146- }
147- } while (hasNext );
143+ hasNext = response .getNext () != null ;
144+ if (page > this .config .getMaxPageCountForGets ()) {
145+ throw new LoopException ("Found too many response object. Quitting after " + (page - 1 ) + " paginated API pages of " + DEFECT_DOJO_OBJET_LIMIT + " each." );
146+ }
147+ } while (hasNext );
148148
149- return objects ;
150- }
149+ return objects ;
150+ }
151151
152- public List <T > search () throws URISyntaxException , JsonProcessingException {
153- return search (new LinkedHashMap <>());
154- }
152+ public List <T > search () throws URISyntaxException , JsonProcessingException {
153+ return search (new LinkedHashMap <>());
154+ }
155155
156- @ SuppressWarnings ("unchecked" )
157- public Optional <T > searchUnique (T searchObject ) throws URISyntaxException , JsonProcessingException {
158- Map <String , Object > queryParams = searchStringMapper .convertValue (searchObject , Map .class );
156+ @ SuppressWarnings ("unchecked" )
157+ public Optional <T > searchUnique (T searchObject ) throws URISyntaxException , JsonProcessingException {
158+ Map <String , Object > queryParams = searchStringMapper .convertValue (searchObject , Map .class );
159159
160- var objects = search (queryParams );
160+ var objects = search (queryParams );
161161
162- return objects .stream ()
163- .filter (object -> object != null && object .equalsQueryString (queryParams ))
164- .findFirst ();
165- }
162+ return objects .stream ()
163+ .filter (object -> object != null && object .equalsQueryString (queryParams ))
164+ .findFirst ();
165+ }
166166
167- public Optional <T > searchUnique (Map <String , Object > queryParams ) throws URISyntaxException , JsonProcessingException {
168- var objects = search (queryParams );
167+ public Optional <T > searchUnique (Map <String , Object > queryParams ) throws URISyntaxException , JsonProcessingException {
168+ var objects = search (queryParams );
169169
170- return objects .stream ()
171- .filter (object -> object .equalsQueryString (queryParams ))
172- .findFirst ();
173- }
170+ return objects .stream ()
171+ .filter (object -> object .equalsQueryString (queryParams ))
172+ .findFirst ();
173+ }
174174
175- public T create (T object ) {
176- var restTemplate = this .getRestTemplate ();
177- HttpEntity <T > payload = new HttpEntity <>(object , getDefectDojoAuthorizationHeaders ());
175+ public T create (T object ) {
176+ var restTemplate = this .getRestTemplate ();
177+ HttpEntity <T > payload = new HttpEntity <>(object , getDefectDojoAuthorizationHeaders ());
178178
179- ResponseEntity <T > response = restTemplate .exchange (this .config .getUrl () + API_PREFIX + getUrlPath () + "/" , HttpMethod .POST , payload , getModelClass ());
180- return response .getBody ();
181- }
179+ ResponseEntity <T > response = restTemplate .exchange (this .config .getUrl () + API_PREFIX + getUrlPath () + "/" , HttpMethod .POST , payload , getModelClass ());
180+ return response .getBody ();
181+ }
182182
183- public void delete (long id ) {
184- var restTemplate = this .getRestTemplate ();
185- HttpEntity <String > payload = new HttpEntity <>(getDefectDojoAuthorizationHeaders ());
183+ public void delete (long id ) {
184+ var restTemplate = this .getRestTemplate ();
185+ HttpEntity <String > payload = new HttpEntity <>(getDefectDojoAuthorizationHeaders ());
186186
187- restTemplate .exchange (this .config .getUrl () + API_PREFIX + getUrlPath () + "/" + id + "/" , HttpMethod .DELETE , payload , String .class );
188- }
187+ restTemplate .exchange (this .config .getUrl () + API_PREFIX + getUrlPath () + "/" + id + "/" , HttpMethod .DELETE , payload , String .class );
188+ }
189189
190- public T update (T object , long objectId ) {
191- var restTemplate = this .getRestTemplate ();
192- HttpEntity <T > payload = new HttpEntity <>(object , getDefectDojoAuthorizationHeaders ());
190+ public T update (T object , long objectId ) {
191+ var restTemplate = this .getRestTemplate ();
192+ HttpEntity <T > payload = new HttpEntity <>(object , getDefectDojoAuthorizationHeaders ());
193193
194- ResponseEntity <T > response = restTemplate .exchange (this .config .getUrl () + API_PREFIX + getUrlPath () + "/" + objectId + "/" , HttpMethod .PUT , payload , getModelClass ());
195- return response .getBody ();
196- }
194+ ResponseEntity <T > response = restTemplate .exchange (this .config .getUrl () + API_PREFIX + getUrlPath () + "/" + objectId + "/" , HttpMethod .PUT , payload , getModelClass ());
195+ return response .getBody ();
196+ }
197197}
0 commit comments