jaxws/src/jdk.xml.bind/share/classes/com/sun/istack/internal/tools/DefaultAuthenticator.java
changeset 43852 93a527059d8a
parent 25871 b80b84e87032
equal deleted inserted replaced
43752:3c68ef249093 43852:93a527059d8a
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    31 import java.io.FileNotFoundException;
    31 import java.io.FileNotFoundException;
    32 import java.io.IOException;
    32 import java.io.IOException;
    33 import java.io.InputStreamReader;
    33 import java.io.InputStreamReader;
    34 import java.io.UnsupportedEncodingException;
    34 import java.io.UnsupportedEncodingException;
    35 import java.lang.reflect.Field;
    35 import java.lang.reflect.Field;
       
    36 import java.lang.reflect.Method;
    36 import java.net.Authenticator;
    37 import java.net.Authenticator;
    37 import java.net.Authenticator.RequestorType;
    38 import java.net.Authenticator.RequestorType;
    38 import java.net.MalformedURLException;
    39 import java.net.MalformedURLException;
    39 import java.net.PasswordAuthentication;
    40 import java.net.PasswordAuthentication;
    40 import java.net.URL;
    41 import java.net.URL;
    41 import java.net.URLDecoder;
    42 import java.net.URLDecoder;
    42 import java.net.URLEncoder;
    43 import java.net.URLEncoder;
    43 import java.security.AccessController;
    44 import java.security.AccessController;
    44 import java.security.PrivilegedAction;
    45 import java.security.PrivilegedAction;
       
    46 import java.security.PrivilegedActionException;
       
    47 import java.security.PrivilegedExceptionAction;
    45 import java.util.ArrayList;
    48 import java.util.ArrayList;
    46 import java.util.List;
    49 import java.util.List;
    47 import java.util.logging.Level;
    50 import java.util.logging.Level;
    48 import java.util.logging.Logger;
    51 import java.util.logging.Logger;
    49 import java.util.regex.Pattern;
    52 import java.util.regex.Pattern;
    54  * @author Vivek Pandey
    57  * @author Vivek Pandey
    55  * @author Lukas Jungmann
    58  * @author Lukas Jungmann
    56  */
    59  */
    57 public class DefaultAuthenticator extends Authenticator {
    60 public class DefaultAuthenticator extends Authenticator {
    58 
    61 
       
    62     private static final Logger LOGGER = Logger.getLogger(DefaultAuthenticator.class.getName());
    59     private static DefaultAuthenticator instance;
    63     private static DefaultAuthenticator instance;
    60     private static Authenticator systemAuthenticator = getCurrentAuthenticator();
    64     private static Authenticator systemAuthenticator = getCurrentAuthenticator();
    61     private String proxyUser;
    65     private String proxyUser;
    62     private String proxyPasswd;
    66     private String proxyPasswd;
    63     private final List<AuthInfo> authInfo = new ArrayList<AuthInfo>();
    67     private final List<AuthInfo> authInfo = new ArrayList<>();
    64     private static int counter = 0;
    68     private static int counter = 0;
    65 
    69 
    66     DefaultAuthenticator() {
    70     DefaultAuthenticator() {
    67         //try undocumented but often used properties
    71         //try undocumented but often used properties
    68         if (System.getProperty("http.proxyUser") != null) {
    72         if (System.getProperty("http.proxyUser") != null) {
   143             locator.setSystemId(f.getAbsolutePath());
   147             locator.setSystemId(f.getAbsolutePath());
   144             try {
   148             try {
   145                 fi = new FileInputStream(f);
   149                 fi = new FileInputStream(f);
   146                 is = new InputStreamReader(fi, "UTF-8");
   150                 is = new InputStreamReader(fi, "UTF-8");
   147                 in = new BufferedReader(is);
   151                 in = new BufferedReader(is);
   148             } catch (UnsupportedEncodingException e) {
   152             } catch (UnsupportedEncodingException | FileNotFoundException e) {
   149                 listener.onError(e, locator);
       
   150                 return;
       
   151             } catch (FileNotFoundException e) {
       
   152                 listener.onError(e, locator);
   153                 listener.onError(e, locator);
   153                 return;
   154                 return;
   154             }
   155             }
   155             try {
   156             try {
   156                 int lineno = 1;
   157                 int lineno = 1;
   168                         listener.onParsingError(text, locator);
   169                         listener.onParsingError(text, locator);
   169                     }
   170                     }
   170                 }
   171                 }
   171             } catch (IOException e) {
   172             } catch (IOException e) {
   172                 listener.onError(e, locator);
   173                 listener.onError(e, locator);
   173                 Logger.getLogger(DefaultAuthenticator.class.getName()).log(Level.SEVERE, e.getMessage(), e);
   174                 LOGGER.log(Level.SEVERE, e.getMessage(), e);
   174             }
   175             }
   175         } finally {
   176         } finally {
   176             try {
   177             try {
   177                 if (in != null) {
   178                 if (in != null) {
   178                     in.close();
   179                     in.close();
   182                 }
   183                 }
   183                 if (fi != null) {
   184                 if (fi != null) {
   184                     fi.close();
   185                     fi.close();
   185                 }
   186                 }
   186             } catch (IOException ex) {
   187             } catch (IOException ex) {
   187                 Logger.getLogger(DefaultAuthenticator.class.getName()).log(Level.SEVERE, null, ex);
   188                 LOGGER.log(Level.SEVERE, null, ex);
   188             }
   189             }
   189         }
   190         }
   190     }
   191     }
   191 
   192 
   192     private AuthInfo parseLine(String text) throws Exception {
   193     private AuthInfo parseLine(String text) throws Exception {
   223         }
   224         }
   224         throw new Exception();
   225         throw new Exception();
   225     }
   226     }
   226 
   227 
   227     static Authenticator getCurrentAuthenticator() {
   228     static Authenticator getCurrentAuthenticator() {
       
   229         try {
       
   230             return AccessController.doPrivileged(new PrivilegedExceptionAction<Authenticator>() {
       
   231                 @Override
       
   232                 public Authenticator run() throws Exception {
       
   233                     Method method = Authenticator.class.getMethod("getDefault");
       
   234                     return (Authenticator) method.invoke(null);
       
   235                 }
       
   236 
       
   237             });
       
   238         } catch (PrivilegedActionException pae) {
       
   239             if (LOGGER.isLoggable(Level.FINE)) {
       
   240                 LOGGER.log(Level.FINE, null, pae);
       
   241             }
       
   242             Exception ex = pae.getException();
       
   243             if (!(ex instanceof NoSuchMethodException)) {
       
   244                 // if Authenticator.getDefault has not been found,
       
   245                 // we likely didn't get through sec, so return null
       
   246                 // and don't care about JDK version we're on
       
   247                 return null;
       
   248             }
       
   249             // or we're on JDK <9, so let's continue the old way...
       
   250         }
       
   251 
   228         final Field f = getTheAuthenticator();
   252         final Field f = getTheAuthenticator();
   229         if (f == null) {
   253         if (f == null) {
   230             return null;
   254             return null;
   231         }
   255         }
   232 
   256 
   237                     f.setAccessible(true);
   261                     f.setAccessible(true);
   238                     return null;
   262                     return null;
   239                 }
   263                 }
   240             });
   264             });
   241             return (Authenticator) f.get(null);
   265             return (Authenticator) f.get(null);
   242         } catch (Exception ex) {
   266         } catch (IllegalAccessException | IllegalArgumentException ex) {
   243             return null;
   267             return null;
   244         } finally {
   268         } finally {
   245             AccessController.doPrivileged(new PrivilegedAction<Void>() {
   269             AccessController.doPrivileged(new PrivilegedAction<Void>() {
   246                 @Override
   270                 @Override
   247                 public Void run() {
   271                 public Void run() {
   253     }
   277     }
   254 
   278 
   255     private static Field getTheAuthenticator() {
   279     private static Field getTheAuthenticator() {
   256         try {
   280         try {
   257             return Authenticator.class.getDeclaredField("theAuthenticator");
   281             return Authenticator.class.getDeclaredField("theAuthenticator");
   258         } catch (Exception ex) {
   282         } catch (NoSuchFieldException | SecurityException ex) {
   259             return null;
   283             return null;
   260         }
   284         }
   261     }
   285     }
   262 
   286 
   263     public static interface Receiver {
   287     public static interface Receiver {
   275         }
   299         }
   276 
   300 
   277         @Override
   301         @Override
   278         public void onError(Exception e, Locator loc) {
   302         public void onError(Exception e, Locator loc) {
   279             System.err.println(getLocationString(loc) + ": " + e.getMessage());
   303             System.err.println(getLocationString(loc) + ": " + e.getMessage());
   280             Logger.getLogger(DefaultAuthenticator.class.getName()).log(Level.SEVERE, e.getMessage(), e);
   304             LOGGER.log(Level.SEVERE, e.getMessage(), e);
   281         }
   305         }
   282 
   306 
   283         private String getLocationString(Locator l) {
   307         private String getLocationString(Locator l) {
   284             return "[" + l.getSystemId() + "#" + l.getLineNumber() + "]";
   308             return "[" + l.getSystemId() + "#" + l.getLineNumber() + "]";
   285         }
   309         }