|
24 | 24 | import io.cdap.plugin.http.common.OAuth2GrantType; |
25 | 25 | import io.cdap.plugin.http.common.pagination.page.JSONUtil; |
26 | 26 | import io.cdap.plugin.http.source.common.BaseHttpSourceConfig; |
| 27 | +import org.apache.http.HttpHost; |
| 28 | +import org.apache.http.auth.AuthScope; |
| 29 | +import org.apache.http.auth.UsernamePasswordCredentials; |
| 30 | +import org.apache.http.client.CredentialsProvider; |
27 | 31 | import org.apache.http.client.entity.UrlEncodedFormEntity; |
28 | 32 | import org.apache.http.client.methods.CloseableHttpResponse; |
29 | 33 | import org.apache.http.client.methods.HttpPost; |
30 | 34 | import org.apache.http.client.utils.URIBuilder; |
| 35 | +import org.apache.http.impl.client.BasicCredentialsProvider; |
31 | 36 | import org.apache.http.impl.client.CloseableHttpClient; |
| 37 | +import org.apache.http.impl.client.HttpClientBuilder; |
32 | 38 | import org.apache.http.impl.client.HttpClients; |
33 | 39 | import org.apache.http.message.BasicHeader; |
34 | 40 | import org.apache.http.message.BasicNameValuePair; |
@@ -83,14 +89,26 @@ public static AccessToken getAccessToken(BaseHttpConfig config) throws IOExcepti |
83 | 89 | // get accessToken from service account |
84 | 90 | return OAuthUtil.getAccessTokenByServiceAccount(config); |
85 | 91 | case OAUTH2: |
| 92 | + HttpClientBuilder httpClientBuilder = HttpClients.custom(); |
| 93 | + |
86 | 94 | if (config instanceof BaseHttpSourceConfig) { |
87 | | - try (CloseableHttpClient client = HttpClients.custom() |
88 | | - .setSSLSocketFactory(new SSLConnectionSocketFactoryCreator((BaseHttpSourceConfig) config).create()) |
89 | | - .build()) { |
90 | | - return getAccessToken(client, config); |
| 95 | + httpClientBuilder.setSSLSocketFactory( |
| 96 | + new SSLConnectionSocketFactoryCreator((BaseHttpSourceConfig) config).create() |
| 97 | + ); |
| 98 | + } |
| 99 | + // Apply Proxy settings if they exist |
| 100 | + if (!Strings.isNullOrEmpty(config.getProxyUrl())) { |
| 101 | + HttpHost proxyHost = HttpHost.create(config.getProxyUrl()); |
| 102 | + |
| 103 | + if (!Strings.isNullOrEmpty(config.getProxyUsername()) && !Strings.isNullOrEmpty(config.getProxyPassword())) { |
| 104 | + CredentialsProvider credsProvider = new BasicCredentialsProvider(); |
| 105 | + credsProvider.setCredentials(new AuthScope(proxyHost), |
| 106 | + new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword())); |
| 107 | + httpClientBuilder.setDefaultCredentialsProvider(credsProvider); |
91 | 108 | } |
| 109 | + httpClientBuilder.setProxy(proxyHost); |
92 | 110 | } |
93 | | - try (CloseableHttpClient client = HttpClients.createDefault()) { |
| 111 | + try (CloseableHttpClient client = httpClientBuilder.build()) { |
94 | 112 | return getAccessToken(client, config); |
95 | 113 | } |
96 | 114 | } |
|
0 commit comments