001/*
002 * Version 0.70 01/04/2002
003 *
004 * Visit my url for update: http://www.geocities.com/beapetrovicova/
005 * 
006 * jFtp was developed by Bea Petrovicova <beapetrovicova@yahoo.com>.
007 * The design and implementation of jFtp are available for royalty-free 
008 * adoption and use. This software is provided 'as is' without any 
009 * guarantees. Copyright is retained by Bea Petrovicova. Redistribution 
010 * of any part of jFtp or any derivative works must include this notice.
011 * 
012 */  
013package cz.dhl.ftp;
014
015import java.net.URL;
016import java.net.URLEncoder;
017import java.net.MalformedURLException;
018import java.util.StringTokenizer;
019
020/**
021 * Wrapper for FTP connect & login information.
022 * 
023 * @Version 0.70 01/04/2002
024 * @author Bea Petrovicova <beapetrovicova@yahoo.com>  
025 * @see Ftp
026 */
027public final class FtpConnect
028{  private String hostname = "ftp.netscape.com";
029   private String pathname = "";
030   
031   private String username = "anonymous";
032   private String password = "";
033   
034   private int portnum = 21;
035
036   /** Creates default FtpConnect object.
037    * <DL><DT>Initial values are:
038    * <DD>&nbsp;&nbsp;hostname='ftp.netscape.com'; 
039    * <DD>&nbsp;&nbsp;pathname=''; 
040    * <DD>&nbsp;&nbsp;username='anonymous'
041    * <DD>&nbsp;&nbsp;password=''
042    * <DD>&nbsp;&nbsp;portnum=21</DL> */
043   public FtpConnect() {}
044
045   public String saveConnect(String hostname,String filename)
046      throws MalformedURLException
047   {  return URLEncoder.encode(
048         (new URL("http",hostname,filename)).toString() +"?config=" +getConnect()); }
049   
050   /** Parses FtpConnect object into string representing 
051    * command-line arguments concatenated by '|' character.
052    * @see #newConnect(String []) */
053   public String getConnect() throws MalformedURLException
054   {  return (new URL("ftp",hostname,portnum,
055         pathname)).toString() +"|-user|" +username; }
056
057   /** Parses string representing command-line arguments 
058    * concatenated by '|' character into FtpConnect object.
059    * @see #newConnect(String []) */
060   public static FtpConnect newConnect(String args)
061   {  if(args!=null)
062      {  StringTokenizer argt = new StringTokenizer(args,"|");
063         int n=argt.countTokens();
064         String argn[]=new String[n];
065         for(int i=0;i<n;i++)
066            argn[i]=argt.nextToken();
067         return newConnect(argn);
068      } else return new FtpConnect();
069   }
070
071   /** Parses array of strings representing 
072    * command-line arguments into FtpConnect object.
073    * @param args Command-line arguments
074    * <DL><DT>Following option values are recognized:
075    * <DD>ftp://ftp.server.com:21/default/pathname/
076    * <DD>&nbsp;&nbsp;hostname='ftp.server.com'; 
077    * <DD>&nbsp;&nbsp;pathname='/default/pathname'; 
078    * <DD>&nbsp;&nbsp;portnum=21
079    * <DD>-user eternity 
080    * <DD>&nbsp;&nbsp;username='eternity'</DL>
081    * @return FtpConnect object */
082   public static FtpConnect newConnect(String args[])
083   {  FtpConnect connect = new FtpConnect();
084      
085      for(int i=0;i<args.length;i++)
086         if(args[i].startsWith("ftp:"))
087            try
088            {  URL argi = new URL(args[i]);
089               connect.hostname = argi.getHost();
090               String pathname = argi.getFile();
091               if(pathname.compareTo("/")!=0)
092                  connect.pathname = pathname;
093               int portnum = argi.getPort();
094               if(portnum != -1)
095                  connect.portnum = portnum;
096               break;
097            } catch (MalformedURLException e) { }
098            finally
099            {  for(i=0;i<(args.length-1);i++)
100                  if(args[i].compareTo("-user")==0)
101                     { connect.username = args[i+1]; break; } 
102               break;
103            }
104      return connect;
105   }
106   
107   /** Get ftp client hostname string. For example: <CODE>ftp.netscape.com</CODE> */
108   public String getHostName() { return hostname; }
109   /** Get initial pathname string. Empty string is indicating default directory. */
110   public String getPathName() { return pathname; }
111   /** Gets username string. */
112   public String getUserName() { return username; }
113   /** Gets password string. */
114   public String getPassWord() { return password; }
115   /** Gets socket port number. */
116   public int getPortNum() { return portnum; }
117   
118   /** Sets ftp client hostname string. For example: <CODE>ftp.netscape.com</CODE> */
119   public void setHostName(String hostname) { this.hostname = hostname; }
120   /** Sets initial pathname string. Empty string is indicating default directory. */
121   public void setPathName(String pathname) { this.pathname = pathname; }
122   /** Sets username string. */
123   public void setUserName(String username) { this.username = username; }
124   /** Sets password string. */
125   public void setPassWord(String password) { this.password = password; }
126   /** Sets socket port number. */
127   public void setPortNum(int portnum) { this.portnum = portnum; }
128}