jdk/src/share/classes/sun/net/www/protocol/ftp/FtpURLConnection.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 51 6fe31bc95bbc
permissions -rw-r--r--
Initial load
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
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
             * Do we have to use a proxie?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            ProxySelector sel = (ProxySelector)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                        new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                return ProxySelector.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (sel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                URI uri = sun.net.www.ParseUtil.toURI(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                Iterator<Proxy> it = sel.select(uri).iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    p = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    if (p == null || p == Proxy.NO_PROXY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                        p.type() == Proxy.Type.SOCKS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    if (p.type() != Proxy.Type.HTTP ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        !(p.address() instanceof InetSocketAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        sel.connectFailed(uri, p.address(), new IOException("Wrong proxy type"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    // OK, we have an http proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    InetSocketAddress paddr = (InetSocketAddress) p.address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        http = new HttpURLConnection(url, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                        if (connectTimeout >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                            http.setConnectTimeout(connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        if (readTimeout >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                            http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        http.connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        sel.connectFailed(uri, paddr, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        http = null;
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
        } else { // per connection proxy specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            p = instProxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (p.type() == Proxy.Type.HTTP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                http = new HttpURLConnection(url, instProxy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                if (connectTimeout >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    http.setConnectTimeout(connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                if (readTimeout >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    http.setReadTimeout(readTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                http.connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (user == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            user = "anonymous";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            String vers = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                new GetPropertyAction("java.version"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            password = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                new GetPropertyAction("ftp.protocol.user",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                                      "Java" + vers +"@"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if (p != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                ftp = new FtpClient(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                ftp = new FtpClient();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            setTimeouts();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (port != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                ftp.openServer(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                ftp.openServer(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            // Maybe do something smart here, like use a proxy like iftp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // Just keep throwing for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            ftp.login(user, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        } catch (sun.net.ftp.FtpLoginException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            ftp.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        connected = true;
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Decodes the path as per the RFC-1738 specifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    private void decodePath(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        int i = path.indexOf(";type=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            String s1 = path.substring(i+6, path.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            if ("i".equalsIgnoreCase(s1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                type = BIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if ("a".equalsIgnoreCase(s1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                type = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if ("d".equalsIgnoreCase(s1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                type = DIR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            path = path.substring(0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (path != null && path.length() > 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            path.charAt(0) == '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            path = path.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (path == null || path.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            path = "./";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (!path.endsWith("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            i = path.lastIndexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if (i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                filename = path.substring(i+1, path.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                filename = ParseUtil.decode(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                pathname = path.substring(0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                filename = ParseUtil.decode(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                pathname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            pathname = path.substring(0, path.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            filename = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (pathname != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            fullpath = pathname + "/" + (filename != null ? filename : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            fullpath = filename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * As part of RFC-1738 it is specified that the path should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * interpreted as a series of FTP CWD commands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * This is because, '/' is not necessarly the directory delimiter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * on every systems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    private void cd(String path) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (path == null || "".equals(path))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (path.indexOf('/') == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            ftp.cd(ParseUtil.decode(path));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        StringTokenizer token = new StringTokenizer(path,"/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        while (token.hasMoreTokens())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            ftp.cd(ParseUtil.decode(token.nextToken()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Get the InputStream to retreive the remote file. It will issue the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * "get" (or "dir") command to the ftp server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @return  the <code>InputStream</code> to the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @throws  IOException if already opened for output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @throws  FtpProtocolException if errors occur during the transfert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (!connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (http != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            return http.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (os != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            throw new IOException("Already opened for output");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (is != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        MessageHeader msgh = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            decodePath(url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (filename == null || type == DIR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                ftp.ascii();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                cd(pathname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                if (filename == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    is = new FtpInputStream(ftp, ftp.list());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    is = new FtpInputStream(ftp, ftp.nameList(filename));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                if (type == ASCII)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    ftp.ascii();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    ftp.binary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                cd(pathname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                is = new FtpInputStream(ftp, ftp.get(filename));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            /* Try to get the size of the file in bytes.  If that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
               successful, then create a MeteredStream. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                String response = ftp.getResponseString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                if ((offset = response.indexOf(" bytes)")) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    int i = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    while (--i >= 0 && ((c = response.charAt(i)) >= '0'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                                        && c <= '9'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    long l = Long.parseLong(response.substring(i + 1, offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    msgh.add("content-length", Long.toString(l));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    if (l > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        // Wrap input stream with MeteredStream to ensure read() will always return -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                        // at expected length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                        // Check if URL should be metered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                        boolean meteredInput = ProgressMonitor.getDefault().shouldMeterInput(url, "GET");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                        ProgressSource pi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                        if (meteredInput)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                            pi = new ProgressSource(url, "GET", l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                            pi.beginTracking();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                        is = new MeteredStream(is, pi, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                /* do nothing, since all we were doing was trying to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                   get the size in bytes of the file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            String type = guessContentTypeFromName(fullpath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            if (type == null && is.markSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                type = guessContentTypeFromStream(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            if (type != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                msgh.add("content-type", type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                cd(fullpath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                /* if that worked, then make a directory listing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                   and build an html stream with all the files in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                   the directory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                ftp.ascii();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                is = new FtpInputStream(ftp, ftp.list());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                msgh.add("content-type", "text/plain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                throw new FileNotFoundException(fullpath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        setProperties(msgh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Get the OutputStream to store the remote file. It will issue the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * "put" command to the ftp server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @return  the <code>OutputStream</code> to the connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @throws  IOException if already opened for input or the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *          points to a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @throws  FtpProtocolException if errors occur during the transfert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    public OutputStream getOutputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        if (!connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        if (http != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            return http.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        if (is != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            throw new IOException("Already opened for input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if (os != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        decodePath(url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        if (filename == null || filename.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            throw new IOException("illegal filename for a PUT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (pathname != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            cd(pathname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        if (type == ASCII)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            ftp.ascii();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            ftp.binary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        os = new FtpOutputStream(ftp, ftp.put(filename));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    String guessContentTypeFromFilename(String fname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        return guessContentTypeFromName(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * Gets the <code>Permission</code> associated with the host & port.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @return  The <code>Permission</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    public Permission getPermission() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        if (permission == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            int port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            port = port < 0 ? FtpClient.FTP_PORT : port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            String host = this.host + ":" + port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            permission = new SocketPermission(host, "connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        return permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * Sets the general request property. If a property with the key already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * exists, overwrite its value with the new value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @param   key     the keyword by which the request is known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *                  (e.g., "<code>accept</code>").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @param   value   the value associated with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @see #getRequestProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    public void setRequestProperty(String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        super.setRequestProperty (key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        if ("type".equals (key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            if ("i".equalsIgnoreCase(value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                type = BIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            else if ("a".equalsIgnoreCase(value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                type = ASCII;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                if ("d".equalsIgnoreCase(value))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    type = DIR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    "Value of '" + key +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    "' request property was '" + value +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    "' when it must be either 'i', 'a' or 'd'");
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * Returns the value of the named general request property for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @param key the keyword by which the request is known (e.g., "accept").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @return  the value of the named general request property for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *           connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @see #setRequestProperty(java.lang.String, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    public String getRequestProperty(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        String value = super.getRequestProperty (key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            if ("type".equals (key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                value = (type == ASCII ? "a" : type == DIR ? "d" : "i");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    public void setConnectTimeout(int timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            throw new IllegalArgumentException("timeouts can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        connectTimeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    public int getConnectTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        return (connectTimeout < 0 ? 0 : connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    public void setReadTimeout(int timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        if (timeout < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            throw new IllegalArgumentException("timeouts can't be negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        readTimeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    public int getReadTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return readTimeout < 0 ? 0 : readTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
}