| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.dev.ftp; |
| 7 |
|
| 8 |
import java.io.BufferedReader; |
| 9 |
import java.io.ByteArrayOutputStream; |
| 10 |
import java.io.File; |
| 11 |
import java.io.FileInputStream; |
| 12 |
import java.io.IOException; |
| 13 |
import java.io.InputStream; |
| 14 |
import java.io.InputStreamReader; |
| 15 |
import java.io.OutputStream; |
| 16 |
import java.io.OutputStreamWriter; |
| 17 |
import java.io.PrintWriter; |
| 18 |
import java.net.InetAddress; |
| 19 |
import java.net.Socket; |
| 20 |
import java.nio.charset.StandardCharsets; |
| 21 |
|
| 22 |
import org.h2.util.IOUtils; |
| 23 |
import org.h2.util.NetUtils; |
| 24 |
import org.h2.util.StringUtils; |
| 25 |
|
| 26 |
/** |
| 27 |
* A simple standalone FTP client. |
| 28 |
*/ |
| 29 |
public class FtpClient { |
| 30 |
private Socket socket; |
| 31 |
private BufferedReader reader; |
| 32 |
private PrintWriter writer; |
| 33 |
private int code; |
| 34 |
private String message; |
| 35 |
private InputStream inData; |
| 36 |
private OutputStream outData; |
| 37 |
|
| 38 |
private FtpClient() { |
| 39 |
// don't allow construction |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Open an FTP connection. |
| 44 |
* |
| 45 |
* @param url the FTP URL |
| 46 |
* @return the ftp client object |
| 47 |
*/ |
| 48 |
public static FtpClient open(String url) throws IOException { |
| 49 |
FtpClient client = new FtpClient(); |
| 50 |
client.connect(url); |
| 51 |
return client; |
| 52 |
} |
| 53 |
|
| 54 |
private void connect(String url) throws IOException { |
| 55 |
socket = NetUtils.createSocket(url, 21, false); |
| 56 |
InputStream in = socket.getInputStream(); |
| 57 |
OutputStream out = socket.getOutputStream(); |
| 58 |
reader = new BufferedReader(new InputStreamReader(in)); |
| 59 |
writer = new PrintWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8)); |
| 60 |
readCode(220); |
| 61 |
} |
| 62 |
|
| 63 |
private void readLine() throws IOException { |
| 64 |
while (true) { |
| 65 |
message = reader.readLine(); |
| 66 |
if (message != null) { |
| 67 |
int idxSpace = message.indexOf(' '); |
| 68 |
int idxMinus = message.indexOf('-'); |
| 69 |
int idx = idxSpace < 0 ? idxMinus : idxMinus < 0 ? idxSpace |
| 70 |
: Math.min(idxSpace, idxMinus); |
| 71 |
if (idx < 0) { |
| 72 |
code = 0; |
| 73 |
} else { |
| 74 |
code = Integer.parseInt(message.substring(0, idx)); |
| 75 |
message = message.substring(idx + 1); |
| 76 |
} |
| 77 |
} |
| 78 |
break; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
private void readCode(int optional, int expected) throws IOException { |
| 83 |
readLine(); |
| 84 |
if (code == optional) { |
| 85 |
readLine(); |
| 86 |
} |
| 87 |
if (code != expected) { |
| 88 |
throw new IOException("Expected: " + expected + " got: " + code |
| 89 |
+ " " + message); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
private void readCode(int expected) throws IOException { |
| 94 |
readCode(-1, expected); |
| 95 |
} |
| 96 |
|
| 97 |
private void send(String command) { |
| 98 |
writer.println(command); |
| 99 |
writer.flush(); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Login to this FTP server (USER, PASS, SYST, SITE, STRU F, TYPE I). |
| 104 |
* |
| 105 |
* @param userName the user name |
| 106 |
* @param password the password |
| 107 |
*/ |
| 108 |
public void login(String userName, String password) throws IOException { |
| 109 |
send("USER " + userName); |
| 110 |
readCode(331); |
| 111 |
send("PASS " + password); |
| 112 |
readCode(230); |
| 113 |
send("SYST"); |
| 114 |
readCode(215); |
| 115 |
send("SITE"); |
| 116 |
readCode(500); |
| 117 |
send("STRU F"); |
| 118 |
readCode(200); |
| 119 |
send("TYPE I"); |
| 120 |
readCode(200); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Close the connection (QUIT). |
| 125 |
*/ |
| 126 |
public void close() throws IOException { |
| 127 |
if (socket != null) { |
| 128 |
send("QUIT"); |
| 129 |
readCode(221); |
| 130 |
socket.close(); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Change the working directory (CWD). |
| 136 |
* |
| 137 |
* @param dir the new directory |
| 138 |
*/ |
| 139 |
public void changeWorkingDirectory(String dir) throws IOException { |
| 140 |
send("CWD " + dir); |
| 141 |
readCode(250); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Change to the parent directory (CDUP). |
| 146 |
*/ |
| 147 |
public void changeDirectoryUp() throws IOException { |
| 148 |
send("CDUP"); |
| 149 |
readCode(250); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Delete a file (DELE). |
| 154 |
* |
| 155 |
* @param fileName the name of the file to delete |
| 156 |
*/ |
| 157 |
void delete(String fileName) throws IOException { |
| 158 |
send("DELE " + fileName); |
| 159 |
readCode(226, 250); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Create a directory (MKD). |
| 164 |
* |
| 165 |
* @param dir the directory to create |
| 166 |
*/ |
| 167 |
public void makeDirectory(String dir) throws IOException { |
| 168 |
send("MKD " + dir); |
| 169 |
readCode(226, 257); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Change the transfer mode (MODE). |
| 174 |
* |
| 175 |
* @param mode the mode |
| 176 |
*/ |
| 177 |
void mode(String mode) throws IOException { |
| 178 |
send("MODE " + mode); |
| 179 |
readCode(200); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Change the modified time of a file (MDTM). |
| 184 |
* |
| 185 |
* @param fileName the file name |
| 186 |
*/ |
| 187 |
void modificationTime(String fileName) throws IOException { |
| 188 |
send("MDTM " + fileName); |
| 189 |
readCode(213); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Issue a no-operation statement (NOOP). |
| 194 |
*/ |
| 195 |
void noOperation() throws IOException { |
| 196 |
send("NOOP"); |
| 197 |
readCode(200); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Print the working directory (PWD). |
| 202 |
* |
| 203 |
* @return the working directory |
| 204 |
*/ |
| 205 |
String printWorkingDirectory() throws IOException { |
| 206 |
send("PWD"); |
| 207 |
readCode(257); |
| 208 |
return removeQuotes(); |
| 209 |
} |
| 210 |
|
| 211 |
private String removeQuotes() { |
| 212 |
int first = message.indexOf('"') + 1; |
| 213 |
int last = message.lastIndexOf('"'); |
| 214 |
StringBuilder buff = new StringBuilder(); |
| 215 |
for (int i = first; i < last; i++) { |
| 216 |
char ch = message.charAt(i); |
| 217 |
buff.append(ch); |
| 218 |
if (ch == '\"') { |
| 219 |
i++; |
| 220 |
} |
| 221 |
} |
| 222 |
return buff.toString(); |
| 223 |
} |
| 224 |
|
| 225 |
private void passive() throws IOException { |
| 226 |
send("PASV"); |
| 227 |
readCode(226, 227); |
| 228 |
int first = message.indexOf('(') + 1; |
| 229 |
int last = message.indexOf(')'); |
| 230 |
String[] address = StringUtils.arraySplit( |
| 231 |
message.substring(first, last), ',', true); |
| 232 |
StringBuilder builder = new StringBuilder(); |
| 233 |
for (int i = 0; i < 4; i++) { |
| 234 |
if (i > 0) { |
| 235 |
builder.append('.'); |
| 236 |
} |
| 237 |
builder.append(address[i]); |
| 238 |
} |
| 239 |
String ip = builder.toString(); |
| 240 |
InetAddress addr = InetAddress.getByName(ip); |
| 241 |
int port = (Integer.parseInt(address[4]) << 8) | Integer.parseInt(address[5]); |
| 242 |
Socket socketData = NetUtils.createSocket(addr, port, false); |
| 243 |
inData = socketData.getInputStream(); |
| 244 |
outData = socketData.getOutputStream(); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Rename a file (RNFR / RNTO). |
| 249 |
* |
| 250 |
* @param fromFileName the old file name |
| 251 |
* @param toFileName the new file name |
| 252 |
*/ |
| 253 |
void rename(String fromFileName, String toFileName) throws IOException { |
| 254 |
send("RNFR " + fromFileName); |
| 255 |
readCode(350); |
| 256 |
send("RNTO " + toFileName); |
| 257 |
readCode(250); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Read a file. |
| 262 |
* |
| 263 |
* @param fileName the file name |
| 264 |
* @return the content, null if the file doesn't exist |
| 265 |
*/ |
| 266 |
public byte[] retrieve(String fileName) throws IOException { |
| 267 |
ByteArrayOutputStream buff = new ByteArrayOutputStream(); |
| 268 |
retrieve(fileName, buff, 0); |
| 269 |
return buff.toByteArray(); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Read a file ([REST] RETR). |
| 274 |
* |
| 275 |
* @param fileName the file name |
| 276 |
* @param out the output stream |
| 277 |
* @param restartAt restart at the given position (0 if no restart is |
| 278 |
* required). |
| 279 |
*/ |
| 280 |
void retrieve(String fileName, OutputStream out, long restartAt) |
| 281 |
throws IOException { |
| 282 |
passive(); |
| 283 |
if (restartAt > 0) { |
| 284 |
send("REST " + restartAt); |
| 285 |
readCode(350); |
| 286 |
} |
| 287 |
send("RETR " + fileName); |
| 288 |
IOUtils.copyAndClose(inData, out); |
| 289 |
readCode(150, 226); |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Remove a directory (RMD). |
| 294 |
* |
| 295 |
* @param dir the directory to remove |
| 296 |
*/ |
| 297 |
public void removeDirectory(String dir) throws IOException { |
| 298 |
send("RMD " + dir); |
| 299 |
readCode(226, 250); |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Remove all files and directory in a directory, and then delete the |
| 304 |
* directory itself. |
| 305 |
* |
| 306 |
* @param dir the directory to remove |
| 307 |
*/ |
| 308 |
public void removeDirectoryRecursive(String dir) throws IOException { |
| 309 |
for (File f : listFiles(dir)) { |
| 310 |
String name = f.getName(); |
| 311 |
if (f.isDirectory()) { |
| 312 |
if (!name.equals(".") && !name.equals("..")) { |
| 313 |
removeDirectoryRecursive(dir + "/" + name); |
| 314 |
} |
| 315 |
} else { |
| 316 |
delete(dir + "/" + name); |
| 317 |
} |
| 318 |
} |
| 319 |
removeDirectory(dir); |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Get the size of a file (SIZE). |
| 324 |
* |
| 325 |
* @param fileName the file name |
| 326 |
* @return the size |
| 327 |
*/ |
| 328 |
long size(String fileName) throws IOException { |
| 329 |
send("SIZE " + fileName); |
| 330 |
readCode(250); |
| 331 |
long size = Long.parseLong(message); |
| 332 |
return size; |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Store a file (STOR). |
| 337 |
* |
| 338 |
* @param fileName the file name |
| 339 |
* @param in the input stream |
| 340 |
*/ |
| 341 |
public void store(String fileName, InputStream in) throws IOException { |
| 342 |
passive(); |
| 343 |
send("STOR " + fileName); |
| 344 |
readCode(150); |
| 345 |
IOUtils.copyAndClose(in, outData); |
| 346 |
readCode(226); |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Copy a local file or directory to the FTP server, recursively. |
| 351 |
* |
| 352 |
* @param file the file to copy |
| 353 |
*/ |
| 354 |
public void storeRecursive(File file) throws IOException { |
| 355 |
if (file.isDirectory()) { |
| 356 |
makeDirectory(file.getName()); |
| 357 |
changeWorkingDirectory(file.getName()); |
| 358 |
for (File f : file.listFiles()) { |
| 359 |
storeRecursive(f); |
| 360 |
} |
| 361 |
changeWorkingDirectory(".."); |
| 362 |
} else { |
| 363 |
InputStream in = new FileInputStream(file); |
| 364 |
store(file.getName(), in); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* Get the directory listing (NLST). |
| 370 |
* |
| 371 |
* @param dir the directory |
| 372 |
* @return the listing |
| 373 |
*/ |
| 374 |
public String nameList(String dir) throws IOException { |
| 375 |
passive(); |
| 376 |
send("NLST " + dir); |
| 377 |
readCode(150); |
| 378 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 379 |
IOUtils.copyAndClose(inData, out); |
| 380 |
readCode(226); |
| 381 |
return out.toString(); |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* Get the directory listing (LIST). |
| 386 |
* |
| 387 |
* @param dir the directory |
| 388 |
* @return the listing |
| 389 |
*/ |
| 390 |
public String list(String dir) throws IOException { |
| 391 |
passive(); |
| 392 |
send("LIST " + dir); |
| 393 |
readCode(150); |
| 394 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 395 |
IOUtils.copyAndClose(inData, out); |
| 396 |
readCode(226); |
| 397 |
return out.toString(); |
| 398 |
} |
| 399 |
|
| 400 |
/** |
| 401 |
* A file on an FTP server. |
| 402 |
*/ |
| 403 |
static class FtpFile extends File { |
| 404 |
private static final long serialVersionUID = 1L; |
| 405 |
private final boolean dir; |
| 406 |
private final long length; |
| 407 |
FtpFile(String name, boolean dir, long length) { |
| 408 |
super(name); |
| 409 |
this.dir = dir; |
| 410 |
this.length = length; |
| 411 |
} |
| 412 |
@Override |
| 413 |
public long length() { |
| 414 |
return length; |
| 415 |
} |
| 416 |
@Override |
| 417 |
public boolean isFile() { |
| 418 |
return !dir; |
| 419 |
} |
| 420 |
@Override |
| 421 |
public boolean isDirectory() { |
| 422 |
return dir; |
| 423 |
} |
| 424 |
@Override |
| 425 |
public boolean exists() { |
| 426 |
return true; |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
/** |
| 431 |
* Check if a file exists on the FTP server. |
| 432 |
* |
| 433 |
* @param dir the directory |
| 434 |
* @param name the directory or file name |
| 435 |
* @return true if it exists |
| 436 |
*/ |
| 437 |
public boolean exists(String dir, String name) throws IOException { |
| 438 |
for (File f : listFiles(dir)) { |
| 439 |
if (f.getName().equals(name)) { |
| 440 |
return true; |
| 441 |
} |
| 442 |
} |
| 443 |
return false; |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* List the files on the FTP server. |
| 448 |
* |
| 449 |
* @param dir the directory |
| 450 |
* @return the list of files |
| 451 |
*/ |
| 452 |
public File[] listFiles(String dir) throws IOException { |
| 453 |
String content = list(dir); |
| 454 |
String[] list = StringUtils.arraySplit(content.trim(), '\n', true); |
| 455 |
File[] files = new File[list.length]; |
| 456 |
for (int i = 0; i < files.length; i++) { |
| 457 |
String s = list[i]; |
| 458 |
while (true) { |
| 459 |
String s2 = StringUtils.replaceAll(s, " ", " "); |
| 460 |
if (s2.equals(s)) { |
| 461 |
break; |
| 462 |
} |
| 463 |
s = s2; |
| 464 |
} |
| 465 |
String[] tokens = StringUtils.arraySplit(s, ' ', true); |
| 466 |
boolean directory = tokens[0].charAt(0) == 'd'; |
| 467 |
long length = Long.parseLong(tokens[4]); |
| 468 |
String name = tokens[8]; |
| 469 |
File f = new FtpFile(name, directory, length); |
| 470 |
files[i] = f; |
| 471 |
} |
| 472 |
return files; |
| 473 |
} |
| 474 |
|
| 475 |
} |