3333import org .apache .nifi .processor .ProcessContext ;
3434import org .apache .nifi .processor .exception .ProcessException ;
3535import org .apache .nifi .processor .util .StandardValidators ;
36+ import org .apache .nifi .proxy .ProxyConfiguration ;
37+ import org .apache .nifi .proxy .ProxyConfigurationService ;
3638import org .apache .nifi .ssl .SSLContextService ;
3739import org .apache .nifi .util .StringUtils ;
3840
3941import javax .net .ssl .SSLContext ;
4042import java .io .IOException ;
4143import java .io .InputStream ;
42- import java .net .InetSocketAddress ;
4344import java .net .Proxy ;
4445import java .net .URL ;
4546import java .util .ArrayList ;
@@ -141,6 +142,7 @@ protected PropertyDescriptor getSupportedDynamicPropertyDescriptor(String proper
141142 static {
142143 final List <PropertyDescriptor > properties = new ArrayList <>();
143144 properties .add (ES_URL );
145+ properties .add (ProxyConfigurationService .PROXY_CONFIGURATION_SERVICE );
144146 properties .add (PROXY_HOST );
145147 properties .add (PROXY_PORT );
146148 properties .add (PROXY_USERNAME );
@@ -164,28 +166,38 @@ protected void createElasticsearchClient(ProcessContext context) throws ProcessE
164166 OkHttpClient .Builder okHttpClient = new OkHttpClient .Builder ();
165167
166168 // Add a proxy if set
167- final String proxyHost = context .getProperty (PROXY_HOST ).evaluateAttributeExpressions ().getValue ();
168- final Integer proxyPort = context .getProperty (PROXY_PORT ).evaluateAttributeExpressions ().asInteger ();
169- if (proxyHost != null && proxyPort != null ) {
170- final Proxy proxy = new Proxy (Proxy .Type .HTTP , new InetSocketAddress (proxyHost , proxyPort ));
169+ final ProxyConfiguration proxyConfig = ProxyConfiguration .getConfiguration (context , () -> {
170+ final String proxyHost = context .getProperty (PROXY_HOST ).evaluateAttributeExpressions ().getValue ();
171+ final Integer proxyPort = context .getProperty (PROXY_PORT ).evaluateAttributeExpressions ().asInteger ();
172+ if (proxyHost != null && proxyPort != null ) {
173+ final ProxyConfiguration componentProxyConfig = new ProxyConfiguration ();
174+ componentProxyConfig .setProxyServerHost (proxyHost );
175+ componentProxyConfig .setProxyServerPort (proxyPort );
176+ componentProxyConfig .setProxyUserName (context .getProperty (PROXY_USERNAME ).evaluateAttributeExpressions ().getValue ());
177+ componentProxyConfig .setProxyUserPassword (context .getProperty (PROXY_PASSWORD ).evaluateAttributeExpressions ().getValue ());
178+ return componentProxyConfig ;
179+ }
180+ return ProxyConfiguration .DIRECT_CONFIGURATION ;
181+ });
182+
183+ if (!Proxy .Type .DIRECT .equals (proxyConfig .getProxyType ())) {
184+ final Proxy proxy = proxyConfig .createProxy ();
171185 okHttpClient .proxy (proxy );
172- }
173186
174- final String proxyUsername = context .getProperty (PROXY_USERNAME ).evaluateAttributeExpressions ().getValue ();
175- final String proxyPassword = context .getProperty (PROXY_PASSWORD ).evaluateAttributeExpressions ().getValue ();
176-
177- if (proxyUsername != null && proxyPassword != null ){
178- okHttpClient .proxyAuthenticator (new Authenticator () {
179- @ Override
180- public Request authenticate (Route route , Response response ) throws IOException {
181- final String credential =Credentials .basic (proxyUsername , proxyPassword );
182- return response .request ().newBuilder ()
183- .header ("Proxy-Authorization" , credential )
184- .build ();
185- }
186- });
187+ if (proxyConfig .hasCredential ()){
188+ okHttpClient .proxyAuthenticator (new Authenticator () {
189+ @ Override
190+ public Request authenticate (Route route , Response response ) throws IOException {
191+ final String credential =Credentials .basic (proxyConfig .getProxyUserName (), proxyConfig .getProxyUserPassword ());
192+ return response .request ().newBuilder ()
193+ .header ("Proxy-Authorization" , credential )
194+ .build ();
195+ }
196+ });
197+ }
187198 }
188199
200+
189201 // Set timeouts
190202 okHttpClient .connectTimeout ((context .getProperty (CONNECT_TIMEOUT ).evaluateAttributeExpressions ().asTimePeriod (TimeUnit .MILLISECONDS ).intValue ()), TimeUnit .MILLISECONDS );
191203 okHttpClient .readTimeout (context .getProperty (RESPONSE_TIMEOUT ).evaluateAttributeExpressions ().asTimePeriod (TimeUnit .MILLISECONDS ).intValue (), TimeUnit .MILLISECONDS );
0 commit comments