
// ProxyDetails.java
// Andrew Davison, dandrew@ratree.psu.ac.th, 22nd Feb. 2001

// process a "-proxy [filename]" option from the command line

/* 
  By default, proxy information is read from the local file
  proxyInfo.txt

  proxyInfo.txt file format:
      * blank lines and lines beginning with // are skipped
      * proxyHost: <text>
      * proxyPort: <integer>
      * authID: <string>

   They can appear in any order, but only 1 per line.
   If authID appears then a dialog box is used to
   prompt for authPass.

   The proxy details are set here; there is no need to do it
   in the main program.

   The authID and authPass are encrypted using the Base64Converter class,
   and the resulting string can be accessed via the getEnIDPass()
   method.
*/

import java.io.*;
import java.util.*;
import javax.swing.*;

public class ProxyDetails
{
  // encrypted proxy authorization details (userID:password)
  private String enIDPass = null;  

  public ProxyDetails(String[] args)
  // examine the command line args and process them
  {
    // proxyFnm hold proxy details
    String proxyFnm = "proxyInfo.txt";   // default
    String enIDPass = null;

    if (args.length == 0)
      System.out.println("No proxy information supplied");
    else {  // some command line args
      if (args[0].equals("-proxy")) {  // proxy argument
        if (args.length == 2)          // filename supplied
          proxyFnm = args[1];
        System.out.println("Proxy details being read from " +
                                proxyFnm);
        initProxyDetails(proxyFnm);
      }
      else {
        System.out.println("Error: do not understand option: " + args[0]);
        System.out.println("Usage: java <program> [-proxy [ filename ]]");
        System.exit(1);
      }
    }
  } // end of processArgs()


  private void initProxyDetails(String fnm)
  // initialise proxyHost, proxyPort, and enIDPass
  // or leave as null
  {  
    // default proxy machine details
    String proxyHost = null;
    String proxyPort = null;
    String authID = null;

    try {
      BufferedReader br = new BufferedReader( 
                              new FileReader(fnm));
 
      String line;
      while ((line = br.readLine()) != null) {       
        if (line.length() == 0) // skip blank lines
          ;
        else if (line.startsWith("//"))	  // skip comment lines
          ;
        else if (line.startsWith("proxyHost: "))
          proxyHost = line.substring(11).trim();
        else if (line.startsWith("proxyPort: "))
          proxyPort = line.substring(11).trim();
        else if (line.startsWith("authID: "))
          authID = line.substring(8).trim();
        else
          System.out.println("Ignoring line: " + line.charAt(0));
      }
    }
    catch(IOException e) {
      System.err.println("IO error: " + e);
    }

    System.out.println("proxyHost: " + proxyHost + 
           "\nproxyPort: " + proxyPort + "\nauthID: " + authID + "\n");

    setProxyProps(proxyHost, proxyPort);
    makeEnIDPass(authID);

  } // end of initProxyDetails()


  private void setProxyProps(String proxyHost, String proxyPort)
  // set the proxy properties for the host and port (if possible)
  {
    if (proxyHost == null)
      System.out.println("No proxy set since host is null");
    else if (proxyPort == null)
      System.out.println("No proxy set since port not set");
    else {
      Properties sysProp = System.getProperties();
      sysProp.put("proxySet", "true");
      sysProp.put("proxyHost", proxyHost);
      sysProp.put("proxyPort", proxyPort);
      System.setProperties(sysProp);
      System.out.println("Proxy host and port set");
    }
  } // end of setProxyProps()


  private void makeEnIDPass(String authID)
  // Ask for the authorization password, and make the
  // encrypted "authID:authPass" string. Assign it to
  // the global enIDPass.
  {
    if (authID != null) {
      JPasswordField passField = new JPasswordField();
      String message = "Enter your proxy authorization password:";
      int result = JOptionPane.showOptionDialog(null,
           new Object[] { message, passField },
           "Authorization Password", JOptionPane.OK_CANCEL_OPTION,
           JOptionPane.QUESTION_MESSAGE,
           null, null, null);

      if (result == JOptionPane.OK_OPTION) {
        String authPass = new String( passField.getPassword() );
        Base64Converter bc = new Base64Converter();
        enIDPass = bc.encode( authID + ":" + authPass );
      }
    }
  } // end of makeEnIDPass()


  public String getEnIDPass()
  {  return enIDPass; }


  public static void main(String[] args)   // for testing
  {
    ProxyDetails pd = new ProxyDetails(args);
    
    System.out.println("Encrypted IDPass: " + pd.getEnIDPass() );
    System.exit(0);
  }

} // end of ProxyDetails class


