This is an automated email from the ASF dual-hosted git repository.

hansva pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hop.git


The following commit(s) were added to refs/heads/master by this push:
     new 88a400c462 #3407: fix authorization as explicitly basic preemptive @ 
httppost and http client
     new 082b68f081 Merge pull request #3430 from roy20021/master
88a400c462 is described below

commit 88a400c46220189d8bd64391a288fe133cbfd516
Author: Andrea Esposito <[email protected]>
AuthorDate: Fri Nov 17 14:47:26 2023 +0100

    #3407: fix authorization as explicitly basic preemptive @ httppost and http 
client
---
 .gitignore                                         |  1 +
 .../apache/hop/pipeline/transforms/http/Http.java  | 41 ++++++++++++----------
 .../hop/pipeline/transforms/http/HttpTest.java     | 34 ++++++++++--------
 .../hop/pipeline/transforms/httppost/HttpPost.java | 36 +++++++++++--------
 4 files changed, 63 insertions(+), 49 deletions(-)

diff --git a/.gitignore b/.gitignore
index 4fa91aa206..aa1a6319a7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,7 @@
 .project
 .classpath
 .settings
+.factorypath
 build
 target
 release.properties
diff --git 
a/plugins/transforms/http/src/main/java/org/apache/hop/pipeline/transforms/http/Http.java
 
b/plugins/transforms/http/src/main/java/org/apache/hop/pipeline/transforms/http/Http.java
index 8f45d8927a..4770004ecc 100644
--- 
a/plugins/transforms/http/src/main/java/org/apache/hop/pipeline/transforms/http/Http.java
+++ 
b/plugins/transforms/http/src/main/java/org/apache/hop/pipeline/transforms/http/Http.java
@@ -17,7 +17,12 @@
 
 package org.apache.hop.pipeline.transforms.http;
 
-import com.google.common.annotations.VisibleForTesting;
+import java.net.HttpURLConnection;
+import java.net.URI;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.List;
+
 import org.apache.commons.lang.StringUtils;
 import org.apache.hop.core.Const;
 import org.apache.hop.core.exception.HopException;
@@ -50,11 +55,7 @@ import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.util.EntityUtils;
 import org.json.simple.JSONObject;
 
-import java.net.HttpURLConnection;
-import java.net.URI;
-import java.net.UnknownHostException;
-import java.util.ArrayList;
-import java.util.List;
+import com.google.common.annotations.VisibleForTesting;
 
 /** Retrieves data from an Http endpoint */
 public class Http extends BaseTransform<HttpMeta, HttpData> {
@@ -152,21 +153,23 @@ public class Http extends BaseTransform<HttpMeta, 
HttpData> {
         long startTime = System.currentTimeMillis();
 
         // Preemptive authentication
+        HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), 
uri.getScheme());
         if (StringUtils.isNotBlank(data.realProxyHost)) {
-          HttpHost target = new HttpHost(data.realProxyHost, 
data.realProxyPort, "http");
-          // Create AuthCache instance
-          AuthCache authCache = new BasicAuthCache();
-          // Generate BASIC scheme object and add it to the local
-          // auth cache
-          BasicScheme basicAuth = new BasicScheme();
-          authCache.put(target, basicAuth);
-          // Add AuthCache to the execution context
-          HttpClientContext localContext = HttpClientContext.create();
-          localContext.setAuthCache(authCache);
-          httpResponse = httpClient.execute(target, method, localContext);
-        } else {
-          httpResponse = httpClient.execute(method);
+          target = new HttpHost(data.realProxyHost, data.realProxyPort, 
"http");
         }
