77import io .securecodebox .persistence .defectdojo .ScanType ;
88import io .securecodebox .persistence .defectdojo .config .Config ;
99import io .securecodebox .persistence .defectdojo .exception .PersistenceException ;
10+ import io .securecodebox .persistence .defectdojo .http .ProxyConfig ;
1011import io .securecodebox .persistence .defectdojo .model .ScanFile ;
1112import lombok .Getter ;
1213import lombok .NonNull ;
@@ -44,22 +45,23 @@ class DefaultImportScanService implements ImportScanService {
4445 new FormHttpMessageConverter (),
4546 new ResourceHttpMessageConverter (),
4647 new MappingJackson2HttpMessageConverter ());
47- @ Deprecated
48- private final SystemPropertyFinder properties = new SystemPropertyFinder ();
4948 @ Getter
5049 private final String defectDojoUrl ;
5150 @ Getter
5251 private final String defectDojoApiKey ;
52+ private final ProxyConfig proxyConfig ;
5353
5454 /**
5555 * Dedicated constructor.
5656 *
57- * @param config not {@code null}
57+ * @param config not {@code null}
58+ * @param proxyConfig not {@code null}
5859 */
59- DefaultImportScanService (final @ NonNull Config config ) {
60+ DefaultImportScanService (final @ NonNull Config config , @ NonNull ProxyConfig proxyConfig ) {
6061 super ();
6162 this .defectDojoUrl = config .getUrl ();
6263 this .defectDojoApiKey = config .getApiKey ();
64+ this .proxyConfig = proxyConfig ;
6365 }
6466
6567 @ Override
@@ -163,8 +165,7 @@ private RestTemplate createRestTemplate() {
163165 }
164166
165167 boolean shouldConfigureProxySettings () {
166- return properties .hasProperty (ProxyConfigNames .HTTP_PROXY_USER )
167- && properties .hasProperty (ProxyConfigNames .HTTP_PROXY_PASSWORD );
168+ return proxyConfig .isComplete ();
168169 }
169170
170171 /**
@@ -184,72 +185,22 @@ boolean shouldConfigureProxySettings() {
184185 * @return never {@code null}
185186 */
186187 ClientHttpRequestFactory createRequestFactoryWithProxyAuthConfig () {
187- if (properties .notHasProperty (ProxyConfigNames .HTTP_PROXY_USER )) {
188- throw new MissingProxyAuthenticationConfig (ProxyConfigNames .HTTP_PROXY_USER );
189- }
190-
191- if (properties .notHasProperty (ProxyConfigNames .HTTP_PROXY_PASSWORD )) {
192- throw new MissingProxyAuthenticationConfig (ProxyConfigNames .HTTP_PROXY_PASSWORD );
193- }
194-
195- if (properties .notHasProperty (ProxyConfigNames .HTTP_PROXY_HOST )) {
196- throw new MissingProxyAuthenticationConfig (ProxyConfigNames .HTTP_PROXY_HOST );
197- }
198-
199- if (properties .notHasProperty (ProxyConfigNames .HTTP_PROXY_PORT )) {
200- throw new MissingProxyAuthenticationConfig (ProxyConfigNames .HTTP_PROXY_PORT );
201- }
202-
203- final var proxyHost = properties .getProperty (ProxyConfigNames .HTTP_PROXY_HOST );
204- final int proxyPort ;
205- try {
206- proxyPort = Integer .parseInt (properties .getProperty (ProxyConfigNames .HTTP_PROXY_PORT ));
207- } catch (final NumberFormatException e ) {
208- throw new IllegalArgumentException (
209- String .format ("Given port for proxy authentication configuration (property '%s') is not a valid number! Given value wa '%s'." ,
210- ProxyConfigNames .HTTP_PROXY_PORT .getLiterat (),
211- System .getProperty ("http.proxyPort" )),
212- e );
213- }
214-
215188 final var credentials = new BasicCredentialsProvider ();
216189 credentials .setCredentials (
217- new AuthScope (proxyHost , proxyPort ),
190+ new AuthScope (proxyConfig . getHost (), proxyConfig . getPort () ),
218191 new UsernamePasswordCredentials (
219- properties . getProperty ( ProxyConfigNames . HTTP_PROXY_USER ),
220- properties . getProperty ( ProxyConfigNames . HTTP_PROXY_PASSWORD ))
192+ proxyConfig . getUser ( ),
193+ proxyConfig . getPassword ( ))
221194 );
222195
223196 final var clientBuilder = HttpClientBuilder .create ();
224197 clientBuilder .useSystemProperties ();
225- clientBuilder .setProxy (new HttpHost (proxyHost , proxyPort ));
198+ clientBuilder .setProxy (new HttpHost (proxyConfig . getHost (), proxyConfig . getPort () ));
226199 clientBuilder .setDefaultCredentialsProvider (credentials );
227200 clientBuilder .setProxyAuthenticationStrategy (new ProxyAuthenticationStrategy ());
228201
229202 final var factory = new HttpComponentsClientHttpRequestFactory ();
230203 factory .setHttpClient (clientBuilder .build ());
231204 return factory ;
232205 }
233-
234- @ Deprecated
235- private static class SystemPropertyFinder {
236- private boolean hasProperty (@ NonNull final ProxyConfigNames name ) {
237- return System .getProperty (name .getLiterat ()) != null ;
238- }
239-
240- private boolean notHasProperty (@ NonNull final ProxyConfigNames name ) {
241- return !hasProperty (name );
242- }
243-
244- private String getProperty (@ NonNull final ProxyConfigNames name ) {
245- return System .getProperty (name .getLiterat ());
246- }
247- }
248-
249- @ Deprecated
250- final static class MissingProxyAuthenticationConfig extends RuntimeException {
251- MissingProxyAuthenticationConfig (ProxyConfigNames name ) {
252- super (String .format ("Expected System property '%s' not set!" , name .getLiterat ()));
253- }
254- }
255206}
0 commit comments