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

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


The following commit(s) were added to refs/heads/main by this push:
     new 318ca3b800 Fix#6697 Refactor API key header handling and prevent 
duplicate headers (#6699)
318ca3b800 is described below

commit 318ca3b80049ca1fd938b17e149cca746b283d4a
Author: lance <[email protected]>
AuthorDate: Thu Mar 5 15:27:11 2026 +0800

    Fix#6697 Refactor API key header handling and prevent duplicate headers 
(#6699)
    
    * Refactor API key header handling and prevent duplicate headers
    
    Signed-off-by: lance <[email protected]>
    
    * Refactor API key header handling and prevent duplicate headers
    
    Signed-off-by: lance <[email protected]>
    
    ---------
    
    Signed-off-by: lance <[email protected]>
---
 .../apache/hop/pipeline/transforms/rest/Rest.java  | 61 ++++++++++++++--------
 .../pipeline/transforms/rest/RestCallRestTest.java |  2 +-
 2 files changed, 41 insertions(+), 22 deletions(-)

diff --git 
a/plugins/transforms/rest/src/main/java/org/apache/hop/pipeline/transforms/rest/Rest.java
 
b/plugins/transforms/rest/src/main/java/org/apache/hop/pipeline/transforms/rest/Rest.java
index 0f041c5f82..7948ed22d2 100644
--- 
a/plugins/transforms/rest/src/main/java/org/apache/hop/pipeline/transforms/rest/Rest.java
+++ 
b/plugins/transforms/rest/src/main/java/org/apache/hop/pipeline/transforms/rest/Rest.java
@@ -196,34 +196,19 @@ public class Rest extends BaseTransform<RestMeta, 
RestData> {
 
       // set the Authentication/Authorization header from the connection 
first, if available.
       // this transform's headers will override this value if available.
-      if (connection != null) {
-        if (connection.getAuthType().equals("API Key")) {
-          if 
(!StringUtils.isEmpty(resolve(connection.getAuthorizationHeaderName())))
-            if 
(!Utils.isEmpty(resolve(connection.getAuthorizationHeaderName()))) {
-              if 
(!StringUtils.isEmpty(resolve(connection.getAuthorizationPrefix()))) {
-                invocationBuilder.header(
-                    resolve(connection.getAuthorizationHeaderName()),
-                    resolve(connection.getAuthorizationPrefix())
-                        + " "
-                        + resolve(connection.getAuthorizationHeaderValue()));
-              } else {
-                invocationBuilder.header(
-                    resolve(connection.getAuthorizationHeaderName()),
-                    resolve(connection.getAuthorizationHeaderValue()));
-              }
-            }
-        }
-      }
+      MultivaluedMap<String, Object> headerMap = new MultivaluedHashMap<>();
+      addApiKeyHeaderIfAbsent(headerMap);
 
       boolean acceptHeaderProvided = false;
-      String contentType = null; // media type override, if not null
+      // media type override, if not null
+      String contentType = null;
       if (data.useHeaders) {
         // Add headers
         for (int i = 0; i < data.nrheader; i++) {
           String value = data.inputRowMeta.getString(rowData, 
data.indexOfHeaderFields[i]);
 
           // unsure if an already set header will be returned to builder
-          invocationBuilder.header(data.headerNames[i], value);
+          headerMap.putSingle(data.headerNames[i], value);
           if ("Content-Type".equals(data.headerNames[i])) {
             contentType = value;
           }
@@ -238,7 +223,7 @@ public class Rest extends BaseTransform<RestMeta, RestData> 
{
       }
 
       if (!acceptHeaderProvided && data.mediaType != null) {
-        invocationBuilder = invocationBuilder.accept(data.mediaType);
+        headerMap.putSingle("Accept", data.mediaType);
       }
 
       Response response = null;
@@ -252,6 +237,7 @@ public class Rest extends BaseTransform<RestMeta, RestData> 
{
       }
 
       // execute first call
+      invocationBuilder.headers(headerMap);
       final Invocation.Builder finalInvocationBuilder = invocationBuilder;
       final String finalEntityString = entityString;
       final String finalContentType = contentType;
@@ -547,6 +533,39 @@ public class Rest extends BaseTransform<RestMeta, 
RestData> {
     }
   }
 
+  /**
+   * set the Authentication/Authorization header from the connection first, if 
available. this
+   * transform's headers will override this value if available.
+   *
+   * @param headers the mutable map containing HTTP headers that will be sent 
with the request
+   */
+  private void addApiKeyHeaderIfAbsent(MultivaluedMap<String, Object> headers) 
{
+    if (connection == null) {
+      return;
+    }
+
+    if (!"API Key".equals(connection.getAuthType())) {
+      return;
+    }
+
+    String headerName = resolve(connection.getAuthorizationHeaderName());
+    String headerValue = resolve(connection.getAuthorizationHeaderValue());
+    String prefix = resolve(connection.getAuthorizationPrefix());
+
+    if (Utils.isEmpty(headerName) || Utils.isEmpty(headerValue)) {
+      return;
+    }
+
+    if (!Utils.isEmpty(prefix)) {
+      headerValue = prefix + " " + headerValue;
+    }
+
+    // Only add header if not already present
+    if (!headers.containsKey(headerName)) {
+      headers.putSingle(headerName, headerValue);
+    }
+  }
+
   private void setConfig() throws HopException {
     if (data.config == null) {
       // Use ApacheHttpClient for supporting proxy authentication.
diff --git 
a/plugins/transforms/rest/src/test/java/org/apache/hop/pipeline/transforms/rest/RestCallRestTest.java
 
b/plugins/transforms/rest/src/test/java/org/apache/hop/pipeline/transforms/rest/RestCallRestTest.java
index be6ec15a8a..7552e6d608 100644
--- 
a/plugins/transforms/rest/src/test/java/org/apache/hop/pipeline/transforms/rest/RestCallRestTest.java
+++ 
b/plugins/transforms/rest/src/test/java/org/apache/hop/pipeline/transforms/rest/RestCallRestTest.java
@@ -783,7 +783,7 @@ class RestCallRestTest {
       // Verify
       assertNotNull(outputRow);
       assertEquals("{\"authenticated\":true}", outputRow[2]);
-      verify(builder, times(2)).header(anyString(), any());
+      verify(builder).headers(any());
     }
   }
 

Reply via email to