Skip to content

Commit

Permalink
Add proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Aug 21, 2023
1 parent 6f0f2be commit 8337714
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions manage-server/src/main/java/manage/control/DatabaseController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
import manage.web.PreemptiveAuthenticationHttpComponentsClientHttpRequestFactory;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.apache.http.protocol.HttpContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
Expand Down Expand Up @@ -55,6 +60,9 @@ public class DatabaseController {

private final Environment environment;

private String proxyHost;
private int proxyPort;

@Autowired
DatabaseController(MetaDataRepository metaDataRepository,
@Value("${push.eb.url}") String pushUri,
Expand Down Expand Up @@ -205,6 +213,25 @@ private ClientHttpRequestFactory getRequestFactory(String user, String password)
BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
basicCredentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
httpClientBuilder.setDefaultCredentialsProvider(basicCredentialsProvider);

this.proxyHost = System.getProperty("http.proxyHost");
if (System.getProperty("http.proxyPort") != null) {
this.proxyPort = Integer.parseInt(System.getProperty("http.proxyPort"));
} else {
this.proxyPort = 8080;
}

if (this.proxyHost != null) {
HttpHost proxy = new HttpHost(this.proxyHost, this.proxyPort);

httpClientBuilder.setRoutePlanner(new DefaultProxyRoutePlanner(proxy) {
@Override
public HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
return super.determineProxy(target, request, context);
}
});
}

CloseableHttpClient httpClient = httpClientBuilder.build();
return new PreemptiveAuthenticationHttpComponentsClientHttpRequestFactory(httpClient, pushUri);
}
Expand Down

0 comments on commit 8337714

Please sign in to comment.