+
+        // Create AuthCache instance
+        AuthCache authCache = new BasicAuthCache();
+        // Generate BASIC scheme object and add it to the local
+        // auth cache
+        BasicScheme basicAuth = new BasicScheme();
+        authCache.put(target, basicAuth);
+        // Add AuthCache to the execution context
+        HttpClientContext localContext = HttpClientContext.create();
+        localContext.setAuthCache(authCache);
+
+        httpResponse = httpClient.execute(target, method, localContext);
+
         // calculate the responseTime
         long responseTime = System.currentTimeMillis() - startTime;
         if (log.isDetailed()) {
diff --git 
a/plugins/transforms/http/src/test/java/org/apache/hop/pipeline/transforms/http/HttpTest.java
 
b/plugins/transforms/http/src/test/java/org/apache/hop/pipeline/transforms/http/HttpTest.java
index 0a20ec780d..c89dea06b2 100644
--- 
a/plugins/transforms/http/src/test/java/org/apache/hop/pipeline/transforms/http/HttpTest.java
+++ 
b/plugins/transforms/http/src/test/java/org/apache/hop/pipeline/transforms/http/HttpTest.java
@@ -17,33 +17,35 @@
 
 package org.apache.hop.pipeline.transforms.http;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doCallRealMethod;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.powermock.api.mockito.PowerMockito.mockStatic;
+import static org.powermock.reflect.Whitebox.setInternalState;
+
+import java.io.ByteArrayInputStream;
+import java.net.HttpURLConnection;
+
 import org.apache.hop.core.logging.ILogChannel;
 import org.apache.hop.core.row.IRowMeta;
 import org.apache.hop.core.util.HttpClientManager;
 import org.apache.http.Header;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
 import org.apache.http.client.methods.CloseableHttpResponse;
-import org.apache.http.client.methods.HttpGet;
 import org.apache.http.entity.BasicHttpEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.protocol.HttpContext;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.powermock.core.classloader.annotations.PrepareForTest;
 import org.powermock.modules.junit4.PowerMockRunner;
 
-import java.io.ByteArrayInputStream;
-import java.net.HttpURLConnection;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotEquals;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.doCallRealMethod;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-import static org.powermock.api.mockito.PowerMockito.mockStatic;
-import static org.powermock.reflect.Whitebox.setInternalState;
-
 @RunWith(PowerMockRunner.class)
 @PrepareForTest(HttpClientManager.class)
 public class HttpTest {
@@ -72,7 +74,9 @@ public class HttpTest {
     doReturn(client).when(builder).build();
 
     CloseableHttpResponse response = mock(CloseableHttpResponse.class);
-    doReturn(response).when(client).execute(any(HttpGet.class));
+    doReturn(response)
+        .when(client)
+        .execute(any(HttpHost.class), any(HttpRequest.class), 
any(HttpContext.class));
 
     BasicHttpEntity entity = new BasicHttpEntity();
     entity.setContent(new ByteArrayInputStream(DATA.getBytes()));
diff --git 
a/plugins/transforms/httppost/src/main/java/org/apache/hop/pipeline/transforms/httppost/HttpPost.java
 
b/plugins/transforms/httppost/src/main/java/org/apache/hop/pipeline/transforms/httppost/HttpPost.java
index 5ba28f090f..a2e802cf29 100644
--- 
a/plugins/transforms/httppost/src/main/java/org/apache/hop/pipeline/transforms/httppost/HttpPost.java
+++ 
b/plugins/transforms/httppost/src/main/java/org/apache/hop/pipeline/transforms/httppost/HttpPost.java
@@ -19,15 +19,16 @@ package org.apache.hop.pipeline.transforms.httppost;
 
 import static 
org.apache.hop.pipeline.transforms.httppost.HttpPostMeta.DEFAULT_ENCODING;
 
-import com.google.common.annotations.VisibleForTesting;
 import java.io.InputStreamReader;
 import java.io.UnsupportedEncodingException;
 import java.net.HttpURLConnection;
+import java.net.URI;
 import java.net.URLEncoder;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+
 import org.apache.commons.lang.StringUtils;
 import org.apache.hop.core.Const;
 import org.apache.hop.core.exception.HopException;
@@ -63,6 +64,8 @@ import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.util.EntityUtils;
 import org.json.simple.JSONObject;
 
+import com.google.common.annotations.VisibleForTesting;
+
 /** Make a HTTP Post call */
 public class HttpPost extends BaseTransform<HttpPostMeta, HttpPostData> {
 
@@ -115,8 +118,9 @@ public class HttpPost extends BaseTransform<HttpPostMeta, 
HttpPostData> {
         logDetailed(BaseMessages.getString(PKG, 
"HTTPPOST.Log.ConnectingToURL", data.realUrl));
       }
       URIBuilder uriBuilder = new URIBuilder(data.realUrl);
+      URI uri = uriBuilder.build();
       org.apache.http.client.methods.HttpPost post =
-          new org.apache.http.client.methods.HttpPost(uriBuilder.build());
+          new org.apache.http.client.methods.HttpPost(uri);
       String bodyParams = null;
 
       // Specify content type and encoding
@@ -243,21 +247,23 @@ public class HttpPost extends BaseTransform<HttpPostMeta, 
HttpPostData> {
         long startTime = System.currentTimeMillis();
 
         // Execute the POST method
+        HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), 
uri.getScheme());
         if (StringUtils.isNotBlank(data.realProxyHost)) {
-          HttpHost target = new HttpHost(data.realProxyHost, 
data.realProxyPort, "http");
-          // Create AuthCache instance
-          AuthCache authCache = new BasicAuthCache();
-          // Generate BASIC scheme object and add it to the local
-          // auth cache
-          BasicScheme basicAuth = new BasicScheme();
-          authCache.put(target, basicAuth);
-          // Add AuthCache to the execution context
-          HttpClientContext localContext = HttpClientContext.create();
-          localContext.setAuthCache(authCache);
-          httpResponse = httpClient.execute(target, post, localContext);
-        } else {
-          httpResponse = httpClient.execute(post);
+          target = new HttpHost(data.realProxyHost, data.realProxyPort, 
"http");
         }
+
+        // Create AuthCache instance
+        AuthCache authCache = new BasicAuthCache();
+        // Generate BASIC scheme object and add it to the local
+        // auth cache
+        BasicScheme basicAuth = new BasicScheme();
+        authCache.put(target, basicAuth);
+        // Add AuthCache to the execution context
+        HttpClientContext localContext = HttpClientContext.create();
+        localContext.setAuthCache(authCache);
+
+        httpResponse = httpClient.execute(target, post, localContext);
+
         int statusCode = requestStatusCode(httpResponse);
 
         // calculate the responseTime

Reply via email to