jdk/src/share/classes/sun/net/ftp/FtpClient.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 908 72d0a60834cf
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 1994-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.ftp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.regex.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.net.TransferProtocolClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.net.TelnetInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.net.TelnetOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.misc.RegexpPool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This class implements the FTP client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author      Jonathan Payne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class FtpClient extends TransferProtocolClient {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static final int FTP_PORT = 21;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static int  FTP_SUCCESS = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static int  FTP_TRY_AGAIN = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static int  FTP_ERROR = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /** remember the ftp server name because we may need it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private String      serverName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /** socket for data transfer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private boolean     replyPending = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private boolean     binaryMode = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private boolean     loggedIn = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /** regexp pool of hosts for which we should connect directly, not Proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *  these are intialized from a property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static RegexpPool nonProxyHostsPool = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /** The string soucre of nonProxyHostsPool
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static String nonProxyHostsSource = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /** last command issued */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    String              command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /** The last reply code from the ftp daemon. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    int                 lastReplyCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /** Welcome message from the server, if any. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public String       welcomeMsg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /* these methods are used to determine whether ftp urls are sent to */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /* an http server instead of using a direct connection to the */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /* host. They aren't used directly here. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @return if the networking layer should send ftp connections through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *          a proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static boolean getUseFtpProxy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // if the ftp.proxyHost is set, use it!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return (getFtpProxyHost() != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @return the host to use, or null if none has been specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static String getFtpProxyHost() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            new java.security.PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                String result = System.getProperty("ftp.proxyHost");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    result = System.getProperty("ftpProxyHost");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    // as a last resort we use the general one if ftp.useProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    // is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    if (Boolean.getBoolean("ftp.useProxy")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    result = System.getProperty("proxyHost");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return the proxy port to use.  Will default reasonably if not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public static int getFtpProxyPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        final int result[] = {80};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        java.security.AccessController.doPrivileged(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   120
            new java.security.PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   121
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                String tmp = System.getProperty("ftp.proxyPort");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                if (tmp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    // for compatibility with 1.0.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    tmp = System.getProperty("ftpProxyPort");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                if (tmp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    // as a last resort we use the general one if ftp.useProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    // is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    if (Boolean.getBoolean("ftp.useProxy")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        tmp = System.getProperty("proxyPort");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                if (tmp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    result[0] = Integer.parseInt(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return result[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public static boolean matchNonProxyHosts(String host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        synchronized (FtpClient.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            String rawList = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    new sun.security.action.GetPropertyAction("ftp.nonProxyHosts"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (rawList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                nonProxyHostsPool = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                if (!rawList.equals(nonProxyHostsSource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    RegexpPool pool = new RegexpPool();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    StringTokenizer st = new StringTokenizer(rawList, "|", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                            pool.add(st.nextToken().toLowerCase(), Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    } catch (sun.misc.REException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        System.err.println("Error in http.nonProxyHosts system property: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    nonProxyHostsPool = pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            nonProxyHostsSource = rawList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (nonProxyHostsPool == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (nonProxyHostsPool.match(host) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * issue the QUIT command to the FTP server and close the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @exception       FtpProtocolException if an error occured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public void closeServer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (serverIsOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            issueCommand("QUIT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            super.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Send a command to the FTP server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param   cmd     String containing the command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @return          reply code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @exception       FtpProtocolException if an error occured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    protected int issueCommand(String cmd) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        command = cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        int reply;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        while (replyPending) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            replyPending = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (readReply() == FTP_ERROR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                throw new FtpProtocolException("Error reading FTP pending reply\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            sendServer(cmd + "\r\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            reply = readReply();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } while (reply == FTP_TRY_AGAIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return reply;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Send a command to the FTP server and check for success.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param   cmd     String containing the command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @exception       FtpProtocolException if an error occured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    protected void issueCommandCheck(String cmd) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (issueCommand(cmd) != FTP_SUCCESS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throw new FtpProtocolException(cmd + ":" + getResponseString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Read the reply from the FTP server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @return          FTP_SUCCESS or FTP_ERROR depending on success
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @exception       FtpProtocolException if an error occured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    protected int readReply() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        lastReplyCode = readServerResponse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        switch (lastReplyCode / 100) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            replyPending = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            /* falls into ... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            return FTP_SUCCESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (lastReplyCode == 530) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                if (!loggedIn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    throw new FtpLoginException("Not logged in");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                return FTP_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (lastReplyCode == 550) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                throw new FileNotFoundException(command + ": " + getResponseString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        /* this statement is not reached */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return FTP_ERROR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Tries to open a Data Connection in "PASSIVE" mode by issuing a EPSV or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * PASV command then opening a Socket to the specified address & port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @return          the opened socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @exception       FtpProtocolException if an error occurs when issuing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *                  PASV command to the ftp server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    protected Socket openPassiveDataConnection() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        String serverAnswer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        InetSocketAddress dest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
         * Here is the idea:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
         * - First we want to try the new (and IPv6 compatible) EPSV command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
         *   But since we want to be nice with NAT software, we'll issue the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         *   EPSV ALL cmd first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
         *   EPSV is documented in RFC2428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
         * - If EPSV fails, then we fall back to the older, yet OK PASV command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         * - If PASV fails as well, then we throw an exception and the calling method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         *   will have to try the EPRT or PORT command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (issueCommand("EPSV ALL") == FTP_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // We can safely use EPSV commands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (issueCommand("EPSV") == FTP_ERROR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                throw new FtpProtocolException("EPSV Failed: " + getResponseString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            serverAnswer = getResponseString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // The response string from a EPSV command will contain the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            // the format will be :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            //  229 Entering Extended Passive Mode (|||58210|)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            // So we'll use the regular expresions package to parse the output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            Pattern p = Pattern.compile("^229 .* \\(\\|\\|\\|(\\d+)\\|\\)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            Matcher m = p.matcher(serverAnswer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (! m.find())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                throw new FtpProtocolException("EPSV failed : " + serverAnswer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            // Yay! Let's extract the port number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            String s = m.group(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            port = Integer.parseInt(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            InetAddress add = serverSocket.getInetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if (add != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                dest = new InetSocketAddress(add, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                // This means we used an Unresolved address to connect in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                // the first place. Most likely because the proxy is doing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                // the name resolution for us, so let's keep using unresolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                // address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                dest = InetSocketAddress.createUnresolved(serverName, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            // EPSV ALL failed, so Let's try the regular PASV cmd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (issueCommand("PASV") == FTP_ERROR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                throw new FtpProtocolException("PASV failed: " + getResponseString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            serverAnswer = getResponseString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            // Let's parse the response String to get the IP & port to connect to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            // the String should be in the following format :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            // 227 Entering Passive Mode (A1,A2,A3,A4,p1,p2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            // Note that the two parenthesis are optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            // The IP address is A1.A2.A3.A4 and the port is p1 * 256 + p2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            // The regular expression is a bit more complex this time, because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            // parenthesis are optionals and we have to use 3 groups.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            Pattern p = Pattern.compile("227 .* \\(?(\\d{1,3},\\d{1,3},\\d{1,3},\\d{1,3}),(\\d{1,3}),(\\d{1,3})\\)?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            Matcher m = p.matcher(serverAnswer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (! m.find())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                throw new FtpProtocolException("PASV failed : " + serverAnswer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            // Get port number out of group 2 & 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            port = Integer.parseInt(m.group(3)) + (Integer.parseInt(m.group(2)) << 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            // IP address is simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            String s = m.group(1).replace(',','.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            dest = new InetSocketAddress(s, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // Got everything, let's open the socket!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (proxy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if (proxy.type() == Proxy.Type.SOCKS) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   346
                s = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   347
                    new PrivilegedAction<Socket>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   348
                        public Socket run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                                  return new Socket(proxy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                              }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                s = new Socket(Proxy.NO_PROXY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            s = new Socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (connectTimeout >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            s.connect(dest, connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            if (defaultConnectTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                s.connect(dest, defaultConnectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                s.connect(dest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (readTimeout >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            s.setSoTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            if (defaultSoTimeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                s.setSoTimeout(defaultSoTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Tries to open a Data Connection with the server. It will first try a passive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * mode connection, then, if it fails, a more traditional PORT command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @param   cmd     the command to execute (RETR, STOR, etc...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @return          the opened socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @exception       FtpProtocolException if an error occurs when issuing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *                  PORT command to the ftp server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    protected Socket openDataConnection(String cmd) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        ServerSocket portSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        Socket  clientSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        String      portCmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        InetAddress myAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        IOException e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        // Let's try passive mode first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            clientSocket = openPassiveDataConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            clientSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (clientSocket != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            // We did get a clientSocket, so the passive mode worked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            // Let's issue the command (GET, DIR, ...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                if (issueCommand(cmd) == FTP_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    clientSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    throw new FtpProtocolException(getResponseString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    return clientSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                clientSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        assert(clientSocket == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        // Passive mode failed, let's fall back to the good old "PORT"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (proxy != null && proxy.type() == Proxy.Type.SOCKS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            // We're behind a firewall and the passive mode fail,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            // since we can't accept a connection through SOCKS (yet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            // throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            throw new FtpProtocolException("Passive mode failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            portSocket = new ServerSocket(0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            myAddress = portSocket.getInetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            if (myAddress.isAnyLocalAddress())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                myAddress = getLocalAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            // Let's try the new, IPv6 compatible EPRT command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            // See RFC2428 for specifics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            // Some FTP servers (like the one on Solaris) are bugged, they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            // will accept the EPRT command but then, the subsequent command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            // (e.g. RETR) will fail, so we have to check BOTH results (the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            // EPRT cmd then the actual command) to decide wether we should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            // fall back on the older PORT command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            portCmd = "EPRT |" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                ((myAddress instanceof Inet6Address) ? "2" : "1") + "|" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                myAddress.getHostAddress() +"|" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                portSocket.getLocalPort()+"|";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            if (issueCommand(portCmd) == FTP_ERROR ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                issueCommand(cmd) == FTP_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                // The EPRT command failed, let's fall back to good old PORT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                portCmd = "PORT ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                byte[] addr = myAddress.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                /* append host addr */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                for (int i = 0; i < addr.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    portCmd = portCmd + (addr[i] & 0xFF) + ",";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                /* append port number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                portCmd = portCmd + ((portSocket.getLocalPort() >>> 8) & 0xff) + ","
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    + (portSocket.getLocalPort() & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                if (issueCommand(portCmd) == FTP_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    e = new FtpProtocolException("PORT :" + getResponseString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                if (issueCommand(cmd) == FTP_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    e = new FtpProtocolException(cmd + ":" + getResponseString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            // Either the EPRT or the PORT command was successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            // Let's create the client socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            if (connectTimeout >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                portSocket.setSoTimeout(connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                if (defaultConnectTimeout > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    portSocket.setSoTimeout(defaultConnectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            clientSocket = portSocket.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (readTimeout >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                clientSocket.setSoTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                if (defaultSoTimeout > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    clientSocket.setSoTimeout(defaultSoTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            portSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        return clientSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    /* public methods */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * Open a FTP connection to host <i>host</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @param   host    The hostname of the ftp server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @exception       FtpProtocolException if connection fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    public void openServer(String host) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        openServer(host, FTP_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * Open a FTP connection to host <i>host</i> on port <i>port</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @param   host    the hostname of the ftp server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @param   port    the port to connect to (usually 21)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @exception       FtpProtocolException if connection fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    public void openServer(String host, int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        this.serverName = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        super.openServer(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        if (readReply() == FTP_ERROR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            throw new FtpProtocolException("Welcome message: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                                           getResponseString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * login user to a host with username <i>user</i> and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * <i>password</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @param   user            Username to use at login
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @param   password        Password to use at login or null of none is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @exception       FtpLoginException if login is unsuccesful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    public void login(String user, String password) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        if (!serverIsOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            throw new FtpLoginException("not connected to host");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        if (user == null || user.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        if (issueCommand("USER " + user) == FTP_ERROR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            throw new FtpLoginException("user " + user + " : " + getResponseString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
         * Checks for "331 User name okay, need password." answer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        if (lastReplyCode == 331)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            if ((password == null) || (password.length() == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                (issueCommand("PASS " + password) == FTP_ERROR))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                throw new FtpLoginException("password: " + getResponseString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        // keep the welcome message around so we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        // put it in the resulting HTML page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        String l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        for (int i = 0; i < serverResponse.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            l = (String)serverResponse.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                if (l.length() >= 4 && l.startsWith("230")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    // get rid of the "230-" prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    l = l.substring(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                sb.append(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        welcomeMsg = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        loggedIn = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * GET a file from the FTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @param   filename        name of the file to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @return  the <code>InputStream</code> to read the file from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @exception       FileNotFoundException if the file can't be opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    public TelnetInputStream get(String filename) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        Socket  s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            s = openDataConnection("RETR " + filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        } catch (FileNotFoundException fileException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            /* Well, "/" might not be the file delimitor for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
               particular ftp server, so let's try a series of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
               "cd" commands to get to the right place. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            /* But don't try this if there are no '/' in the path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            if (filename.indexOf('/') == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                throw fileException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            StringTokenizer t = new StringTokenizer(filename, "/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            String          pathElement = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            while (t.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                pathElement = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                if (!t.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    /* This is the file component.  Look it up now. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    cd(pathElement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                } catch (FtpProtocolException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    /* Giving up. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    throw fileException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            if (pathElement != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                s = openDataConnection("RETR " + pathElement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                throw fileException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        return new TelnetInputStream(s.getInputStream(), binaryMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * PUT a file to the FTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @param   filename        name of the file to store
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @return  the <code>OutputStream</code> to write the file to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    public TelnetOutputStream put(String filename) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        Socket s = openDataConnection("STOR " + filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        TelnetOutputStream out = new TelnetOutputStream(s.getOutputStream(), binaryMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        if (!binaryMode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            out.setStickyCRLF(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * Append to a file on the FTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @param   filename        name of the file to append to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * @return  the <code>OutputStream</code> to write the file to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    public TelnetOutputStream append(String filename) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        Socket s = openDataConnection("APPE " + filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        TelnetOutputStream out = new TelnetOutputStream(s.getOutputStream(), binaryMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        if (!binaryMode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            out.setStickyCRLF(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * LIST files in the current directory on a remote FTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @return  the <code>InputStream</code> to read the list from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    public TelnetInputStream list() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        Socket s = openDataConnection("LIST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        return new TelnetInputStream(s.getInputStream(), binaryMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * List (NLST) file names on a remote FTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @param   path    pathname to the directory to list, null for current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *                  directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * @return  the <code>InputStream</code> to read the list from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @exception       <code>FtpProtocolException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    public TelnetInputStream nameList(String path) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        Socket s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        if (path != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            s = openDataConnection("NLST " + path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            s = openDataConnection("NLST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        return new TelnetInputStream(s.getInputStream(), binaryMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * CD to a specific directory on a remote FTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @param   remoteDirectory path of the directory to CD to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @exception       <code>FtpProtocolException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    public void cd(String remoteDirectory) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        if (remoteDirectory == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            "".equals(remoteDirectory))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        issueCommandCheck("CWD " + remoteDirectory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * CD to the parent directory on a remote FTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    public void cdUp() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        issueCommandCheck("CDUP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * Print working directory of remote FTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @exception FtpProtocolException if the command fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    public String pwd() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        String answ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        issueCommandCheck("PWD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
         * answer will be of the following format :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
         * 257 "/" is current directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        answ = getResponseString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        if (!answ.startsWith("257"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            throw new FtpProtocolException("PWD failed. " + answ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        return answ.substring(5, answ.lastIndexOf('"'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * Set transfer type to 'I'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * @exception FtpProtocolException if the command fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    public void binary() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        issueCommandCheck("TYPE I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        binaryMode = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * Set transfer type to 'A'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * @exception FtpProtocolException if the command fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    public void ascii() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        issueCommandCheck("TYPE A");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        binaryMode = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * Rename a file on the ftp server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * @exception FtpProtocolException if the command fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    public void rename(String from, String to) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        issueCommandCheck("RNFR " + from);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        issueCommandCheck("RNTO " + to);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * Get the "System string" from the FTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * @exception       FtpProtocolException if it fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    public String system() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        String answ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        issueCommandCheck("SYST");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        answ = getResponseString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        if (!answ.startsWith("215"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            throw new FtpProtocolException("SYST failed." + answ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        return answ.substring(4); // Skip "215 "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * Send a No-operation command. It's usefull for testing the connection status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * @exception FtpProtocolException if the command fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    public void noop() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        issueCommandCheck("NOOP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * Reinitialize the USER parameters on the FTp server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * @exception FtpProtocolException if the command fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    public void reInit() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        issueCommandCheck("REIN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        loggedIn = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * New FTP client connected to host <i>host</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * @param   host    Hostname of the FTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * @exception FtpProtocolException if the connection fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    public FtpClient(String host) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        openServer(host, FTP_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * New FTP client connected to host <i>host</i>, port <i>port</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * @param   host    Hostname of the FTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * @param   port    port number to connect to (usually 21)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @exception FtpProtocolException if the connection fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    public FtpClient(String host, int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        openServer(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    /** Create an uninitialized FTP client. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    public FtpClient() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    public FtpClient(Proxy p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        proxy = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    protected void finalize() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
         * Do not call the "normal" closeServer() as we want finalization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
         * to be as efficient as possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        if (serverIsOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            super.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
}