[java-identity-provider] branch main updated: IDP-1922 - Add more customization to HTTP connection in "simple" CLI

Scott Cantor cantor.2 at osu.edu
Thu Mar 17 20:19:05 UTC 2022


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch main
in repository java-identity-provider.

View the commit online:
http://git.shibboleth.net/view/?p=java-identity-provider.git;a=commit;h=425680506b034d90adef2d25050fe22fb7e3da92

The following commit(s) were added to refs/heads/main by this push:
     new 425680506 IDP-1922 - Add more customization to HTTP connection in "simple" CLI
425680506 is described below

commit 425680506b034d90adef2d25050fe22fb7e3da92
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Thu Mar 17 16:19:02 2022 -0400

    IDP-1922 - Add more customization to HTTP connection in "simple" CLI
    
    https://shibboleth.atlassian.net/browse/IDP-1922
    
    Add custom header option.
---
 .../idp/cli/AbstractCommandLineArguments.java      | 36 +++++++++++++++++++++-
 .../src/main/java/net/shibboleth/idp/cli/CLI.java  | 12 +++++++-
 .../shibboleth/idp/cli/CommandLineArguments.java   | 16 ++++++++++
 3 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/idp-core/src/main/java/net/shibboleth/idp/cli/AbstractCommandLineArguments.java b/idp-core/src/main/java/net/shibboleth/idp/cli/AbstractCommandLineArguments.java
index a034696db..4d2061438 100644
--- a/idp-core/src/main/java/net/shibboleth/idp/cli/AbstractCommandLineArguments.java
+++ b/idp-core/src/main/java/net/shibboleth/idp/cli/AbstractCommandLineArguments.java
@@ -20,6 +20,10 @@ package net.shibboleth.idp.cli;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
@@ -27,7 +31,10 @@ import javax.net.ssl.HostnameVerifier;
 import javax.net.ssl.HttpsURLConnection;
 import javax.net.ssl.SSLSession;
 
+import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
 import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
+import net.shibboleth.utilities.java.support.annotation.constraint.NotLive;
+import net.shibboleth.utilities.java.support.annotation.constraint.Unmodifiable;
 import net.shibboleth.utilities.java.support.codec.Base64Support;
 import net.shibboleth.utilities.java.support.codec.EncodingException;
 import net.shibboleth.utilities.java.support.primitive.StringSupport;
@@ -82,6 +89,10 @@ public abstract class AbstractCommandLineArguments implements CommandLineArgumen
             description = "Password to be used in HTTP-Basic Auth")
     @Nullable private String password;
 
+    /** HTTP header name/value pair(s). */
+    @Parameter(names = {"--header"}, required = false, arity = 2, description = "HTTP header name and value")
+    @Nullable private List<String> headers;
+    
     /** Constructor. */
     public AbstractCommandLineArguments() {
         url = System.getProperty(BASEURL_PROPERTY);
@@ -176,6 +187,29 @@ public abstract class AbstractCommandLineArguments implements CommandLineArgumen
     @Nullable public String getPassword() {
         return password;
     }
+    
+    /** {@inheritDoc} */
+    @Nullable @NonnullElements @NotLive @Unmodifiable public Map<String,String> getHeaders() {
+        if (headers != null) {
+            final Map<String,String> map = new HashMap<>();
+            final Iterator<String> iter = headers.iterator();
+            while (iter.hasNext()) {
+                final String h = iter.next();
+                if (iter.hasNext()) {
+                    final String v = iter.next();
+                    if (h != null && v != null) {
+                        if (map.containsKey(h)) {
+                            map.put(h, map.get(h).concat(",").concat(v));
+                        } else {
+                            map.put(h, v);
+                        }
+                    }
+                }
+            }
+            return map;
+        }
+        return null;
+    }
 
     /** {@inheritDoc} */
     @Override
@@ -236,7 +270,7 @@ public abstract class AbstractCommandLineArguments implements CommandLineArgumen
         try {
             return "Basic " + Base64Support.encode(rawHeader.getBytes(StandardCharsets.UTF_8), false);
         } catch (final EncodingException e) {
-            System.err.print(e.getMessage());
+            System.err.println(e.getMessage());
             return null;
         }
     }
diff --git a/idp-core/src/main/java/net/shibboleth/idp/cli/CLI.java b/idp-core/src/main/java/net/shibboleth/idp/cli/CLI.java
index b62a73114..3b76b49ba 100644
--- a/idp-core/src/main/java/net/shibboleth/idp/cli/CLI.java
+++ b/idp-core/src/main/java/net/shibboleth/idp/cli/CLI.java
@@ -25,6 +25,9 @@ import java.lang.reflect.Constructor;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
 
 import javax.annotation.Nonnull;
 
@@ -105,9 +108,16 @@ public final class CLI {
             final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
             final String authorization = args.getBasicAuthHeader();
             if (authorization != null) {
-                System.out.println("Using HTTP-Basic authentication");
                 connection.setRequestProperty("Authorization", authorization);
             }
+            
+            final Map<String,String> headers = args.getHeaders();
+            if (headers != null) {
+                for (final Map.Entry<String,String> h : headers.entrySet()) {
+                    connection.setRequestProperty(h.getKey(), h.getValue());
+                }
+            }
+            
             try (final InputStream stream = connection.getInputStream()) {
                 try (final InputStreamReader reader = new InputStreamReader(stream)) {
                     try (final BufferedReader in = new BufferedReader(reader)) {
diff --git a/idp-core/src/main/java/net/shibboleth/idp/cli/CommandLineArguments.java b/idp-core/src/main/java/net/shibboleth/idp/cli/CommandLineArguments.java
index fb41f0cbd..fa8e31c39 100644
--- a/idp-core/src/main/java/net/shibboleth/idp/cli/CommandLineArguments.java
+++ b/idp-core/src/main/java/net/shibboleth/idp/cli/CommandLineArguments.java
@@ -19,10 +19,15 @@ package net.shibboleth.idp.cli;
 
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.Map;
 
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
+import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
+import net.shibboleth.utilities.java.support.annotation.constraint.NotLive;
+import net.shibboleth.utilities.java.support.annotation.constraint.Unmodifiable;
+
 /**
  * Interface for JCommander command line argument handling for an HTTP-based remote service call. 
  */
@@ -62,4 +67,15 @@ public interface CommandLineArguments {
         return null;
     }
 
+    /**
+     * Values of "header" parameter.
+     * 
+     * @return header parameter
+     * 
+     * @since 4.2.0
+     */
+    @Nullable @NonnullElements @NotLive @Unmodifiable public default Map<String,String> getHeaders() {
+        return null;
+    }
+
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list