You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2021/02/06 20:16:28 UTC

[cxf] 01/03: CXF-8407: Incorporate changes caused by JEP-396 integration into JDK-16+ (part 2) (#744)

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

reta pushed a commit to branch 3.4.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit 1efdffb512580ec229e498cd785e83b5bdc69dc5
Author: Andriy Redko <dr...@gmail.com>
AuthorDate: Sat Feb 6 10:39:28 2021 -0500

    CXF-8407: Incorporate changes caused by JEP-396 integration into JDK-16+ (part 2) (#744)
    
    (cherry picked from commit 9ef7499fc3f86bdfecbcfe9d1ac537a783876918)
---
 .../transport/http/ReferencingAuthenticator.java   | 68 ++++++++++++++++++++++
 1 file changed, 68 insertions(+)

diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/ReferencingAuthenticator.java b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/ReferencingAuthenticator.java
index 2b63316..3ad9260 100644
--- a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/ReferencingAuthenticator.java
+++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/ReferencingAuthenticator.java
@@ -18,13 +18,18 @@
  */
 package org.apache.cxf.transport.http;
 
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
 import java.lang.ref.Reference;
 import java.lang.ref.WeakReference;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.net.Authenticator;
+import java.net.InetAddress;
 import java.net.PasswordAuthentication;
+import java.net.URL;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 
@@ -82,6 +87,33 @@ public class ReferencingAuthenticator extends Authenticator {
     }
 
     private void remove() {
+        try { 
+            // Try Authenticator.getDefault() first, JDK9+
+            final MethodHandle mt = MethodHandles
+               .lookup()
+               .findStatic(Authenticator.class, "getDefault", MethodType.methodType(Authenticator.class));
+            removeInternal((Authenticator)mt.invoke());
+        } catch (final NoSuchMethodException | IllegalAccessException ex) {
+            removeInternal();
+        } catch (Throwable e) {
+            //ignore
+        }
+    }
+    
+    private void removeInternal(final Authenticator def) {
+        try {
+            if (def == this) {
+                //this is at the root of any chain of authenticators
+                Authenticator.setDefault(wrapped);
+            } else {
+                removeFromChain(def);
+            }
+        } catch (Throwable t) {
+            //ignore
+        }
+    }
+    
+    private void removeInternal() {
         try {
             for (final Field f : Authenticator.class.getDeclaredFields()) {
                 if (f.getType().equals(Authenticator.class)) {
@@ -126,6 +158,42 @@ public class ReferencingAuthenticator extends Authenticator {
         if (a == null) {
             return null;
         }
+        
+        try {
+            // Try Authenticator.requestPasswordAuthentication() first, JDK9+
+            final MethodHandle mt = MethodHandles
+               .lookup()
+               .findStatic(Authenticator.class, "requestPasswordAuthentication", 
+                   MethodType.methodType(PasswordAuthentication.class, new Class<?>[] {
+                       Authenticator.class,
+                       String.class,
+                       InetAddress.class,
+                       int.class,
+                       String.class,
+                       String.class,
+                       String.class,
+                       URL.class,
+                       RequestorType.class
+                   }));
+    
+            return (PasswordAuthentication)mt.invoke(a, getRequestingHost(), getRequestingSite(), 
+                getRequestingPort(), getRequestingProtocol(), getRequestingPrompt(), getRequestingScheme(), 
+                    getRequestingURL(), getRequestorType());
+        } catch (final NoSuchMethodException | IllegalAccessException ex) {
+            return tryWithInternal(a);
+        } catch (final Throwable ex) {
+            if (ex instanceof Exception) {
+                throw (Exception)ex;
+            } else {
+                throw new Exception(ex);
+            }
+        }
+    }
+    
+    private PasswordAuthentication tryWithInternal(Authenticator a) throws Exception {
+        if (a == null) {
+            return null;
+        }
         Field[] fields = null;
         if (SKIPCHECK) {
             fields = Authenticator.class.getDeclaredFields();