src/java.base/share/classes/sun/net/www/protocol/ftp/FtpURLConnection.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 57879 095c2f21dd10
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55711
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
     2
 * Copyright (c) 1994, 2019, 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;
55711
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
    39
import java.net.MalformedURLException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.net.URL;
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.InetSocketAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.net.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.net.ProxySelector;
57879
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents: 55711
diff changeset
    47
import java.util.List;
2
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;
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36858
diff changeset
    51
import java.util.Properties;
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
    52
import sun.net.NetworkClient;
55711
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
    53
import sun.net.util.IPAddressUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import sun.net.www.MessageHeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import sun.net.www.MeteredStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import sun.net.www.URLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import sun.net.www.protocol.http.HttpURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import sun.net.ftp.FtpClient;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import sun.net.ftp.FtpProtocolException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import sun.net.ProgressSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import sun.net.ProgressMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import sun.net.www.ParseUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * This class Opens an FTP input (or output) stream given a URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * It works as a one shot FTP transfer :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <LI>Login</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <LI>Get (or Put) the file</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <LI>Disconnect</LI>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * 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
    75
 * 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
    76
 * <pre>{@code
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
    77
 * URL url = new URL("ftp://ftp.sun.com/pub/test.txt");
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
    78
 * UrlConnection con = url.openConnection();
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
    79
 * InputStream is = con.getInputStream();
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
    80
 * ...
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
    81
 * is.close();
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
    82
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @see sun.net.ftp.FtpClient
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
public class FtpURLConnection extends URLConnection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // In case we have to use proxies, we use HttpURLConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    HttpURLConnection http = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private Proxy instProxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    OutputStream os = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    FtpClient ftp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    Permission permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    String password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    String user;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    String pathname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    String filename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    String fullpath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    static final int NONE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    static final int ASCII = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    static final int BIN = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    static final int DIR = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    int type = NONE;
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   111
    /* Redefine timeouts from java.net.URLConnection as we need -1 to mean
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * not set. This is to ensure backward compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
7022
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   114
    private int connectTimeout = NetworkClient.DEFAULT_CONNECT_TIMEOUT;;
1066bfde0f5e 6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents: 5506
diff changeset
   115
    private int readTimeout = NetworkClient.DEFAULT_READ_TIMEOUT;;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * For FTP URLs we need to have a special InputStream because we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * need to close 2 sockets after we're done with it :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *  - The Data socket (for the file).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *   - The command socket (FtpClient).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Since that's the only class that needs to see that, it is an inner class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    protected class FtpInputStream extends FilterInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        FtpClient ftp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        FtpInputStream(FtpClient cl, InputStream fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            super(new BufferedInputStream(fd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            ftp = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   131
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            super.close();
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   134
            if (ftp != null) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   135
                ftp.close();
2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * For FTP URLs we need to have a special OutputStream because we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * need to close 2 sockets after we're done with it :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *  - The Data socket (for the file).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *   - The command socket (FtpClient).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Since that's the only class that needs to see that, it is an inner class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    protected class FtpOutputStream extends FilterOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        FtpClient ftp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        FtpOutputStream(FtpClient cl, OutputStream fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            super(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            ftp = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   154
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            super.close();
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   157
            if (ftp != null) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   158
                ftp.close();
2
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
55711
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   163
    static URL checkURL(URL u) throws IllegalArgumentException {
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   164
        if (u != null) {
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   165
            if (u.toExternalForm().indexOf('\n') > -1) {
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   166
                Exception mfue = new MalformedURLException("Illegal character in URL");
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   167
                throw new IllegalArgumentException(mfue.getMessage(), mfue);
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   168
            }
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   169
        }
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   170
        String s = IPAddressUtil.checkAuthority(u);
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   171
        if (s != null) {
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   172
            Exception mfue = new MalformedURLException(s);
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   173
            throw new IllegalArgumentException(mfue.getMessage(), mfue);
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   174
        }
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   175
        return u;
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   176
    }
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   177
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Creates an FtpURLConnection from a URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
   181
     * @param   url     The {@code URL} to retrieve or store.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public FtpURLConnection(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        this(url, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Same as FtpURLconnection(URL) with a per connection proxy specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    FtpURLConnection(URL url, Proxy p) {
55711
0c143aaa2c99 8221518: Normalize normalization
dfuchs
parents: 53018
diff changeset
   191
        super(checkURL(url));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        instProxy = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        port = url.getPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        String userInfo = url.getUserInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (userInfo != null) { // get the user and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            int delimiter = userInfo.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (delimiter == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                user = ParseUtil.decode(userInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                password = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                user = ParseUtil.decode(userInfo.substring(0, delimiter++));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                password = ParseUtil.decode(userInfo.substring(delimiter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private void setTimeouts() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (ftp != null) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   211
            if (connectTimeout >= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                ftp.setConnectTimeout(connectTimeout);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   213
            }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   214
            if (readTimeout >= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                ftp.setReadTimeout(readTimeout);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   216
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Connects to the FTP server and logs in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @throws  FtpLoginException if the login is unsuccessful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @throws  FtpProtocolException if an error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @throws  UnknownHostException if trying to connect to an unknown host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public synchronized void connect() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        Proxy p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (instProxy == null) { // no per connection proxy specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            /**
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   236
             * Do we have to use a proxy?
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
             */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   238
            ProxySelector sel = java.security.AccessController.doPrivileged(
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   239
                    new java.security.PrivilegedAction<ProxySelector>() {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   240
                        public ProxySelector run() {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   241
                            return ProxySelector.getDefault();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   242
                        }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   243
                    });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if (sel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                URI uri = sun.net.www.ParseUtil.toURI(url);
57879
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents: 55711
diff changeset
   246
                final List<Proxy> proxies;
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents: 55711
diff changeset
   247
                try {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents: 55711
diff changeset
   248
                    proxies = sel.select(uri);
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents: 55711
diff changeset
   249
                } catch (IllegalArgumentException iae) {
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents: 55711
diff changeset
   250
                    throw new IOException("Failed to select a proxy", iae);
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents: 55711
diff changeset
   251
                }
095c2f21dd10 8177648: getResponseCode() throws IllegalArgumentException caused by protocol error while following redirect
michaelm
parents: 55711
diff changeset
   252
                final Iterator<Proxy> it = proxies.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    p = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    if (p == null || p == Proxy.NO_PROXY ||
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   256
                        p.type() == Proxy.Type.SOCKS) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                        break;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   258
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    if (p.type() != Proxy.Type.HTTP ||
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   260
                            !(p.address() instanceof InetSocketAddress)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        sel.connectFailed(uri, p.address(), new IOException("Wrong proxy type"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    // OK, we have an http proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    InetSocketAddress paddr = (InetSocketAddress) p.address();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        http = new HttpURLConnection(url, p);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   268
                        http.setDoInput(getDoInput());
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   269
                        http.setDoOutput(getDoOutput());
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   270
                        if (connectTimeout >= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                            http.setConnectTimeout(connectTimeout);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   272
                        }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   273
                        if (readTimeout >= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                            http.setReadTimeout(readTimeout);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   275
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        http.connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                        connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        sel.connectFailed(uri, paddr, ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        http = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        } else { // per connection proxy specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            p = instProxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (p.type() == Proxy.Type.HTTP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                http = new HttpURLConnection(url, instProxy);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   289
                http.setDoInput(getDoInput());
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   290
                http.setDoOutput(getDoOutput());
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   291
                if (connectTimeout >= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    http.setConnectTimeout(connectTimeout);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   293
                }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   294
                if (readTimeout >= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    http.setReadTimeout(readTimeout);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   296
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                http.connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (user == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            user = "anonymous";
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
   305
            Properties props = GetPropertyAction.privilegedGetProperties();
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36858
diff changeset
   306
            String vers = props.getProperty("java.version");
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36858
diff changeset
   307
            password = props.getProperty("ftp.protocol.user",
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 36858
diff changeset
   308
                    "Java" + vers + "@");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        try {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   311
            ftp = FtpClient.create();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   312
            if (p != null) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   313
                ftp.setProxy(p);
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   314
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            setTimeouts();
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   316
            if (port != -1) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   317
                ftp.connect(new InetSocketAddress(host, port));
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   318
            } else {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   319
                ftp.connect(new InetSocketAddress(host, FtpClient.defaultPort()));
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   320
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            // Maybe do something smart here, like use a proxy like iftp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            // Just keep throwing for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throw e;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   325
        } catch (FtpProtocolException fe) {
36858
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   326
            if (ftp != null) {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   327
                try {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   328
                    ftp.close();
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   329
                } catch (IOException ioe) {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   330
                    fe.addSuppressed(ioe);
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   331
                }
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   332
            }
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   333
            throw new IOException(fe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        try {
25805
0bc2aa4aadbe 8031435: Ftp download does not work properly for ftp user without password
robm
parents: 7668
diff changeset
   336
            ftp.login(user, password == null ? null : password.toCharArray());
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   337
        } catch (sun.net.ftp.FtpProtocolException e) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   338
            ftp.close();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   339
            // Backward compatibility
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   340
            throw new sun.net.ftp.FtpLoginException("Invalid username/password");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Decodes the path as per the RFC-1738 specifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private void decodePath(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        int i = path.indexOf(";type=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (i >= 0) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   352
            String s1 = path.substring(i + 6, path.length());
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   353
            if ("i".equalsIgnoreCase(s1)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                type = BIN;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   355
            }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   356
            if ("a".equalsIgnoreCase(s1)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                type = ASCII;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   358
            }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   359
            if ("d".equalsIgnoreCase(s1)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                type = DIR;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   361
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            path = path.substring(0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (path != null && path.length() > 1 &&
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   365
                path.charAt(0) == '/') {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            path = path.substring(1);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   367
        }
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
   368
        if (path == null || path.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            path = "./";
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   370
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (!path.endsWith("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            i = path.lastIndexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            if (i > 0) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   374
                filename = path.substring(i + 1, path.length());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                filename = ParseUtil.decode(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                pathname = path.substring(0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                filename = ParseUtil.decode(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                pathname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            pathname = path.substring(0, path.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            filename = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   385
        if (pathname != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            fullpath = pathname + "/" + (filename != null ? filename : "");
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   387
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            fullpath = filename;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   389
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * As part of RFC-1738 it is specified that the path should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * interpreted as a series of FTP CWD commands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * This is because, '/' is not necessarly the directory delimiter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * on every systems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   398
    private void cd(String path) throws FtpProtocolException, IOException {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   399
        if (path == null || path.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            return;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   401
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (path.indexOf('/') == -1) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   403
            ftp.changeDirectory(ParseUtil.decode(path));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   407
        StringTokenizer token = new StringTokenizer(path, "/");
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   408
        while (token.hasMoreTokens()) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   409
            ftp.changeDirectory(ParseUtil.decode(token.nextToken()));
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * Get the InputStream to retreive the remote file. It will issue the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * "get" (or "dir") command to the ftp server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
   417
     * @return  the {@code InputStream} to the connection.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @throws  IOException if already opened for output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @throws  FtpProtocolException if errors occur during the transfert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   422
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    public InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (!connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   428
        if (http != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            return http.getInputStream();
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   430
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   432
        if (os != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            throw new IOException("Already opened for output");
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   434
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (is != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        MessageHeader msgh = new MessageHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   442
        boolean isAdir = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            decodePath(url.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            if (filename == null || type == DIR) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   446
                ftp.setAsciiType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                cd(pathname);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   448
                if (filename == null) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   449
                    is = new FtpInputStream(ftp, ftp.list(null));
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   450
                } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    is = new FtpInputStream(ftp, ftp.nameList(filename));
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   452
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            } else {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   454
                if (type == ASCII) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   455
                    ftp.setAsciiType();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   456
                } else {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   457
                    ftp.setBinaryType();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   458
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                cd(pathname);
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   460
                is = new FtpInputStream(ftp, ftp.getFileStream(filename));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            /* 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
   464
            successful, then create a MeteredStream. */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            try {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   466
                long l = ftp.getLastTransferSize();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   467
                msgh.add("content-length", Long.toString(l));
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   468
                if (l > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   470
                    // 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
   471
                    // at expected length.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   473
                    // Check if URL should be metered
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   474
                    boolean meteredInput = ProgressMonitor.getDefault().shouldMeterInput(url, "GET");
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   475
                    ProgressSource pi = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   477
                    if (meteredInput) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   478
                        pi = new ProgressSource(url, "GET", l);
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   479
                        pi.beginTracking();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   480
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   482
                    is = new MeteredStream(is, pi, l);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                e.printStackTrace();
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   486
            /* do nothing, since all we were doing was trying to
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   487
            get the size in bytes of the file */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   490
            if (isAdir) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   491
                msgh.add("content-type", "text/plain");
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   492
                msgh.add("access-type", "directory");
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   493
            } else {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   494
                msgh.add("access-type", "file");
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   495
                String ftype = guessContentTypeFromName(fullpath);
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   496
                if (ftype == null && is.markSupported()) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   497
                    ftype = guessContentTypeFromStream(is);
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   498
                }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   499
                if (ftype != null) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   500
                    msgh.add("content-type", ftype);
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   501
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        } catch (FileNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                cd(fullpath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                /* if that worked, then make a directory listing
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   507
                and build an html stream with all the files in
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   508
                the directory */
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   509
                ftp.setAsciiType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   511
                is = new FtpInputStream(ftp, ftp.list(null));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                msgh.add("content-type", "text/plain");
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   513
                msgh.add("access-type", "directory");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            } catch (IOException ex) {
36858
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   515
                FileNotFoundException fnfe = new FileNotFoundException(fullpath);
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   516
                if (ftp != null) {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   517
                    try {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   518
                        ftp.close();
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   519
                    } catch (IOException ioe) {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   520
                        fnfe.addSuppressed(ioe);
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   521
                    }
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   522
                }
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   523
                throw fnfe;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   524
            } catch (FtpProtocolException ex2) {
36858
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   525
                FileNotFoundException fnfe = new FileNotFoundException(fullpath);
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   526
                if (ftp != null) {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   527
                    try {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   528
                        ftp.close();
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   529
                    } catch (IOException ioe) {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   530
                        fnfe.addSuppressed(ioe);
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   531
                    }
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   532
                }
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   533
                throw fnfe;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            }
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   535
        } catch (FtpProtocolException ftpe) {
36858
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   536
            if (ftp != null) {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   537
                try {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   538
                    ftp.close();
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   539
                } catch (IOException ioe) {
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   540
                    ftpe.addSuppressed(ioe);
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   541
                }
53f90d1cdac4 7167293: FtpURLConnection connection leak on FileNotFoundException
vtewari
parents: 31061
diff changeset
   542
            }
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   543
            throw new IOException(ftpe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        setProperties(msgh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * Get the OutputStream to store the remote file. It will issue the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * "put" command to the ftp server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
   553
     * @return  the {@code OutputStream} to the connection.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @throws  IOException if already opened for input or the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *          points to a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @throws  FtpProtocolException if errors occur during the transfert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   559
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    public OutputStream getOutputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        if (!connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   565
        if (http != null) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   566
            OutputStream out = http.getOutputStream();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   567
            // getInputStream() is neccessary to force a writeRequests()
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   568
            // on the http client.
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   569
            http.getInputStream();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   570
            return out;
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   571
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   573
        if (is != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            throw new IOException("Already opened for input");
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   575
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        if (os != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        decodePath(url.getPath());
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
   582
        if (filename == null || filename.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            throw new IOException("illegal filename for a PUT");
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   584
        }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   585
        try {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   586
            if (pathname != null) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   587
                cd(pathname);
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   588
            }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   589
            if (type == ASCII) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   590
                ftp.setAsciiType();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   591
            } else {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   592
                ftp.setBinaryType();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   593
            }
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   594
            os = new FtpOutputStream(ftp, ftp.putFileStream(filename, false));
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   595
        } catch (FtpProtocolException e) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   596
            throw new IOException(e);
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   597
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    String guessContentTypeFromFilename(String fname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        return guessContentTypeFromName(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /**
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
   606
     * Gets the {@code Permission} associated with the host and port.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
   608
     * @return  The {@code Permission} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     */
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   610
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    public Permission getPermission() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        if (permission == null) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   613
            int urlport = url.getPort();
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   614
            urlport = urlport < 0 ? FtpClient.defaultPort() : urlport;
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   615
            String urlhost = this.host + ":" + urlport;
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   616
            permission = new SocketPermission(urlhost, "connect");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        return permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * Sets the general request property. If a property with the key already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * exists, overwrite its value with the new value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @param   key     the keyword by which the request is known
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 25859
diff changeset
   626
     *                  (e.g., "{@code accept}").
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @param   value   the value associated with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * @see #getRequestProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     */
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   631
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    public void setRequestProperty(String key, String value) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   633
        super.setRequestProperty(key, value);
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   634
        if ("type".equals(key)) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   635
            if ("i".equalsIgnoreCase(value)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                type = BIN;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   637
            } else if ("a".equalsIgnoreCase(value)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                type = ASCII;
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   639
            } else if ("d".equalsIgnoreCase(value)) {
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   640
                type = DIR;
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   641
            } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                throw new IllegalArgumentException(
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   643
                        "Value of '" + key +
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   644
                        "' request property was '" + value +
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   645
                        "' when it must be either 'i', 'a' or 'd'");
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   646
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * Returns the value of the named general request property for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @param key the keyword by which the request is known (e.g., "accept").
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @return  the value of the named general request property for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *           connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @throws IllegalStateException if already connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @see #setRequestProperty(java.lang.String, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     */
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   660
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    public String getRequestProperty(String key) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   662
        String value = super.getRequestProperty(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        if (value == null) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   665
            if ("type".equals(key)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                value = (type == ASCII ? "a" : type == DIR ? "d" : "i");
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   667
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   673
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    public void setConnectTimeout(int timeout) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   675
        if (timeout < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            throw new IllegalArgumentException("timeouts can't be negative");
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   677
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        connectTimeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   681
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    public int getConnectTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        return (connectTimeout < 0 ? 0 : connectTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   686
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    public void setReadTimeout(int timeout) {
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   688
        if (timeout < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            throw new IllegalArgumentException("timeouts can't be negative");
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   690
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        readTimeout = timeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
4155
460e37d40f12 6893702: Overhaul of Ftp Client internal code
jccollet
parents: 715
diff changeset
   694
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    public int getReadTimeout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        return readTimeout < 0 ? 0 : readTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
}