Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4202080

Authenticator not called for ftp URL connections

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.2.0
    • core-libs
    • x86
    • windows_nt



      Name: gsC80088 Date: 01/11/99


      // A simple program to try the Authenticator class for a FTP connection
      // When trying to download a file from an "anonymous" ftp site this program
      // runs fine.
      //
      // But when a username,password is required the operation fails, and the function
      // MyAuthenticator::getPasswordAuthentication() is not called as expected.
      //

      import java.io.*;
      import java.net.*;

      // override getPasswordAuthentication()
      class MyAuthenticator extends Authenticator {

             public MyAuthenticator() {
               super() ;
               System.err.println("MyAuthenticator::MyAuthenticator() Called");
             }

            protected PasswordAuthentication getPasswordAuthentication() {
                  System.err.println("MyAuthenticator::getPasswordAuthentication() Called");
                  char[] pwd = {'r','o','b','f','_' };
                  return new PasswordAuthentication("robf",pwd);
           }
      }


      //
      public class ConnURL {
        public ConnURL (String[] args) {
          InputStream in = null;
          OutputStream out = null;
          try {
            // Check the arguments
            if ((args.length != 1) && (args.length != 2))
              throw new IllegalArgumentException("Wrong number of arguments");

            // Set up the streams
            URL url = new URL(args[0]); // Create the URL
           
            URLConnection urlC = url.openConnection() ;
             urlC.setAllowUserInteraction(true);
             urlC.setDoInput(true);
             urlC.connect();
           
             
            in = urlC.getInputStream(); // Open a stream to it
            if (args.length == 2) // Get an appropriate output stream
              out = new FileOutputStream(args[1]);
            else out = System.out;

            // Now copy bytes from the URL to the output stream
            byte[] buffer = new byte[4096];
            int bytes_read;
            while((bytes_read = in.read(buffer)) != -1)
              out.write(buffer, 0, bytes_read);
          }
          // On exceptions, print error message and usage message.
          catch (MalformedURLException e) {
            System.err.println(e);
            System.err.println("Usage: java GetURL <URL> [<filename>]");
          }
          catch (FileNotFoundException e) {
            System.err.println(e);
            System.err.println("File not found at URL");
          }
          catch (IOException e) {
            System.err.println(e);
            System.err.println("Error reading URL");
          }
          finally { // Always close the streams, no matter what.
            try { in.close(); out.close(); } catch (Exception e) {}
          }
        }


        public static void main(String[] args) {
            Authenticator auth = new MyAuthenticator();
            auth.setDefault(auth);
            new ConnURL(args);
         }
      }
      (Review ID: 40601)
      ======================================================================

            jccollet Jean-Christophe Collet (Inactive)
            gstone Greg Stone
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: