< prev index next >

jaxws/src/jdk.xml.bind/share/classes/com/sun/istack/internal/tools/DefaultAuthenticator.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -31,19 +31,22 @@
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.UnsupportedEncodingException;
 import java.lang.reflect.Field;
+import java.lang.reflect.Method;
 import java.net.Authenticator;
 import java.net.Authenticator.RequestorType;
 import java.net.MalformedURLException;
 import java.net.PasswordAuthentication;
 import java.net.URL;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.regex.Pattern;

@@ -54,15 +57,16 @@
  * @author Vivek Pandey
  * @author Lukas Jungmann
  */
 public class DefaultAuthenticator extends Authenticator {
 
+    private static final Logger LOGGER = Logger.getLogger(DefaultAuthenticator.class.getName());
     private static DefaultAuthenticator instance;
     private static Authenticator systemAuthenticator = getCurrentAuthenticator();
     private String proxyUser;
     private String proxyPasswd;
-    private final List<AuthInfo> authInfo = new ArrayList<AuthInfo>();
+    private final List<AuthInfo> authInfo = new ArrayList<>();
     private static int counter = 0;
 
     DefaultAuthenticator() {
         //try undocumented but often used properties
         if (System.getProperty("http.proxyUser") != null) {

@@ -143,14 +147,11 @@
             locator.setSystemId(f.getAbsolutePath());
             try {
                 fi = new FileInputStream(f);
                 is = new InputStreamReader(fi, "UTF-8");
                 in = new BufferedReader(is);
-            } catch (UnsupportedEncodingException e) {
-                listener.onError(e, locator);
-                return;
-            } catch (FileNotFoundException e) {
+            } catch (UnsupportedEncodingException | FileNotFoundException e) {
                 listener.onError(e, locator);
                 return;
             }
             try {
                 int lineno = 1;

@@ -168,11 +169,11 @@
                         listener.onParsingError(text, locator);
                     }
                 }
             } catch (IOException e) {
                 listener.onError(e, locator);
-                Logger.getLogger(DefaultAuthenticator.class.getName()).log(Level.SEVERE, e.getMessage(), e);
+                LOGGER.log(Level.SEVERE, e.getMessage(), e);
             }
         } finally {
             try {
                 if (in != null) {
                     in.close();

@@ -182,11 +183,11 @@
                 }
                 if (fi != null) {
                     fi.close();
                 }
             } catch (IOException ex) {
-                Logger.getLogger(DefaultAuthenticator.class.getName()).log(Level.SEVERE, null, ex);
+                LOGGER.log(Level.SEVERE, null, ex);
             }
         }
     }
 
     private AuthInfo parseLine(String text) throws Exception {

@@ -223,10 +224,33 @@
         }
         throw new Exception();
     }
 
     static Authenticator getCurrentAuthenticator() {
+        try {
+            return AccessController.doPrivileged(new PrivilegedExceptionAction<Authenticator>() {
+                @Override
+                public Authenticator run() throws Exception {
+                    Method method = Authenticator.class.getMethod("getDefault");
+                    return (Authenticator) method.invoke(null);
+                }
+
+            });
+        } catch (PrivilegedActionException pae) {
+            if (LOGGER.isLoggable(Level.FINE)) {
+                LOGGER.log(Level.FINE, null, pae);
+            }
+            Exception ex = pae.getException();
+            if (!(ex instanceof NoSuchMethodException)) {
+                // if Authenticator.getDefault has not been found,
+                // we likely didn't get through sec, so return null
+                // and don't care about JDK version we're on
+                return null;
+            }
+            // or we're on JDK <9, so let's continue the old way...
+        }
+
         final Field f = getTheAuthenticator();
         if (f == null) {
             return null;
         }
 

@@ -237,11 +261,11 @@
                     f.setAccessible(true);
                     return null;
                 }
             });
             return (Authenticator) f.get(null);
-        } catch (Exception ex) {
+        } catch (IllegalAccessException | IllegalArgumentException ex) {
             return null;
         } finally {
             AccessController.doPrivileged(new PrivilegedAction<Void>() {
                 @Override
                 public Void run() {

@@ -253,11 +277,11 @@
     }
 
     private static Field getTheAuthenticator() {
         try {
             return Authenticator.class.getDeclaredField("theAuthenticator");
-        } catch (Exception ex) {
+        } catch (NoSuchFieldException | SecurityException ex) {
             return null;
         }
     }
 
     public static interface Receiver {

@@ -275,11 +299,11 @@
         }
 
         @Override
         public void onError(Exception e, Locator loc) {
             System.err.println(getLocationString(loc) + ": " + e.getMessage());
-            Logger.getLogger(DefaultAuthenticator.class.getName()).log(Level.SEVERE, e.getMessage(), e);
+            LOGGER.log(Level.SEVERE, e.getMessage(), e);
         }
 
         private String getLocationString(Locator l) {
             return "[" + l.getSystemId() + "#" + l.getLineNumber() + "]";
         }
< prev index next >