10
10
import com .fasterxml .jackson .databind .cfg .CoercionAction ;
11
11
import com .fasterxml .jackson .databind .cfg .CoercionInputShape ;
12
12
import io .securecodebox .persistence .defectdojo .config .ClientConfig ;
13
- import io .securecodebox .persistence .defectdojo .exception .PersistenceException ;
14
13
import io .securecodebox .persistence .defectdojo .exception .TooManyResponsesException ;
15
14
import io .securecodebox .persistence .defectdojo .http .AuthHeaderFactory ;
16
15
import io .securecodebox .persistence .defectdojo .http .Foo ;
34
33
import org .springframework .web .util .UriComponentsBuilder ;
35
34
36
35
import java .net .URI ;
37
- import java .net .URISyntaxException ;
38
36
import java .util .*;
39
37
40
38
/**
44
42
*/
45
43
@ Slf4j
46
44
abstract class GenericDefectDojoService <T extends Model > implements DefectDojoService <T > {
47
- private static final String API_PREFIX = "/api/v2/" ;
48
45
private static final long DEFECT_DOJO_OBJET_LIMIT = 100L ;
49
46
protected ClientConfig clientConfig ;
50
47
@@ -81,7 +78,7 @@ public final T get(long id) {
81
78
final var restTemplate = this .getRestTemplate ();
82
79
final HttpEntity <String > payload = new HttpEntity <>(getDefectDojoAuthorizationHeaders ());
83
80
84
- final var url = createBaseUrl () + id ;
81
+ final var url = createBaseUrl (). resolve ( String . valueOf ( id )) ;
85
82
log .debug ("Requesting URL: {}" , url );
86
83
final ResponseEntity <T > response = restTemplate .exchange (
87
84
url ,
@@ -151,20 +148,22 @@ public final void delete(long id) {
151
148
final var restTemplate = this .getRestTemplate ();
152
149
final HttpEntity <String > payload = new HttpEntity <>(getDefectDojoAuthorizationHeaders ());
153
150
154
- restTemplate .exchange (createBaseUrl () + id + "/" , HttpMethod .DELETE , payload , String .class );
151
+ final var url = createBaseUrl ().resolve (id + "/" );
152
+ restTemplate .exchange (url , HttpMethod .DELETE , payload , String .class );
155
153
}
156
154
157
155
@ Override
158
156
public final T update (@ NonNull T object , long id ) {
159
157
final var restTemplate = this .getRestTemplate ();
160
158
final HttpEntity <T > payload = new HttpEntity <>(object , getDefectDojoAuthorizationHeaders ());
161
- final ResponseEntity <T > response = restTemplate .exchange (createBaseUrl () + id + "/" , HttpMethod .PUT , payload , getModelClass ());
159
+ final var url = createBaseUrl ().resolve (id + "/" );
160
+ final ResponseEntity <T > response = restTemplate .exchange (url , HttpMethod .PUT , payload , getModelClass ());
162
161
163
162
return response .getBody ();
164
163
}
165
-
164
+
166
165
/**
167
- * Get the URL path for the REST endpoint relative to {@link #API_PREFIX}
166
+ * Get the URL path for the REST endpoint relative to {@link ClientConfig #API_PREFIX}
168
167
*
169
168
* @return not {@code null}, not empty
170
169
*/
@@ -185,8 +184,13 @@ public final T update(@NonNull T object, long id) {
185
184
*/
186
185
protected abstract PaginatedResult <T > deserializeList (@ NonNull String response );
187
186
188
- private String createBaseUrl () {
189
- return this .clientConfig .getUrl () + API_PREFIX + getUrlPath () + "/" ;
187
+ final URI createBaseUrl () {
188
+ final var buffer = clientConfig .getUrl () +
189
+ ClientConfig .API_PREFIX +
190
+ getUrlPath () +
191
+ '/' ;
192
+
193
+ return URI .create (buffer ).normalize ();
190
194
}
191
195
192
196
/**
@@ -226,13 +230,9 @@ protected PaginatedResult<T> internalSearch(Map<String, Object> queryParams, lon
226
230
227
231
final var url = createBaseUrl ();
228
232
final UriComponentsBuilder builder ;
229
- try {
230
- builder = UriComponentsBuilder
231
- .fromUri (new URI (url ))
232
- .queryParams (multiValueMap );
233
- } catch (URISyntaxException e ) {
234
- throw new PersistenceException ("Bad URL given: " + url , e );
235
- }
233
+ builder = UriComponentsBuilder
234
+ .fromUri (url )
235
+ .queryParams (multiValueMap );
236
236
237
237
final ResponseEntity <String > responseString = restTemplate .exchange (
238
238
builder .build (mutableQueryParams ),
0 commit comments