src/java.base/share/classes/sun/net/www/protocol/ftp/FtpURLConnection.java
author redestad
Thu, 13 Dec 2018 15:31:05 +0100
changeset 53018 8bf9268df0e2
parent 47216 71c04702a3d5
child 55711 0c143aaa2c99
child 58678 9cf78a70fa4f
permissions -rw-r--r--
8215281: Use String.isEmpty() when applicable in java.base Reviewed-by: dfuchs, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36858
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
     2
 * Copyright (c) 1994, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4155
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4155
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4155
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4155
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4155
diff changeset
    23
 * questions.
2
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.SocketPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.net.InetSocketAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.net.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.net.ProxySelector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.security.Permission;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36858
diff changeset
    49
import java.util.Properties;
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    50
import sun.net.NetworkClient;
2
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
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
    72
 * in a abstract layer. Here is an example of how to use the class:
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
    73
 * <pre>{@code
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
    74
 * URL url = new URL("ftp://ftp.sun.com/pub/test.txt");
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
    75
 * UrlConnection con = url.openConnection();
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
    76
 * InputStream is = con.getInputStream();
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
    77
 * ...
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
    78
 * is.close();
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
    79
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @see sun.net.ftp.FtpClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
public class FtpURLConnection extends URLConnection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // In case we have to use proxies, we use HttpURLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    HttpURLConnection http = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private Proxy instProxy;
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;
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   108
    /* Redefine timeouts from java.net.URLConnection as we need -1 to mean
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * not set. This is to ensure backward compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   111
    private int connectTimeout = NetworkClient.DEFAULT_CONNECT_TIMEOUT;;
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   112
    private int readTimeout = NetworkClient.DEFAULT_READ_TIMEOUT;;
2
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
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   128
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            super.close();
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   131
            if (ftp != null) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   132
                ftp.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
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
     * For FTP URLs we need to have a special OutputStream because we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * need to close 2 sockets after we're done with it :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *  - The Data socket (for the file).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *   - The command socket (FtpClient).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Since that's the only class that needs to see that, it is an inner class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    protected class FtpOutputStream extends FilterOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        FtpClient ftp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        FtpOutputStream(FtpClient cl, OutputStream fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            super(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            ftp = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   151
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            super.close();
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   154
            if (ftp != null) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   155
                ftp.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
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
     * Creates an FtpURLConnection from a URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
   163
     * @param   url     The {@code URL} to retrieve or store.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public FtpURLConnection(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        this(url, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Same as FtpURLconnection(URL) with a per connection proxy specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    FtpURLConnection(URL url, Proxy p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        super(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        instProxy = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        String userInfo = url.getUserInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (userInfo != null) { // get the user and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            int delimiter = userInfo.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (delimiter == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                user = ParseUtil.decode(userInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                password = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                user = ParseUtil.decode(userInfo.substring(0, delimiter++));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                password = ParseUtil.decode(userInfo.substring(delimiter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private void setTimeouts() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (ftp != null) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   193
            if (connectTimeout >= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                ftp.setConnectTimeout(connectTimeout);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   195
            }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   196
            if (readTimeout >= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                ftp.setReadTimeout(readTimeout);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   198
            }
2
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(
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   221
                    new java.security.PrivilegedAction<ProxySelector>() {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   222
                        public ProxySelector run() {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   223
                            return ProxySelector.getDefault();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   224
                        }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   225
                    });
2
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 ||
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   232
                        p.type() == Proxy.Type.SOCKS) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                        break;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   234
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    if (p.type() != Proxy.Type.HTTP ||
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   236
                            !(p.address() instanceof InetSocketAddress)) {
2
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);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   244
                        http.setDoInput(getDoInput());
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   245
                        http.setDoOutput(getDoOutput());
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   246
                        if (connectTimeout >= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                            http.setConnectTimeout(connectTimeout);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   248
                        }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   249
                        if (readTimeout >= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                            http.setReadTimeout(readTimeout);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   251
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        http.connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        sel.connectFailed(uri, paddr, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        http = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } else { // per connection proxy specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            p = instProxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (p.type() == Proxy.Type.HTTP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                http = new HttpURLConnection(url, instProxy);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   265
                http.setDoInput(getDoInput());
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   266
                http.setDoOutput(getDoOutput());
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   267
                if (connectTimeout >= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    http.setConnectTimeout(connectTimeout);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   269
                }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   270
                if (readTimeout >= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    http.setReadTimeout(readTimeout);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   272
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                http.connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (user == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            user = "anonymous";
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   281
            Properties props = GetPropertyAction.privilegedGetProperties();
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36858
diff changeset
   282
            String vers = props.getProperty("java.version");
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36858
diff changeset
   283
            password = props.getProperty("ftp.protocol.user",
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36858
diff changeset
   284
                    "Java" + vers + "@");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        try {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   287
            ftp = FtpClient.create();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   288
            if (p != null) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   289
                ftp.setProxy(p);
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   290
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            setTimeouts();
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   292
            if (port != -1) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   293
                ftp.connect(new InetSocketAddress(host, port));
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   294
            } else {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   295
                ftp.connect(new InetSocketAddress(host, FtpClient.defaultPort()));
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   296
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            // Maybe do something smart here, like use a proxy like iftp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            // Just keep throwing for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            throw e;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   301
        } catch (FtpProtocolException fe) {
36858
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   302
            if (ftp != null) {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   303
                try {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   304
                    ftp.close();
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   305
                } catch (IOException ioe) {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   306
                    fe.addSuppressed(ioe);
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   307
                }
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   308
            }
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   309
            throw new IOException(fe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        try {
25805
0bc2aa4aadbe 8031435: Ftp download does not work properly for ftp user without password
robm
parents: 7668
diff changeset
   312
            ftp.login(user, password == null ? null : password.toCharArray());
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   313
        } catch (sun.net.ftp.FtpProtocolException e) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   314
            ftp.close();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   315
            // Backward compatibility
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   316
            throw new sun.net.ftp.FtpLoginException("Invalid username/password");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Decodes the path as per the RFC-1738 specifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private void decodePath(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int i = path.indexOf(";type=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (i >= 0) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   328
            String s1 = path.substring(i + 6, path.length());
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   329
            if ("i".equalsIgnoreCase(s1)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                type = BIN;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   331
            }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   332
            if ("a".equalsIgnoreCase(s1)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                type = ASCII;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   334
            }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   335
            if ("d".equalsIgnoreCase(s1)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                type = DIR;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   337
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            path = path.substring(0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (path != null && path.length() > 1 &&
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   341
                path.charAt(0) == '/') {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            path = path.substring(1);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   343
        }
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
   344
        if (path == null || path.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            path = "./";
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   346
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (!path.endsWith("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            i = path.lastIndexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            if (i > 0) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   350
                filename = path.substring(i + 1, path.length());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                filename = ParseUtil.decode(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                pathname = path.substring(0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                filename = ParseUtil.decode(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                pathname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            pathname = path.substring(0, path.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            filename = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   361
        if (pathname != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            fullpath = pathname + "/" + (filename != null ? filename : "");
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   363
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            fullpath = filename;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   365
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * As part of RFC-1738 it is specified that the path should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * interpreted as a series of FTP CWD commands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * This is because, '/' is not necessarly the directory delimiter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * on every systems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   374
    private void cd(String path) throws FtpProtocolException, IOException {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   375
        if (path == null || path.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            return;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   377
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (path.indexOf('/') == -1) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   379
            ftp.changeDirectory(ParseUtil.decode(path));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   383
        StringTokenizer token = new StringTokenizer(path, "/");
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   384
        while (token.hasMoreTokens()) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   385
            ftp.changeDirectory(ParseUtil.decode(token.nextToken()));
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   386
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Get the InputStream to retreive the remote file. It will issue the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * "get" (or "dir") command to the ftp server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
   393
     * @return  the {@code InputStream} to the connection.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @throws  IOException if already opened for output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @throws  FtpProtocolException if errors occur during the transfert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   398
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    public InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (!connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   404
        if (http != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            return http.getInputStream();
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   406
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   408
        if (os != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            throw new IOException("Already opened for output");
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   410
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        if (is != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        MessageHeader msgh = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   418
        boolean isAdir = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            decodePath(url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            if (filename == null || type == DIR) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   422
                ftp.setAsciiType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                cd(pathname);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   424
                if (filename == null) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   425
                    is = new FtpInputStream(ftp, ftp.list(null));
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   426
                } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    is = new FtpInputStream(ftp, ftp.nameList(filename));
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   428
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            } else {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   430
                if (type == ASCII) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   431
                    ftp.setAsciiType();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   432
                } else {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   433
                    ftp.setBinaryType();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   434
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                cd(pathname);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   436
                is = new FtpInputStream(ftp, ftp.getFileStream(filename));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            /* Try to get the size of the file in bytes.  If that is
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   440
            successful, then create a MeteredStream. */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            try {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   442
                long l = ftp.getLastTransferSize();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   443
                msgh.add("content-length", Long.toString(l));
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   444
                if (l > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   446
                    // Wrap input stream with MeteredStream to ensure read() will always return -1
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   447
                    // at expected length.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   449
                    // Check if URL should be metered
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   450
                    boolean meteredInput = ProgressMonitor.getDefault().shouldMeterInput(url, "GET");
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   451
                    ProgressSource pi = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   453
                    if (meteredInput) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   454
                        pi = new ProgressSource(url, "GET", l);
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   455
                        pi.beginTracking();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   456
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   458
                    is = new MeteredStream(is, pi, l);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                e.printStackTrace();
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   462
            /* do nothing, since all we were doing was trying to
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   463
            get the size in bytes of the file */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   466
            if (isAdir) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   467
                msgh.add("content-type", "text/plain");
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   468
                msgh.add("access-type", "directory");
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   469
            } else {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   470
                msgh.add("access-type", "file");
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   471
                String ftype = guessContentTypeFromName(fullpath);
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   472
                if (ftype == null && is.markSupported()) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   473
                    ftype = guessContentTypeFromStream(is);
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   474
                }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   475
                if (ftype != null) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   476
                    msgh.add("content-type", ftype);
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   477
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                cd(fullpath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                /* if that worked, then make a directory listing
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   483
                and build an html stream with all the files in
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   484
                the directory */
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   485
                ftp.setAsciiType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   487
                is = new FtpInputStream(ftp, ftp.list(null));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                msgh.add("content-type", "text/plain");
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   489
                msgh.add("access-type", "directory");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            } catch (IOException ex) {
36858
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   491
                FileNotFoundException fnfe = new FileNotFoundException(fullpath);
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   492
                if (ftp != null) {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   493
                    try {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   494
                        ftp.close();
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   495
                    } catch (IOException ioe) {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   496
                        fnfe.addSuppressed(ioe);
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   497
                    }
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   498
                }
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   499
                throw fnfe;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   500
            } catch (FtpProtocolException ex2) {
36858
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   501
                FileNotFoundException fnfe = new FileNotFoundException(fullpath);
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   502
                if (ftp != null) {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   503
                    try {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   504
                        ftp.close();
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   505
                    } catch (IOException ioe) {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   506
                        fnfe.addSuppressed(ioe);
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   507
                    }
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   508
                }
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   509
                throw fnfe;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            }
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   511
        } catch (FtpProtocolException ftpe) {
36858
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   512
            if (ftp != null) {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   513
                try {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   514
                    ftp.close();
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   515
                } catch (IOException ioe) {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   516
                    ftpe.addSuppressed(ioe);
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   517
                }
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   518
            }
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   519
            throw new IOException(ftpe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        setProperties(msgh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * Get the OutputStream to store the remote file. It will issue the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * "put" command to the ftp server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
   529
     * @return  the {@code OutputStream} to the connection.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @throws  IOException if already opened for input or the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *          points to a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @throws  FtpProtocolException if errors occur during the transfert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   535
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    public OutputStream getOutputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        if (!connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   541
        if (http != null) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   542
            OutputStream out = http.getOutputStream();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   543
            // getInputStream() is neccessary to force a writeRequests()
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   544
            // on the http client.
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   545
            http.getInputStream();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   546
            return out;
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   547
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   549
        if (is != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            throw new IOException("Already opened for input");
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   551
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        if (os != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        decodePath(url.getPath());
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
   558
        if (filename == null || filename.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            throw new IOException("illegal filename for a PUT");
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   560
        }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   561
        try {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   562
            if (pathname != null) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   563
                cd(pathname);
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   564
            }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   565
            if (type == ASCII) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   566
                ftp.setAsciiType();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   567
            } else {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   568
                ftp.setBinaryType();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   569
            }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   570
            os = new FtpOutputStream(ftp, ftp.putFileStream(filename, false));
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   571
        } catch (FtpProtocolException e) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   572
            throw new IOException(e);
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   573
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    String guessContentTypeFromFilename(String fname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        return guessContentTypeFromName(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    /**
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
   582
     * Gets the {@code Permission} associated with the host and port.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
   584
     * @return  The {@code Permission} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     */
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   586
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    public Permission getPermission() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        if (permission == null) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   589
            int urlport = url.getPort();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   590
            urlport = urlport < 0 ? FtpClient.defaultPort() : urlport;
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   591
            String urlhost = this.host + ":" + urlport;
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   592
            permission = new SocketPermission(urlhost, "connect");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Sets the general request property. If a property with the key already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * exists, overwrite its value with the new value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @param   key     the keyword by which the request is known
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
   602
     *                  (e.g., "{@code accept}").
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @param   value   the value associated with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @see #getRequestProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     */
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   607
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    public void setRequestProperty(String key, String value) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   609
        super.setRequestProperty(key, value);
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   610
        if ("type".equals(key)) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   611
            if ("i".equalsIgnoreCase(value)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                type = BIN;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   613
            } else if ("a".equalsIgnoreCase(value)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                type = ASCII;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   615
            } else if ("d".equalsIgnoreCase(value)) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   616
                type = DIR;
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   617
            } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                throw new IllegalArgumentException(
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   619
                        "Value of '" + key +
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   620
                        "' request property was '" + value +
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   621
                        "' when it must be either 'i', 'a' or 'd'");
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   622
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * Returns the value of the named general request property for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @param key the keyword by which the request is known (e.g., "accept").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @return  the value of the named general request property for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *           connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @see #setRequestProperty(java.lang.String, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     */
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   636
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    public String getRequestProperty(String key) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   638
        String value = super.getRequestProperty(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        if (value == null) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   641
            if ("type".equals(key)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                value = (type == ASCII ? "a" : type == DIR ? "d" : "i");
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   643
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   649
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    public void setConnectTimeout(int timeout) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   651
        if (timeout < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            throw new IllegalArgumentException("timeouts can't be negative");
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   653
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        connectTimeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   657
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    public int getConnectTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        return (connectTimeout < 0 ? 0 : connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   662
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    public void setReadTimeout(int timeout) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   664
        if (timeout < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            throw new IllegalArgumentException("timeouts can't be negative");
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   666
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        readTimeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   670
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    public int getReadTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        return readTimeout < 0 ? 0 : readTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
}