jdk/src/share/classes/sun/net/www/protocol/ftp/FtpURLConnection.java
author martin
Mon, 10 Mar 2008 15:07:09 -0700
changeset 51 6fe31bc95bbc
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6600143: Remove another 450 unnecessary casts Reviewed-by: alanb, iris, lmalvent, bristor, peterjones, darcy, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1994-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * FTP stream opener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
package sun.net.www.protocol.ftp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.FilterInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.FilterOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.FileNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.net.URLStreamHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.net.SocketPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.net.InetSocketAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.net.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.net.ProxySelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import sun.net.www.MessageHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.net.www.MeteredStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.net.www.URLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import sun.net.www.protocol.http.HttpURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import sun.net.ftp.FtpClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import sun.net.ftp.FtpProtocolException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import sun.net.ProgressSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import sun.net.ProgressMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import sun.net.www.ParseUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * This class Opens an FTP input (or output) stream given a URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * It works as a one shot FTP transfer :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <LI>Login</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <LI>Get (or Put) the file</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <LI>Disconnect</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * You should not have to use it directly in most cases because all will be handled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * in a abstract layer. Here is an example of how to use the class :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <code>URL url = new URL("ftp://ftp.sun.com/pub/test.txt");<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * UrlConnection con = url.openConnection();<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * InputStream is = con.getInputStream();<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * ...<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * is.close();</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @see sun.net.ftp.FtpClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
public class FtpURLConnection extends URLConnection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // In case we have to use proxies, we use HttpURLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    HttpURLConnection http = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private Proxy instProxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    Proxy proxy = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    OutputStream os = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    FtpClient ftp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    Permission permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    String password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    String user;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    String pathname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    String filename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    String fullpath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    static final int NONE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    static final int ASCII = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    static final int BIN = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    static final int DIR = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    int type = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /* Redefine timeouts from java.net.URLConnection as we nee -1 to mean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * not set. This is to ensure backward compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private int connectTimeout = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private int readTimeout = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * For FTP URLs we need to have a special InputStream because we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * need to close 2 sockets after we're done with it :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *  - The Data socket (for the file).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *   - The command socket (FtpClient).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Since that's the only class that needs to see that, it is an inner class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    protected class FtpInputStream extends FilterInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        FtpClient ftp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        FtpInputStream(FtpClient cl, InputStream fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            super(new BufferedInputStream(fd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            ftp = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                if (ftp != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    ftp.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * For FTP URLs we need to have a special OutputStream because we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * need to close 2 sockets after we're done with it :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *  - The Data socket (for the file).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *   - The command socket (FtpClient).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Since that's the only class that needs to see that, it is an inner class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    protected class FtpOutputStream extends FilterOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        FtpClient ftp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        FtpOutputStream(FtpClient cl, OutputStream fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            super(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            ftp = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                if (ftp != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    ftp.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Creates an FtpURLConnection from a URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param   url     The <code>URL</code> to retrieve or store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public FtpURLConnection(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        this(url, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Same as FtpURLconnection(URL) with a per connection proxy specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    FtpURLConnection(URL url, Proxy p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        super(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        instProxy = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        String userInfo = url.getUserInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (userInfo != null) { // get the user and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            int delimiter = userInfo.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (delimiter == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                user = ParseUtil.decode(userInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                password = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                user = ParseUtil.decode(userInfo.substring(0, delimiter++));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                password = ParseUtil.decode(userInfo.substring(delimiter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private void setTimeouts() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (ftp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (connectTimeout >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                ftp.setConnectTimeout(connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (readTimeout >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                ftp.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Connects to the FTP server and logs in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @throws  FtpLoginException if the login is unsuccessful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @throws  FtpProtocolException if an error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @throws  UnknownHostException if trying to connect to an unknown host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public synchronized void connect() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        Proxy p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (instProxy == null) { // no per connection proxy specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            /**
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   218
             * Do we have to use a proxy?
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
             */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   220
            ProxySelector sel = java.security.AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   221
                new java.security.PrivilegedAction<ProxySelector>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   222
                    public ProxySelector run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                return ProxySelector.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (sel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                URI uri = sun.net.www.ParseUtil.toURI(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                Iterator<Proxy> it = sel.select(uri).iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    p = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    if (p == null || p == Proxy.NO_PROXY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                        p.type() == Proxy.Type.SOCKS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    if (p.type() != Proxy.Type.HTTP ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        !(p.address() instanceof InetSocketAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        sel.connectFailed(uri, p.address(), new IOException("Wrong proxy type"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    // OK, we have an http proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    InetSocketAddress paddr = (InetSocketAddress) p.address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        http = new HttpURLConnection(url, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        if (connectTimeout >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                            http.setConnectTimeout(connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        if (readTimeout >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                            http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        http.connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        sel.connectFailed(uri, paddr, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        http = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } else { // per connection proxy specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            p = instProxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            if (p.type() == Proxy.Type.HTTP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                http = new HttpURLConnection(url, instProxy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                if (connectTimeout >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    http.setConnectTimeout(connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                if (readTimeout >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                http.connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (user == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            user = "anonymous";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            String vers = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                new GetPropertyAction("java.version"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            password = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                new GetPropertyAction("ftp.protocol.user",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                                      "Java" + vers +"@"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (p != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                ftp = new FtpClient(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                ftp = new FtpClient();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            setTimeouts();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (port != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                ftp.openServer(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                ftp.openServer(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            // Maybe do something smart here, like use a proxy like iftp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            // Just keep throwing for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            ftp.login(user, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        } catch (sun.net.ftp.FtpLoginException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            ftp.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Decodes the path as per the RFC-1738 specifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    private void decodePath(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        int i = path.indexOf(";type=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            String s1 = path.substring(i+6, path.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            if ("i".equalsIgnoreCase(s1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                type = BIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if ("a".equalsIgnoreCase(s1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                type = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            if ("d".equalsIgnoreCase(s1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                type = DIR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            path = path.substring(0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (path != null && path.length() > 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            path.charAt(0) == '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            path = path.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (path == null || path.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            path = "./";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (!path.endsWith("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            i = path.lastIndexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                filename = path.substring(i+1, path.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                filename = ParseUtil.decode(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                pathname = path.substring(0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                filename = ParseUtil.decode(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                pathname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            pathname = path.substring(0, path.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            filename = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (pathname != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            fullpath = pathname + "/" + (filename != null ? filename : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            fullpath = filename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * As part of RFC-1738 it is specified that the path should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * interpreted as a series of FTP CWD commands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * This is because, '/' is not necessarly the directory delimiter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * on every systems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    private void cd(String path) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (path == null || "".equals(path))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (path.indexOf('/') == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            ftp.cd(ParseUtil.decode(path));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        StringTokenizer token = new StringTokenizer(path,"/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        while (token.hasMoreTokens())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            ftp.cd(ParseUtil.decode(token.nextToken()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Get the InputStream to retreive the remote file. It will issue the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * "get" (or "dir") command to the ftp server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @return  the <code>InputStream</code> to the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @throws  IOException if already opened for output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @throws  FtpProtocolException if errors occur during the transfert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (!connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (http != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            return http.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if (os != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            throw new IOException("Already opened for output");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (is != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        MessageHeader msgh = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            decodePath(url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            if (filename == null || type == DIR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                ftp.ascii();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                cd(pathname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                if (filename == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    is = new FtpInputStream(ftp, ftp.list());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    is = new FtpInputStream(ftp, ftp.nameList(filename));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                if (type == ASCII)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    ftp.ascii();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    ftp.binary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                cd(pathname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                is = new FtpInputStream(ftp, ftp.get(filename));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            /* Try to get the size of the file in bytes.  If that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
               successful, then create a MeteredStream. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                String response = ftp.getResponseString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                if ((offset = response.indexOf(" bytes)")) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    int i = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    while (--i >= 0 && ((c = response.charAt(i)) >= '0'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                                        && c <= '9'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    long l = Long.parseLong(response.substring(i + 1, offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    msgh.add("content-length", Long.toString(l));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    if (l > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        // Wrap input stream with MeteredStream to ensure read() will always return -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        // at expected length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        // Check if URL should be metered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                        boolean meteredInput = ProgressMonitor.getDefault().shouldMeterInput(url, "GET");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                        ProgressSource pi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                        if (meteredInput)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                            pi = new ProgressSource(url, "GET", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                            pi.beginTracking();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        is = new MeteredStream(is, pi, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                /* do nothing, since all we were doing was trying to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                   get the size in bytes of the file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            String type = guessContentTypeFromName(fullpath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            if (type == null && is.markSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                type = guessContentTypeFromStream(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            if (type != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                msgh.add("content-type", type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                cd(fullpath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                /* if that worked, then make a directory listing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                   and build an html stream with all the files in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                   the directory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                ftp.ascii();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                is = new FtpInputStream(ftp, ftp.list());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                msgh.add("content-type", "text/plain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                throw new FileNotFoundException(fullpath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        setProperties(msgh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * Get the OutputStream to store the remote file. It will issue the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * "put" command to the ftp server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @return  the <code>OutputStream</code> to the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @throws  IOException if already opened for input or the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *          points to a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @throws  FtpProtocolException if errors occur during the transfert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    public OutputStream getOutputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        if (!connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        if (http != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            return http.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if (is != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            throw new IOException("Already opened for input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        if (os != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        decodePath(url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (filename == null || filename.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            throw new IOException("illegal filename for a PUT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        if (pathname != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            cd(pathname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if (type == ASCII)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            ftp.ascii();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            ftp.binary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        os = new FtpOutputStream(ftp, ftp.put(filename));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    String guessContentTypeFromFilename(String fname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        return guessContentTypeFromName(fname);
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
     * Gets the <code>Permission</code> associated with the host & port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @return  The <code>Permission</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    public Permission getPermission() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        if (permission == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            int port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            port = port < 0 ? FtpClient.FTP_PORT : port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            String host = this.host + ":" + port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            permission = new SocketPermission(host, "connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        return permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * Sets the general request property. If a property with the key already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * exists, overwrite its value with the new value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @param   key     the keyword by which the request is known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *                  (e.g., "<code>accept</code>").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @param   value   the value associated with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @see #getRequestProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    public void setRequestProperty(String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        super.setRequestProperty (key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if ("type".equals (key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            if ("i".equalsIgnoreCase(value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                type = BIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            else if ("a".equalsIgnoreCase(value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                type = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                if ("d".equalsIgnoreCase(value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    type = DIR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    "Value of '" + key +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    "' request property was '" + value +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    "' when it must be either 'i', 'a' or 'd'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
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
     * Returns the value of the named general request property for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @param key the keyword by which the request is known (e.g., "accept").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @return  the value of the named general request property for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *           connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @see #setRequestProperty(java.lang.String, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    public String getRequestProperty(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        String value = super.getRequestProperty (key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            if ("type".equals (key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                value = (type == ASCII ? "a" : type == DIR ? "d" : "i");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    public void setConnectTimeout(int timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            throw new IllegalArgumentException("timeouts can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        connectTimeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    public int getConnectTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        return (connectTimeout < 0 ? 0 : connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    public void setReadTimeout(int timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            throw new IllegalArgumentException("timeouts can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        readTimeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    public int getReadTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        return readTimeout < 0 ? 0 : readTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
}