-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add proxy config to http client - Refactor rest template to use client - Fix SLF4J duplicate impls
- Loading branch information
Luke Sikina
committed
Oct 4, 2024
1 parent
0061638
commit a563728
Showing
7 changed files
with
104 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...uth-services/src/main/java/edu/harvard/hms/dbmi/avillach/auth/utils/RestClientConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package edu.harvard.hms.dbmi.avillach.auth.utils; | ||
|
||
|
||
import org.apache.hc.client5.http.auth.AuthScope; | ||
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; | ||
import org.apache.hc.client5.http.classic.HttpClient; | ||
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider; | ||
import org.apache.hc.client5.http.impl.classic.HttpClients; | ||
import org.apache.hc.core5.http.HttpHost; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
public class RestClientConfig { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(RestClientConfig.class); | ||
|
||
@Value("${http.proxyHost:}") | ||
private String proxyHost; | ||
|
||
@Value("${http.proxyPort:}") | ||
private int proxyPort; | ||
|
||
@Value("${http.proxyUser:}") | ||
private String proxyUser; | ||
|
||
@Value("${http.proxyPassword:}") | ||
private String proxyPassword; | ||
|
||
@Bean | ||
public HttpClient getHttpClient() { | ||
if (!StringUtils.hasLength(proxyHost)) { | ||
return HttpClients.createDefault(); | ||
} else if (!StringUtils.hasLength(proxyUser)) { | ||
LOG.info("Utilizing unauthenticated proxy: host={}", proxyHost); | ||
return HttpClients.custom() | ||
.setProxy(new HttpHost(proxyHost, proxyPort)) | ||
.build(); | ||
} else { | ||
LOG.info("Utilizing authenticated proxy: host={}, user={}", proxyHost, proxyUser); | ||
|
||
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); | ||
credentialsProvider.setCredentials( | ||
new AuthScope(proxyHost, proxyPort), | ||
new UsernamePasswordCredentials(proxyUser, proxyPassword.toCharArray())); | ||
|
||
return HttpClients.custom() | ||
.setDefaultCredentialsProvider(credentialsProvider) | ||
.setProxy(new HttpHost(proxyHost, proxyPort)) | ||
.build(); | ||
} | ||
} | ||
|
||
@Bean | ||
public RestTemplate getRestTemplate(@Autowired HttpClient client) { | ||
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); | ||
factory.setHttpClient(client); | ||
return new RestTemplate(factory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters