author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 57879 | 095c2f21dd10 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55711 | 2 |
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/** |
|
27 |
* FTP stream opener. |
|
28 |
*/ |
|
29 |
||
30 |
package sun.net.www.protocol.ftp; |
|
31 |
||
32 |
import java.io.IOException; |
|
33 |
import java.io.InputStream; |
|
34 |
import java.io.OutputStream; |
|
35 |
import java.io.BufferedInputStream; |
|
36 |
import java.io.FilterInputStream; |
|
37 |
import java.io.FilterOutputStream; |
|
38 |
import java.io.FileNotFoundException; |
|
55711 | 39 |
import java.net.MalformedURLException; |
2 | 40 |
import java.net.URL; |
41 |
import java.net.SocketPermission; |
|
42 |
import java.net.UnknownHostException; |
|
43 |
import java.net.InetSocketAddress; |
|
44 |
import java.net.URI; |
|
45 |
import java.net.Proxy; |
|
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 | 48 |
import java.util.StringTokenizer; |
49 |
import java.util.Iterator; |
|
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 | 53 |
import sun.net.util.IPAddressUtil; |
2 | 54 |
import sun.net.www.MessageHeader; |
55 |
import sun.net.www.MeteredStream; |
|
56 |
import sun.net.www.URLConnection; |
|
57 |
import sun.net.www.protocol.http.HttpURLConnection; |
|
58 |
import sun.net.ftp.FtpClient; |
|
59 |
import sun.net.ftp.FtpProtocolException; |
|
60 |
import sun.net.ProgressSource; |
|
61 |
import sun.net.ProgressMonitor; |
|
62 |
import sun.net.www.ParseUtil; |
|
63 |
import sun.security.action.GetPropertyAction; |
|
64 |
||
65 |
||
66 |
/** |
|
67 |
* This class Opens an FTP input (or output) stream given a URL. |
|
68 |
* It works as a one shot FTP transfer : |
|
69 |
* <UL> |
|
70 |
* <LI>Login</LI> |
|
71 |
* <LI>Get (or Put) the file</LI> |
|
72 |
* <LI>Disconnect</LI> |
|
73 |
* </UL> |
|
74 |
* You should not have to use it directly in most cases because all will be handled |
|
31061 | 75 |
* in a abstract layer. Here is an example of how to use the class: |
76 |
* <pre>{@code |
|
77 |
* URL url = new URL("ftp://ftp.sun.com/pub/test.txt"); |
|
78 |
* UrlConnection con = url.openConnection(); |
|
79 |
* InputStream is = con.getInputStream(); |
|
80 |
* ... |
|
81 |
* is.close(); |
|
82 |
* }</pre> |
|
2 | 83 |
* |
84 |
* @see sun.net.ftp.FtpClient |
|
85 |
*/ |
|
86 |
public class FtpURLConnection extends URLConnection { |
|
87 |
||
88 |
// In case we have to use proxies, we use HttpURLConnection |
|
89 |
HttpURLConnection http = null; |
|
90 |
private Proxy instProxy; |
|
91 |
||
92 |
InputStream is = null; |
|
93 |
OutputStream os = null; |
|
94 |
||
95 |
FtpClient ftp = null; |
|
96 |
Permission permission; |
|
97 |
||
98 |
String password; |
|
99 |
String user; |
|
100 |
||
101 |
String host; |
|
102 |
String pathname; |
|
103 |
String filename; |
|
104 |
String fullpath; |
|
105 |
int port; |
|
106 |
static final int NONE = 0; |
|
107 |
static final int ASCII = 1; |
|
108 |
static final int BIN = 2; |
|
109 |
static final int DIR = 3; |
|
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 | 112 |
* not set. This is to ensure backward compatibility. |
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 | 116 |
|
117 |
/** |
|
118 |
* For FTP URLs we need to have a special InputStream because we |
|
119 |
* need to close 2 sockets after we're done with it : |
|
120 |
* - The Data socket (for the file). |
|
121 |
* - The command socket (FtpClient). |
|
122 |
* Since that's the only class that needs to see that, it is an inner class. |
|
123 |
*/ |
|
124 |
protected class FtpInputStream extends FilterInputStream { |
|
125 |
FtpClient ftp; |
|
126 |
FtpInputStream(FtpClient cl, InputStream fd) { |
|
127 |
super(new BufferedInputStream(fd)); |
|
128 |
ftp = cl; |
|
129 |
} |
|
130 |
||
4155 | 131 |
@Override |
2 | 132 |
public void close() throws IOException { |
133 |
super.close(); |
|
4155 | 134 |
if (ftp != null) { |
135 |
ftp.close(); |
|
2 | 136 |
} |
137 |
} |
|
138 |
} |
|
139 |
||
140 |
/** |
|
141 |
* For FTP URLs we need to have a special OutputStream because we |
|
142 |
* need to close 2 sockets after we're done with it : |
|
143 |
* - The Data socket (for the file). |
|
144 |
* - The command socket (FtpClient). |
|
145 |
* Since that's the only class that needs to see that, it is an inner class. |
|
146 |
*/ |
|
147 |
protected class FtpOutputStream extends FilterOutputStream { |
|
148 |
FtpClient ftp; |
|
149 |
FtpOutputStream(FtpClient cl, OutputStream fd) { |
|
150 |
super(fd); |
|
151 |
ftp = cl; |
|
152 |
} |
|
153 |
||
4155 | 154 |
@Override |
2 | 155 |
public void close() throws IOException { |
156 |
super.close(); |
|
4155 | 157 |
if (ftp != null) { |
158 |
ftp.close(); |
|
2 | 159 |
} |
160 |
} |
|
161 |
} |
|
162 |
||
55711 | 163 |
static URL checkURL(URL u) throws IllegalArgumentException { |
164 |
if (u != null) { |
|
165 |
if (u.toExternalForm().indexOf('\n') > -1) { |
|
166 |
Exception mfue = new MalformedURLException("Illegal character in URL"); |
|
167 |
throw new IllegalArgumentException(mfue.getMessage(), mfue); |
|
168 |
} |
|
169 |
} |
|
170 |
String s = IPAddressUtil.checkAuthority(u); |
|
171 |
if (s != null) { |
|
172 |
Exception mfue = new MalformedURLException(s); |
|
173 |
throw new IllegalArgumentException(mfue.getMessage(), mfue); |
|
174 |
} |
|
175 |
return u; |
|
176 |
} |
|
177 |
||
2 | 178 |
/** |
179 |
* Creates an FtpURLConnection from a URL. |
|
180 |
* |
|
31061 | 181 |
* @param url The {@code URL} to retrieve or store. |
2 | 182 |
*/ |
183 |
public FtpURLConnection(URL url) { |
|
184 |
this(url, null); |
|
185 |
} |
|
186 |
||
187 |
/** |
|
188 |
* Same as FtpURLconnection(URL) with a per connection proxy specified |
|
189 |
*/ |
|
190 |
FtpURLConnection(URL url, Proxy p) { |
|
55711 | 191 |
super(checkURL(url)); |
2 | 192 |
instProxy = p; |
193 |
host = url.getHost(); |
|
194 |
port = url.getPort(); |
|
195 |
String userInfo = url.getUserInfo(); |
|
196 |
||
197 |
if (userInfo != null) { // get the user and password |
|
198 |
int delimiter = userInfo.indexOf(':'); |
|
199 |
if (delimiter == -1) { |
|
200 |
user = ParseUtil.decode(userInfo); |
|
201 |
password = null; |
|
202 |
} else { |
|
203 |
user = ParseUtil.decode(userInfo.substring(0, delimiter++)); |
|
204 |
password = ParseUtil.decode(userInfo.substring(delimiter)); |
|
205 |
} |
|
206 |
} |
|
207 |
} |
|
208 |
||
209 |
private void setTimeouts() { |
|
210 |
if (ftp != null) { |
|
4155 | 211 |
if (connectTimeout >= 0) { |
2 | 212 |
ftp.setConnectTimeout(connectTimeout); |
4155 | 213 |
} |
214 |
if (readTimeout >= 0) { |
|
2 | 215 |
ftp.setReadTimeout(readTimeout); |
4155 | 216 |
} |
2 | 217 |
} |
218 |
} |
|
219 |
||
220 |
/** |
|
221 |
* Connects to the FTP server and logs in. |
|
222 |
* |
|
223 |
* @throws FtpLoginException if the login is unsuccessful |
|
224 |
* @throws FtpProtocolException if an error occurs |
|
225 |
* @throws UnknownHostException if trying to connect to an unknown host |
|
226 |
*/ |
|
227 |
||
228 |
public synchronized void connect() throws IOException { |
|
229 |
if (connected) { |
|
230 |
return; |
|
231 |
} |
|
232 |
||
233 |
Proxy p = null; |
|
234 |
if (instProxy == null) { // no per connection proxy specified |
|
235 |
/** |
|
51 | 236 |
* Do we have to use a proxy? |
2 | 237 |
*/ |
51 | 238 |
ProxySelector sel = java.security.AccessController.doPrivileged( |
4155 | 239 |
new java.security.PrivilegedAction<ProxySelector>() { |
240 |
public ProxySelector run() { |
|
241 |
return ProxySelector.getDefault(); |
|
242 |
} |
|
243 |
}); |
|
2 | 244 |
if (sel != null) { |
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 | 253 |
while (it.hasNext()) { |
254 |
p = it.next(); |
|
255 |
if (p == null || p == Proxy.NO_PROXY || |
|
4155 | 256 |
p.type() == Proxy.Type.SOCKS) { |
2 | 257 |
break; |
4155 | 258 |
} |
2 | 259 |
if (p.type() != Proxy.Type.HTTP || |
4155 | 260 |
!(p.address() instanceof InetSocketAddress)) { |
2 | 261 |
sel.connectFailed(uri, p.address(), new IOException("Wrong proxy type")); |
262 |
continue; |
|
263 |
} |
|
264 |
// OK, we have an http proxy |
|
265 |
InetSocketAddress paddr = (InetSocketAddress) p.address(); |
|
266 |
try { |
|
267 |
http = new HttpURLConnection(url, p); |
|
4155 | 268 |
http.setDoInput(getDoInput()); |
269 |
http.setDoOutput(getDoOutput()); |
|
270 |
if (connectTimeout >= 0) { |
|
2 | 271 |
http.setConnectTimeout(connectTimeout); |
4155 | 272 |
} |
273 |
if (readTimeout >= 0) { |
|
2 | 274 |
http.setReadTimeout(readTimeout); |
4155 | 275 |
} |
2 | 276 |
http.connect(); |
277 |
connected = true; |
|
278 |
return; |
|
279 |
} catch (IOException ioe) { |
|
280 |
sel.connectFailed(uri, paddr, ioe); |
|
281 |
http = null; |
|
282 |
} |
|
283 |
} |
|
284 |
} |
|
285 |
} else { // per connection proxy specified |
|
286 |
p = instProxy; |
|
287 |
if (p.type() == Proxy.Type.HTTP) { |
|
288 |
http = new HttpURLConnection(url, instProxy); |
|
4155 | 289 |
http.setDoInput(getDoInput()); |
290 |
http.setDoOutput(getDoOutput()); |
|
291 |
if (connectTimeout >= 0) { |
|
2 | 292 |
http.setConnectTimeout(connectTimeout); |
4155 | 293 |
} |
294 |
if (readTimeout >= 0) { |
|
2 | 295 |
http.setReadTimeout(readTimeout); |
4155 | 296 |
} |
2 | 297 |
http.connect(); |
298 |
connected = true; |
|
299 |
return; |
|
300 |
} |
|
301 |
} |
|
302 |
||
303 |
if (user == null) { |
|
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 | 309 |
} |
310 |
try { |
|
4155 | 311 |
ftp = FtpClient.create(); |
312 |
if (p != null) { |
|
313 |
ftp.setProxy(p); |
|
314 |
} |
|
2 | 315 |
setTimeouts(); |
4155 | 316 |
if (port != -1) { |
317 |
ftp.connect(new InetSocketAddress(host, port)); |
|
318 |
} else { |
|
319 |
ftp.connect(new InetSocketAddress(host, FtpClient.defaultPort())); |
|
320 |
} |
|
2 | 321 |
} catch (UnknownHostException e) { |
322 |
// Maybe do something smart here, like use a proxy like iftp. |
|
323 |
// Just keep throwing for now. |
|
324 |
throw e; |
|
4155 | 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 | 333 |
throw new IOException(fe); |
2 | 334 |
} |
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 | 337 |
} catch (sun.net.ftp.FtpProtocolException e) { |
338 |
ftp.close(); |
|
339 |
// Backward compatibility |
|
340 |
throw new sun.net.ftp.FtpLoginException("Invalid username/password"); |
|
2 | 341 |
} |
342 |
connected = true; |
|
343 |
} |
|
344 |
||
345 |
||
346 |
/* |
|
347 |
* Decodes the path as per the RFC-1738 specifications. |
|
348 |
*/ |
|
349 |
private void decodePath(String path) { |
|
350 |
int i = path.indexOf(";type="); |
|
351 |
if (i >= 0) { |
|
4155 | 352 |
String s1 = path.substring(i + 6, path.length()); |
353 |
if ("i".equalsIgnoreCase(s1)) { |
|
2 | 354 |
type = BIN; |
4155 | 355 |
} |
356 |
if ("a".equalsIgnoreCase(s1)) { |
|
2 | 357 |
type = ASCII; |
4155 | 358 |
} |
359 |
if ("d".equalsIgnoreCase(s1)) { |
|
2 | 360 |
type = DIR; |
4155 | 361 |
} |
2 | 362 |
path = path.substring(0, i); |
363 |
} |
|
364 |
if (path != null && path.length() > 1 && |
|
4155 | 365 |
path.charAt(0) == '/') { |
2 | 366 |
path = path.substring(1); |
4155 | 367 |
} |
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
47216
diff
changeset
|
368 |
if (path == null || path.isEmpty()) { |
2 | 369 |
path = "./"; |
4155 | 370 |
} |
2 | 371 |
if (!path.endsWith("/")) { |
372 |
i = path.lastIndexOf('/'); |
|
373 |
if (i > 0) { |
|
4155 | 374 |
filename = path.substring(i + 1, path.length()); |
2 | 375 |
filename = ParseUtil.decode(filename); |
376 |
pathname = path.substring(0, i); |
|
377 |
} else { |
|
378 |
filename = ParseUtil.decode(path); |
|
379 |
pathname = null; |
|
380 |
} |
|
381 |
} else { |
|
382 |
pathname = path.substring(0, path.length() - 1); |
|
383 |
filename = null; |
|
384 |
} |
|
4155 | 385 |
if (pathname != null) { |
2 | 386 |
fullpath = pathname + "/" + (filename != null ? filename : ""); |
4155 | 387 |
} else { |
2 | 388 |
fullpath = filename; |
4155 | 389 |
} |
2 | 390 |
} |
391 |
||
392 |
/* |
|
393 |
* As part of RFC-1738 it is specified that the path should be |
|
394 |
* interpreted as a series of FTP CWD commands. |
|
395 |
* This is because, '/' is not necessarly the directory delimiter |
|
396 |
* on every systems. |
|
397 |
*/ |
|
4155 | 398 |
private void cd(String path) throws FtpProtocolException, IOException { |
399 |
if (path == null || path.isEmpty()) { |
|
2 | 400 |
return; |
4155 | 401 |
} |
2 | 402 |
if (path.indexOf('/') == -1) { |
4155 | 403 |
ftp.changeDirectory(ParseUtil.decode(path)); |
2 | 404 |
return; |
405 |
} |
|
406 |
||
4155 | 407 |
StringTokenizer token = new StringTokenizer(path, "/"); |
408 |
while (token.hasMoreTokens()) { |
|
409 |
ftp.changeDirectory(ParseUtil.decode(token.nextToken())); |
|
410 |
} |
|
2 | 411 |
} |
412 |
||
413 |
/** |
|
414 |
* Get the InputStream to retreive the remote file. It will issue the |
|
415 |
* "get" (or "dir") command to the ftp server. |
|
416 |
* |
|
31061 | 417 |
* @return the {@code InputStream} to the connection. |
2 | 418 |
* |
419 |
* @throws IOException if already opened for output |
|
420 |
* @throws FtpProtocolException if errors occur during the transfert. |
|
421 |
*/ |
|
4155 | 422 |
@Override |
2 | 423 |
public InputStream getInputStream() throws IOException { |
424 |
if (!connected) { |
|
425 |
connect(); |
|
426 |
} |
|
427 |
||
4155 | 428 |
if (http != null) { |
2 | 429 |
return http.getInputStream(); |
4155 | 430 |
} |
2 | 431 |
|
4155 | 432 |
if (os != null) { |
2 | 433 |
throw new IOException("Already opened for output"); |
4155 | 434 |
} |
2 | 435 |
|
436 |
if (is != null) { |
|
437 |
return is; |
|
438 |
} |
|
439 |
||
440 |
MessageHeader msgh = new MessageHeader(); |
|
441 |
||
4155 | 442 |
boolean isAdir = false; |
2 | 443 |
try { |
444 |
decodePath(url.getPath()); |
|
445 |
if (filename == null || type == DIR) { |
|
4155 | 446 |
ftp.setAsciiType(); |
2 | 447 |
cd(pathname); |
4155 | 448 |
if (filename == null) { |
449 |
is = new FtpInputStream(ftp, ftp.list(null)); |
|
450 |
} else { |
|
2 | 451 |
is = new FtpInputStream(ftp, ftp.nameList(filename)); |
4155 | 452 |
} |
2 | 453 |
} else { |
4155 | 454 |
if (type == ASCII) { |
455 |
ftp.setAsciiType(); |
|
456 |
} else { |
|
457 |
ftp.setBinaryType(); |
|
458 |
} |
|
2 | 459 |
cd(pathname); |
4155 | 460 |
is = new FtpInputStream(ftp, ftp.getFileStream(filename)); |
2 | 461 |
} |
462 |
||
463 |
/* Try to get the size of the file in bytes. If that is |
|
4155 | 464 |
successful, then create a MeteredStream. */ |
2 | 465 |
try { |
4155 | 466 |
long l = ftp.getLastTransferSize(); |
467 |
msgh.add("content-length", Long.toString(l)); |
|
468 |
if (l > 0) { |
|
2 | 469 |
|
4155 | 470 |
// Wrap input stream with MeteredStream to ensure read() will always return -1 |
471 |
// at expected length. |
|
2 | 472 |
|
4155 | 473 |
// Check if URL should be metered |
474 |
boolean meteredInput = ProgressMonitor.getDefault().shouldMeterInput(url, "GET"); |
|
475 |
ProgressSource pi = null; |
|
2 | 476 |
|
4155 | 477 |
if (meteredInput) { |
478 |
pi = new ProgressSource(url, "GET", l); |
|
479 |
pi.beginTracking(); |
|
480 |
} |
|
2 | 481 |
|
4155 | 482 |
is = new MeteredStream(is, pi, l); |
2 | 483 |
} |
484 |
} catch (Exception e) { |
|
485 |
e.printStackTrace(); |
|
4155 | 486 |
/* do nothing, since all we were doing was trying to |
487 |
get the size in bytes of the file */ |
|
2 | 488 |
} |
489 |
||
4155 | 490 |
if (isAdir) { |
491 |
msgh.add("content-type", "text/plain"); |
|
492 |
msgh.add("access-type", "directory"); |
|
493 |
} else { |
|
494 |
msgh.add("access-type", "file"); |
|
495 |
String ftype = guessContentTypeFromName(fullpath); |
|
496 |
if (ftype == null && is.markSupported()) { |
|
497 |
ftype = guessContentTypeFromStream(is); |
|
498 |
} |
|
499 |
if (ftype != null) { |
|
500 |
msgh.add("content-type", ftype); |
|
501 |
} |
|
2 | 502 |
} |
503 |
} catch (FileNotFoundException e) { |
|
504 |
try { |
|
505 |
cd(fullpath); |
|
506 |
/* if that worked, then make a directory listing |
|
4155 | 507 |
and build an html stream with all the files in |
508 |
the directory */ |
|
509 |
ftp.setAsciiType(); |
|
2 | 510 |
|
4155 | 511 |
is = new FtpInputStream(ftp, ftp.list(null)); |
2 | 512 |
msgh.add("content-type", "text/plain"); |
4155 | 513 |
msgh.add("access-type", "directory"); |
2 | 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 | 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 | 534 |
} |
4155 | 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 | 543 |
throw new IOException(ftpe); |
2 | 544 |
} |
545 |
setProperties(msgh); |
|
546 |
return is; |
|
547 |
} |
|
548 |
||
549 |
/** |
|
550 |
* Get the OutputStream to store the remote file. It will issue the |
|
551 |
* "put" command to the ftp server. |
|
552 |
* |
|
31061 | 553 |
* @return the {@code OutputStream} to the connection. |
2 | 554 |
* |
555 |
* @throws IOException if already opened for input or the URL |
|
556 |
* points to a directory |
|
557 |
* @throws FtpProtocolException if errors occur during the transfert. |
|
558 |
*/ |
|
4155 | 559 |
@Override |
2 | 560 |
public OutputStream getOutputStream() throws IOException { |
561 |
if (!connected) { |
|
562 |
connect(); |
|
563 |
} |
|
564 |
||
4155 | 565 |
if (http != null) { |
566 |
OutputStream out = http.getOutputStream(); |
|
567 |
// getInputStream() is neccessary to force a writeRequests() |
|
568 |
// on the http client. |
|
569 |
http.getInputStream(); |
|
570 |
return out; |
|
571 |
} |
|
2 | 572 |
|
4155 | 573 |
if (is != null) { |
2 | 574 |
throw new IOException("Already opened for input"); |
4155 | 575 |
} |
2 | 576 |
|
577 |
if (os != null) { |
|
578 |
return os; |
|
579 |
} |
|
580 |
||
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 | 583 |
throw new IOException("illegal filename for a PUT"); |
4155 | 584 |
} |
585 |
try { |
|
586 |
if (pathname != null) { |
|
587 |
cd(pathname); |
|
588 |
} |
|
589 |
if (type == ASCII) { |
|
590 |
ftp.setAsciiType(); |
|
591 |
} else { |
|
592 |
ftp.setBinaryType(); |
|
593 |
} |
|
594 |
os = new FtpOutputStream(ftp, ftp.putFileStream(filename, false)); |
|
595 |
} catch (FtpProtocolException e) { |
|
596 |
throw new IOException(e); |
|
597 |
} |
|
2 | 598 |
return os; |
599 |
} |
|
600 |
||
601 |
String guessContentTypeFromFilename(String fname) { |
|
602 |
return guessContentTypeFromName(fname); |
|
603 |
} |
|
604 |
||
605 |
/** |
|
31061 | 606 |
* Gets the {@code Permission} associated with the host and port. |
2 | 607 |
* |
31061 | 608 |
* @return The {@code Permission} object. |
2 | 609 |
*/ |
4155 | 610 |
@Override |
2 | 611 |
public Permission getPermission() { |
612 |
if (permission == null) { |
|
4155 | 613 |
int urlport = url.getPort(); |
614 |
urlport = urlport < 0 ? FtpClient.defaultPort() : urlport; |
|
615 |
String urlhost = this.host + ":" + urlport; |
|
616 |
permission = new SocketPermission(urlhost, "connect"); |
|
2 | 617 |
} |
618 |
return permission; |
|
619 |
} |
|
620 |
||
621 |
/** |
|
622 |
* Sets the general request property. If a property with the key already |
|
623 |
* exists, overwrite its value with the new value. |
|
624 |
* |
|
625 |
* @param key the keyword by which the request is known |
|
31061 | 626 |
* (e.g., "{@code accept}"). |
2 | 627 |
* @param value the value associated with it. |
628 |
* @throws IllegalStateException if already connected |
|
629 |
* @see #getRequestProperty(java.lang.String) |
|
630 |
*/ |
|
4155 | 631 |
@Override |
2 | 632 |
public void setRequestProperty(String key, String value) { |
4155 | 633 |
super.setRequestProperty(key, value); |
634 |
if ("type".equals(key)) { |
|
635 |
if ("i".equalsIgnoreCase(value)) { |
|
2 | 636 |
type = BIN; |
4155 | 637 |
} else if ("a".equalsIgnoreCase(value)) { |
2 | 638 |
type = ASCII; |
4155 | 639 |
} else if ("d".equalsIgnoreCase(value)) { |
640 |
type = DIR; |
|
641 |
} else { |
|
2 | 642 |
throw new IllegalArgumentException( |
4155 | 643 |
"Value of '" + key + |
644 |
"' request property was '" + value + |
|
645 |
"' when it must be either 'i', 'a' or 'd'"); |
|
646 |
} |
|
2 | 647 |
} |
648 |
} |
|
649 |
||
650 |
/** |
|
651 |
* Returns the value of the named general request property for this |
|
652 |
* connection. |
|
653 |
* |
|
654 |
* @param key the keyword by which the request is known (e.g., "accept"). |
|
655 |
* @return the value of the named general request property for this |
|
656 |
* connection. |
|
657 |
* @throws IllegalStateException if already connected |
|
658 |
* @see #setRequestProperty(java.lang.String, java.lang.String) |
|
659 |
*/ |
|
4155 | 660 |
@Override |
2 | 661 |
public String getRequestProperty(String key) { |
4155 | 662 |
String value = super.getRequestProperty(key); |
2 | 663 |
|
664 |
if (value == null) { |
|
4155 | 665 |
if ("type".equals(key)) { |
2 | 666 |
value = (type == ASCII ? "a" : type == DIR ? "d" : "i"); |
4155 | 667 |
} |
2 | 668 |
} |
669 |
||
670 |
return value; |
|
671 |
} |
|
672 |
||
4155 | 673 |
@Override |
2 | 674 |
public void setConnectTimeout(int timeout) { |
4155 | 675 |
if (timeout < 0) { |
2 | 676 |
throw new IllegalArgumentException("timeouts can't be negative"); |
4155 | 677 |
} |
2 | 678 |
connectTimeout = timeout; |
679 |
} |
|
680 |
||
4155 | 681 |
@Override |
2 | 682 |
public int getConnectTimeout() { |
683 |
return (connectTimeout < 0 ? 0 : connectTimeout); |
|
684 |
} |
|
685 |
||
4155 | 686 |
@Override |
2 | 687 |
public void setReadTimeout(int timeout) { |
4155 | 688 |
if (timeout < 0) { |
2 | 689 |
throw new IllegalArgumentException("timeouts can't be negative"); |
4155 | 690 |
} |
2 | 691 |
readTimeout = timeout; |
692 |
} |
|
693 |
||
4155 | 694 |
@Override |
2 | 695 |
public int getReadTimeout() { |
696 |
return readTimeout < 0 ? 0 : readTimeout; |
|
697 |
} |
|
698 |
} |