@@ -39,159 +39,159 @@ public abstract class GenericDefectDojoService<T extends Model> {
39
39
private static final String API_PREFIX = "/api/v2/" ;
40
40
protected Config config ;
41
41
42
- protected ObjectMapper objectMapper ;
43
- protected ObjectMapper searchStringMapper ;
42
+ protected ObjectMapper objectMapper ;
43
+ protected ObjectMapper searchStringMapper ;
44
44
45
- @ Getter
46
- protected RestTemplate restTemplate ;
45
+ @ Getter
46
+ protected RestTemplate restTemplate ;
47
47
48
- public GenericDefectDojoService (Config config ) {
49
- this .config = config ;
48
+ public GenericDefectDojoService (Config config ) {
49
+ this .config = config ;
50
50
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 ();
55
55
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 );
60
60
61
- this .restTemplate = this .setupRestTemplate ();
62
- }
61
+ this .restTemplate = this .setupRestTemplate ();
62
+ }
63
63
64
64
65
- protected long DEFECT_DOJO_OBJET_LIMIT = 100L ;
65
+ protected long DEFECT_DOJO_OBJET_LIMIT = 100L ;
66
66
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
+ }
73
73
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
+ }
86
86
87
- protected abstract String getUrlPath ();
87
+ protected abstract String getUrlPath ();
88
88
89
- protected abstract Class <T > getModelClass ();
89
+ protected abstract Class <T > getModelClass ();
90
90
91
- protected abstract Response <T > deserializeList (String response ) throws JsonProcessingException ;
91
+ protected abstract Response <T > deserializeList (String response ) throws JsonProcessingException ;
92
92
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 ());
96
96
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
+ );
103
103
104
- return response .getBody ();
105
- }
104
+ return response .getBody ();
105
+ }
106
106
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 ());
110
110
111
- var mutableQueryParams = new HashMap <String , Object >(queryParams );
111
+ var mutableQueryParams = new HashMap <String , Object >(queryParams );
112
112
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 ));
115
115
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
+ }
120
120
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 );
123
123
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
+ );
130
130
131
- return deserializeList (responseString .getBody ());
132
- }
131
+ return deserializeList (responseString .getBody ());
132
+ }
133
133
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 <>();
136
136
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 ());
142
142
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 );
148
148
149
- return objects ;
150
- }
149
+ return objects ;
150
+ }
151
151
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
+ }
155
155
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 );
159
159
160
- var objects = search (queryParams );
160
+ var objects = search (queryParams );
161
161
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
+ }
166
166
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 );
169
169
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
+ }
174
174
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 ());
178
178
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
+ }
182
182
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 ());
186
186
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
+ }
189
189
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 ());
193
193
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
+ }
197
197
}
0 commit comments