From 01c75a7aa09d0d9fd7a4c61f952aaa93972e7c99 Mon Sep 17 00:00:00 2001
From: Joe Robinson
+ * To derive the full benefits of the FTP class requires some knowledge
+ * of the FTP protocol defined in RFC 959. However, there is no reason
+ * why you should have to use the FTP class. The
+ * {@link org.apache.commons.net.ftp.FTPClient} class,
+ * derived from FTP,
+ * implements all the functionality required of an FTP client. The
+ * FTP class is made public to provide access to various FTP constants
+ * and to make it easier for adventurous programmers (or those with
+ * special needs) to interact with the FTP protocol and implement their
+ * own clients. A set of methods with names corresponding to the FTP
+ * command names are provided to facilitate this interaction.
+ *
+ * You should keep in mind that the FTP server may choose to prematurely
+ * close a connection if the client has been idle for longer than a
+ * given time period (usually 900 seconds). The FTP class will detect a
+ * premature FTP server connection closing when it receives a
+ * {@link org.apache.commons.net.ftp.FTPReply#SERVICE_NOT_AVAILABLE FTPReply.SERVICE_NOT_AVAILABLE }
+ * response to a command.
+ * When that occurs, the FTP class method encountering that reply will throw
+ * an {@link org.apache.commons.net.ftp.FTPConnectionClosedException}
+ * .
+ * Rather than list it separately for each method, we mention here that
+ * every method communicating with the server and throwing an IOException
+ * can also throw a
+ * {@link org.apache.commons.net.MalformedServerReplyException}
+ * , which is a subclass
+ * of IOException. A MalformedServerReplyException will be thrown when
+ * the reply received from the server deviates enough from the protocol
+ * specification that it cannot be interpreted in a useful manner despite
+ * attempts to be as lenient as possible.
+ *
+ *
+ * @author Daniel F. Savarese
+ * @author Rory Winston
+ * @author Joseph Hindsley
+ * @see FTPClient
+ * @see FTPConnectionClosedException
+ * @see org.apache.commons.net.MalformedServerReplyException
+ * @version $Id: FTP.java 658520 2008-05-21 01:14:11Z sebb $
+ ***/
+
+public class FTP extends SocketClient
+{
+ /*** The default FTP data port (20). ***/
+ public static final int DEFAULT_DATA_PORT = 20;
+ /*** The default FTP control port (21). ***/
+ public static final int DEFAULT_PORT = 21;
+
+ /***
+ * A constant used to indicate the file(s) being transfered should
+ * be treated as ASCII. This is the default file type. All constants
+ * ending in
+ * @param listener The ProtocolCommandListener to add.
+ ***/
+ public void addProtocolCommandListener(ProtocolCommandListener listener)
+ {
+ _commandSupport_.addProtocolCommandListener(listener);
+ }
+
+ /***
+ * Removes a ProtocolCommandListener. Delegates this task to
+ * {@link #_commandSupport_ _commandSupport_ }.
+ *
+ * @param listener The ProtocolCommandListener to remove.
+ ***/
+ public void removeProtocolCommandListener(ProtocolCommandListener listener)
+ {
+ _commandSupport_.removeProtocolCommandListener(listener);
+ }
+
+
+ /***
+ * Closes the control connection to the FTP server and sets to null
+ * some internal data so that the memory may be reclaimed by the
+ * garbage collector. The reply text and code information from the
+ * last command is voided so that the memory it used may be reclaimed.
+ * Also sets {@link #_controlInput_} and {@link #_controlOutput_} to null.
+ *
+ * @exception IOException If an error occurs while disconnecting.
+ ***/
+ @Override
+ public void disconnect() throws IOException
+ {
+ super.disconnect();
+ _controlInput_ = null;
+ _controlOutput_ = null;
+ _newReplyString = false;
+ _replyString = null;
+ }
+
+
+ /***
+ * Sends an FTP command to the server, waits for a reply and returns the
+ * numerical response code. After invocation, for more detailed
+ * information, the actual reply text can be accessed by calling
+ * {@link #getReplyString getReplyString } or
+ * {@link #getReplyStrings getReplyStrings }.
+ *
+ * @param command The text representation of the FTP command to send.
+ * @param args The arguments to the FTP command. If this parameter is
+ * set to null, then the command is sent with no argument.
+ * @return The integer value of the FTP reply code returned by the server
+ * in response to the command.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int sendCommand(String command, String args) throws IOException
+ {
+ String message;
+
+ __commandBuffer.setLength(0);
+ __commandBuffer.append(command);
+
+ if (args != null)
+ {
+ __commandBuffer.append(' ');
+ __commandBuffer.append(args);
+ }
+ __commandBuffer.append(SocketClient.NETASCII_EOL);
+
+ try{
+ _controlOutput_.write(message = __commandBuffer.toString());
+ _controlOutput_.flush();
+ }
+ catch (SocketException e)
+ {
+ if (!isConnected() || !socketIsConnected(_socket_))
+ {
+ throw new FTPConnectionClosedException("Connection unexpectedly closed.");
+ }
+ else
+ {
+ throw e;
+ }
+ }
+
+
+ if (_commandSupport_.getListenerCount() > 0)
+ _commandSupport_.fireCommandSent(command, message);
+
+ __getReply();
+ return _replyCode;
+ }
+
+ /**
+ * Checks if the socket is connected
+ *
+ * @param socket
+ * @return true if connected
+ */
+ private boolean socketIsConnected(Socket socket)
+ {
+ if (socket == null)
+ {
+ return false;
+ }
+
+ return socket.isConnected();
+
+ }
+
+ /***
+ * Sends an FTP command to the server, waits for a reply and returns the
+ * numerical response code. After invocation, for more detailed
+ * information, the actual reply text can be accessed by calling
+ * {@link #getReplyString getReplyString } or
+ * {@link #getReplyStrings getReplyStrings }.
+ *
+ * @param command The FTPCommand constant corresponding to the FTP command
+ * to send.
+ * @param args The arguments to the FTP command. If this parameter is
+ * set to null, then the command is sent with no argument.
+ * @return The integer value of the FTP reply code returned by the server
+ * in response to the command.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int sendCommand(int command, String args) throws IOException
+ {
+ return sendCommand(FTPCommand._commands[command], args);
+ }
+
+
+ /***
+ * Sends an FTP command with no arguments to the server, waits for a
+ * reply and returns the numerical response code. After invocation, for
+ * more detailed information, the actual reply text can be accessed by
+ * calling {@link #getReplyString getReplyString } or
+ * {@link #getReplyStrings getReplyStrings }.
+ *
+ * @param command The text representation of the FTP command to send.
+ * @return The integer value of the FTP reply code returned by the server
+ * in response to the command.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int sendCommand(String command) throws IOException
+ {
+ return sendCommand(command, null);
+ }
+
+
+ /***
+ * Sends an FTP command with no arguments to the server, waits for a
+ * reply and returns the numerical response code. After invocation, for
+ * more detailed information, the actual reply text can be accessed by
+ * calling {@link #getReplyString getReplyString } or
+ * {@link #getReplyStrings getReplyStrings }.
+ *
+ * @param command The FTPCommand constant corresponding to the FTP command
+ * to send.
+ * @return The integer value of the FTP reply code returned by the server
+ * in response to the command.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int sendCommand(int command) throws IOException
+ {
+ return sendCommand(command, null);
+ }
+
+
+ /***
+ * Returns the integer value of the reply code of the last FTP reply.
+ * You will usually only use this method after you connect to the
+ * FTP server to check that the connection was successful since
+ *
+ * @return The integer value of the reply code of the last FTP reply.
+ ***/
+ public int getReplyCode()
+ {
+ return _replyCode;
+ }
+
+ /***
+ * Fetches a reply from the FTP server and returns the integer reply
+ * code. After calling this method, the actual reply text can be accessed
+ * from either calling {@link #getReplyString getReplyString } or
+ * {@link #getReplyStrings getReplyStrings }. Only use this
+ * method if you are implementing your own FTP client or if you need to
+ * fetch a secondary response from the FTP server.
+ *
+ * @return The integer value of the reply code of the fetched FTP reply.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while receiving the
+ * server reply.
+ ***/
+ public int getReply() throws IOException
+ {
+ __getReply();
+ return _replyCode;
+ }
+
+
+ /***
+ * Returns the lines of text from the last FTP server response as an array
+ * of strings, one entry per line. The end of line markers of each are
+ * stripped from each line.
+ *
+ * @return The lines of text from the last FTP response as an array.
+ ***/
+ public String[] getReplyStrings()
+ {
+ String[] lines;
+ lines = new String[_replyLines.size()];
+ _replyLines.addAll(Arrays.asList(lines));
+ return lines;
+ }
+
+ /***
+ * Returns the entire text of the last FTP server response exactly
+ * as it was received, including all end of line markers in NETASCII
+ * format.
+ *
+ * @return The entire text from the last FTP response as a String.
+ ***/
+ public String getReplyString()
+ {
+ StringBuilder buffer;
+
+ if (!_newReplyString) {
+ return _replyString;
+ }
+
+ buffer = new StringBuilder(256);
+
+ for (String line : _replyLines) {
+ buffer.append(line);
+ buffer.append(SocketClient.NETASCII_EOL);
+ }
+
+ _newReplyString = false;
+
+ return (_replyString = buffer.toString());
+ }
+
+
+ /***
+ * A convenience method to send the FTP USER command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @param username The username to login under.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int user(String username) throws IOException
+ {
+ return sendCommand(FTPCommand.USER, username);
+ }
+
+ /**
+ * A convenience method to send the FTP PASS command to the server,
+ * receive the reply, and return the reply code.
+ * @param password The plain text password of the username being logged into.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ */
+ public int pass(String password) throws IOException
+ {
+ return sendCommand(FTPCommand.PASS, password);
+ }
+
+ /***
+ * A convenience method to send the FTP ACCT command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @param account The account name to access.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int acct(String account) throws IOException
+ {
+ return sendCommand(FTPCommand.ACCT, account);
+ }
+
+
+ /***
+ * A convenience method to send the FTP ABOR command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int abor() throws IOException
+ {
+ return sendCommand(FTPCommand.ABOR);
+ }
+
+ /***
+ * A convenience method to send the FTP CWD command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @param directory The new working directory.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int cwd(String directory) throws IOException
+ {
+ return sendCommand(FTPCommand.CWD, directory);
+ }
+
+ /***
+ * A convenience method to send the FTP CDUP command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int cdup() throws IOException
+ {
+ return sendCommand(FTPCommand.CDUP);
+ }
+
+ /***
+ * A convenience method to send the FTP QUIT command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int quit() throws IOException
+ {
+ return sendCommand(FTPCommand.QUIT);
+ }
+
+ /***
+ * A convenience method to send the FTP REIN command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int rein() throws IOException
+ {
+ return sendCommand(FTPCommand.REIN);
+ }
+
+ /***
+ * A convenience method to send the FTP SMNT command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @param dir The directory name.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int smnt(String dir) throws IOException
+ {
+ return sendCommand(FTPCommand.SMNT, dir);
+ }
+
+ /***
+ * A convenience method to send the FTP PORT command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @param host The host owning the port.
+ * @param port The new port.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int port(InetAddress host, int port) throws IOException
+ {
+ int num;
+ StringBuffer info = new StringBuffer(24);
+
+ info.append(host.getHostAddress().replace('.', ','));
+ num = port >>> 8;
+ info.append(',');
+ info.append(num);
+ info.append(',');
+ num = port & 0xff;
+ info.append(num);
+
+ return sendCommand(FTPCommand.PORT, info.toString());
+ }
+
+ /***
+ * A convenience method to send the FTP PASV command to the server,
+ * receive the reply, and return the reply code. Remember, it's up
+ * to you to interpret the reply string containing the host/port
+ * information.
+ *
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int pasv() throws IOException
+ {
+ return sendCommand(FTPCommand.PASV);
+ }
+
+ /**
+ * A convenience method to send the FTP TYPE command for text files
+ * to the server, receive the reply, and return the reply code.
+ * @param fileType The type of the file (one of the
+ * @param fileType The type of the file (one of the
+ * @param structure The structure of the file (one of the
+ *
+ * @param mode The transfer mode to use (one of the
+ *
+ * @param pathname The pathname of the file to retrieve.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int retr(String pathname) throws IOException
+ {
+ return sendCommand(FTPCommand.RETR, pathname);
+ }
+
+ /***
+ * A convenience method to send the FTP STOR command to the server,
+ * receive the reply, and return the reply code. Remember, it is up
+ * to you to manage the data connection. If you don't need this low
+ * level of access, use {@link org.apache.commons.net.ftp.FTPClient}
+ * , which will handle all low level details for you.
+ *
+ * @param pathname The pathname to use for the file when stored at
+ * the remote end of the transfer.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int stor(String pathname) throws IOException
+ {
+ return sendCommand(FTPCommand.STOR, pathname);
+ }
+
+ /***
+ * A convenience method to send the FTP STOU command to the server,
+ * receive the reply, and return the reply code. Remember, it is up
+ * to you to manage the data connection. If you don't need this low
+ * level of access, use {@link org.apache.commons.net.ftp.FTPClient}
+ * , which will handle all low level details for you.
+ *
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int stou() throws IOException
+ {
+ return sendCommand(FTPCommand.STOU);
+ }
+
+ /***
+ * A convenience method to send the FTP STOU command to the server,
+ * receive the reply, and return the reply code. Remember, it is up
+ * to you to manage the data connection. If you don't need this low
+ * level of access, use {@link org.apache.commons.net.ftp.FTPClient}
+ * , which will handle all low level details for you.
+ * @param pathname The base pathname to use for the file when stored at
+ * the remote end of the transfer. Some FTP servers
+ * require this.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ */
+ public int stou(String pathname) throws IOException
+ {
+ return sendCommand(FTPCommand.STOU, pathname);
+ }
+
+ /***
+ * A convenience method to send the FTP APPE command to the server,
+ * receive the reply, and return the reply code. Remember, it is up
+ * to you to manage the data connection. If you don't need this low
+ * level of access, use {@link org.apache.commons.net.ftp.FTPClient}
+ * , which will handle all low level details for you.
+ *
+ * @param pathname The pathname to use for the file when stored at
+ * the remote end of the transfer.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int appe(String pathname) throws IOException
+ {
+ return sendCommand(FTPCommand.APPE, pathname);
+ }
+
+ /***
+ * A convenience method to send the FTP ALLO command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @param bytes The number of bytes to allocate.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int allo(int bytes) throws IOException
+ {
+ return sendCommand(FTPCommand.ALLO, Integer.toString(bytes));
+ }
+
+ /***
+ * A convenience method to send the FTP ALLO command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @param bytes The number of bytes to allocate.
+ * @param recordSize The size of a record.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int allo(int bytes, int recordSize) throws IOException
+ {
+ return sendCommand(FTPCommand.ALLO, Integer.toString(bytes) + " R " +
+ Integer.toString(recordSize));
+ }
+
+ /***
+ * A convenience method to send the FTP REST command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @param marker The marker at which to restart a transfer.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int rest(String marker) throws IOException
+ {
+ return sendCommand(FTPCommand.REST, marker);
+ }
+
+
+ /**
+ * @since 2.0
+ **/
+ public int mdtm(String file) throws IOException
+ {
+ return sendCommand(FTPCommand.MDTM, file);
+ }
+
+ /***
+ * A convenience method to send the FTP RNFR command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @param pathname The pathname to rename from.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int rnfr(String pathname) throws IOException
+ {
+ return sendCommand(FTPCommand.RNFR, pathname);
+ }
+
+ /***
+ * A convenience method to send the FTP RNTO command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @param pathname The pathname to rename to
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int rnto(String pathname) throws IOException
+ {
+ return sendCommand(FTPCommand.RNTO, pathname);
+ }
+
+ /***
+ * A convenience method to send the FTP DELE command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @param pathname The pathname to delete.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int dele(String pathname) throws IOException
+ {
+ return sendCommand(FTPCommand.DELE, pathname);
+ }
+
+ /***
+ * A convenience method to send the FTP RMD command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @param pathname The pathname of the directory to remove.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int rmd(String pathname) throws IOException
+ {
+ return sendCommand(FTPCommand.RMD, pathname);
+ }
+
+ /***
+ * A convenience method to send the FTP MKD command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @param pathname The pathname of the new directory to create.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int mkd(String pathname) throws IOException
+ {
+ return sendCommand(FTPCommand.MKD, pathname);
+ }
+
+ /***
+ * A convenience method to send the FTP PWD command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int pwd() throws IOException
+ {
+ return sendCommand(FTPCommand.PWD);
+ }
+
+ /***
+ * A convenience method to send the FTP LIST command to the server,
+ * receive the reply, and return the reply code. Remember, it is up
+ * to you to manage the data connection. If you don't need this low
+ * level of access, use {@link org.apache.commons.net.ftp.FTPClient}
+ * , which will handle all low level details for you.
+ *
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int list() throws IOException
+ {
+ return sendCommand(FTPCommand.LIST);
+ }
+
+ /***
+ * A convenience method to send the FTP LIST command to the server,
+ * receive the reply, and return the reply code. Remember, it is up
+ * to you to manage the data connection. If you don't need this low
+ * level of access, use {@link org.apache.commons.net.ftp.FTPClient}
+ * , which will handle all low level details for you.
+ *
+ * @param pathname The pathname to list.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int list(String pathname) throws IOException
+ {
+ return sendCommand(FTPCommand.LIST, pathname);
+ }
+
+ /***
+ * A convenience method to send the FTP NLST command to the server,
+ * receive the reply, and return the reply code. Remember, it is up
+ * to you to manage the data connection. If you don't need this low
+ * level of access, use {@link org.apache.commons.net.ftp.FTPClient}
+ * , which will handle all low level details for you.
+ *
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int nlst() throws IOException
+ {
+ return sendCommand(FTPCommand.NLST);
+ }
+
+ /***
+ * A convenience method to send the FTP NLST command to the server,
+ * receive the reply, and return the reply code. Remember, it is up
+ * to you to manage the data connection. If you don't need this low
+ * level of access, use {@link org.apache.commons.net.ftp.FTPClient}
+ * , which will handle all low level details for you.
+ *
+ * @param pathname The pathname to list.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int nlst(String pathname) throws IOException
+ {
+ return sendCommand(FTPCommand.NLST, pathname);
+ }
+
+ /***
+ * A convenience method to send the FTP SITE command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @param parameters The site parameters to send.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int site(String parameters) throws IOException
+ {
+ return sendCommand(FTPCommand.SITE, parameters);
+ }
+
+ /***
+ * A convenience method to send the FTP SYST command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int syst() throws IOException
+ {
+ return sendCommand(FTPCommand.SYST);
+ }
+
+ /***
+ * A convenience method to send the FTP STAT command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int stat() throws IOException
+ {
+ return sendCommand(FTPCommand.STAT);
+ }
+
+ /***
+ * A convenience method to send the FTP STAT command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @param pathname A pathname to list.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int stat(String pathname) throws IOException
+ {
+ return sendCommand(FTPCommand.STAT, pathname);
+ }
+
+ /***
+ * A convenience method to send the FTP HELP command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int help() throws IOException
+ {
+ return sendCommand(FTPCommand.HELP);
+ }
+
+ /***
+ * A convenience method to send the FTP HELP command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @param command The command name on which to request help.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int help(String command) throws IOException
+ {
+ return sendCommand(FTPCommand.HELP, command);
+ }
+
+ /***
+ * A convenience method to send the FTP NOOP command to the server,
+ * receive the reply, and return the reply code.
+ *
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int noop() throws IOException
+ {
+ return sendCommand(FTPCommand.NOOP);
+ }
+
+ /**
+ * Return whether strict multiline parsing is enabled, as per RFX 959, section 4.2.
+ * @return True if strict, false if lenient
+ * @since 2.0
+ */
+ public boolean isStrictMultilineParsing() {
+ return strictMultilineParsing;
+ }
+
+ /**
+ * Set strict multiline parsing.
+ * @param strictMultilineParsing
+ * @since 2.0
+ */
+ public void setStrictMultilineParsing(boolean strictMultilineParsing) {
+ this.strictMultilineParsing = strictMultilineParsing;
+ }
+}
+
+/* Emacs configuration
+ * Local variables: **
+ * mode: java **
+ * c-basic-offset: 4 **
+ * indent-tabs-mode: nil **
+ * End: **
+ */
diff --git a/src/org/apache/commons/net/ftp/FTPClient.java b/src/org/apache/commons/net/ftp/FTPClient.java
new file mode 100644
index 0000000..9a2c459
--- /dev/null
+++ b/src/org/apache/commons/net/ftp/FTPClient.java
@@ -0,0 +1,2447 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.net.ftp;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.ArrayList;
+
+import org.apache.commons.net.MalformedServerReplyException;
+import org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory;
+import org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory;
+import org.apache.commons.net.ftp.parser.ParserInitializationException;
+import org.apache.commons.net.io.CopyStreamEvent;
+import org.apache.commons.net.io.CopyStreamException;
+import org.apache.commons.net.io.FromNetASCIIInputStream;
+import org.apache.commons.net.io.ToNetASCIIOutputStream;
+import org.apache.commons.net.io.Util;
+
+/***
+ * FTPClient encapsulates all the functionality necessary to store and
+ * retrieve files from an FTP server. This class takes care of all
+ * low level details of interacting with an FTP server and provides
+ * a convenient higher level interface. As with all classes derived
+ * from {@link org.apache.commons.net.SocketClient},
+ * you must first connect to the server with
+ * {@link org.apache.commons.net.SocketClient#connect connect }
+ * before doing anything, and finally
+ * {@link org.apache.commons.net.SocketClient#disconnect disconnect }
+ * after you're completely finished interacting with the server.
+ * Then you need to check the FTP reply code to see if the connection
+ * was successful. For example:
+ *
+ * Immediately after connecting is the only real time you need to check the
+ * reply code (because connect is of type void). The convention for all the
+ * FTP command methods in FTPClient is such that they either return a
+ * boolean value or some other value.
+ * The boolean methods return true on a successful completion reply from
+ * the FTP server and false on a reply resulting in an error condition or
+ * failure. The methods returning a value other than boolean return a value
+ * containing the higher level data produced by the FTP command, or null if a
+ * reply resulted in an error condition or failure. If you want to access
+ * the exact FTP reply code causing a success or failure, you must call
+ * {@link org.apache.commons.net.ftp.FTP#getReplyCode getReplyCode } after
+ * a success or failure.
+ *
+ * The default settings for FTPClient are for it to use
+ *
+ *
+ * Because the handling of sockets on different platforms can differ
+ * significantly, the FTPClient automatically issues a new PORT command
+ * prior to every transfer requiring that the server connect to the client's
+ * data port. This ensures identical problem-free behavior on Windows, Unix,
+ * and Macintosh platforms. Additionally, it relieves programmers from
+ * having to issue the PORT command themselves and dealing with platform
+ * dependent issues.
+ *
+ * Additionally, for security purposes, all data connections to the
+ * client are verified to ensure that they originated from the intended
+ * party (host and port). If a data connection is initiated by an unexpected
+ * party, the command will close the socket and throw an IOException. You
+ * may disable this behavior with
+ * {@link #setRemoteVerificationEnabled setRemoteVerificationEnabled()}.
+ *
+ * You should keep in mind that the FTP server may choose to prematurely
+ * close a connection if the client has been idle for longer than a
+ * given time period (usually 900 seconds). The FTPClient class will detect a
+ * premature FTP server connection closing when it receives a
+ * {@link org.apache.commons.net.ftp.FTPReply#SERVICE_NOT_AVAILABLE FTPReply.SERVICE_NOT_AVAILABLE }
+ * response to a command.
+ * When that occurs, the FTP class method encountering that reply will throw
+ * an {@link org.apache.commons.net.ftp.FTPConnectionClosedException}
+ * .
+ *
+ * Rather than list it separately for each method, we mention here that
+ * every method communicating with the server and throwing an IOException
+ * can also throw a
+ * {@link org.apache.commons.net.MalformedServerReplyException}
+ * , which is a subclass
+ * of IOException. A MalformedServerReplyException will be thrown when
+ * the reply received from the server deviates enough from the protocol
+ * specification that it cannot be interpreted in a useful manner despite
+ * attempts to be as lenient as possible.
+ *
+ * Listing API Examples
+ * Both paged and unpaged examples of directory listings are available,
+ * as follows:
+ *
+ * Unpaged (whole list) access, using a parser accessible by auto-detect:
+ *
+ * Paged access, using a parser not accessible by auto-detect. The class
+ * defined in the first parameter of initateListParsing should be derived
+ * from org.apache.commons.net.FTPFileEntryParser:
+ *
+ * Paged access, using a parser accessible by auto-detect:
+ *
+ * For examples of using FTPClient on servers whose directory listings
+ *
+ * @author Daniel F. Savarese
+ * @author Rory Winston
+ * @see FTP
+ * @see FTPConnectionClosedException
+ * @see FTPFileEntryParser
+ * @see FTPFileEntryParserFactory
+ * @see DefaultFTPFileEntryParserFactory
+ * @see FTPClientConfig
+ *
+ * @see org.apache.commons.net.MalformedServerReplyException
+ **/
+public class FTPClient extends FTP
+implements Configurable
+{
+ /***
+ * A constant indicating the FTP session is expecting all transfers
+ * to occur between the client (local) and server and that the server
+ * should connect to the client's data port to initiate a data transfer.
+ * This is the default data connection mode when and FTPClient instance
+ * is created.
+ ***/
+ public static final int ACTIVE_LOCAL_DATA_CONNECTION_MODE = 0;
+ /***
+ * A constant indicating the FTP session is expecting all transfers
+ * to occur between two remote servers and that the server
+ * the client is connected to should connect to the other server's
+ * data port to initiate a data transfer.
+ ***/
+ public static final int ACTIVE_REMOTE_DATA_CONNECTION_MODE = 1;
+ /***
+ * A constant indicating the FTP session is expecting all transfers
+ * to occur between the client (local) and server and that the server
+ * is in passive mode, requiring the client to connect to the
+ * server's data port to initiate a transfer.
+ ***/
+ public static final int PASSIVE_LOCAL_DATA_CONNECTION_MODE = 2;
+ /***
+ * A constant indicating the FTP session is expecting all transfers
+ * to occur between two remote servers and that the server
+ * the client is connected to is in passive mode, requiring the other
+ * server to connect to the first server's data port to initiate a data
+ * transfer.
+ ***/
+ public static final int PASSIVE_REMOTE_DATA_CONNECTION_MODE = 3;
+
+ private int __dataConnectionMode, __dataTimeout;
+ private int __passivePort;
+ private String __passiveHost;
+ private int __fileType, __fileFormat, __fileStructure, __fileTransferMode;
+ private boolean __remoteVerificationEnabled;
+ private long __restartOffset;
+ private FTPFileEntryParserFactory __parserFactory;
+ private int __bufferSize;
+ private boolean __listHiddenFiles;
+
+ // __systemName is a cached value that should not be referenced directly
+ // except when assigned in getSystemName and __initDefaults.
+ private String __systemName;
+
+ // __entryParser is a cached value that should not be referenced directly
+ // except when assigned in listFiles(String, String) and __initDefaults.
+ private FTPFileEntryParser __entryParser;
+
+ private FTPClientConfig __configuration;
+
+ /** Pattern for PASV mode responses */
+ private static String __parms = "\\d{1,3},\\d{1,3},\\d{1,3},\\d{1,3},\\d{1,3},\\d{1,3}";
+ private static java.util.regex.Pattern __parms_pat;
+ static {
+ __parms_pat = java.util.regex.Pattern.compile(__parms);
+ }
+
+ /***
+ * Default FTPClient constructor. Creates a new FTPClient instance
+ * with the data connection mode set to
+ *
+ * @param command The text representation of the FTP command to send.
+ * @param arg The arguments to the FTP command. If this parameter is
+ * set to null, then the command is sent with no argument.
+ * @return A Socket corresponding to the established data connection.
+ * Null is returned if an FTP protocol error is reported at
+ * any point during the establishment and initialization of
+ * the connection.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ */
+ protected Socket _openDataConnection_(int command, String arg)
+ throws IOException
+ {
+ Socket socket;
+
+ if (__dataConnectionMode != ACTIVE_LOCAL_DATA_CONNECTION_MODE &&
+ __dataConnectionMode != PASSIVE_LOCAL_DATA_CONNECTION_MODE)
+ return null;
+
+ if (__dataConnectionMode == ACTIVE_LOCAL_DATA_CONNECTION_MODE)
+ {
+ ServerSocket server;
+ server = _serverSocketFactory_.createServerSocket(0, 1, getLocalAddress());
+
+ if (!FTPReply.isPositiveCompletion(port(getLocalAddress(),
+ server.getLocalPort())))
+ {
+ server.close();
+ return null;
+ }
+
+ if ((__restartOffset > 0) && !restart(__restartOffset))
+ {
+ server.close();
+ return null;
+ }
+
+ if (!FTPReply.isPositivePreliminary(sendCommand(command, arg)))
+ {
+ server.close();
+ return null;
+ }
+
+ // For now, let's just use the data timeout value for waiting for
+ // the data connection. It may be desirable to let this be a
+ // separately configurable value. In any case, we really want
+ // to allow preventing the accept from blocking indefinitely.
+ if (__dataTimeout >= 0)
+ server.setSoTimeout(__dataTimeout);
+ try {
+ socket = server.accept();
+ } finally {
+ server.close();
+ }
+ }
+ else
+ { // We must be in PASSIVE_LOCAL_DATA_CONNECTION_MODE
+
+ if (pasv() != FTPReply.ENTERING_PASSIVE_MODE)
+ return null;
+
+ __parsePassiveModeReply(_replyLines.get(_replyLines.size() - 1));
+
+ socket = _socketFactory_.createSocket(__passiveHost, __passivePort);
+ if ((__restartOffset > 0) && !restart(__restartOffset))
+ {
+ socket.close();
+ return null;
+ }
+
+ if (!FTPReply.isPositivePreliminary(sendCommand(command, arg)))
+ {
+ socket.close();
+ return null;
+ }
+ }
+
+ if (__remoteVerificationEnabled && !verifyRemote(socket))
+ {
+ InetAddress host1, host2;
+
+ host1 = socket.getInetAddress();
+ host2 = getRemoteAddress();
+
+ socket.close();
+
+ throw new IOException(
+ "Host attempting data connection " + host1.getHostAddress() +
+ " is not same as server " + host2.getHostAddress());
+ }
+
+ if (__dataTimeout >= 0)
+ socket.setSoTimeout(__dataTimeout);
+
+ return socket;
+ }
+
+
+ @Override
+ protected void _connectAction_() throws IOException
+ {
+ super._connectAction_();
+ __initDefaults();
+ }
+
+
+ /***
+ * Sets the timeout in milliseconds to use when reading from the
+ * data connection. This timeout will be set immediately after
+ * opening the data connection.
+ *
+ * @param timeout The default timeout in milliseconds that is used when
+ * opening a data connection socket.
+ ***/
+ public void setDataTimeout(int timeout)
+ {
+ __dataTimeout = timeout;
+ }
+
+ /**
+ * set the factory used for parser creation to the supplied factory object.
+ *
+ * @param parserFactory
+ * factory object used to create FTPFileEntryParsers
+ *
+ * @see org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory
+ * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
+ */
+ public void setParserFactory(FTPFileEntryParserFactory parserFactory) {
+ __parserFactory = parserFactory;
+ }
+
+
+ /***
+ * Closes the connection to the FTP server and restores
+ * connection parameters to the default values.
+ *
+ * @exception IOException If an error occurs while disconnecting.
+ ***/
+ @Override
+ public void disconnect() throws IOException
+ {
+ super.disconnect();
+ __initDefaults();
+ }
+
+
+ /***
+ * Enable or disable verification that the remote host taking part
+ * of a data connection is the same as the host to which the control
+ * connection is attached. The default is for verification to be
+ * enabled. You may set this value at any time, whether the
+ * FTPClient is currently connected or not.
+ *
+ * @param enable True to enable verification, false to disable verification.
+ ***/
+ public void setRemoteVerificationEnabled(boolean enable)
+ {
+ __remoteVerificationEnabled = enable;
+ }
+
+ /***
+ * Return whether or not verification of the remote host participating
+ * in data connections is enabled. The default behavior is for
+ * verification to be enabled.
+ *
+ * @return True if verification is enabled, false if not.
+ ***/
+ public boolean isRemoteVerificationEnabled()
+ {
+ return __remoteVerificationEnabled;
+ }
+
+ /***
+ * Login to the FTP server using the provided username and password.
+ *
+ * @param username The username to login under.
+ * @param password The password to use.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean login(String username, String password) throws IOException
+ {
+ user(username);
+
+ if (FTPReply.isPositiveCompletion(_replyCode))
+ return true;
+
+ // If we get here, we either have an error code, or an intermmediate
+ // reply requesting password.
+ if (!FTPReply.isPositiveIntermediate(_replyCode))
+ return false;
+
+ return FTPReply.isPositiveCompletion(pass(password));
+ }
+
+
+ /***
+ * Login to the FTP server using the provided username, password,
+ * and account. If no account is required by the server, only
+ * the username and password, the account information is not used.
+ *
+ * @param username The username to login under.
+ * @param password The password to use.
+ * @param account The account to use.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean login(String username, String password, String account)
+ throws IOException
+ {
+ user(username);
+
+ if (FTPReply.isPositiveCompletion(_replyCode))
+ return true;
+
+ // If we get here, we either have an error code, or an intermmediate
+ // reply requesting password.
+ if (!FTPReply.isPositiveIntermediate(_replyCode))
+ return false;
+
+ pass(password);
+
+ if (FTPReply.isPositiveCompletion(_replyCode))
+ return true;
+
+ if (!FTPReply.isPositiveIntermediate(_replyCode))
+ return false;
+
+ return FTPReply.isPositiveCompletion(acct(account));
+ }
+
+ /***
+ * Logout of the FTP server by sending the QUIT command.
+ *
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean logout() throws IOException
+ {
+ return FTPReply.isPositiveCompletion(quit());
+ }
+
+
+ /***
+ * Change the current working directory of the FTP session.
+ *
+ * @param pathname The new current working directory.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean changeWorkingDirectory(String pathname) throws IOException
+ {
+ return FTPReply.isPositiveCompletion(cwd(pathname));
+ }
+
+
+ /***
+ * Change to the parent directory of the current working directory.
+ *
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean changeToParentDirectory() throws IOException
+ {
+ return FTPReply.isPositiveCompletion(cdup());
+ }
+
+
+ /***
+ * Issue the FTP SMNT command.
+ *
+ * @param pathname The pathname to mount.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean structureMount(String pathname) throws IOException
+ {
+ return FTPReply.isPositiveCompletion(smnt(pathname));
+ }
+
+ /***
+ * Reinitialize the FTP session. Not all FTP servers support this
+ * command, which issues the FTP REIN command.
+ *
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ boolean reinitialize() throws IOException
+ {
+ rein();
+
+ if (FTPReply.isPositiveCompletion(_replyCode) ||
+ (FTPReply.isPositivePreliminary(_replyCode) &&
+ FTPReply.isPositiveCompletion(getReply())))
+ {
+
+ __initDefaults();
+
+ return true;
+ }
+
+ return false;
+ }
+
+
+ /***
+ * Set the current data connection mode to
+ *
+ * @param host The passive mode server accepting connections for data
+ * transfers.
+ * @param port The passive mode server's data port.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean enterRemoteActiveMode(InetAddress host, int port)
+ throws IOException
+ {
+ if (FTPReply.isPositiveCompletion(port(host, port)))
+ {
+ __dataConnectionMode = ACTIVE_REMOTE_DATA_CONNECTION_MODE;
+ __passiveHost = null;
+ __passivePort = -1;
+ return true;
+ }
+ return false;
+ }
+
+ /***
+ * Set the current data connection mode to
+ *
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean enterRemotePassiveMode() throws IOException
+ {
+ if (pasv() != FTPReply.ENTERING_PASSIVE_MODE)
+ return false;
+
+ __dataConnectionMode = PASSIVE_REMOTE_DATA_CONNECTION_MODE;
+ __parsePassiveModeReply(_replyLines.get(0));
+
+ return true;
+ }
+
+ /***
+ * Returns the hostname or IP address (in the form of a string) returned
+ * by the server when entering passive mode. If not in passive mode,
+ * returns null. This method only returns a valid value AFTER a
+ * data connection has been opened after a call to
+ * {@link #enterLocalPassiveMode enterLocalPassiveMode()}.
+ * This is because FTPClient sends a PASV command to the server only
+ * just before opening a data connection, and not when you call
+ * {@link #enterLocalPassiveMode enterLocalPassiveMode()}.
+ *
+ * @return The passive host name if in passive mode, otherwise null.
+ ***/
+ public String getPassiveHost()
+ {
+ return __passiveHost;
+ }
+
+ /***
+ * If in passive mode, returns the data port of the passive host.
+ * This method only returns a valid value AFTER a
+ * data connection has been opened after a call to
+ * {@link #enterLocalPassiveMode enterLocalPassiveMode()}.
+ * This is because FTPClient sends a PASV command to the server only
+ * just before opening a data connection, and not when you call
+ * {@link #enterLocalPassiveMode enterLocalPassiveMode()}.
+ *
+ * @return The data port of the passive server. If not in passive
+ * mode, undefined.
+ ***/
+ public int getPassivePort()
+ {
+ return __passivePort;
+ }
+
+
+ /***
+ * Returns the current data connection mode (one of the
+ *
+ * @return The current data connection mode (one of the
+ *
+ * @param fileType The
+ * @param fileType The
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean setFileType(int fileType, int formatOrByteSize)
+ throws IOException
+ {
+ if (FTPReply.isPositiveCompletion(type(fileType, formatOrByteSize)))
+ {
+ __fileType = fileType;
+ __fileFormat = formatOrByteSize;
+ return true;
+ }
+ return false;
+ }
+
+
+ /***
+ * Sets the file structure. The default structure is
+ *
+ * @param structure The structure of the file (one of the FTP class
+ *
+ * @param mode The new transfer mode to use (one of the FTP class
+ *
+ * @param filename The name of the file to retrieve.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean remoteRetrieve(String filename) throws IOException
+ {
+ if (__dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE ||
+ __dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE)
+ return FTPReply.isPositivePreliminary(retr(filename));
+ return false;
+ }
+
+
+ /***
+ * Initiate a server to server file transfer. This method tells the
+ * server to which the client is connected to store a file on
+ * the other server using the given filename. The other server must
+ * have had a
+ * @param filename The name to call the file that is to be stored.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean remoteStore(String filename) throws IOException
+ {
+ if (__dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE ||
+ __dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE)
+ return FTPReply.isPositivePreliminary(stor(filename));
+ return false;
+ }
+
+
+ /***
+ * Initiate a server to server file transfer. This method tells the
+ * server to which the client is connected to store a file on
+ * the other server using a unique filename based on the given filename.
+ * The other server must have had a
+ * @param filename The name on which to base the filename of the file
+ * that is to be stored.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean remoteStoreUnique(String filename) throws IOException
+ {
+ if (__dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE ||
+ __dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE)
+ return FTPReply.isPositivePreliminary(stou(filename));
+ return false;
+ }
+
+
+ /***
+ * Initiate a server to server file transfer. This method tells the
+ * server to which the client is connected to store a file on
+ * the other server using a unique filename.
+ * The other server must have had a
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean remoteStoreUnique() throws IOException
+ {
+ if (__dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE ||
+ __dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE)
+ return FTPReply.isPositivePreliminary(stou());
+ return false;
+ }
+
+ // For server to server transfers
+ /***
+ * Initiate a server to server file transfer. This method tells the
+ * server to which the client is connected to append to a given file on
+ * the other server. The other server must have had a
+ *
+ * @param filename The name of the file to be appended to, or if the
+ * file does not exist, the name to call the file being stored.
+ *
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean remoteAppend(String filename) throws IOException
+ {
+ if (__dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE ||
+ __dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE)
+ return FTPReply.isPositivePreliminary(stor(filename));
+ return false;
+ }
+
+ /***
+ * There are a few FTPClient methods that do not complete the
+ * entire sequence of FTP commands to complete a transaction. These
+ * commands require some action by the programmer after the reception
+ * of a positive intermediate command. After the programmer's code
+ * completes its actions, it must call this method to receive
+ * the completion reply from the server and verify the success of the
+ * entire transaction.
+ *
+ * For example,
+ *
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean completePendingCommand() throws IOException
+ {
+ return FTPReply.isPositiveCompletion(getReply());
+ }
+
+
+ /***
+ * Retrieves a named file from the server and writes it to the given
+ * OutputStream. This method does NOT close the given OutputStream.
+ * If the current file type is ASCII, line separators in the file are
+ * converted to the local representation.
+ *
+ * @param remote The name of the remote file.
+ * @param local The local OutputStream to which to write the file.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception CopyStreamException If an I/O error occurs while actually
+ * transferring the file. The CopyStreamException allows you to
+ * determine the number of bytes transferred and the IOException
+ * causing the error. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean retrieveFile(String remote, OutputStream local)
+ throws IOException
+ {
+ InputStream input;
+ Socket socket;
+
+ if ((socket = _openDataConnection_(FTPCommand.RETR, remote)) == null)
+ return false;
+
+ input = new BufferedInputStream(socket.getInputStream(),
+ getBufferSize());
+ if (__fileType == ASCII_FILE_TYPE)
+ input = new FromNetASCIIInputStream(input);
+ // Treat everything else as binary for now
+ try
+ {
+ Util.copyStream(input, local, getBufferSize(),
+ CopyStreamEvent.UNKNOWN_STREAM_SIZE, null,
+ false);
+ }
+ catch (IOException e)
+ {
+ try
+ {
+ socket.close();
+ }
+ catch (IOException f)
+ {}
+ throw e;
+ }
+ socket.close();
+ return completePendingCommand();
+ }
+
+ /***
+ * Returns an InputStream from which a named file from the server
+ * can be read. If the current file type is ASCII, the returned
+ * InputStream will convert line separators in the file to
+ * the local representation. You must close the InputStream when you
+ * finish reading from it. The InputStream itself will take care of
+ * closing the parent data connection socket upon being closed. To
+ * finalize the file transfer you must call
+ * {@link #completePendingCommand completePendingCommand } and
+ * check its return value to verify success.
+ *
+ * @param remote The name of the remote file.
+ * @return An InputStream from which the remote file can be read. If
+ * the data connection cannot be opened (e.g., the file does not
+ * exist), null is returned (in which case you may check the reply
+ * code to determine the exact reason for failure).
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public InputStream retrieveFileStream(String remote) throws IOException
+ {
+ InputStream input;
+ Socket socket;
+
+ if ((socket = _openDataConnection_(FTPCommand.RETR, remote)) == null)
+ return null;
+
+ input = socket.getInputStream();
+ if (__fileType == ASCII_FILE_TYPE) {
+ // We buffer ascii transfers because the buffering has to
+ // be interposed between FromNetASCIIOutputSream and the underlying
+ // socket input stream. We don't buffer binary transfers
+ // because we don't want to impose a buffering policy on the
+ // programmer if possible. Programmers can decide on their
+ // own if they want to wrap the SocketInputStream we return
+ // for file types other than ASCII.
+ input = new BufferedInputStream(input,
+ getBufferSize());
+ input = new FromNetASCIIInputStream(input);
+ }
+ return new org.apache.commons.net.io.SocketInputStream(socket, input);
+ }
+
+
+ /***
+ * Stores a file on the server using the given name and taking input
+ * from the given InputStream. This method does NOT close the given
+ * InputStream. If the current file type is ASCII, line separators in
+ * the file are transparently converted to the NETASCII format (i.e.,
+ * you should not attempt to create a special InputStream to do this).
+ *
+ * @param remote The name to give the remote file.
+ * @param local The local InputStream from which to read the file.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception CopyStreamException If an I/O error occurs while actually
+ * transferring the file. The CopyStreamException allows you to
+ * determine the number of bytes transferred and the IOException
+ * causing the error. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean storeFile(String remote, InputStream local)
+ throws IOException
+ {
+ return __storeFile(FTPCommand.STOR, remote, local);
+ }
+
+
+ /***
+ * Returns an OutputStream through which data can be written to store
+ * a file on the server using the given name. If the current file type
+ * is ASCII, the returned OutputStream will convert line separators in
+ * the file to the NETASCII format (i.e., you should not attempt to
+ * create a special OutputStream to do this). You must close the
+ * OutputStream when you finish writing to it. The OutputStream itself
+ * will take care of closing the parent data connection socket upon being
+ * closed. To finalize the file transfer you must call
+ * {@link #completePendingCommand completePendingCommand } and
+ * check its return value to verify success.
+ *
+ * @param remote The name to give the remote file.
+ * @return An OutputStream through which the remote file can be written. If
+ * the data connection cannot be opened (e.g., the file does not
+ * exist), null is returned (in which case you may check the reply
+ * code to determine the exact reason for failure).
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public OutputStream storeFileStream(String remote) throws IOException
+ {
+ return __storeFileStream(FTPCommand.STOR, remote);
+ }
+
+ /***
+ * Appends to a file on the server with the given name, taking input
+ * from the given InputStream. This method does NOT close the given
+ * InputStream. If the current file type is ASCII, line separators in
+ * the file are transparently converted to the NETASCII format (i.e.,
+ * you should not attempt to create a special InputStream to do this).
+ *
+ * @param remote The name of the remote file.
+ * @param local The local InputStream from which to read the data to
+ * be appended to the remote file.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception CopyStreamException If an I/O error occurs while actually
+ * transferring the file. The CopyStreamException allows you to
+ * determine the number of bytes transferred and the IOException
+ * causing the error. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean appendFile(String remote, InputStream local)
+ throws IOException
+ {
+ return __storeFile(FTPCommand.APPE, remote, local);
+ }
+
+ /***
+ * Returns an OutputStream through which data can be written to append
+ * to a file on the server with the given name. If the current file type
+ * is ASCII, the returned OutputStream will convert line separators in
+ * the file to the NETASCII format (i.e., you should not attempt to
+ * create a special OutputStream to do this). You must close the
+ * OutputStream when you finish writing to it. The OutputStream itself
+ * will take care of closing the parent data connection socket upon being
+ * closed. To finalize the file transfer you must call
+ * {@link #completePendingCommand completePendingCommand } and
+ * check its return value to verify success.
+ *
+ * @param remote The name of the remote file.
+ * @return An OutputStream through which the remote file can be appended.
+ * If the data connection cannot be opened (e.g., the file does not
+ * exist), null is returned (in which case you may check the reply
+ * code to determine the exact reason for failure).
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public OutputStream appendFileStream(String remote) throws IOException
+ {
+ return __storeFileStream(FTPCommand.APPE, remote);
+ }
+
+ /***
+ * Stores a file on the server using a unique name derived from the
+ * given name and taking input
+ * from the given InputStream. This method does NOT close the given
+ * InputStream. If the current file type is ASCII, line separators in
+ * the file are transparently converted to the NETASCII format (i.e.,
+ * you should not attempt to create a special InputStream to do this).
+ *
+ * @param remote The name on which to base the unique name given to
+ * the remote file.
+ * @param local The local InputStream from which to read the file.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception CopyStreamException If an I/O error occurs while actually
+ * transferring the file. The CopyStreamException allows you to
+ * determine the number of bytes transferred and the IOException
+ * causing the error. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean storeUniqueFile(String remote, InputStream local)
+ throws IOException
+ {
+ return __storeFile(FTPCommand.STOU, remote, local);
+ }
+
+
+ /***
+ * Returns an OutputStream through which data can be written to store
+ * a file on the server using a unique name derived from the given name.
+ * If the current file type
+ * is ASCII, the returned OutputStream will convert line separators in
+ * the file to the NETASCII format (i.e., you should not attempt to
+ * create a special OutputStream to do this). You must close the
+ * OutputStream when you finish writing to it. The OutputStream itself
+ * will take care of closing the parent data connection socket upon being
+ * closed. To finalize the file transfer you must call
+ * {@link #completePendingCommand completePendingCommand } and
+ * check its return value to verify success.
+ *
+ * @param remote The name on which to base the unique name given to
+ * the remote file.
+ * @return An OutputStream through which the remote file can be written. If
+ * the data connection cannot be opened (e.g., the file does not
+ * exist), null is returned (in which case you may check the reply
+ * code to determine the exact reason for failure).
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public OutputStream storeUniqueFileStream(String remote) throws IOException
+ {
+ return __storeFileStream(FTPCommand.STOU, remote);
+ }
+
+ /**
+ * Stores a file on the server using a unique name assigned by the
+ * server and taking input from the given InputStream. This method does
+ * NOT close the given
+ * InputStream. If the current file type is ASCII, line separators in
+ * the file are transparently converted to the NETASCII format (i.e.,
+ * you should not attempt to create a special InputStream to do this).
+ *
+ * @param local The local InputStream from which to read the file.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception CopyStreamException If an I/O error occurs while actually
+ * transferring the file. The CopyStreamException allows you to
+ * determine the number of bytes transferred and the IOException
+ * causing the error. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ */
+ public boolean storeUniqueFile(InputStream local) throws IOException
+ {
+ return __storeFile(FTPCommand.STOU, null, local);
+ }
+
+ /**
+ * Returns an OutputStream through which data can be written to store
+ * a file on the server using a unique name assigned by the server.
+ * If the current file type
+ * is ASCII, the returned OutputStream will convert line separators in
+ * the file to the NETASCII format (i.e., you should not attempt to
+ * create a special OutputStream to do this). You must close the
+ * OutputStream when you finish writing to it. The OutputStream itself
+ * will take care of closing the parent data connection socket upon being
+ * closed. To finalize the file transfer you must call
+ * {@link #completePendingCommand completePendingCommand } and
+ * check its return value to verify success.
+ *
+ * @return An OutputStream through which the remote file can be written. If
+ * the data connection cannot be opened (e.g., the file does not
+ * exist), null is returned (in which case you may check the reply
+ * code to determine the exact reason for failure).
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ */
+ public OutputStream storeUniqueFileStream() throws IOException
+ {
+ return __storeFileStream(FTPCommand.STOU, null);
+ }
+
+ /***
+ * Reserve a number of bytes on the server for the next file transfer.
+ *
+ * @param bytes The number of bytes which the server should allocate.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean allocate(int bytes) throws IOException
+ {
+ return FTPReply.isPositiveCompletion(allo(bytes));
+ }
+
+
+ /**
+ * Reserve space on the server for the next file transfer.
+ *
+ * @param bytes The number of bytes which the server should allocate.
+ * @param recordSize The size of a file record.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ */
+ public boolean allocate(int bytes, int recordSize) throws IOException
+ {
+ return FTPReply.isPositiveCompletion(allo(bytes, recordSize));
+ }
+
+
+ /***
+ * Restart a
+ * @param offset The offset into the remote file at which to start the
+ * next file transfer.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ private boolean restart(long offset) throws IOException
+ {
+ __restartOffset = 0;
+ return FTPReply.isPositiveIntermediate(rest(Long.toString(offset)));
+ }
+
+ /***
+ * Sets the restart offset. The restart command is sent to the server
+ * only before sending the file transfer command. When this is done,
+ * the restart marker is reset to zero.
+ *
+ * @param offset The offset into the remote file at which to start the
+ * next file transfer. This must be a value greater than or
+ * equal to zero.
+ ***/
+ public void setRestartOffset(long offset)
+ {
+ if (offset >= 0)
+ __restartOffset = offset;
+ }
+
+ /***
+ * Fetches the restart offset.
+ *
+ * @return offset The offset into the remote file at which to start the
+ * next file transfer.
+ ***/
+ public long getRestartOffset()
+ {
+ return __restartOffset;
+ }
+
+
+
+ /***
+ * Renames a remote file.
+ *
+ * @param from The name of the remote file to rename.
+ * @param to The new name of the remote file.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean rename(String from, String to) throws IOException
+ {
+ if (!FTPReply.isPositiveIntermediate(rnfr(from)))
+ return false;
+
+ return FTPReply.isPositiveCompletion(rnto(to));
+ }
+
+
+ /***
+ * Abort a transfer in progress.
+ *
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean abort() throws IOException
+ {
+ return FTPReply.isPositiveCompletion(abor());
+ }
+
+ /***
+ * Deletes a file on the FTP server.
+ *
+ * @param pathname The pathname of the file to be deleted.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean deleteFile(String pathname) throws IOException
+ {
+ return FTPReply.isPositiveCompletion(dele(pathname));
+ }
+
+
+ /***
+ * Removes a directory on the FTP server (if empty).
+ *
+ * @param pathname The pathname of the directory to remove.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean removeDirectory(String pathname) throws IOException
+ {
+ return FTPReply.isPositiveCompletion(rmd(pathname));
+ }
+
+
+ /***
+ * Creates a new subdirectory on the FTP server in the current directory
+ * (if a relative pathname is given) or where specified (if an absolute
+ * pathname is given).
+ *
+ * @param pathname The pathname of the directory to create.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean makeDirectory(String pathname) throws IOException
+ {
+ return FTPReply.isPositiveCompletion(mkd(pathname));
+ }
+
+
+ /***
+ * Returns the pathname of the current working directory.
+ *
+ * @return The pathname of the current working directory. If it cannot
+ * be obtained, returns null.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public String printWorkingDirectory() throws IOException
+ {
+ if (pwd() != FTPReply.PATHNAME_CREATED)
+ return null;
+
+ return __parsePathname(_replyLines.get( _replyLines.size() - 1));
+ }
+
+
+ /**
+ * Send a site specific command.
+ * @param arguments The site specific command and arguments.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ */
+ public boolean sendSiteCommand(String arguments) throws IOException
+ {
+ return FTPReply.isPositiveCompletion(site(arguments));
+ }
+
+
+ /***
+ * Fetches the system type name from the server and returns the string.
+ * This value is cached for the duration of the connection after the
+ * first call to this method. In other words, only the first time
+ * that you invoke this method will it issue a SYST command to the
+ * FTP server. FTPClient will remember the value and return the
+ * cached value until a call to disconnect.
+ *
+ * @return The system type name obtained from the server. null if the
+ * information could not be obtained.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public String getSystemName() throws IOException
+ {
+ //if (syst() == FTPReply.NAME_SYSTEM_TYPE)
+ // Technically, we should expect a NAME_SYSTEM_TYPE response, but
+ // in practice FTP servers deviate, so we soften the condition to
+ // a positive completion.
+ if (__systemName == null && FTPReply.isPositiveCompletion(syst()))
+ __systemName = _replyLines.get(_replyLines.size() - 1).substring(4);
+
+ return __systemName;
+ }
+
+
+ /***
+ * Fetches the system help information from the server and returns the
+ * full string.
+ *
+ * @return The system help string obtained from the server. null if the
+ * information could not be obtained.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public String listHelp() throws IOException
+ {
+ if (FTPReply.isPositiveCompletion(help()))
+ return getReplyString();
+ return null;
+ }
+
+
+ /**
+ * Fetches the help information for a given command from the server and
+ * returns the full string.
+ * @param command The command on which to ask for help.
+ * @return The command help string obtained from the server. null if the
+ * information could not be obtained.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ */
+ public String listHelp(String command) throws IOException
+ {
+ if (FTPReply.isPositiveCompletion(help(command)))
+ return getReplyString();
+ return null;
+ }
+
+
+ /***
+ * Sends a NOOP command to the FTP server. This is useful for preventing
+ * server timeouts.
+ *
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean sendNoOp() throws IOException
+ {
+ return FTPReply.isPositiveCompletion(noop());
+ }
+
+
+ /***
+ * Obtain a list of filenames in a directory (or just the name of a given
+ * file, which is not particularly useful). This information is obtained
+ * through the NLST command. If the given pathname is a directory and
+ * contains no files, a zero length array is returned only
+ * if the FTP server returned a positive completion code, otherwise
+ * null is returned (the FTP server returned a 550 error No files found.).
+ * If the directory is not empty, an array of filenames in the directory is
+ * returned. If the pathname corresponds
+ * to a file, only that file will be listed. The server may or may not
+ * expand glob expressions.
+ *
+ * @param pathname The file or directory to list.
+ * @return The list of filenames contained in the given path. null if
+ * the list could not be obtained. If there are no filenames in
+ * the directory, a zero-length array is returned.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public String[] listNames(String pathname) throws IOException
+ {
+ String line;
+ Socket socket;
+ BufferedReader reader;
+ ArrayList
+ * @return The list of filenames contained in the current working
+ * directory. null if the list could not be obtained.
+ * If there are no filenames in the directory, a zero-length array
+ * is returned.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public String[] listNames() throws IOException
+ {
+ return listNames(null);
+ }
+
+
+
+ /**
+ * Using the default system autodetect mechanism, obtain a
+ * list of file information for the current working directory
+ * or for just a single file.
+ *
+ * This information is obtained through the LIST command. The contents of
+ * the returned array is determined by the
+ * @param pathname The file or directory to list. Since the server may
+ * or may not expand glob expressions, using them here
+ * is not recommended and may well cause this method to
+ * fail.
+ *
+ * @return The list of file information contained in the given path in
+ * the format determined by the autodetection mechanism
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection
+ * as a result of the client being idle or some other
+ * reason causing the server to send FTP reply code 421.
+ * This exception may be caught either as an IOException
+ * or independently as itself.
+ * @exception IOException
+ * If an I/O error occurs while either sending a
+ * command to the server or receiving a reply
+ * from the server.
+ * @exception ParserInitializationException
+ * Thrown if the parserKey parameter cannot be
+ * resolved by the selected parser factory.
+ * In the DefaultFTPEntryParserFactory, this will
+ * happen when parserKey is neither
+ * the fully qualified class name of a class
+ * implementing the interface
+ * org.apache.commons.net.ftp.FTPFileEntryParser
+ * nor a string containing one of the recognized keys
+ * mapping to such a parser or if class loader
+ * security issues prevent its being loaded.
+ * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
+ * @see org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory
+ * @see org.apache.commons.net.ftp.FTPFileEntryParser
+ */
+ public FTPFile[] listFiles(String pathname)
+ throws IOException
+ {
+ String key = null;
+ FTPListParseEngine engine =
+ initiateListParsing(key, pathname);
+ return engine.getFiles();
+
+ }
+ /**
+ * Using the default system autodetect mechanism, obtain a
+ * list of file information for the current working directory.
+ *
+ * This information is obtained through the LIST command. The contents of
+ * the returned array is determined by the
+ * @return The list of file information contained in the current directory
+ * in the format determined by the autodetection mechanism.
+ *
+ * NOTE: This array may contain null members if any of the
+ * individual file listings failed to parse. The caller should
+ * check each entry for null before referencing it.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection
+ * as a result of the client being idle or some other
+ * reason causing the server to send FTP reply code 421.
+ * This exception may be caught either as an IOException
+ * or independently as itself.
+ * @exception IOException
+ * If an I/O error occurs while either sending a
+ * command to the server or receiving a reply
+ * from the server.
+ * @exception ParserInitializationException
+ * Thrown if the parserKey parameter cannot be
+ * resolved by the selected parser factory.
+ * In the DefaultFTPEntryParserFactory, this will
+ * happen when parserKey is neither
+ * the fully qualified class name of a class
+ * implementing the interface
+ * org.apache.commons.net.ftp.FTPFileEntryParser
+ * nor a string containing one of the recognized keys
+ * mapping to such a parser or if class loader
+ * security issues prevent its being loaded.
+ * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
+ * @see org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory
+ * @see org.apache.commons.net.ftp.FTPFileEntryParser
+ */
+ public FTPFile[] listFiles()
+ throws IOException
+ {
+ return listFiles((String) null);
+ }
+
+ /**
+ * Using the default autodetect mechanism, initialize an FTPListParseEngine
+ * object containing a raw file information for the current working
+ * directory on the server
+ * This information is obtained through the LIST command. This object
+ * is then capable of being iterated to return a sequence of FTPFile
+ * objects with information filled in by the
+ *
+ * This method differs from using the listFiles() methods in that
+ * expensive FTPFile objects are not created until needed which may be
+ * an advantage on large lists.
+ *
+ * @return A FTPListParseEngine object that holds the raw information and
+ * is capable of providing parsed FTPFile objects, one for each file
+ * containing information contained in the given path in the format
+ * determined by the
+ * The server may or may not expand glob expressions. You should avoid
+ * using glob expressions because the return format for glob listings
+ * differs from server to server and will likely cause this method to fail.
+ *
+ * This method differs from using the listFiles() methods in that
+ * expensive FTPFile objects are not created until needed which may be
+ * an advantage on large lists.
+ *
+ *
+ * The server may or may not expand glob expressions. You should avoid
+ * using glob expressions because the return format for glob listings
+ * differs from server to server and will likely cause this method to fail.
+ *
+ * This method differs from using the listFiles() methods in that
+ * expensive FTPFile objects are not created until needed which may be
+ * an advantage on large lists.
+ *
+ * @param parserKey A string representing a designated code or fully-qualified
+ * class name of an
+ * @return The status information returned by the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public String getStatus() throws IOException
+ {
+ if (FTPReply.isPositiveCompletion(stat()))
+ return getReplyString();
+ return null;
+ }
+
+
+ /***
+ * Issue the FTP STAT command to the server for a given pathname. This
+ * should produce a listing of the file or directory.
+ *
+ * @return The status information returned by the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public String getStatus(String pathname) throws IOException
+ {
+ if (FTPReply.isPositiveCompletion(stat(pathname)))
+ return getReplyString();
+ return null;
+ }
+
+
+ /**
+ * Issue the FTP MDTM command (not supported by all servers to retrieve the last
+ * modification time of a file. The modification string should be in the
+ * ISO 3077 form "YYYYMMDDhhmmss(.xxx)?". The timestamp represented should also be in
+ * GMT, but not all FTP servers honour this.
+ *
+ * @param pathname The file path to query.
+ * @return A string representing the last file modification time in
+ * This class implements an alternate means of configuring the
+ * {@link org.apache.commons.net.ftp.FTPClient FTPClient} object and
+ * also subordinate objects which it uses. Any class implementing the
+ * {@link org.apache.commons.net.ftp.Configurable Configurable }
+ * interface can be configured by this object.
+ *
+ * In particular this class was designed primarily to support configuration
+ * of FTP servers which express file timestamps in formats and languages
+ * other than those for the US locale, which although it is the most common
+ * is not universal. Unfortunately, nothing in the FTP spec allows this to
+ * be determined in an automated way, so manual configuration such as this
+ * is necessary.
+ *
+ * This functionality was designed to allow existing clients to work exactly
+ * as before without requiring use of this component. This component should
+ * only need to be explicitly invoked by the user of this package for problem
+ * cases that previous implementations could not solve.
+ *
+ * Unpaged (whole list) access on a UNIX server that uses French month names
+ * but uses the "standard"
+ * Paged access on a UNIX server that uses Danish month names
+ * and "European" date formatting in Denmark's time zone, when you
+ * are in some other time zone.
+ *
+ * Unpaged (whole list) access on a VMS server that uses month names
+ * in a language not {@link #getSupportedLanguageCodes() supported} by the system.
+ * but uses the "standard" config
are somehow inadequate to configure the
+ * Configurable object.
+ */
+ public void configure(FTPClientConfig config);
+}
diff --git a/src/org/apache/commons/net/ftp/FTP.java b/src/org/apache/commons/net/ftp/FTP.java
new file mode 100644
index 0000000..6e2eff8
--- /dev/null
+++ b/src/org/apache/commons/net/ftp/FTP.java
@@ -0,0 +1,1513 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.net.ftp;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.SocketException;
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import org.apache.commons.net.MalformedServerReplyException;
+import org.apache.commons.net.ProtocolCommandListener;
+import org.apache.commons.net.ProtocolCommandSupport;
+import org.apache.commons.net.SocketClient;
+
+/***
+ * FTP provides the basic the functionality necessary to implement your
+ * own FTP client. It extends org.apache.commons.net.SocketClient since
+ * extending TelnetClient was causing unwanted behavior (like connections
+ * that did not time out properly).
+ * FTPConectionClosedException
+ * is a subclass of IOException
and therefore need not be
+ * caught separately, but if you are going to catch it separately, its
+ * catch block must appear before the more general IOException
+ * catch block. When you encounter an
+ * {@link org.apache.commons.net.ftp.FTPConnectionClosedException}
+ * , you must disconnect the connection with
+ * {@link #disconnect disconnect() } to properly clean up the
+ * system resources used by FTP. Before disconnecting, you may check the
+ * last reply code and text with
+ * {@link #getReplyCode getReplyCode },
+ * {@link #getReplyString getReplyString },
+ * and {@link #getReplyStrings getReplyStrings}.
+ * You may avoid server disconnections while the client is idle by
+ * periodicaly sending NOOP commands to the server.
+ * FILE_TYPE
are used to indicate file types.
+ ***/
+ public static final int ASCII_FILE_TYPE = 0;
+
+ /***
+ * A constant used to indicate the file(s) being transfered should
+ * be treated as EBCDIC. Note however that there are several different
+ * EBCDIC formats. All constants ending in FILE_TYPE
+ * are used to indicate file types.
+ ***/
+ public static final int EBCDIC_FILE_TYPE = 1;
+
+
+ /***
+ * A constant used to indicate the file(s) being transfered should
+ * be treated as a binary image, i.e., no translations should be
+ * performed. All constants ending in FILE_TYPE
are used to
+ * indicate file types.
+ ***/
+ public static final int BINARY_FILE_TYPE = 2;
+
+ /***
+ * A constant used to indicate the file(s) being transfered should
+ * be treated as a local type. All constants ending in
+ * FILE_TYPE
are used to indicate file types.
+ ***/
+ public static final int LOCAL_FILE_TYPE = 3;
+
+ /***
+ * A constant used for text files to indicate a non-print text format.
+ * This is the default format.
+ * All constants ending in TEXT_FORMAT
are used to indicate
+ * text formatting for text transfers (both ASCII and EBCDIC).
+ ***/
+ public static final int NON_PRINT_TEXT_FORMAT = 4;
+
+ /***
+ * A constant used to indicate a text file contains format vertical format
+ * control characters.
+ * All constants ending in TEXT_FORMAT
are used to indicate
+ * text formatting for text transfers (both ASCII and EBCDIC).
+ ***/
+ public static final int TELNET_TEXT_FORMAT = 5;
+
+ /***
+ * A constant used to indicate a text file contains ASA vertical format
+ * control characters.
+ * All constants ending in TEXT_FORMAT
are used to indicate
+ * text formatting for text transfers (both ASCII and EBCDIC).
+ ***/
+ public static final int CARRIAGE_CONTROL_TEXT_FORMAT = 6;
+
+ /***
+ * A constant used to indicate a file is to be treated as a continuous
+ * sequence of bytes. This is the default structure. All constants ending
+ * in _STRUCTURE
are used to indicate file structure for
+ * file transfers.
+ ***/
+ public static final int FILE_STRUCTURE = 7;
+
+ /***
+ * A constant used to indicate a file is to be treated as a sequence
+ * of records. All constants ending in _STRUCTURE
+ * are used to indicate file structure for file transfers.
+ ***/
+ public static final int RECORD_STRUCTURE = 8;
+
+ /***
+ * A constant used to indicate a file is to be treated as a set of
+ * independent indexed pages. All constants ending in
+ * _STRUCTURE
are used to indicate file structure for file
+ * transfers.
+ ***/
+ public static final int PAGE_STRUCTURE = 9;
+
+ /***
+ * A constant used to indicate a file is to be transfered as a stream
+ * of bytes. This is the default transfer mode. All constants ending
+ * in TRANSFER_MODE
are used to indicate file transfer
+ * modes.
+ ***/
+ public static final int STREAM_TRANSFER_MODE = 10;
+
+ /***
+ * A constant used to indicate a file is to be transfered as a series
+ * of blocks. All constants ending in TRANSFER_MODE
are used
+ * to indicate file transfer modes.
+ ***/
+ public static final int BLOCK_TRANSFER_MODE = 11;
+
+ /***
+ * A constant used to indicate a file is to be transfered as FTP
+ * compressed data. All constants ending in TRANSFER_MODE
+ * are used to indicate file transfer modes.
+ ***/
+ public static final int COMPRESSED_TRANSFER_MODE = 12;
+
+ // We have to ensure that the protocol communication is in ASCII
+ // but we use ISO-8859-1 just in case 8-bit characters cross
+ // the wire.
+ /**
+ * The default character encoding used for communicating over an
+ * FTP control connection. The default encoding is an
+ * ASCII-compatible encoding. Some FTP servers expect other
+ * encodings. You can change the encoding used by an FTP instance
+ * with {@link #setControlEncoding setControlEncoding}.
+ */
+ public static final String DEFAULT_CONTROL_ENCODING = "ISO-8859-1";
+ private static final String __modes = "AEILNTCFRPSBC";
+
+ private StringBuilder __commandBuffer = new StringBuilder();
+
+ protected int _replyCode;
+ protected ArrayListDEFAULT_PORT
and initializes internal data structures
+ * for saving FTP reply information.
+ ***/
+ public FTP()
+ {
+ super();
+ setDefaultPort(DEFAULT_PORT);
+ _replyLines = new ArrayList connect
is of type void.
+ * FILE_TYPE
+ * constants).
+ * @param formatOrByteSize The format of the file (one of the
+ * _FORMAT
constants. In the case of
+ * LOCAL_FILE_TYPE
, the byte size.
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ */
+ public int type(int fileType, int formatOrByteSize) throws IOException
+ {
+ StringBuffer arg = new StringBuffer();
+
+ arg.append(__modes.charAt(fileType));
+ arg.append(' ');
+ if (fileType == LOCAL_FILE_TYPE)
+ arg.append(formatOrByteSize);
+ else
+ arg.append(__modes.charAt(formatOrByteSize));
+
+ return sendCommand(FTPCommand.TYPE, arg.toString());
+ }
+
+
+ /**
+ * A convenience method to send the FTP TYPE command to the server,
+ * receive the reply, and return the reply code.
+ * FILE_TYPE
+ * constants).
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ */
+ public int type(int fileType) throws IOException
+ {
+ return sendCommand(FTPCommand.TYPE,
+ __modes.substring(fileType, fileType + 1));
+ }
+
+ /***
+ * A convenience method to send the FTP STRU command to the server,
+ * receive the reply, and return the reply code.
+ * _STRUCTURE
constants).
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int stru(int structure) throws IOException
+ {
+ return sendCommand(FTPCommand.STRU,
+ __modes.substring(structure, structure + 1));
+ }
+
+ /***
+ * A convenience method to send the FTP MODE command to the server,
+ * receive the reply, and return the reply code.
+ * TRANSFER_MODE
constants).
+ * @return The reply code received from the server.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending the
+ * command or receiving the server reply.
+ ***/
+ public int mode(int mode) throws IOException
+ {
+ return sendCommand(FTPCommand.MODE,
+ __modes.substring(mode, mode + 1));
+ }
+
+ /***
+ * A convenience method to send the FTP RETR command to the server,
+ * receive the reply, and return the reply code. Remember, it is up
+ * to you to manage the data connection. If you don't need this low
+ * level of access, use {@link org.apache.commons.net.ftp.FTPClient}
+ * , which will handle all low level details for you.
+ *
+ * boolean error = false;
+ * try {
+ * int reply;
+ * ftp.connect("ftp.foobar.com");
+ * System.out.println("Connected to " + server + ".");
+ * System.out.print(ftp.getReplyString());
+ *
+ * // After connection attempt, you should check the reply code to verify
+ * // success.
+ * reply = ftp.getReplyCode();
+ *
+ * if(!FTPReply.isPositiveCompletion(reply)) {
+ * ftp.disconnect();
+ * System.err.println("FTP server refused connection.");
+ * System.exit(1);
+ * }
+ * ... // transfer files
+ * ftp.logout();
+ * } catch(IOException e) {
+ * error = true;
+ * e.printStackTrace();
+ * } finally {
+ * if(ftp.isConnected()) {
+ * try {
+ * ftp.disconnect();
+ * } catch(IOException ioe) {
+ * // do nothing
+ * }
+ * }
+ * System.exit(error ? 1 : 0);
+ * }
+ *
+ * FTP.ASCII_FILE_TYPE
,
+ * FTP.NON_PRINT_TEXT_FORMAT
,
+ * FTP.STREAM_TRANSFER_MODE
, and
+ * FTP.FILE_STRUCTURE
. The only file types directly supported
+ * are FTP.ASCII_FILE_TYPE
and
+ * FTP.BINARY_FILE_TYPE
. Because there are at least 4
+ * different EBCDIC encodings, we have opted not to provide direct support
+ * for EBCDIC. To transfer EBCDIC and other unsupported file types you
+ * must create your own filter InputStreams and OutputStreams and wrap
+ * them around the streams returned or required by the FTPClient methods.
+ * FTPClient uses the {@link ToNetASCIIOutputStream NetASCII}
+ * filter streams to provide transparent handling of ASCII files. We will
+ * consider incorporating EBCDIC support if there is enough demand.
+ * FTP.NON_PRINT_TEXT_FORMAT
,
+ * FTP.STREAM_TRANSFER_MODE
, and
+ * FTP.FILE_STRUCTURE
are the only supported formats,
+ * transfer modes, and file structures.
+ * FTPConnectionClosedException
+ * is a subclass of IOException
and therefore need not be
+ * caught separately, but if you are going to catch it separately, its
+ * catch block must appear before the more general IOException
+ * catch block. When you encounter an
+ * {@link org.apache.commons.net.ftp.FTPConnectionClosedException}
+ * , you must disconnect the connection with
+ * {@link #disconnect disconnect() } to properly clean up the
+ * system resources used by FTPClient. Before disconnecting, you may check the
+ * last reply code and text with
+ * {@link org.apache.commons.net.ftp.FTP#getReplyCode getReplyCode },
+ * {@link org.apache.commons.net.ftp.FTP#getReplyString getReplyString },
+ * and
+ * {@link org.apache.commons.net.ftp.FTP#getReplyStrings getReplyStrings}.
+ * You may avoid server disconnections while the client is idle by
+ * periodicaly sending NOOP commands to the server.
+ *
+ * FTPClient f=FTPClient();
+ * f.connect(server);
+ * f.login(username, password);
+ * FTPFile[] files = listFiles(directory);
+ *
+ *
+ * FTPClient f=FTPClient();
+ * f.connect(server);
+ * f.login(username, password);
+ * FTPListParseEngine engine =
+ * f.initiateListParsing("com.whatever.YourOwnParser", directory);
+ *
+ * while (engine.hasNext()) {
+ * FTPFile[] files = engine.getNext(25); // "page size" you want
+ * //do whatever you want with these files, display them, etc.
+ * //expensive FTPFile objects not created until needed.
+ * }
+ *
+ *
+ * FTPClient f=FTPClient();
+ * f.connect(server);
+ * f.login(username, password);
+ * FTPListParseEngine engine = f.initiateListParsing(directory);
+ *
+ * while (engine.hasNext()) {
+ * FTPFile[] files = engine.getNext(25); // "page size" you want
+ * //do whatever you want with these files, display them, etc.
+ * //expensive FTPFile objects not created until needed.
+ * }
+ *
+ *
+ *
see {@link FTPClientConfig FTPClientConfig}.
+ * MM d yyyy
ACTIVE_LOCAL_DATA_CONNECTION_MODE
, the file type
+ * set to FTP.ASCII_FILE_TYPE
, the
+ * file format set to FTP.NON_PRINT_TEXT_FORMAT
,
+ * the file structure set to FTP.FILE_STRUCTURE
, and
+ * the transfer mode set to FTP.STREAM_TRANSFER_MODE
.
+ ***/
+ public FTPClient()
+ {
+ __initDefaults();
+ __dataTimeout = -1;
+ __remoteVerificationEnabled = true;
+ __parserFactory = new DefaultFTPFileEntryParserFactory();
+ __configuration = null;
+ __listHiddenFiles = false;
+ }
+
+
+ private void __initDefaults()
+ {
+ __dataConnectionMode = ACTIVE_LOCAL_DATA_CONNECTION_MODE;
+ __passiveHost = null;
+ __passivePort = -1;
+ __fileType = FTP.ASCII_FILE_TYPE;
+ __fileStructure = FTP.FILE_STRUCTURE;
+ __fileFormat = FTP.NON_PRINT_TEXT_FORMAT;
+ __fileTransferMode = FTP.STREAM_TRANSFER_MODE;
+ __restartOffset = 0;
+ __systemName = null;
+ __entryParser = null;
+ __bufferSize = Util.DEFAULT_COPY_BUFFER_SIZE;
+ }
+
+ private String __parsePathname(String reply)
+ {
+ int begin, end;
+
+ begin = reply.indexOf('"') + 1;
+ end = reply.indexOf('"', begin);
+
+ return reply.substring(begin, end);
+ }
+
+
+ private void __parsePassiveModeReply(String reply)
+ throws MalformedServerReplyException
+ {
+ java.util.regex.Matcher m = __parms_pat.matcher(reply);
+ if (!m.find()) {
+ throw new MalformedServerReplyException(
+ "Could not parse passive host information.\nServer Reply: " + reply);
+ }
+ reply = m.group();
+ String parts[] = m.group().split(",");
+
+ __passiveHost = parts[0] + '.' + parts[1] + '.' + parts[2] + '.' + parts[3];
+
+ try
+ {
+ int oct1 = Integer.parseInt(parts[4]);
+ int oct2 = Integer.parseInt(parts[5]);
+ __passivePort = (oct1 << 8) | oct2;
+ }
+ catch (NumberFormatException e)
+ {
+ throw new MalformedServerReplyException(
+ "Could not parse passive host information.\nServer Reply: " + reply);
+ }
+
+ }
+
+ private boolean __storeFile(int command, String remote, InputStream local)
+ throws IOException
+ {
+ OutputStream output;
+ Socket socket;
+
+ if ((socket = _openDataConnection_(command, remote)) == null)
+ return false;
+
+ output = new BufferedOutputStream(socket.getOutputStream(),
+ getBufferSize()
+ );
+ if (__fileType == ASCII_FILE_TYPE)
+ output = new ToNetASCIIOutputStream(output);
+ // Treat everything else as binary for now
+ try
+ {
+ Util.copyStream(local, output, getBufferSize(),
+ CopyStreamEvent.UNKNOWN_STREAM_SIZE, null,
+ false);
+ }
+ catch (IOException e)
+ {
+ try
+ {
+ socket.close();
+ }
+ catch (IOException f)
+ {}
+ throw e;
+ }
+ output.close();
+ socket.close();
+ return completePendingCommand();
+ }
+
+ private OutputStream __storeFileStream(int command, String remote)
+ throws IOException
+ {
+ OutputStream output;
+ Socket socket;
+
+ if ((socket = _openDataConnection_(command, remote)) == null)
+ return null;
+
+ output = socket.getOutputStream();
+ if (__fileType == ASCII_FILE_TYPE) {
+ // We buffer ascii transfers because the buffering has to
+ // be interposed between ToNetASCIIOutputSream and the underlying
+ // socket output stream. We don't buffer binary transfers
+ // because we don't want to impose a buffering policy on the
+ // programmer if possible. Programmers can decide on their
+ // own if they want to wrap the SocketOutputStream we return
+ // for file types other than ASCII.
+ output = new BufferedOutputStream(output,
+ getBufferSize());
+ output = new ToNetASCIIOutputStream(output);
+
+ }
+ return new org.apache.commons.net.io.SocketOutputStream(socket, output);
+ }
+
+
+ /**
+ * Establishes a data connection with the FTP server, returning
+ * a Socket for the connection if successful. If a restart
+ * offset has been set with {@link #setRestartOffset(long)},
+ * a REST command is issued to the server with the offset as
+ * an argument before establishing the data connection. Active
+ * mode connections also cause a local PORT command to be issued.
+ * ACTIVE_LOCAL_DATA_CONNECTION_MODE
. No communication
+ * with the FTP server is conducted, but this causes all future data
+ * transfers to require the FTP server to connect to the client's
+ * data port. Additionally, to accommodate differences between socket
+ * implementations on different platforms, this method causes the
+ * client to issue a PORT command before every data transfer.
+ ***/
+ public void enterLocalActiveMode()
+ {
+ __dataConnectionMode = ACTIVE_LOCAL_DATA_CONNECTION_MODE;
+ __passiveHost = null;
+ __passivePort = -1;
+ }
+
+
+ /***
+ * Set the current data connection mode to
+ * PASSIVE_LOCAL_DATA_CONNECTION_MODE
. Use this
+ * method only for data transfers between the client and server.
+ * This method causes a PASV command to be issued to the server
+ * before the opening of every data connection, telling the server to
+ * open a data port to which the client will connect to conduct
+ * data transfers. The FTPClient will stay in
+ * PASSIVE_LOCAL_DATA_CONNECTION_MODE
until the
+ * mode is changed by calling some other method such as
+ * {@link #enterLocalActiveMode enterLocalActiveMode() }
+ ***/
+ public void enterLocalPassiveMode()
+ {
+ __dataConnectionMode = PASSIVE_LOCAL_DATA_CONNECTION_MODE;
+ // These will be set when just before a data connection is opened
+ // in _openDataConnection_()
+ __passiveHost = null;
+ __passivePort = -1;
+ }
+
+
+ /***
+ * Set the current data connection mode to
+ * ACTIVE_REMOTE_DATA_CONNECTION
. Use this method only
+ * for server to server data transfers. This method issues a PORT
+ * command to the server, indicating the other server and port to which
+ * it should connect for data transfers. You must call this method
+ * before EVERY server to server transfer attempt. The FTPClient will
+ * NOT automatically continue to issue PORT commands. You also
+ * must remember to call
+ * {@link #enterLocalActiveMode enterLocalActiveMode() } if you
+ * wish to return to the normal data connection mode.
+ * PASSIVE_REMOTE_DATA_CONNECTION_MODE
. Use this
+ * method only for server to server data transfers.
+ * This method issues a PASV command to the server, telling it to
+ * open a data port to which the active server will connect to conduct
+ * data transfers. You must call this method
+ * before EVERY server to server transfer attempt. The FTPClient will
+ * NOT automatically continue to issue PASV commands. You also
+ * must remember to call
+ * {@link #enterLocalActiveMode enterLocalActiveMode() } if you
+ * wish to return to the normal data connection mode.
+ * _DATA_CONNECTION_MODE
constants.
+ * _DATA_CONNECTION_MODE
constants.
+ ***/
+ public int getDataConnectionMode()
+ {
+ return __dataConnectionMode;
+ }
+
+
+ /***
+ * Sets the file type to be transferred. This should be one of
+ * FTP.ASCII_FILE_TYPE
, FTP.BINARY_FILE_TYPE
,
+ * etc. The file type only needs to be set when you want to change the
+ * type. After changing it, the new type stays in effect until you change
+ * it again. The default file type is FTP.ASCII_FILE_TYPE
+ * if this method is never called.
+ * _FILE_TYPE
constant indcating the
+ * type of file.
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean setFileType(int fileType) throws IOException
+ {
+ if (FTPReply.isPositiveCompletion(type(fileType)))
+ {
+ __fileType = fileType;
+ __fileFormat = FTP.NON_PRINT_TEXT_FORMAT;
+ return true;
+ }
+ return false;
+ }
+
+
+ /***
+ * Sets the file type to be transferred and the format. The type should be
+ * one of FTP.ASCII_FILE_TYPE
,
+ * FTP.BINARY_FILE_TYPE
, etc. The file type only needs to
+ * be set when you want to change the type. After changing it, the new
+ * type stays in effect until you change it again. The default file type
+ * is FTP.ASCII_FILE_TYPE
if this method is never called.
+ * The format should be one of the FTP class TEXT_FORMAT
+ * constants, or if the type is FTP.LOCAL_FILE_TYPE
, the
+ * format should be the byte size for that type. The default format
+ * is FTP.NON_PRINT_TEXT_FORMAT
if this method is never
+ * called.
+ * _FILE_TYPE
constant indcating the
+ * type of file.
+ * @param formatOrByteSize The format of the file (one of the
+ * _FORMAT
constants. In the case of
+ * LOCAL_FILE_TYPE
, the byte size.
+ * FTP.FILE_STRUCTURE
if this method is never called.
+ * _STRUCTURE
constants).
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean setFileStructure(int structure) throws IOException
+ {
+ if (FTPReply.isPositiveCompletion(stru(structure)))
+ {
+ __fileStructure = structure;
+ return true;
+ }
+ return false;
+ }
+
+
+ /***
+ * Sets the transfer mode. The default transfer mode
+ * FTP.STREAM_TRANSFER_MODE
if this method is never called.
+ * _TRANSFER_MODE
constants).
+ * @return True if successfully completed, false if not.
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ ***/
+ public boolean setFileTransferMode(int mode) throws IOException
+ {
+ if (FTPReply.isPositiveCompletion(mode(mode)))
+ {
+ __fileTransferMode = mode;
+ return true;
+ }
+ return false;
+ }
+
+
+ /***
+ * Initiate a server to server file transfer. This method tells the
+ * server to which the client is connected to retrieve a given file from
+ * the other server.
+ * remoteRetrieve
issued to it by another
+ * FTPClient.
+ * remoteRetrieve
issued
+ * to it by another FTPClient.
+ * remoteRetrieve
issued
+ * to it by another FTPClient. Many FTP servers require that a base
+ * filename be given from which the unique filename can be derived. For
+ * those servers use the other version of remoteStoreUnique
+ * remoteRetrieve
issued to it by another FTPClient.
+ *
+ * InputStream input;
+ * OutputStream output;
+ * input = new FileInputStream("foobaz.txt");
+ * output = ftp.storeFileStream("foobar.txt")
+ * if(!FTPReply.isPositiveIntermediate(ftp.getReplyCode())) {
+ * input.close();
+ * output.close();
+ * ftp.logout();
+ * ftp.disconnect();
+ * System.err.println("File transfer failed.");
+ * System.exit(1);
+ * }
+ * Util.copyStream(input, output);
+ * input.close();
+ * output.close();
+ * // Must call completePendingCommand() to finish command.
+ * if(!ftp.completePendingCommand()) {
+ * ftp.logout();
+ * ftp.disconnect();
+ * System.err.println("File transfer failed.");
+ * System.exit(1);
+ * }
+ *
+ * STREAM_TRANSFER_MODE
file transfer starting
+ * from the given offset. This will only work on FTP servers supporting
+ * the REST comand for the stream transfer mode. However, most FTP
+ * servers support this. Any subsequent file transfer will start
+ * reading or writing the remote file from the indicated offset.
+ * FTPFileEntryParser
+ * used.
+ * FTPFileEntryParser
+ * used.
+ * FTPFileEntryParser
used.
+ * parser
parameter. Null will be
+ * returned if a data connection cannot be opened. If the current working
+ * directory contains no files, an empty array will be the return.
+ *
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException
+ * If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ * @exception ParserInitializationException
+ * Thrown if the autodetect mechanism cannot
+ * resolve the type of system we are connected with.
+ * @see FTPListParseEngine
+ */
+ public FTPListParseEngine initiateListParsing()
+ throws IOException
+ {
+ return initiateListParsing((String) null);
+ }
+
+ /**
+ * Using the default autodetect mechanism, initialize an FTPListParseEngine
+ * object containing a raw file information for the supplied directory.
+ * This information is obtained through the LIST command. This object
+ * is then capable of being iterated to return a sequence of FTPFile
+ * objects with information filled in by the
+ * FTPFileEntryParser
used.
+ *
+ * FTPClient f=FTPClient();
+ * f.connect(server);
+ * f.login(username, password);
+ * FTPListParseEngine engine = f.initiateListParsing(directory);
+ *
+ * while (engine.hasNext()) {
+ * FTPFile[] files = engine.getNext(25); // "page size" you want
+ * //do whatever you want with these files, display them, etc.
+ * //expensive FTPFile objects not created until needed.
+ * }
+ *
+ *
+ * @return A FTPListParseEngine object that holds the raw information and
+ * is capable of providing parsed FTPFile objects, one for each file
+ * containing information contained in the given path in the format
+ * determined by the parser
parameter. Null will be
+ * returned if a data connection cannot be opened. If the current working
+ * directory contains no files, an empty array will be the return.
+ *
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException
+ * If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ * @exception ParserInitializationException
+ * Thrown if the autodetect mechanism cannot
+ * resolve the type of system we are connected with.
+ * @see FTPListParseEngine
+ */
+ public FTPListParseEngine initiateListParsing(
+ String pathname)
+ throws IOException
+ {
+ String key = null;
+ return initiateListParsing(key, pathname);
+ }
+
+ /**
+ * Using the supplied parser key, initialize an FTPListParseEngine
+ * object containing a raw file information for the supplied directory.
+ * This information is obtained through the LIST command. This object
+ * is then capable of being iterated to return a sequence of FTPFile
+ * objects with information filled in by the
+ * FTPFileEntryParser
used.
+ * FTPFileEntryParser
that should be
+ * used to parse each server file listing.
+ *
+ * @return A FTPListParseEngine object that holds the raw information and
+ * is capable of providing parsed FTPFile objects, one for each file
+ * containing information contained in the given path in the format
+ * determined by the parser
parameter. Null will be
+ * returned if a data connection cannot be opened. If the current working
+ * directory contains no files, an empty array will be the return.
+ *
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException
+ * If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ * @exception ParserInitializationException
+ * Thrown if the parserKey parameter cannot be
+ * resolved by the selected parser factory.
+ * In the DefaultFTPEntryParserFactory, this will
+ * happen when parserKey is neither
+ * the fully qualified class name of a class
+ * implementing the interface
+ * org.apache.commons.net.ftp.FTPFileEntryParser
+ * nor a string containing one of the recognized keys
+ * mapping to such a parser or if class loader
+ * security issues prevent its being loaded.
+ * @see FTPListParseEngine
+ */
+ public FTPListParseEngine initiateListParsing(
+ String parserKey, String pathname)
+ throws IOException
+ {
+ // We cache the value to avoid creation of a new object every
+ // time a file listing is generated.
+ if(__entryParser == null) {
+ if (null != parserKey) {
+ // if a parser key was supplied in the parameters,
+ // use that to create the paraser
+ __entryParser =
+ __parserFactory.createFileEntryParser(parserKey);
+
+ } else {
+ // if no parserKey was supplied, check for a configuration
+ // in the params, and if non-null, use that.
+ if (null != __configuration) {
+ __entryParser =
+ __parserFactory.createFileEntryParser(__configuration);
+ } else {
+ // if a parserKey hasn't been supplied, and a configuration
+ // hasn't been supplied, then autodetect by calling
+ // the SYST command and use that to choose the parser.
+ __entryParser =
+ __parserFactory.createFileEntryParser(getSystemName());
+ }
+ }
+ }
+
+ return initiateListParsing(__entryParser, pathname);
+
+ }
+
+
+ /**
+ * private method through which all listFiles() and
+ * initiateListParsing methods pass once a parser is determined.
+ *
+ * @exception FTPConnectionClosedException
+ * If the FTP server prematurely closes the connection as a result
+ * of the client being idle or some other reason causing the server
+ * to send FTP reply code 421. This exception may be caught either
+ * as an IOException or independently as itself.
+ * @exception IOException
+ * If an I/O error occurs while either sending a
+ * command to the server or receiving a reply from the server.
+ * @see FTPListParseEngine
+ */
+ private FTPListParseEngine initiateListParsing(
+ FTPFileEntryParser parser, String pathname)
+ throws IOException
+ {
+ Socket socket;
+
+ FTPListParseEngine engine = new FTPListParseEngine(parser);
+
+ if ((socket = _openDataConnection_(FTPCommand.LIST, getListArguments(pathname))) == null)
+ {
+ return engine;
+ }
+
+
+ try {
+ engine.readServerList(socket.getInputStream(), getControlEncoding());
+ }
+ finally {
+ socket.close();
+ }
+
+ completePendingCommand();
+ return engine;
+ }
+
+ /**
+ * @since 2.0
+ */
+ protected String getListArguments(String pathname) {
+ if (getListHiddenFiles())
+ {
+ StringBuffer sb = new StringBuffer(pathname.length() + 3);
+ sb.append("-a ");
+ sb.append(pathname);
+ return sb.toString();
+ }
+
+ return pathname;
+ }
+
+
+ /***
+ * Issue the FTP STAT command to the server.
+ * YYYYMMDDhhmmss
format.
+ * @throws IOException if an I/O error occurs.
+ * @since 2.0
+ */
+ public String getModificationTime(String pathname) throws IOException {
+ if (FTPReply.isPositiveCompletion(mdtm(pathname)))
+ return getReplyString();
+ return null;
+ }
+
+
+ /**
+ * Set the internal buffer size.
+ *
+ * @param bufSize The size of the buffer
+ */
+ public void setBufferSize(int bufSize) {
+ __bufferSize = bufSize;
+ }
+
+ /**
+ * Retrieve the current internal buffer size.
+ * @return The current buffer size.
+ */
+ public int getBufferSize() {
+ return __bufferSize;
+ }
+
+
+ /**
+ * Implementation of the {@link Configurable Configurable} interface.
+ * In the case of this class, configuring merely makes the config object available for the
+ * factory methods that construct parsers.
+ * @param config {@link FTPClientConfig FTPClientConfig} object used to
+ * provide non-standard configurations to the parser.
+ * @since 1.4
+ */
+ public void configure(FTPClientConfig config) {
+ this.__configuration = config;
+ }
+
+ /**
+ * You can set this to true if you would like to get hidden files when {@link #listFiles} too.
+ * A LIST -a
will be issued to the ftp server.
+ * It depends on your ftp server if you need to call this method, also dont expect to get rid
+ * of hidden files if you call this method with "false".
+ *
+ * @param listHiddenFiles true if hidden files should be listed
+ * @since 2.0
+ */
+ public void setListHiddenFiles(boolean listHiddenFiles) {
+ this.__listHiddenFiles = listHiddenFiles;
+ }
+
+ /**
+ * @see #setListHiddenFiles(boolean)
+ * @return the current state
+ * @since 2.0
+ */
+ public boolean getListHiddenFiles() {
+ return this.__listHiddenFiles;
+ }
+}
+
+/* Emacs configuration
+ * Local variables: **
+ * mode: java **
+ * c-basic-offset: 4 **
+ * indent-tabs-mode: nil **
+ * End: **
+ */
diff --git a/src/org/apache/commons/net/ftp/FTPClientConfig.java b/src/org/apache/commons/net/ftp/FTPClientConfig.java
new file mode 100644
index 0000000..450eddc
--- /dev/null
+++ b/src/org/apache/commons/net/ftp/FTPClientConfig.java
@@ -0,0 +1,580 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.net.ftp;
+
+import java.text.DateFormatSymbols;
+import java.util.Collection;
+import java.util.Locale;
+import java.util.Map;
+import java.util.StringTokenizer;
+import java.util.TreeMap;
+
+/**
+ * Examples of use of FTPClientConfig
+ * Use cases:
+ * You are trying to access a server that
+ *
+ *
+ * MM dd yyyy
MMM d yyyy
date formatting
+ *
+ * FTPClient f=FTPClient();
+ * FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
+ * conf.setServerLanguageCode("fr");
+ * f.configure(conf);
+ * f.connect(server);
+ * f.login(username, password);
+ * FTPFile[] files = listFiles(directory);
+ *
+ *
+ * FTPClient f=FTPClient();
+ * FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
+ * conf.setServerLanguageCode("da");
+ * conf.setDefaultDateFormat("d MMM yyyy");
+ * conf.setRecentDateFormat("d MMM HH:mm");
+ * conf.setTimeZoneId("Europe/Copenhagen");
+ * f.configure(conf);
+ * f.connect(server);
+ * f.login(username, password);
+ * FTPListParseEngine engine =
+ * f.initiateListParsing("com.whatever.YourOwnParser", directory);
+ *
+ * while (engine.hasNext()) {
+ * FTPFile[] files = engine.getNext(25); // "page size" you want
+ * //do whatever you want with these files, display them, etc.
+ * //expensive FTPFile objects not created until needed.
+ * }
+ *
+ * MMM d yyyy
date formatting
+ *
+ * FTPClient f=FTPClient();
+ * FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_VMS);
+ * conf.setShortMonthNames(
+ * "jan|feb|mar|apr|ma\u00ED|j\u00FAn|j\u00FAl|\u00e1g\u00FA|sep|okt|n\u00F3v|des");
+ * f.configure(conf);
+ * f.connect(server);
+ * f.login(username, password);
+ * FTPFile[] files = listFiles(directory);
+ *
+ *
+ * Unpaged (whole list) access on a Windows-NT server in a different time zone. + * (Note, since the NT Format uses numeric date formatting, language issues + * are irrelevant here). + *
+ * FTPClient f=FTPClient(); + * FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT); + * conf.setTimeZoneId("America/Denver"); + * f.configure(conf); + * f.connect(server); + * f.login(username, password); + * FTPFile[] files = listFiles(directory); + *+ * + * Unpaged (whole list) access on a Windows-NT server in a different time zone + * but which has been configured to use a unix-style listing format. + *
+ * FTPClient f=FTPClient(); + * FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX); + * conf.setTimeZoneId("America/Denver"); + * f.configure(conf); + * f.connect(server); + * f.login(username, password); + * FTPFile[] files = listFiles(directory); + *+ * + * @since 1.4 + * @see org.apache.commons.net.ftp.Configurable + * @see org.apache.commons.net.ftp.FTPClient + * @see org.apache.commons.net.ftp.parser.FTPTimestampParserImpl#configure(FTPClientConfig) + * @see org.apache.commons.net.ftp.parser.ConfigurableFTPFileEntryParserImpl + */ +public class FTPClientConfig +{ + + /** + * Identifier by which a unix-based ftp server is known throughout + * the commons-net ftp system. + */ + public static final String SYST_UNIX = "UNIX"; + + /** + * Identifier by which a vms-based ftp server is known throughout + * the commons-net ftp system. + */ + public static final String SYST_VMS = "VMS"; + + /** + * Identifier by which a WindowsNT-based ftp server is known throughout + * the commons-net ftp system. + */ + public static final String SYST_NT = "WINDOWS"; + + /** + * Identifier by which an OS/2-based ftp server is known throughout + * the commons-net ftp system. + */ + public static final String SYST_OS2 = "OS/2"; + + /** + * Identifier by which an OS/400-based ftp server is known throughout + * the commons-net ftp system. + */ + public static final String SYST_OS400 = "OS/400"; + + /** + * Identifier by which an AS/400-based ftp server is known throughout + * the commons-net ftp system. + */ + public static final String SYST_AS400 = "AS/400"; + + /** + * Identifier by which an MVS-based ftp server is known throughout + * the commons-net ftp system. + */ + public static final String SYST_MVS = "MVS"; + + /** + * Some servers return an "UNKNOWN Type: L8" message + * in response to the SYST command. We set these to be a Unix-type system. + * This may happen if the ftpd in question was compiled without system + * information. + * + * NET-230 - Updated to be UPPERCASE so that the check done in + * createFileEntryParser will succeed. + * + * @since 1.5 + */ + public static final String SYST_L8 = "TYPE: L8"; + + /** + * Identifier by which an Netware-based ftp server is known throughout + * the commons-net ftp system. + * + * @since 1.5 + */ + public static final String SYST_NETWARE = "NETWARE"; + + private final String serverSystemKey; + private String defaultDateFormatStr = null; + private String recentDateFormatStr = null; + private boolean lenientFutureDates = false; + private String serverLanguageCode = null; + private String shortMonthNames = null; + private String serverTimeZoneId = null; + + + /** + * The main constructor for an FTPClientConfig object + * @param systemKey key representing system type of the server being + * connected to. See {@link #getServerSystemKey() serverSystemKey} + */ + public FTPClientConfig(String systemKey) { + this.serverSystemKey = systemKey; + } + + /** + * Convenience constructor mainly for use in testing. + * Constructs a UNIX configuration. + */ + public FTPClientConfig() { + this(SYST_UNIX); + } + + /** + * Constructor which allows setting of all member fields + * @param systemKey key representing system type of the server being + * connected to. See + * {@link #getServerSystemKey() serverSystemKey} + * @param defaultDateFormatStr See + * {@link #setDefaultDateFormatStr(String) defaultDateFormatStr} + * @param recentDateFormatStr See + * {@link #setRecentDateFormatStr(String) recentDateFormatStr} + * @param serverLanguageCode See + * {@link #setServerLanguageCode(String) serverLanguageCode} + * @param shortMonthNames See + * {@link #setShortMonthNames(String) shortMonthNames} + * @param serverTimeZoneId See + * {@link #setServerTimeZoneId(String) serverTimeZoneId} + */ + public FTPClientConfig(String systemKey, + String defaultDateFormatStr, + String recentDateFormatStr, + String serverLanguageCode, + String shortMonthNames, + String serverTimeZoneId) + { + this(systemKey); + this.defaultDateFormatStr = defaultDateFormatStr; + this.recentDateFormatStr = recentDateFormatStr; + this.serverLanguageCode = serverLanguageCode; + this.shortMonthNames = shortMonthNames; + this.serverTimeZoneId = serverTimeZoneId; + } + + private static Map
FTPClientConfig.SYST_*
codes
+ * or else the fully qualified class name of a parser implementing both
+ * the FTPFileEntryParser
and Configurable
+ * interfaces.
+ * @return Returns the serverSystemKey property.
+ */
+ public String getServerSystemKey() {
+ return serverSystemKey;
+ }
+
+ /**
+ * getter for the {@link #setDefaultDateFormatStr(String) defaultDateFormatStr}
+ * property.
+ * @return Returns the defaultDateFormatStr property.
+ */
+ public String getDefaultDateFormatStr() {
+ return defaultDateFormatStr;
+ }
+
+ /**
+ * getter for the {@link #setRecentDateFormatStr(String) recentDateFormatStr} property.
+ * @return Returns the recentDateFormatStr property.
+ */
+
+ public String getRecentDateFormatStr() {
+ return recentDateFormatStr;
+ }
+
+ /**
+ * getter for the {@link #setServerTimeZoneId(String) serverTimeZoneId} property.
+ * @return Returns the serverTimeZoneId property.
+ */
+ public String getServerTimeZoneId() {
+ return serverTimeZoneId;
+ }
+
+ /**
+ * + * getter for the {@link #setShortMonthNames(String) shortMonthNames} + * property. + *
+ * @return Returns the shortMonthNames. + */ + public String getShortMonthNames() { + return shortMonthNames; + } + + /** + *+ * getter for the {@link #setServerLanguageCode(String) serverLanguageCode} property. + *
+ * @return Returns the serverLanguageCode property. + */ + public String getServerLanguageCode() { + return serverLanguageCode; + } + + /** + *+ * getter for the {@link #setLenientFutureDates(boolean) lenientFutureDates} property. + *
+ * @return Returns the lenientFutureDates. + * @since 1.5 + */ + public boolean isLenientFutureDates() { + return lenientFutureDates; + } + /** + *
+ * setter for the defaultDateFormatStr property. This property
+ * specifies the main date format that will be used by a parser configured
+ * by this configuration to parse file timestamps. If this is not
+ * specified, such a parser will use as a default value, the most commonly
+ * used format which will be in as used in en_US
locales.
+ *
+ * This should be in the format described for
+ * java.text.SimpleDateFormat
.
+ * property.
+ *
+ * setter for the recentDateFormatStr property. This property + * specifies a secondary date format that will be used by a parser + * configured by this configuration to parse file timestamps, typically + * those less than a year old. If this is not specified, such a parser + * will not attempt to parse using an alternate format. + *
+ * This is used primarily in unix-based systems. + * + * This should be in the format described for + *java.text.SimpleDateFormat
.
+ *
+ * @param recentDateFormatStr The recentDateFormatStr to set.
+ */
+ public void setRecentDateFormatStr(String recentDateFormatStr) {
+ this.recentDateFormatStr = recentDateFormatStr;
+ }
+
+ /**
+ * + * setter for the lenientFutureDates property. This boolean property + * (default: false) only has meaning when a + * {@link #setRecentDateFormatStr(String) recentDateFormatStr} property + * has been set. In that case, if this property is set true, then the + * parser, when it encounters a listing parseable with the recent date + * format, will only consider a date to belong to the previous year if + * it is more than one day in the future. This will allow all + * out-of-synch situations (whether based on "slop" - i.e. servers simply + * out of synch with one another or because of time zone differences - + * but in the latter case it is highly recommended to use the + * {@link #setServerTimeZoneId(String) serverTimeZoneId} property + * instead) to resolve correctly. + *
+ * This is used primarily in unix-based systems. + *
+ * @param lenientFutureDates set true to compensate for out-of-synch + * conditions. + */ + public void setLenientFutureDates(boolean lenientFutureDates) { + this.lenientFutureDates = lenientFutureDates; + } + /** + *+ * setter for the serverTimeZoneId property. This property + * allows a time zone to be specified corresponding to that known to be + * used by an FTP server in file listings. This might be particularly + * useful to clients such as Ant that try to use these timestamps for + * dependency checking. + *
+ * This should be one of the identifiers used by
+ * java.util.TimeZone
to refer to time zones, for example,
+ * America/Chicago
or Asia/Rangoon
.
+ *
+ * setter for the shortMonthNames property. + * This property allows the user to specify a set of month names + * used by the server that is different from those that may be + * specified using the {@link #setServerLanguageCode(String) serverLanguageCode} + * property. + *
+ * This should be a string containing twelve strings each composed of
+ * three characters, delimited by pipe (|) characters. Currently,
+ * only 8-bit ASCII characters are known to be supported. For example,
+ * a set of month names used by a hypothetical Icelandic FTP server might
+ * conceivably be specified as
+ * "jan|feb|mar|apr|maí|jún|júl|ágú|sep|okt|nóv|des"
.
+ *
+ * setter for the serverLanguageCode property. This property allows + * user to specify a + * + * two-letter ISO-639 language code that will be used to + * configure the set of month names used by the file timestamp parser. + * If neither this nor the {@link #setShortMonthNames(String) shortMonthNames} + * is specified, parsing will assume English month names, which may or + * may not be significant, depending on whether the date format(s) + * specified via {@link #setDefaultDateFormatStr(String) defaultDateFormatStr} + * and/or {@link #setRecentDateFormatStr(String) recentDateFormatStr} are using + * numeric or alphabetic month names. + *
+ *If the code supplied is not supported here, en_US
+ * month names will be used. We are supporting here those language
+ * codes which, when a java.util.Locale
is constucted
+ * using it, and a java.text.SimpleDateFormat
is
+ * constructed using that Locale, the array returned by the
+ * SimpleDateFormat's getShortMonths()
method consists
+ * solely of three 8-bit ASCII character strings. Additionally,
+ * languages which do not meet this requirement are included if a
+ * common alternative set of short month names is known to be used.
+ * This means that users who can tell us of additional such encodings
+ * may get them added to the list of supported languages by contacting
+ * the jakarta-commons-net team.
+ *
+ * Please note that this attribute will NOT be used to determine a
+ * locale-based date format for the language.
+ * Experience has shown that many if not most FTP servers outside the
+ * United States employ the standard en_US
date format
+ * orderings of MMM d yyyy
and MMM d HH:mm
+ * and attempting to deduce this automatically here would cause more
+ * problems than it would solve. The date format must be changed
+ * via the {@link #setDefaultDateFormatStr(String) defaultDateFormatStr} and/or
+ * {@link #setRecentDateFormatStr(String) recentDateFormatStr} parameters.
+ *
Locale.US
+ * @param languageCode See {@link #setServerLanguageCode(String) serverLanguageCode}
+ * @return a DateFormatSymbols object configured with short month names
+ * corresponding to the supplied code, or with month names for
+ * Locale.US
if there is no corresponding entry in the internal
+ * table.
+ */
+ public static DateFormatSymbols lookupDateFormatSymbols(String languageCode)
+ {
+ Object lang = LANGUAGE_CODE_MAP.get(languageCode);
+ if (lang != null) {
+ if (lang instanceof Locale) {
+ return new DateFormatSymbols((Locale) lang);
+ } else if (lang instanceof String){
+ return getDateFormatSymbols((String) lang);
+ }
+ }
+ return new DateFormatSymbols(Locale.US);
+ }
+
+ /**
+ * Returns a DateFormatSymbols object configured with short month names
+ * as in the supplied string
+ * @param shortmonths This should be as described in
+ * {@link #setShortMonthNames(String) shortMonthNames}
+ * @return a DateFormatSymbols object configured with short month names
+ * as in the supplied string
+ */
+ public static DateFormatSymbols getDateFormatSymbols(String shortmonths)
+ {
+ String[] months = splitShortMonthString(shortmonths);
+ DateFormatSymbols dfs = new DateFormatSymbols(Locale.US);
+ dfs.setShortMonths(months);
+ return dfs;
+ }
+
+ private static String[] splitShortMonthString(String shortmonths) {
+ StringTokenizer st = new StringTokenizer(shortmonths, "|");
+ int monthcnt = st.countTokens();
+ if (12 != monthcnt) {
+ throw new IllegalArgumentException(
+ "expecting a pipe-delimited string containing 12 tokens");
+ }
+ String[] months = new String[13];
+ int pos = 0;
+ while(st.hasMoreTokens()) {
+ months[pos++] = st.nextToken();
+ }
+ months[pos]="";
+ return months;
+ }
+
+ /**
+ * Returns a Collection of all the language codes currently supported
+ * by this class. See {@link #setServerLanguageCode(String) serverLanguageCode}
+ * for a functional descrption of language codes within this system.
+ *
+ * @return a Collection of all the language codes currently supported
+ * by this class
+ */
+ public static Collection+ *
+ * @author Daniel F. Savarese + ***/ + +public final class FTPCommand +{ + + + public static final int USER = 0; + public static final int PASS = 1; + public static final int ACCT = 2; + public static final int CWD = 3; + public static final int CDUP = 4; + public static final int SMNT = 5; + public static final int REIN = 6; + public static final int QUIT = 7; + public static final int PORT = 8; + public static final int PASV = 9; + public static final int TYPE = 10; + public static final int STRU = 11; + public static final int MODE = 12; + public static final int RETR = 13; + public static final int STOR = 14; + public static final int STOU = 15; + public static final int APPE = 16; + public static final int ALLO = 17; + public static final int REST = 18; + public static final int RNFR = 19; + public static final int RNTO = 20; + public static final int ABOR = 21; + public static final int DELE = 22; + public static final int RMD = 23; + public static final int MKD = 24; + public static final int PWD = 25; + public static final int LIST = 26; + public static final int NLST = 27; + public static final int SITE = 28; + public static final int SYST = 29; + public static final int STAT = 30; + public static final int HELP = 31; + public static final int NOOP = 32; + /** @since 2.0 */ + public static final int MDTM = 33; + + public static final int USERNAME = USER; + public static final int PASSWORD = PASS; + public static final int ACCOUNT = ACCT; + public static final int CHANGE_WORKING_DIRECTORY = CWD; + public static final int CHANGE_TO_PARENT_DIRECTORY = CDUP; + public static final int STRUCTURE_MOUNT = SMNT; + public static final int REINITIALIZE = REIN; + public static final int LOGOUT = QUIT; + public static final int DATA_PORT = PORT; + public static final int PASSIVE = PASV; + public static final int REPRESENTATION_TYPE = TYPE; + public static final int FILE_STRUCTURE = STRU; + public static final int TRANSFER_MODE = MODE; + public static final int RETRIEVE = RETR; + public static final int STORE = STOR; + public static final int STORE_UNIQUE = STOU; + public static final int APPEND = APPE; + public static final int ALLOCATE = ALLO; + public static final int RESTART = REST; + public static final int RENAME_FROM = RNFR; + public static final int RENAME_TO = RNTO; + public static final int ABORT = ABOR; + public static final int DELETE = DELE; + public static final int REMOVE_DIRECTORY = RMD; + public static final int MAKE_DIRECTORY = MKD; + public static final int PRINT_WORKING_DIRECTORY = PWD; + // public static final int LIST = LIST; + public static final int NAME_LIST = NLST; + public static final int SITE_PARAMETERS = SITE; + public static final int SYSTEM = SYST; + public static final int STATUS = STAT; + //public static final int HELP = HELP; + //public static final int NOOP = NOOP; + /** @since 2.0 */ + public static final int MOD_TIME = MDTM; + + // Cannot be instantiated + private FTPCommand() + {} + + static final String[] _commands = { + "USER", "PASS", "ACCT", "CWD", "CDUP", "SMNT", "REIN", "QUIT", "PORT", + "PASV", "TYPE", "STRU", "MODE", "RETR", "STOR", "STOU", "APPE", "ALLO", + "REST", "RNFR", "RNTO", "ABOR", "DELE", "RMD", "MKD", "PWD", "LIST", + "NLST", "SITE", "SYST", "STAT", "HELP", "NOOP" + }; + + /** + * Retrieve the FTP protocol command string corresponding to a specified + * command code. + *
+ * @param command The command code. + * @return The FTP protcol command string corresponding to a specified + * command code. + */ + public static final String getCommand(int command) + { + return _commands[command]; + } +} diff --git a/src/org/apache/commons/net/ftp/FTPConnectionClosedException.java b/src/org/apache/commons/net/ftp/FTPConnectionClosedException.java new file mode 100644 index 0000000..3eccbf4 --- /dev/null +++ b/src/org/apache/commons/net/ftp/FTPConnectionClosedException.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.net.ftp; +import java.io.IOException; + +/*** + * FTPConnectionClosedException is used to indicate the premature or + * unexpected closing of an FTP connection resulting from a + * {@link org.apache.commons.net.ftp.FTPReply#SERVICE_NOT_AVAILABLE FTPReply.SERVICE_NOT_AVAILABLE } + * response (FTP reply code 421) to a + * failed FTP command. This exception is derived from IOException and + * therefore may be caught either as an IOException or specifically as an + * FTPConnectionClosedException. + *
+ *
+ * @author Daniel F. Savarese + * @see FTP + * @see FTPClient + ***/ + +public class FTPConnectionClosedException extends IOException +{ + + /*** Constructs a FTPConnectionClosedException with no message ***/ + public FTPConnectionClosedException() + { + super(); + } + + /*** + * Constructs a FTPConnectionClosedException with a specified message. + *
+ * @param message The message explaining the reason for the exception. + ***/ + public FTPConnectionClosedException(String message) + { + super(message); + } + +} diff --git a/src/org/apache/commons/net/ftp/FTPFile.java b/src/org/apache/commons/net/ftp/FTPFile.java new file mode 100644 index 0000000..dd67904 --- /dev/null +++ b/src/org/apache/commons/net/ftp/FTPFile.java @@ -0,0 +1,392 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.net.ftp; +import java.io.Serializable; +import java.util.Calendar; + +/*** + * The FTPFile class is used to represent information about files stored + * on an FTP server. Because there is no standard representation for + * file information on FTP servers, it may not always be possible to + * extract all the information that can be represented by FTPFile, or + * it may even be possible to extract more information. In cases where + * more information can be extracted, you will want to subclass FTPFile + * and implement your own {@link org.apache.commons.net.ftp.FTPFileListParser} + * to extract the information. + * However, most FTP servers return file information in a format that + * can be completely parsed by + * {@link org.apache.commons.net.ftp.DefaultFTPFileListParser} + * and stored in FTPFile. + *
+ *
+ * @author Daniel F. Savarese + * @see FTPFileListParser + * @see DefaultFTPFileListParser + * @see FTPClient#listFiles + ***/ + +public class FTPFile implements Serializable +{ + /** A constant indicating an FTPFile is a file. ***/ + public static final int FILE_TYPE = 0; + /** A constant indicating an FTPFile is a directory. ***/ + public static final int DIRECTORY_TYPE = 1; + /** A constant indicating an FTPFile is a symbolic link. ***/ + public static final int SYMBOLIC_LINK_TYPE = 2; + /** A constant indicating an FTPFile is of unknown type. ***/ + public static final int UNKNOWN_TYPE = 3; + + /** A constant indicating user access permissions. ***/ + public static final int USER_ACCESS = 0; + /** A constant indicating group access permissions. ***/ + public static final int GROUP_ACCESS = 1; + /** A constant indicating world access permissions. ***/ + public static final int WORLD_ACCESS = 2; + + /** A constant indicating file/directory read permission. ***/ + public static final int READ_PERMISSION = 0; + /** A constant indicating file/directory write permission. ***/ + public static final int WRITE_PERMISSION = 1; + /** + * A constant indicating file execute permission or directory listing + * permission. + ***/ + public static final int EXECUTE_PERMISSION = 2; + + int _type, _hardLinkCount; + long _size; + String _rawListing, _user, _group, _name, _link; + Calendar _date; + boolean[] _permissions[]; + + /*** Creates an empty FTPFile. ***/ + public FTPFile() + { + _permissions = new boolean[3][3]; + _rawListing = null; + _type = UNKNOWN_TYPE; + _hardLinkCount = 0; + _size = 0; + _user = null; + _group = null; + _date = null; + _name = null; + } + + + /*** + * Set the original FTP server raw listing from which the FTPFile was + * created. + *
+ * @param rawListing The raw FTP server listing. + ***/ + public void setRawListing(String rawListing) + { + _rawListing = rawListing; + } + + /*** + * Get the original FTP server raw listing used to initialize the FTPFile. + *
+ * @return The original FTP server raw listing used to initialize the + * FTPFile. + ***/ + public String getRawListing() + { + return _rawListing; + } + + + /*** + * Determine if the file is a directory. + *
+ * @return True if the file is of type DIRECTORY_TYPE
, false if
+ * not.
+ ***/
+ public boolean isDirectory()
+ {
+ return (_type == DIRECTORY_TYPE);
+ }
+
+ /***
+ * Determine if the file is a regular file.
+ *
+ * @return True if the file is of type FILE_TYPE
, false if
+ * not.
+ ***/
+ public boolean isFile()
+ {
+ return (_type == FILE_TYPE);
+ }
+
+ /***
+ * Determine if the file is a symbolic link.
+ *
+ * @return True if the file is of type UNKNOWN_TYPE
, false if
+ * not.
+ ***/
+ public boolean isSymbolicLink()
+ {
+ return (_type == SYMBOLIC_LINK_TYPE);
+ }
+
+ /***
+ * Determine if the type of the file is unknown.
+ *
+ * @return True if the file is of type UNKNOWN_TYPE
, false if
+ * not.
+ ***/
+ public boolean isUnknown()
+ {
+ return (_type == UNKNOWN_TYPE);
+ }
+
+
+ /***
+ * Set the type of the file (DIRECTORY_TYPE
,
+ * FILE_TYPE
, etc.).
+ *
+ * @param type The integer code representing the type of the file.
+ ***/
+ public void setType(int type)
+ {
+ _type = type;
+ }
+
+
+ /***
+ * Return the type of the file (one of the _TYPE
constants),
+ * e.g., if it is a directory, a regular file, or a symbolic link.
+ *
+ * @return The type of the file. + ***/ + public int getType() + { + return _type; + } + + + /*** + * Set the name of the file. + *
+ * @param name The name of the file. + ***/ + public void setName(String name) + { + _name = name; + } + + /*** + * Return the name of the file. + *
+ * @return The name of the file. + ***/ + public String getName() + { + return _name; + } + + + /** + * Set the file size in bytes. + * @param size The file size in bytes. + */ + public void setSize(long size) + { + _size = size; + } + + + /*** + * Return the file size in bytes. + *
+ * @return The file size in bytes. + ***/ + public long getSize() + { + return _size; + } + + + /*** + * Set the number of hard links to this file. This is not to be + * confused with symbolic links. + *
+ * @param links The number of hard links to this file. + ***/ + public void setHardLinkCount(int links) + { + _hardLinkCount = links; + } + + + /*** + * Return the number of hard links to this file. This is not to be + * confused with symbolic links. + *
+ * @return The number of hard links to this file. + ***/ + public int getHardLinkCount() + { + return _hardLinkCount; + } + + + /*** + * Set the name of the group owning the file. This may be + * a string representation of the group number. + *
+ * @param group The name of the group owning the file. + ***/ + public void setGroup(String group) + { + _group = group; + } + + + /*** + * Returns the name of the group owning the file. Sometimes this will be + * a string representation of the group number. + *
+ * @return The name of the group owning the file. + ***/ + public String getGroup() + { + return _group; + } + + + /*** + * Set the name of the user owning the file. This may be + * a string representation of the user number; + *
+ * @param user The name of the user owning the file. + ***/ + public void setUser(String user) + { + _user = user; + } + + /*** + * Returns the name of the user owning the file. Sometimes this will be + * a string representation of the user number. + *
+ * @return The name of the user owning the file. + ***/ + public String getUser() + { + return _user; + } + + + /*** + * If the FTPFile is a symbolic link, use this method to set the name of the + * file being pointed to by the symbolic link. + *
+ * @param link The file pointed to by the symbolic link. + ***/ + public void setLink(String link) + { + _link = link; + } + + + /*** + * If the FTPFile is a symbolic link, this method returns the name of the + * file being pointed to by the symbolic link. Otherwise it returns null. + *
+ * @return The file pointed to by the symbolic link (null if the FTPFile + * is not a symbolic link). + ***/ + public String getLink() + { + return _link; + } + + + /*** + * Set the file timestamp. This usually the last modification time. + * The parameter is not cloned, so do not alter its value after calling + * this method. + *
+ * @param date A Calendar instance representing the file timestamp. + ***/ + public void setTimestamp(Calendar date) + { + _date = date; + } + + + /*** + * Returns the file timestamp. This usually the last modification time. + *
+ * @return A Calendar instance representing the file timestamp.
+ ***/
+ public Calendar getTimestamp()
+ {
+ return _date;
+ }
+
+
+ /***
+ * Set if the given access group (one of the _ACCESS
+ * constants) has the given access permission (one of the
+ * _PERMISSION
constants) to the file.
+ *
+ * @param access The access group (one of the _ACCESS
+ * constants)
+ * @param permission The access permission (one of the
+ * _PERMISSION
constants)
+ * @param value True if permission is allowed, false if not.
+ ***/
+ public void setPermission(int access, int permission, boolean value)
+ {
+ _permissions[access][permission] = value;
+ }
+
+
+ /***
+ * Determines if the given access group (one of the _ACCESS
+ * constants) has the given access permission (one of the
+ * _PERMISSION
constants) to the file.
+ *
+ * @param access The access group (one of the _ACCESS
+ * constants)
+ * @param permission The access permission (one of the
+ * _PERMISSION
constants)
+ ***/
+ public boolean hasPermission(int access, int permission)
+ {
+ return _permissions[access][permission];
+ }
+
+
+ /***
+ * Returns a string representation of the FTPFile information. This
+ * will be the raw FTP server listing that was used to initialize the
+ * FTPFile instance.
+ *
+ * @return A string representation of the FTPFile information. + ***/ + @Override + public String toString() + { + return _rawListing; + } + +} diff --git a/src/org/apache/commons/net/ftp/FTPFileEntryParser.java b/src/org/apache/commons/net/ftp/FTPFileEntryParser.java new file mode 100644 index 0000000..8e6d09c --- /dev/null +++ b/src/org/apache/commons/net/ftp/FTPFileEntryParser.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.net.ftp; +import java.io.BufferedReader; +import java.io.IOException; +import java.util.List; + +/** + * FTPFileEntryParser defines the interface for parsing a single FTP file + * listing and converting that information into an + * {@link org.apache.commons.net.ftp.FTPFile} instance. + * Sometimes you will want to parse unusual listing formats, in which + * case you would create your own implementation of FTPFileEntryParser and + * if necessary, subclass FTPFile. + *
+ * Here are some examples showing how to use one of the classes that + * implement this interface. + *
+ * The first example shows how to get an iterable list of files in which the
+ * more expensive FTPFile
objects are not created until needed. This
+ * is suitable for paged displays. It requires that a parser object be created
+ * beforehand: parser
is an object (in the package
+ * org.apache.commons.net.ftp.parser
)
+ * implementing this inteface.
+ *
+ *
+ * FTPClient f=FTPClient(); + * f.connect(server); + * f.login(username, password); + * FTPFileList list = f.createFileList(directory, parser); + * FTPFileIterator iter = list.iterator(); + * + * while (iter.hasNext()) { + * FTPFile[] files = iter.getNext(25); // "page size" you want + * //do whatever you want with these files, display them, etc. + * //expensive FTPFile objects not created until needed. + * } + *+ * + * The second example uses the revised
FTPClient.listFiles()
+ * API to pull the whole list from the subfolder subfolder
in
+ * one call, attempting to automatically detect the parser type. This
+ * method, without a parserKey parameter, indicates that autodection should
+ * be used.
+ *
+ * + * FTPClient f=FTPClient(); + * f.connect(server); + * f.login(username, password); + * FTPFile[] files = f.listFiles("subfolder"); + *+ * + * The third example uses the revised
FTPClient.listFiles()
>
+ * API to pull the whole list from the current working directory in one call,
+ * but specifying by classname the parser to be used. For this particular
+ * parser class, this approach is necessary since there is no way to
+ * autodetect this server type.
+ *
+ * + * FTPClient f=FTPClient(); + * f.connect(server); + * f.login(username, password); + * FTPFile[] files = f.listFiles( + * "org.apache.commons.net.ftp.parser.EnterpriseUnixFTPFileEntryParser", + * "."); + *+ * + * The fourth example uses the revised
FTPClient.listFiles()
+ * API to pull a single file listing in an arbitrary directory in one call,
+ * specifying by KEY the parser to be used, in this case, VMS.
+ *
+ * + * FTPClient f=FTPClient(); + * f.connect(server); + * f.login(username, password); + * FTPFile[] files = f.listFiles("VMS", "subfolder/foo.java"); + *+ * + * @author Steve Cohen + * @version $Id: FTPFileEntryParser.java 636854 2008-03-13 19:55:01Z sebb $ + * @see org.apache.commons.net.ftp.FTPFile + * @see org.apache.commons.net.ftp.FTPClient#createFileList + */ +public interface FTPFileEntryParser +{ + /** + * Parses a line of an FTP server file listing and converts it into a usable + * format in the form of an
FTPFile
instance. If the
+ * file listing line doesn't describe a file, null
should be
+ * returned, otherwise a FTPFile
instance representing the
+ * files in the directory is returned.
+ *
+ * @param listEntry A line of text from the file listing
+ * @return An FTPFile instance corresponding to the supplied entry
+ */
+ FTPFile parseFTPEntry(String listEntry);
+
+ /**
+ * Reads the next entry using the supplied BufferedReader object up to
+ * whatever delemits one entry from the next. Implementors must define
+ * this for the particular ftp system being parsed. In many but not all
+ * cases, this can be defined simply by calling BufferedReader.readLine().
+ *
+ * @param reader The BufferedReader object from which entries are to be
+ * read.
+ *
+ * @return A string representing the next ftp entry or null if none found.
+ * @exception IOException thrown on any IO Error reading from the reader.
+ */
+ String readNextEntry(BufferedReader reader) throws IOException;
+
+
+ /**
+ * This method is a hook for those implementors (such as
+ * VMSVersioningFTPEntryParser, and possibly others) which need to
+ * perform some action upon the FTPFileList after it has been created
+ * from the server stream, but before any clients see the list.
+ *
+ * The default implementation can be a no-op.
+ *
+ * @param original Original list after it has been created from the server stream
+ *
+ * @return Original list as processed by this method.
+ */
+ List
+ * This object defines a two-part parsing mechanism.
+ *
+ * The first part is comprised of reading the raw input into an internal
+ * list of strings. Every item in this list corresponds to an actual
+ * file. All extraneous matter emitted by the server will have been
+ * removed by the end of this phase. This is accomplished in conjunction
+ * with the FTPFileEntryParser associated with this engine, by calling
+ * its methods
+ * The second part is composed of the actual parsing, again in conjunction
+ * with the particular parser used by this engine. This is controlled
+ * by an iterator over the internal list of strings. This may be done
+ * either in block mode, by calling the
+ * Examples:
+ *
+ * Paged access:
+ *
+ * For unpaged access, simply use FTPClient.listFiles(). That method
+ * uses this class transparently.
+ * @version $Id: FTPListParseEngine.java 658518 2008-05-21 01:04:30Z sebb $
+ */
+public class FTPListParseEngine {
+ private List
+ * NOTE: This array may contain null members if any of the
+ * individual file listings failed to parse. The caller should
+ * check each entry for null before referencing it.
+ */
+ public FTPFile[] getNext(int quantityRequested) {
+ List
+ * NOTE: This array may contain null members if any of the
+ * individual file listings failed to parse. The caller should
+ * check each entry for null before referencing it.
+ */
+ public FTPFile[] getPrevious(int quantityRequested) {
+ List
+ * NOTE: This array may contain null members if any of the
+ * individual file listings failed to parse. The caller should
+ * check each entry for null before referencing it.
+ * @exception IOException
+ */
+ public FTPFile[] getFiles()
+ throws IOException
+ {
+ List
+ *
+ * @author Daniel F. Savarese
+ ***/
+
+public final class FTPReply
+{
+
+ public static final int CODE_110 = 110;
+ public static final int CODE_120 = 120;
+ public static final int CODE_125 = 125;
+ public static final int CODE_150 = 150;
+ public static final int CODE_200 = 200;
+ public static final int CODE_202 = 202;
+ public static final int CODE_211 = 211;
+ public static final int CODE_212 = 212;
+ public static final int CODE_213 = 213;
+ public static final int CODE_214 = 214;
+ public static final int CODE_215 = 215;
+ public static final int CODE_220 = 220;
+ public static final int CODE_221 = 221;
+ public static final int CODE_225 = 225;
+ public static final int CODE_226 = 226;
+ public static final int CODE_227 = 227;
+ public static final int CODE_230 = 230;
+ public static final int CODE_250 = 250;
+ public static final int CODE_257 = 257;
+ public static final int CODE_331 = 331;
+ public static final int CODE_332 = 332;
+ public static final int CODE_350 = 350;
+ public static final int CODE_421 = 421;
+ public static final int CODE_425 = 425;
+ public static final int CODE_426 = 426;
+ public static final int CODE_450 = 450;
+ public static final int CODE_451 = 451;
+ public static final int CODE_452 = 452;
+ public static final int CODE_500 = 500;
+ public static final int CODE_501 = 501;
+ public static final int CODE_502 = 502;
+ public static final int CODE_503 = 503;
+ public static final int CODE_504 = 504;
+ public static final int CODE_521 = 521;
+ public static final int CODE_530 = 530;
+ public static final int CODE_532 = 532;
+ public static final int CODE_550 = 550;
+ public static final int CODE_551 = 551;
+ public static final int CODE_552 = 552;
+ public static final int CODE_553 = 553;
+
+ public static final int RESTART_MARKER = CODE_110;
+ public static final int SERVICE_NOT_READY = CODE_120;
+ public static final int DATA_CONNECTION_ALREADY_OPEN = CODE_125;
+ public static final int FILE_STATUS_OK = CODE_150;
+ public static final int COMMAND_OK = CODE_200;
+ public static final int COMMAND_IS_SUPERFLUOUS = CODE_202;
+ public static final int SYSTEM_STATUS = CODE_211;
+ public static final int DIRECTORY_STATUS = CODE_212;
+ public static final int FILE_STATUS = CODE_213;
+ public static final int HELP_MESSAGE = CODE_214;
+ public static final int NAME_SYSTEM_TYPE = CODE_215;
+ public static final int SERVICE_READY = CODE_220;
+ public static final int SERVICE_CLOSING_CONTROL_CONNECTION = CODE_221;
+ public static final int DATA_CONNECTION_OPEN = CODE_225;
+ public static final int CLOSING_DATA_CONNECTION = CODE_226;
+ public static final int ENTERING_PASSIVE_MODE = CODE_227;
+ public static final int USER_LOGGED_IN = CODE_230;
+ public static final int FILE_ACTION_OK = CODE_250;
+ public static final int PATHNAME_CREATED = CODE_257;
+ public static final int NEED_PASSWORD = CODE_331;
+ public static final int NEED_ACCOUNT = CODE_332;
+ public static final int FILE_ACTION_PENDING = CODE_350;
+ public static final int SERVICE_NOT_AVAILABLE = CODE_421;
+ public static final int CANNOT_OPEN_DATA_CONNECTION = CODE_425;
+ public static final int TRANSFER_ABORTED = CODE_426;
+ public static final int FILE_ACTION_NOT_TAKEN = CODE_450;
+ public static final int ACTION_ABORTED = CODE_451;
+ public static final int INSUFFICIENT_STORAGE = CODE_452;
+ public static final int UNRECOGNIZED_COMMAND = CODE_500;
+ public static final int SYNTAX_ERROR_IN_ARGUMENTS = CODE_501;
+ public static final int COMMAND_NOT_IMPLEMENTED = CODE_502;
+ public static final int BAD_COMMAND_SEQUENCE = CODE_503;
+ public static final int COMMAND_NOT_IMPLEMENTED_FOR_PARAMETER = CODE_504;
+ public static final int NOT_LOGGED_IN = CODE_530;
+ public static final int NEED_ACCOUNT_FOR_STORING_FILES = CODE_532;
+ public static final int FILE_UNAVAILABLE = CODE_550;
+ public static final int PAGE_TYPE_UNKNOWN = CODE_551;
+ public static final int STORAGE_ALLOCATION_EXCEEDED = CODE_552;
+ public static final int FILE_NAME_NOT_ALLOWED = CODE_553;
+
+ // FTPS Reply Codes
+ /** @since 2.0 */
+ public static final int CODE_234 = 234;
+ /** @since 2.0 */
+ public static final int CODE_235 = 235;
+ /** @since 2.0 */
+ public static final int CODE_334 = 334;
+ /** @since 2.0 */
+ public static final int CODE_335 = 335;
+ /** @since 2.0 */
+ public static final int CODE_431 = 431;
+ /** @since 2.0 */
+ public static final int CODE_533 = 533;
+ /** @since 2.0 */
+ public static final int CODE_534 = 534;
+ /** @since 2.0 */
+ public static final int CODE_535 = 535;
+ /** @since 2.0 */
+ public static final int CODE_536 = 536;
+
+ /** @since 2.0 */
+ public static final int SECURITY_DATA_EXCHANGE_COMPLETE = CODE_234;
+ /** @since 2.0 */
+ public static final int SECURITY_DATA_EXCHANGE_SUCCESSFULLY = CODE_235;
+ /** @since 2.0 */
+ public static final int SECURITY_MECHANISM_IS_OK = CODE_334;
+ /** @since 2.0 */
+ public static final int SECURITY_DATA_IS_ACCEPTABLE = CODE_335;
+ /** @since 2.0 */
+ public static final int UNAVAILABLE_RESOURCE = CODE_431;
+ /** @since 2.0 */
+ public static final int DENIED_FOR_POLICY_REASONS = CODE_533;
+ /** @since 2.0 */
+ public static final int REQUEST_DENIED = CODE_534;
+ /** @since 2.0 */
+ public static final int FAILED_SECURITY_CHECK = CODE_535;
+ /** @since 2.0 */
+ public static final int REQUESTED_PROT_LEVEL_NOT_SUPPORTED = CODE_536;
+
+
+ // Cannot be instantiated
+ private FTPReply()
+ {}
+
+ /***
+ * Determine if a reply code is a positive preliminary response. All
+ * codes beginning with a 1 are positive preliminary responses.
+ * Postitive preliminary responses are used to indicate tentative success.
+ * No further commands can be issued to the FTP server after a positive
+ * preliminary response until a follow up response is received from the
+ * server.
+ *
+ * @param reply The reply code to test.
+ * @return True if a reply code is a postive preliminary response, false
+ * if not.
+ ***/
+ public static boolean isPositivePreliminary(int reply)
+ {
+ return (reply >= 100 && reply < 200);
+ }
+
+ /***
+ * Determine if a reply code is a positive completion response. All
+ * codes beginning with a 2 are positive completion responses.
+ * The FTP server will send a positive completion response on the final
+ * successful completion of a command.
+ *
+ * @param reply The reply code to test.
+ * @return True if a reply code is a postive completion response, false
+ * if not.
+ ***/
+ public static boolean isPositiveCompletion(int reply)
+ {
+ return (reply >= 200 && reply < 300);
+ }
+
+ /***
+ * Determine if a reply code is a positive intermediate response. All
+ * codes beginning with a 3 are positive intermediate responses.
+ * The FTP server will send a positive intermediate response on the
+ * successful completion of one part of a multi-part sequence of
+ * commands. For example, after a successful USER command, a positive
+ * intermediate response will be sent to indicate that the server is
+ * ready for the PASS command.
+ *
+ * @param reply The reply code to test.
+ * @return True if a reply code is a postive intermediate response, false
+ * if not.
+ ***/
+ public static boolean isPositiveIntermediate(int reply)
+ {
+ return (reply >= 300 && reply < 400);
+ }
+
+ /***
+ * Determine if a reply code is a negative transient response. All
+ * codes beginning with a 4 are negative transient responses.
+ * The FTP server will send a negative transient response on the
+ * failure of a command that can be reattempted with success.
+ *
+ * @param reply The reply code to test.
+ * @return True if a reply code is a negative transient response, false
+ * if not.
+ ***/
+ public static boolean isNegativeTransient(int reply)
+ {
+ return (reply >= 400 && reply < 500);
+ }
+
+ /***
+ * Determine if a reply code is a negative permanent response. All
+ * codes beginning with a 5 are negative permanent responses.
+ * The FTP server will send a negative permanent response on the
+ * failure of a command that cannot be reattempted with success.
+ *
+ * @param reply The reply code to test.
+ * @return True if a reply code is a negative permanent response, false
+ * if not.
+ ***/
+ public static boolean isNegativePermanent(int reply)
+ {
+ return (reply >= 500 && reply < 600);
+ }
+
+}
diff --git a/src/org/apache/commons/net/ftp/FTPSClient.java b/src/org/apache/commons/net/ftp/FTPSClient.java
new file mode 100644
index 0000000..f809c41
--- /dev/null
+++ b/src/org/apache/commons/net/ftp/FTPSClient.java
@@ -0,0 +1,533 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.net.ftp;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.net.Socket;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLException;
+import javax.net.ssl.SSLServerSocketFactory;
+import javax.net.ssl.SSLSocket;
+import javax.net.ssl.SSLSocketFactory;
+import javax.net.ssl.TrustManager;
+
+/**
+ * FTP over SSL processing. If desired, the JVM property -Djavax.net.debug=all can be used to
+ * see wire-level SSL details.
+ *
+ * @version $Id: FTPSClient.java 658520 2008-05-21 01:14:11Z sebb $
+ * @since 2.0
+ */
+public class FTPSClient extends FTPClient {
+
+ /** keystore algorithm name. */
+ public static String KEYSTORE_ALGORITHM;
+ /** truststore algorithm name. */
+ public static String TRUSTSTORE_ALGORITHM;
+ /** provider name. */
+ public static String PROVIDER;
+ /** truststore type. */
+ public static String STORE_TYPE;
+
+ /** The value that I can set in PROT command */
+ private static final String[] PROT_COMMAND_VALUE = {"C","E","S","P"};
+ /** Default PROT Command */
+ private static final String DEFAULT_PROT = "C";
+ /** Default protocol name */
+ private static final String DEFAULT_PROTOCOL = "TLS";
+
+ /** The security mode. (True - Implicit Mode / False - Explicit Mode) */
+ private boolean isImplicit;
+ /** The use SSL/TLS protocol. */
+ private String protocol = DEFAULT_PROTOCOL;
+ /** The AUTH Command value */
+ private String auth = DEFAULT_PROTOCOL;
+ /** The context object. */
+ private SSLContext context;
+ /** The socket object. */
+ private Socket planeSocket;
+ /** The established socket flag. */
+ private boolean isCreation = true;
+ /** The use client mode flag. */
+ private boolean isClientMode = true;
+ /** The need client auth flag. */
+ private boolean isNeedClientAuth = false;
+ /** The want client auth flag. */
+ private boolean isWantClientAuth = false;
+ /** The cipher suites */
+ private String[] suites = null;
+ /** The protocol versions */
+ private String[] protocols = null;
+
+ /** The FTPS {@link TrustManager} implementation. */
+ private TrustManager trustManager = new FTPSTrustManager();
+
+ /** The {@link KeyManager} */
+ private KeyManager keyManager;
+
+ /**
+ * Constructor for FTPSClient.
+ * @throws NoSuchAlgorithmException A requested cryptographic algorithm
+ * is not available in the environment.
+ */
+ public FTPSClient() throws NoSuchAlgorithmException {
+ this.protocol = DEFAULT_PROTOCOL;
+ this.isImplicit = false;
+ }
+
+ /**
+ * Constructor for FTPSClient.
+ * @param isImplicit The secutiry mode(Implicit/Explicit).
+ * @throws NoSuchAlgorithmException A requested cryptographic algorithm
+ * is not available in the environment.
+ */
+ public FTPSClient(boolean isImplicit) throws NoSuchAlgorithmException {
+ this.protocol = DEFAULT_PROTOCOL;
+ this.isImplicit = isImplicit;
+ }
+
+ /**
+ * Constructor for FTPSClient.
+ * @param protocol the protocol
+ * @throws NoSuchAlgorithmException A requested cryptographic algorithm
+ * is not available in the environment.
+ */
+ public FTPSClient(String protocol) throws NoSuchAlgorithmException {
+ this.protocol = protocol;
+ this.isImplicit = false;
+ }
+
+ /**
+ * Constructor for FTPSClient.
+ * @param protocol the protocol
+ * @param isImplicit The secutiry mode(Implicit/Explicit).
+ * @throws NoSuchAlgorithmException A requested cryptographic algorithm
+ * is not available in the environment.
+ */
+ public FTPSClient(String protocol, boolean isImplicit)
+ throws NoSuchAlgorithmException {
+ this.protocol = protocol;
+ this.isImplicit = isImplicit;
+ }
+
+
+ /**
+ * Set AUTH command use value.
+ * This processing is done before connected processing.
+ * @param auth AUTH command use value.
+ */
+ public void setAuthValue(String auth) {
+ this.auth = auth;
+ }
+
+ /**
+ * Return AUTH command use value.
+ * @return AUTH command use value.
+ */
+ public String getAuthValue() {
+ return this.auth;
+ }
+
+
+ /**
+ * Because there are so many connect() methods,
+ * the _connectAction_() method is provided as a means of performing
+ * some action immediately after establishing a connection,
+ * rather than reimplementing all of the connect() methods.
+ * @throws IOException If it throw by _connectAction_.
+ * @see org.apache.commons.net.SocketClient#_connectAction_()
+ */
+ @Override
+ protected void _connectAction_() throws IOException {
+ // Implicit mode.
+ if (isImplicit) sslNegotiation();
+ super._connectAction_();
+ // Explicit mode.
+ if (!isImplicit) {
+ execAUTH();
+ sslNegotiation();
+ }
+ }
+
+ /**
+ * AUTH command.
+ * @throws SSLException If it server reply code not equal "234" and "334".
+ * @throws IOException If an I/O error occurs while either sending
+ * the command.
+ */
+ private void execAUTH() throws SSLException, IOException {
+ int replyCode = sendCommand(
+ FTPSCommand._commands[FTPSCommand.AUTH], auth);
+ if (FTPReply.SECURITY_MECHANISM_IS_OK == replyCode) {
+ // replyCode = 334
+ // I carry out an ADAT command.
+ } else if (FTPReply.SECURITY_DATA_EXCHANGE_COMPLETE != replyCode) {
+ throw new SSLException(getReplyString());
+ }
+ }
+
+ /**
+ * Performs a lazy init of the SSL context
+ * @throws IOException
+ */
+ private void initSslContext() throws IOException {
+ if(context == null) {
+ try {
+ context = SSLContext.getInstance(protocol);
+
+ context.init(new KeyManager[] { getKeyManager() } , new TrustManager[] { getTrustManager() } , null);
+ } catch (KeyManagementException e) {
+ IOException ioe = new IOException("Could not initialize SSL context");
+ ioe.initCause(e);
+ throw ioe;
+ } catch (NoSuchAlgorithmException e) {
+ IOException ioe = new IOException("Could not initialize SSL context");
+ ioe.initCause(e);
+ throw ioe;
+ }
+ }
+ }
+
+ /**
+ * SSL/TLS negotiation. Acquires an SSL socket of a control
+ * connection and carries out handshake processing.
+ * @throws IOException A handicap breaks out by sever negotiation.
+ */
+ private void sslNegotiation() throws IOException {
+ // Evacuation not ssl socket.
+ planeSocket = _socket_;
+
+ initSslContext();
+
+ SSLSocketFactory ssf = context.getSocketFactory();
+ String ip = _socket_.getInetAddress().getHostAddress();
+ int port = _socket_.getPort();
+ SSLSocket socket =
+ (SSLSocket) ssf.createSocket(_socket_, ip, port, true);
+ socket.setEnableSessionCreation(isCreation);
+ socket.setUseClientMode(isClientMode);
+ // server mode
+ if (!isClientMode) {
+ socket.setNeedClientAuth(isNeedClientAuth);
+ socket.setWantClientAuth(isWantClientAuth);
+ }
+ if (protocols != null) socket.setEnabledProtocols(protocols);
+ if (suites != null) socket.setEnabledCipherSuites(suites);
+
+ socket.startHandshake();
+
+ _socket_ = socket;
+ _controlInput_ = new BufferedReader(new InputStreamReader(
+ socket .getInputStream(), getControlEncoding()));
+ _controlOutput_ = new BufferedWriter(new OutputStreamWriter(
+ socket.getOutputStream(), getControlEncoding()));
+ }
+
+ /**
+ * Get the {@link KeyManager} instance.
+ * @return The {@link KeyManager} instance
+ */
+ private KeyManager getKeyManager() {
+ return keyManager;
+ }
+
+ /**
+ * Set a {@link KeyManager} to use
+ *
+ * @param keyManager The KeyManager implementation to set.
+ */
+ public void setKeyManager(KeyManager keyManager) {
+ this.keyManager = keyManager;
+ }
+
+ /**
+ * Controls whether new a SSL session may be established by this socket.
+ * @param isCreation The established socket flag.
+ */
+ public void setEnabledSessionCreation(boolean isCreation) {
+ this.isCreation = isCreation;
+ }
+
+ /**
+ * Returns true if new SSL sessions may be established by this socket.
+ * When a socket does not have a ssl socket, This return False.
+ * @return true - Indicates that sessions may be created;
+ * this is the default.
+ * false - indicates that an existing session must be resumed.
+ */
+ public boolean getEnableSessionCreation() {
+ if (_socket_ instanceof SSLSocket)
+ return ((SSLSocket)_socket_).getEnableSessionCreation();
+ return false;
+ }
+
+ /**
+ * Configures the socket to require client authentication.
+ * @param isNeedClientAuth The need client auth flag.
+ */
+ public void setNeedClientAuth(boolean isNeedClientAuth) {
+ this.isNeedClientAuth = isNeedClientAuth;
+ }
+
+ /**
+ * Returns true if the socket will require client authentication.
+ * When a socket does not have a ssl socket, This return False.
+ * @return true - If the server mode socket should request
+ * that the client authenticate itself.
+ */
+ public boolean getNeedClientAuth() {
+ if (_socket_ instanceof SSLSocket)
+ return ((SSLSocket)_socket_).getNeedClientAuth();
+ return false;
+ }
+
+ /**
+ * Configures the socket to request client authentication,
+ * but only if such a request is appropriate to the cipher
+ * suite negotiated.
+ * @param isWantClientAuth The want client auth flag.
+ */
+ public void setWantClientAuth(boolean isWantClientAuth) {
+ this.isWantClientAuth = isWantClientAuth;
+ }
+
+ /**
+ * Returns true if the socket will request client authentication.
+ * When a socket does not have a ssl socket, This return False.
+ * @return true - If the server mode socket should request
+ * that the client authenticate itself.
+ */
+ public boolean getWantClientAuth() {
+ if (_socket_ instanceof SSLSocket)
+ return ((SSLSocket)_socket_).getWantClientAuth();
+ return false;
+ }
+
+ /**
+ * Configures the socket to use client (or server) mode in its first
+ * handshake.
+ * @param isClientMode The use client mode flag.
+ */
+ public void setUseClientMode(boolean isClientMode) {
+ this.isClientMode = isClientMode;
+ }
+
+ /**
+ * Returns true if the socket is set to use client mode
+ * in its first handshake.
+ * When a socket does not have a ssl socket, This return False.
+ * @return true - If the socket should start its first handshake
+ * in "client" mode.
+ */
+ public boolean getUseClientMode() {
+ if (_socket_ instanceof SSLSocket)
+ return ((SSLSocket)_socket_).getUseClientMode();
+ return false;
+ }
+
+ /**
+ * Controls which particular cipher suites are enabled for use on this
+ * connection. I perform setting before a server negotiation.
+ * @param cipherSuites The cipher suites.
+ */
+ public void setEnabledCipherSuites(String[] cipherSuites) {
+ suites = new String[cipherSuites.length];
+ System.arraycopy(cipherSuites, 0, suites, 0, cipherSuites.length);
+ }
+
+ /**
+ * Returns the names of the cipher suites which could be enabled
+ * for use on this connection.
+ * When a socket does not have a ssl socket, This return null.
+ * @return An array of cipher suite names.
+ */
+ public String[] getEnabledCipherSuites() {
+ if (_socket_ instanceof SSLSocket)
+ return ((SSLSocket)_socket_).getEnabledCipherSuites();
+ return null;
+ }
+
+ /**
+ * Controls which particular protocol versions are enabled for use on this
+ * connection. I perform setting before a server negotiation.
+ * @param protocolVersions The protocol versions.
+ */
+ public void setEnabledProtocols(String[] protocolVersions) {
+ protocols = new String[protocolVersions.length];
+ System.arraycopy(protocolVersions, 0, protocols, 0, protocolVersions.length);
+ }
+
+ /**
+ * Returns the names of the protocol versions which are currently
+ * enabled for use on this connection.
+ * When a socket does not have a ssl socket, This return null.
+ * @return An array of protocols.
+ */
+ public String[] getEnabledProtocols() {
+ if (_socket_ instanceof SSLSocket)
+ return ((SSLSocket)_socket_).getEnabledProtocols();
+ return null;
+ }
+
+ /**
+ * PBSZ command. pbsz value: 0 to (2^32)-1 decimal integer.
+ * @param pbsz Protection Buffer Size.
+ * @throws SSLException If it server reply code not equal "200".
+ * @throws IOException If an I/O error occurs while either sending
+ * the command.
+ */
+ public void execPBSZ(long pbsz) throws SSLException, IOException {
+ if (pbsz < 0 || 4294967295L < pbsz)
+ throw new IllegalArgumentException();
+ if (FTPReply.COMMAND_OK != sendCommand(
+ FTPSCommand._commands[FTPSCommand.PBSZ],String.valueOf(pbsz)))
+ throw new SSLException(getReplyString());
+ }
+
+ /**
+ * PROT command.
+ * C - Clear
+ * S - Safe(SSL protocol only)
+ * E - Confidential(SSL protocol only)
+ * P - Private
+ * @param prot Data Channel Protection Level.
+ * @throws SSLException If it server reply code not equal "200".
+ * @throws IOException If an I/O error occurs while either sending
+ * the command.
+ */
+ public void execPROT(String prot) throws SSLException, IOException {
+ if (prot == null) prot = DEFAULT_PROT;
+ if (!checkPROTValue(prot)) throw new IllegalArgumentException();
+ if (FTPReply.COMMAND_OK != sendCommand(
+ FTPSCommand._commands[FTPSCommand.PROT], prot))
+ throw new SSLException(getReplyString());
+ if (DEFAULT_PROT.equals(prot)) {
+ setSocketFactory(null);
+ setServerSocketFactory(null);
+ } else {
+ setSocketFactory(new FTPSSocketFactory(context));
+
+ initSslContext();
+
+ SSLServerSocketFactory ssf = context.getServerSocketFactory();
+
+ setServerSocketFactory(ssf);
+ }
+ }
+
+ /**
+ * I check the value that I can set in PROT Command value.
+ * @param prot Data Channel Protection Level.
+ * @return True - A set point is right / False - A set point is not right
+ */
+ private boolean checkPROTValue(String prot) {
+ for (int p = 0; p < PROT_COMMAND_VALUE.length; p++) {
+ if (PROT_COMMAND_VALUE[p].equals(prot)) return true;
+ }
+ return false;
+ }
+
+ /**
+ * I carry out an ftp command.
+ * When a CCC command was carried out, I steep socket and SocketFactory
+ * in a state of not ssl.
+ * @parm command ftp command.
+ * @return server reply.
+ * @throws IOException If an I/O error occurs while either sending
+ * the command.
+ * @see org.apache.commons.net.ftp.FTP#sendCommand(java.lang.String)
+ */
+ @Override
+ public int sendCommand(String command, String args) throws IOException {
+ int repCode = super.sendCommand(command, args);
+ if (FTPSCommand._commands[FTPSCommand.CCC].equals(command)) {
+ if (FTPReply.COMMAND_OK == repCode) {
+ // TODO Check this - is this necessary at all?
+ _socket_ = planeSocket;
+ setSocketFactory(null);
+ } else {
+ throw new SSLException(getReplyString());
+ }
+ }
+ return repCode;
+ }
+
+ /**
+ * Returns a socket of the data connection.
+ * Wrapped as an {@link SSLSocket}, which carries out handshake processing.
+ * @pram command The text representation of the FTP command to send.
+ * @param arg The arguments to the FTP command.
+ * If this parameter is set to null, then the command is sent with
+ * no argument.
+ * @return A Socket corresponding to the established data connection.
+ * Null is returned if an FTP protocol error is reported at any point
+ * during the establishment and initialization of the connection.
+ * @throws IOException If there is any problem with the connection.
+ * @see org.apache.commons.net.ftp.FTPClient#_openDataConnection_(java.lang.String, int)
+ */
+ @Override
+ protected Socket _openDataConnection_(int command, String arg)
+ throws IOException {
+ Socket socket = super._openDataConnection_(command, arg);
+ if (socket != null && socket instanceof SSLSocket) {
+ SSLSocket sslSocket = (SSLSocket)socket;
+ sslSocket.setUseClientMode(isClientMode);
+ sslSocket.setEnableSessionCreation(isCreation);
+ // server mode
+ if (!isClientMode) {
+ sslSocket.setNeedClientAuth(isNeedClientAuth);
+ sslSocket.setWantClientAuth(isWantClientAuth);
+ }
+ if (suites != null)
+ sslSocket.setEnabledCipherSuites(suites);
+ if (protocols != null)
+ sslSocket.setEnabledProtocols(protocols);
+ sslSocket.startHandshake();
+ }
+ return socket;
+ }
+
+ /**
+ * Get the currently configured {@link TrustManager}.
+ *
+ * @return A TrustManager instance.
+ */
+ public TrustManager getTrustManager() {
+ return trustManager;
+ }
+
+ /**
+ * Override the default {@link TrustManager} to use.
+ *
+ * @param trustManager The TrustManager implementation to set.
+ */
+ public void setTrustManager(TrustManager trustManager) {
+ this.trustManager = trustManager;
+ }
+
+
+
+}
diff --git a/src/org/apache/commons/net/ftp/FTPSCommand.java b/src/org/apache/commons/net/ftp/FTPSCommand.java
new file mode 100644
index 0000000..6fd2efa
--- /dev/null
+++ b/src/org/apache/commons/net/ftp/FTPSCommand.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.net.ftp;
+
+/**
+ * I acquire a command added in FTPS.
+ * @since 2.0
+ */
+public final class FTPSCommand {
+ public static final int AUTH = 0;
+ public static final int ADAT = 1;
+ public static final int PBSZ = 2;
+ public static final int PROT = 3;
+ public static final int CCC = 4;
+
+ public static final int AUTHENTICATION_SECURITY_MECHANISM = AUTH;
+ public static final int AUTHENTICATION_SECURITY_DATA = ADAT;
+ public static final int PROTECTION_BUFFER_SIZE = PBSZ;
+ public static final int DATA_CHANNEL_PROTECTION_LEVEL = PROT;
+ public static final int CLEAR_COMMAND_CHANNEL = CCC;
+
+ static final String[] _commands = {"AUTH","ADAT","PBSZ","PROT","CCC"};
+
+ /**
+ * Retrieve the FTPS command string corresponding to a specified
+ * command code.
+ *
+ * @param command The command code.
+ * @return The FTPS command string corresponding to a specified
+ * command code.
+ */
+ public static final String getCommand(int command) {
+ return _commands[command];
+ }
+}
diff --git a/src/org/apache/commons/net/ftp/FTPSSocketFactory.java b/src/org/apache/commons/net/ftp/FTPSSocketFactory.java
new file mode 100644
index 0000000..b7e1186
--- /dev/null
+++ b/src/org/apache/commons/net/ftp/FTPSSocketFactory.java
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.commons.net.ftp;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+import javax.net.SocketFactory;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLServerSocket;
+
+
+/**
+ *
+ * Implementation of org.apache.commons.net.SocketFactory
+ *
+ * @since 2.0
+ */
+public class FTPSSocketFactory extends SocketFactory {
+
+ private SSLContext context;
+
+ public FTPSSocketFactory(SSLContext context) {
+ this.context = context;
+ }
+
+ @Override
+ public Socket createSocket(String address, int port) throws UnknownHostException, IOException {
+ return this.context.getSocketFactory().createSocket(address, port);
+ }
+
+ @Override
+ public Socket createSocket(InetAddress address, int port) throws IOException {
+ return this.context.getSocketFactory().createSocket(address, port);
+ }
+
+ @Override
+ public Socket createSocket(String address, int port, InetAddress localAddress, int localPort) throws UnknownHostException, IOException {
+ return this.context.getSocketFactory().createSocket(address, port, localAddress, localPort);
+ }
+
+ @Override
+ public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
+ return this.context.getSocketFactory().createSocket(address, port, localAddress, localPort);
+ }
+
+ public ServerSocket createServerSocket(int port) throws IOException {
+ return this.init(this.context.getServerSocketFactory().createServerSocket(port));
+ }
+
+ public ServerSocket createServerSocket(int port, int backlog) throws IOException {
+ return this.init(this.context.getServerSocketFactory().createServerSocket(port, backlog));
+ }
+
+ public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException {
+ return this.init(this.context.getServerSocketFactory().createServerSocket(port, backlog, ifAddress));
+ }
+
+ public ServerSocket init(ServerSocket socket) throws IOException {
+ ((SSLServerSocket) socket).setUseClientMode(true);
+ return socket;
+ }
+}
diff --git a/src/org/apache/commons/net/ftp/FTPSTrustManager.java b/src/org/apache/commons/net/ftp/FTPSTrustManager.java
new file mode 100644
index 0000000..d1a2dd7
--- /dev/null
+++ b/src/org/apache/commons/net/ftp/FTPSTrustManager.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.net.ftp;
+
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+
+/**
+ * Custom {@link TrustManager} implementation.
+ *
+ * @version $Id: FTPSTrustManager.java 658520 2008-05-21 01:14:11Z sebb $
+ * @since 2.0
+ */
+public class FTPSTrustManager implements X509TrustManager
+{
+ /**
+ * No-op
+ */
+ public void checkClientTrusted(X509Certificate[] certificates, String authType)
+ {
+ return;
+ }
+
+ public void checkServerTrusted(X509Certificate[] certificates, String authType) throws CertificateException
+ {
+ for (int i = 0; i < certificates.length; ++i)
+ {
+ certificates[i].checkValidity();
+ }
+ }
+
+ public X509Certificate[] getAcceptedIssuers()
+ {
+ return null;
+ }
+}
diff --git a/src/org/apache/commons/net/ftp/parser/CompositeFileEntryParser.java b/src/org/apache/commons/net/ftp/parser/CompositeFileEntryParser.java
new file mode 100644
index 0000000..4977b3f
--- /dev/null
+++ b/src/org/apache/commons/net/ftp/parser/CompositeFileEntryParser.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.net.ftp.parser;
+
+import org.apache.commons.net.ftp.FTPFile;
+import org.apache.commons.net.ftp.FTPFileEntryParser;
+import org.apache.commons.net.ftp.FTPFileEntryParserImpl;
+
+/**
+ * This implementation allows to pack some FileEntryParsers together
+ * and handle the case where to returned dirstyle isnt clearly defined.
+ * The matching parser will be cached.
+ * If the cached parser wont match due to the server changed the dirstyle,
+ * a new matching parser will be searched.
+ *
+ * @author Mario Ivankovits
+ * This abstract class implements the common timestamp parsing
+ * algorithm for all the concrete parsers. Classes derived from
+ * this one will parse file listings via a supplied regular expression
+ * that pulls out the date portion as a separate string which is
+ * passed to the underlying {@link FTPTimestampParser delegate} to
+ * handle parsing of the file timestamp.
+ *
+ * This class also implements the {@link Configurable Configurable}
+ * interface to allow the parser to be configured from the outside.
+ *
+ * @param timestampStr the timestamp string pulled from the
+ * file listing by the regular expression parser, to be submitted
+ * to the Implementation extracts a key from the supplied
+ * {@link FTPClientConfig FTPClientConfig}
+ * parameter and creates an object implementing the
+ * interface FTPFileEntryParser and uses the supplied configuration
+ * to configure it.
+ *
+ * Note that this method will generally not be called in scenarios
+ * that call for autodetection of parser type but rather, for situations
+ * where the user knows that the server uses a non-default configuration
+ * and knows what that configuration is.
+ *
+ * Note: EnterpriseUnixFTPEntryParser can only be instantiated through the
+ * DefaultFTPParserFactory by classname. It will not be chosen
+ * by the autodetection scheme.
+ *
+ * @version $Id: EnterpriseUnixFTPEntryParser.java 658518 2008-05-21 01:04:30Z sebb $
+ * @author Winston Ojeda
+ * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
+ * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
+ */
+public class EnterpriseUnixFTPEntryParser extends RegexFTPFileEntryParserImpl
+{
+
+ /**
+ * months abbreviations looked for by this parser. Also used
+ * to determine which month has been matched by the parser.
+ */
+ private static final String MONTHS =
+ "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)";
+
+ /**
+ * this is the regular expression used by this parser.
+ */
+ private static final String REGEX =
+ "(([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])"
+ + "([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z])([\\-]|[A-Z]))"
+ + "(\\S*)\\s*"
+ + "(\\S+)\\s*"
+ + "(\\S*)\\s*"
+ + "(\\d*)\\s*"
+ + "(\\d*)\\s*"
+ + MONTHS
+ + "\\s*"
+ + "((?:[012]\\d*)|(?:3[01]))\\s*"
+ + "((\\d\\d\\d\\d)|((?:[01]\\d)|(?:2[0123])):([012345]\\d))\\s"
+ + "(\\S*)(\\s*.*)";
+
+ /**
+ * The sole constructor for a EnterpriseUnixFTPEntryParser object.
+ *
+ */
+ public EnterpriseUnixFTPEntryParser()
+ {
+ super(REGEX);
+ }
+
+ /**
+ * Parses a line of a unix FTP server file listing and converts it into a
+ * usable format in the form of an
+ * Implementation should be a method that extracts
+ * a key from the supplied {@link FTPClientConfig FTPClientConfig}
+ * parameter and creates an object implementing the
+ * interface FTPFileEntryParser and uses the supplied configuration
+ * to configure it.
+ *
+ * Note that this method will generally not be called in scenarios
+ * that call for autodetection of parser type but rather, for situations
+ * where the user knows that the server uses a non-default configuration
+ * and knows what that configuration is.
+ *
+ * Set up the {@link FTPClientConfig#setDefaultDateFormatStr(java.lang.String) defaultDateFormat}
+ * and optionally the {@link FTPClientConfig#setRecentDateFormatStr(String) recentDateFormat}
+ * to values supplied in the config based on month names configured as follows:
+ *
+ * Finally if a {@link org.apache.commons.net.ftp.FTPClientConfig#setServerTimeZoneId(String) serverTimeZoneId}
+ * has been supplied via the config, set that into all date formats that have
+ * been configured.
+ *
+ * @param entry A line of text from the file listing
+ * @return An FTPFile instance corresponding to the supplied entry
+ */
+ public FTPFile parseFTPEntry(String entry)
+ {
+ FTPFile f = new FTPFile();
+ f.setRawListing(entry);
+
+ if (matches(entry))
+ {
+ String datestr = group(1)+" "+group(2);
+ String dirString = group(3);
+ String size = group(4);
+ String name = group(5);
+ try
+ {
+ f.setTimestamp(super.parseTimestamp(datestr));
+ }
+ catch (ParseException e)
+ {
+ // intentionally do nothing
+ }
+
+ if (null == name || name.equals(".") || name.equals(".."))
+ {
+ return (null);
+ }
+ f.setName(name);
+
+
+ if ("
+ *
+ * Netware file permissions are in the following format: RWCEAFMS, and are explained as follows:
+ *
+ * @param entry A line of text from the file listing
+ * @return An FTPFile instance corresponding to the supplied entry
+ */
+ public FTPFile parseFTPEntry(String entry)
+ {
+
+ FTPFile f = new FTPFile();
+ if (matches(entry))
+ {
+ String size = group(1);
+ String attrib = group(2);
+ String dirString = group(3);
+ String datestr = group(4)+" "+group(5);
+ String name = group(6);
+ try
+ {
+ f.setTimestamp(super.parseTimestamp(datestr));
+ }
+ catch (ParseException e)
+ {
+ // intentionally do nothing
+ }
+
+
+ //is it a DIR or a file
+ if (dirString.trim().equals("DIR") || attrib.trim().equals("DIR"))
+ {
+ f.setType(FTPFile.DIRECTORY_TYPE);
+ }
+ else
+ {
+ f.setType(FTPFile.FILE_TYPE);
+ }
+
+
+ //set the name
+ f.setName(name.trim());
+
+ //set the size
+ f.setSize(Long.parseLong(size.trim()));
+
+ return (f);
+ }
+ return null;
+
+ }
+
+ /**
+ * Defines a default configuration to be used when this class is
+ * instantiated without a {@link FTPClientConfig FTPClientConfig}
+ * parameter being specified.
+ * @return the default configuration for this parser.
+ */
+ @Override
+ protected FTPClientConfig getDefaultConfiguration() {
+ return new FTPClientConfig(
+ FTPClientConfig.SYST_OS2,
+ DEFAULT_DATE_FORMAT,
+ null, null, null, null);
+ }
+
+}
diff --git a/src/org/apache/commons/net/ftp/parser/OS400FTPEntryParser.java b/src/org/apache/commons/net/ftp/parser/OS400FTPEntryParser.java
new file mode 100644
index 0000000..66c370b
--- /dev/null
+++ b/src/org/apache/commons/net/ftp/parser/OS400FTPEntryParser.java
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.net.ftp.parser;
+
+import java.text.ParseException;
+
+import org.apache.commons.net.ftp.FTPClientConfig;
+import org.apache.commons.net.ftp.FTPFile;
+
+/**
+ * @version $Id: OS400FTPEntryParser.java 658518 2008-05-21 01:04:30Z sebb $
+ */
+
+public class OS400FTPEntryParser extends ConfigurableFTPFileEntryParserImpl
+{
+ private static final String DEFAULT_DATE_FORMAT
+ = "yy/MM/dd HH:mm:ss"; //01/11/09 12:30:24
+
+
+
+ private static final String REGEX =
+ "(\\S+)\\s+" // user
+ + "(\\d+)\\s+" // size
+ + "(\\S+)\\s+(\\S+)\\s+" // date stuff
+ + "(\\*\\S+)\\s+" // *STMF/*DIR
+ + "(\\S+/?)\\s*"; // filename
+
+
+ /**
+ * The default constructor for a OS400FTPEntryParser object.
+ *
+ * @exception IllegalArgumentException
+ * Thrown if the regular expression is unparseable. Should not be seen
+ * under normal conditions. It it is seen, this is a sign that
+ *
+ * For now end users may specify this format only via
+ *
+ * @param entry A line of text from the file listing
+ * @return An FTPFile instance corresponding to the supplied entry
+ */
+ public FTPFile parseFTPEntry(String entry) {
+ FTPFile file = new FTPFile();
+ file.setRawListing(entry);
+ int type;
+ boolean isDevice = false;
+
+ if (matches(entry))
+ {
+ String typeStr = group(1);
+ String hardLinkCount = group(15);
+ String usr = group(16);
+ String grp = group(17);
+ String filesize = group(18);
+ String datestr = group(19) + " " + group(20);
+ String name = group(21);
+ String endtoken = group(22);
+
+ try
+ {
+ file.setTimestamp(super.parseTimestamp(datestr));
+ }
+ catch (ParseException e)
+ {
+ // intentionally do nothing
+ }
+
+
+ // bcdlfmpSs-
+ switch (typeStr.charAt(0))
+ {
+ case 'd':
+ type = FTPFile.DIRECTORY_TYPE;
+ break;
+ case 'e':
+ type = FTPFile.SYMBOLIC_LINK_TYPE;
+ break;
+ case 'l':
+ type = FTPFile.SYMBOLIC_LINK_TYPE;
+ break;
+ case 'b':
+ case 'c':
+ isDevice = true;
+ // break; - fall through
+ case 'f':
+ case '-':
+ type = FTPFile.FILE_TYPE;
+ break;
+ default:
+ type = FTPFile.UNKNOWN_TYPE;
+ }
+
+ file.setType(type);
+
+ int g = 4;
+ for (int access = 0; access < 3; access++, g += 4)
+ {
+ // Use != '-' to avoid having to check for suid and sticky bits
+ file.setPermission(access, FTPFile.READ_PERMISSION,
+ (!group(g).equals("-")));
+ file.setPermission(access, FTPFile.WRITE_PERMISSION,
+ (!group(g + 1).equals("-")));
+
+ String execPerm = group(g + 2);
+ if (!execPerm.equals("-") && !Character.isUpperCase(execPerm.charAt(0)))
+ {
+ file.setPermission(access, FTPFile.EXECUTE_PERMISSION, true);
+ }
+ else
+ {
+ file.setPermission(access, FTPFile.EXECUTE_PERMISSION, false);
+ }
+ }
+
+ if (!isDevice)
+ {
+ try
+ {
+ file.setHardLinkCount(Integer.parseInt(hardLinkCount));
+ }
+ catch (NumberFormatException e)
+ {
+ // intentionally do nothing
+ }
+ }
+
+ file.setUser(usr);
+ file.setGroup(grp);
+
+ try
+ {
+ file.setSize(Long.parseLong(filesize));
+ }
+ catch (NumberFormatException e)
+ {
+ // intentionally do nothing
+ }
+
+ if (null == endtoken)
+ {
+ file.setName(name);
+ }
+ else
+ {
+ // oddball cases like symbolic links, file names
+ // with spaces in them.
+ name += endtoken;
+ if (type == FTPFile.SYMBOLIC_LINK_TYPE)
+ {
+
+ int end = name.indexOf(" -> ");
+ // Give up if no link indicator is present
+ if (end == -1)
+ {
+ file.setName(name);
+ }
+ else
+ {
+ file.setName(name.substring(0, end));
+ file.setLink(name.substring(end + 4));
+ }
+
+ }
+ else
+ {
+ file.setName(name);
+ }
+ }
+ return file;
+ }
+ return null;
+ }
+
+ /**
+ * Defines a default configuration to be used when this class is
+ * instantiated without a {@link FTPClientConfig FTPClientConfig}
+ * parameter being specified.
+ * @return the default configuration for this parser.
+ */
+ @Override
+ protected FTPClientConfig getDefaultConfiguration() {
+ return new FTPClientConfig(
+ FTPClientConfig.SYST_UNIX,
+ DEFAULT_DATE_FORMAT,
+ DEFAULT_RECENT_DATE_FORMAT,
+ null, null, null);
+ }
+
+}
diff --git a/src/org/apache/commons/net/ftp/parser/VMSFTPEntryParser.java b/src/org/apache/commons/net/ftp/parser/VMSFTPEntryParser.java
new file mode 100644
index 0000000..1e55ede
--- /dev/null
+++ b/src/org/apache/commons/net/ftp/parser/VMSFTPEntryParser.java
@@ -0,0 +1,288 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.net.ftp.parser;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.text.ParseException;
+import java.util.StringTokenizer;
+
+import org.apache.commons.net.ftp.FTPClientConfig;
+import org.apache.commons.net.ftp.FTPFile;
+import org.apache.commons.net.ftp.FTPListParseEngine;
+
+/**
+ * Implementation FTPFileEntryParser and FTPFileListParser for VMS Systems.
+ * This is a sample of VMS LIST output
+ *
+ * "1-JUN.LIS;1 9/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,RE)",
+ * "1-JUN.LIS;2 9/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,RE)",
+ * "DATA.DIR;1 1/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,RE)",
+ *
+ * Note: VMSFTPEntryParser can only be instantiated through the
+ * DefaultFTPParserFactory by classname. It will not be chosen
+ * by the autodetection scheme.
+ *
+ *
+ *
+ * @author Winston Ojeda
+ * @author Steve Cohen
+ * @author Stephane ESTE-GRACIAS
+ * @version $Id: VMSFTPEntryParser.java 658518 2008-05-21 01:04:30Z sebb $
+ *
+ * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
+ * @see org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory
+ */
+public class VMSFTPEntryParser extends ConfigurableFTPFileEntryParserImpl
+{
+
+ private static final String DEFAULT_DATE_FORMAT
+ = "d-MMM-yyyy HH:mm:ss"; //9-NOV-2001 12:30:24
+
+ /**
+ * this is the regular expression used by this parser.
+ */
+ private static final String REGEX =
+ "(.*;[0-9]+)\\s*" //1 file and version
+ + "(\\d+)/\\d+\\s*" //2 size/allocated
+ +"(\\S+)\\s+(\\S+)\\s+" //3+4 date and time
+ + "\\[(([0-9$A-Za-z_]+)|([0-9$A-Za-z_]+),([0-9$a-zA-Z_]+))\\]?\\s*" //5(6,7,8) owner
+ + "\\([a-zA-Z]*,([a-zA-Z]*),([a-zA-Z]*),([a-zA-Z]*)\\)"; //9,10,11 Permissions (O,G,W)
+ // TODO - perhaps restrict permissions to [RWED]* ?
+
+
+
+ /**
+ * Constructor for a VMSFTPEntryParser object.
+ *
+ * @exception IllegalArgumentException
+ * Thrown if the regular expression is unparseable. Should not be seen
+ * under normal conditions. It it is seen, this is a sign that
+ *
+ * @param listStream The InputStream from which the file list should be
+ * read.
+ * @return The list of file information contained in the given path. null
+ * if the list could not be obtained or if there are no files in
+ * the directory.
+ * @exception IOException If an I/O error occurs reading the listStream.
+ ***/
+ public FTPFile[] parseFileList(InputStream listStream) throws IOException {
+ FTPListParseEngine engine = new FTPListParseEngine(this);
+ engine.readServerList(listStream);
+ return engine.getFiles();
+ }
+
+
+
+ /**
+ * Parses a line of a VMS FTP server file listing and converts it into a
+ * usable format in the form of an
+ * @param entry A line of text from the file listing
+ * @return An FTPFile instance corresponding to the supplied entry
+ */
+ public FTPFile parseFTPEntry(String entry)
+ {
+ //one block in VMS equals 512 bytes
+ long longBlock = 512;
+
+ if (matches(entry))
+ {
+ FTPFile f = new FTPFile();
+ f.setRawListing(entry);
+ String name = group(1);
+ String size = group(2);
+ String datestr = group(3)+" "+group(4);
+ String owner = group(5);
+ String permissions[] = new String[3];
+ permissions[0]= group(9);
+ permissions[1]= group(10);
+ permissions[2]= group(11);
+ try
+ {
+ f.setTimestamp(super.parseTimestamp(datestr));
+ }
+ catch (ParseException e)
+ {
+ // intentionally do nothing
+ }
+
+
+ String grp;
+ String user;
+ StringTokenizer t = new StringTokenizer(owner, ",");
+ switch (t.countTokens()) {
+ case 1:
+ grp = null;
+ user = t.nextToken();
+ break;
+ case 2:
+ grp = t.nextToken();
+ user = t.nextToken();
+ break;
+ default:
+ grp = null;
+ user = null;
+ }
+
+ if (name.lastIndexOf(".DIR") != -1)
+ {
+ f.setType(FTPFile.DIRECTORY_TYPE);
+ }
+ else
+ {
+ f.setType(FTPFile.FILE_TYPE);
+ }
+ //set FTPFile name
+ //Check also for versions to be returned or not
+ if (isVersioning())
+ {
+ f.setName(name);
+ }
+ else
+ {
+ name = name.substring(0, name.lastIndexOf(";"));
+ f.setName(name);
+ }
+ //size is retreived in blocks and needs to be put in bytes
+ //for us humans and added to the FTPFile array
+ long sizeInBytes = Long.parseLong(size) * longBlock;
+ f.setSize(sizeInBytes);
+
+ f.setGroup(grp);
+ f.setUser(user);
+ //set group and owner
+
+ //Set file permission.
+ //VMS has (SYSTEM,OWNER,GROUP,WORLD) users that can contain
+ //R (read) W (write) E (execute) D (delete)
+
+ //iterate for OWNER GROUP WORLD permissions
+ for (int access = 0; access < 3; access++)
+ {
+ String permission = permissions[access];
+
+ f.setPermission(access, FTPFile.READ_PERMISSION, permission.indexOf('R')>=0);
+ f.setPermission(access, FTPFile.WRITE_PERMISSION, permission.indexOf('W')>=0);
+ f.setPermission(access, FTPFile.EXECUTE_PERMISSION, permission.indexOf('E')>=0);
+ }
+
+ return f;
+ }
+ return null;
+ }
+
+
+ /**
+ * Reads the next entry using the supplied BufferedReader object up to
+ * whatever delemits one entry from the next. This parser cannot use
+ * the default implementation of simply calling BufferedReader.readLine(),
+ * because one entry may span multiple lines.
+ *
+ * @param reader The BufferedReader object from which entries are to be
+ * read.
+ *
+ * @return A string representing the next ftp entry or null if none found.
+ * @exception IOException thrown on any IO Error reading from the reader.
+ */
+ @Override
+ public String readNextEntry(BufferedReader reader) throws IOException
+ {
+ String line = reader.readLine();
+ StringBuffer entry = new StringBuffer();
+ while (line != null)
+ {
+ if (line.startsWith("Directory") || line.startsWith("Total")) {
+ line = reader.readLine();
+ continue;
+ }
+
+ entry.append(line);
+ if (line.trim().endsWith(")"))
+ {
+ break;
+ }
+ line = reader.readLine();
+ }
+ return (entry.length() == 0 ? null : entry.toString());
+ }
+
+ protected boolean isVersioning() {
+ return false;
+ }
+
+ /**
+ * Defines a default configuration to be used when this class is
+ * instantiated without a {@link FTPClientConfig FTPClientConfig}
+ * parameter being specified.
+ * @return the default configuration for this parser.
+ */
+ @Override
+ protected FTPClientConfig getDefaultConfiguration() {
+ return new FTPClientConfig(
+ FTPClientConfig.SYST_VMS,
+ DEFAULT_DATE_FORMAT,
+ null, null, null, null);
+ }
+
+
+}
+
+/* Emacs configuration
+ * Local variables: **
+ * mode: java **
+ * c-basic-offset: 4 **
+ * indent-tabs-mode: nil **
+ * End: **
+ */
diff --git a/src/org/apache/commons/net/ftp/parser/VMSVersioningFTPEntryParser.java b/src/org/apache/commons/net/ftp/parser/VMSVersioningFTPEntryParser.java
new file mode 100644
index 0000000..cb25709
--- /dev/null
+++ b/src/org/apache/commons/net/ftp/parser/VMSVersioningFTPEntryParser.java
@@ -0,0 +1,183 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.net.ftp.parser;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.regex.MatchResult;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
+
+import org.apache.commons.net.ftp.FTPClientConfig;
+
+/**
+ * Special implementation VMSFTPEntryParser with versioning turned on.
+ * This parser removes all duplicates and only leaves the version with the highest
+ * version number for each filename.
+ *
+ * This is a sample of VMS LIST output
+ *
+ * "1-JUN.LIS;1 9/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,RE)",
+ * "1-JUN.LIS;2 9/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,RE)",
+ * "DATA.DIR;1 1/9 2-JUN-1998 07:32:04 [GROUP,OWNER] (RWED,RWED,RWED,RE)",
+ *
+ *
+ * @author Winston Ojeda
+ * @author Stephane ESTE-GRACIAS
+ * @version $Id: VMSVersioningFTPEntryParser.java 636854 2008-03-13 19:55:01Z sebb $
+ *
+ * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
+ */
+public class VMSVersioningFTPEntryParser extends VMSFTPEntryParser
+{
+
+ private Matcher _preparse_matcher_;
+ private Pattern _preparse_pattern_;
+ private static final String PRE_PARSE_REGEX =
+ "(.*);([0-9]+)\\s*.*";
+
+ /**
+ * Constructor for a VMSFTPEntryParser object. Sets the versioning member
+ * to the supplied value.
+ *
+ * @exception IllegalArgumentException
+ * Thrown if the regular expression is unparseable. Should not be seen
+ * under normal conditions. It it is seen, this is a sign that
+ * original
unmodified.
+ */
+ public ListreadNextEntry()
- which handles the issue of
+ * what delimits one entry from another, usually but not always a line
+ * feed and preParse()
- which handles removal of
+ * extraneous matter such as the preliminary lines of a listing, removal
+ * of duplicates on versioning systems, etc.
+ * getNext()
and
+ * getPrevious()
methods to provide "paged" output of less
+ * than the whole list at one time, or by calling the
+ * getFiles()
method to return the entire list.
+ *
+ * FTPClient f=FTPClient();
+ * f.connect(server);
+ * f.login(username, password);
+ * FTPListParseEngine engine = f.initiateListParsing(directory);
+ *
+ * while (engine.hasNext()) {
+ * FTPFile[] files = engine.getNext(25); // "page size" you want
+ * //do whatever you want with these files, display them, etc.
+ * //expensive FTPFile objects not created until needed.
+ * }
+ *
+ * entries
list.
+ * After this method has completed, entries
will contain a
+ * collection of entries (as defined by
+ * FTPFileEntryParser.readNextEntry()
), but this may contain
+ * various non-entry preliminary lines from the server output, duplicates,
+ * and other data that will not be part of the final listing.
+ *
+ * @param stream The socket stream on which the input will be read.
+ * @param encoding The encoding to use.
+ *
+ * @exception IOException
+ * thrown on any failure to read the stream
+ */
+ private void readStream(InputStream stream, String encoding) throws IOException
+ {
+ BufferedReader reader;
+ if (encoding == null)
+ {
+ reader = new BufferedReader(new InputStreamReader(stream));
+ }
+ else
+ {
+ reader = new BufferedReader(new InputStreamReader(stream, encoding));
+ }
+
+ String line = this.parser.readNextEntry(reader);
+
+ while (line != null)
+ {
+ this.entries.add(line);
+ line = this.parser.readNextEntry(reader);
+ }
+ reader.close();
+ }
+
+ /**
+ * Returns an array of at most quantityRequested
FTPFile
+ * objects starting at this object's internal iterator's current position.
+ * If fewer than quantityRequested
such
+ * elements are available, the returned array will have a length equal
+ * to the number of entries at and after after the current position.
+ * If no such entries are found, this array will have a length of 0.
+ *
+ * After this method is called this object's internal iterator is advanced
+ * by a number of positions equal to the size of the array returned.
+ *
+ * @param quantityRequested
+ * the maximum number of entries we want to get.
+ *
+ * @return an array of at most quantityRequested
FTPFile
+ * objects starting at the current position of this iterator within its
+ * list and at least the number of elements which exist in the list at
+ * and after its current position.
+ * quantityRequested
FTPFile
+ * objects starting at this object's internal iterator's current position,
+ * and working back toward the beginning.
+ *
+ * If fewer than quantityRequested
such
+ * elements are available, the returned array will have a length equal
+ * to the number of entries at and after after the current position.
+ * If no such entries are found, this array will have a length of 0.
+ *
+ * After this method is called this object's internal iterator is moved
+ * back by a number of positions equal to the size of the array returned.
+ *
+ * @param quantityRequested
+ * the maximum number of entries we want to get.
+ *
+ * @return an array of at most quantityRequested
FTPFile
+ * objects starting at the current position of this iterator within its
+ * list and at least the number of elements which exist in the list at
+ * and after its current position. This array will be in the same order
+ * as the underlying list (not reversed).
+ * timestampParser
for extracting the timestamp.
+ * @return a java.util.Calendar
containing results of the
+ * timestamp parse.
+ */
+ public Calendar parseTimestamp(String timestampStr) throws ParseException {
+ return this.timestampParser.parseTimestamp(timestampStr);
+ }
+
+
+ /**
+ * Implementation of the {@link Configurable Configurable}
+ * interface. Configures this parser by delegating to the
+ * underlying Configurable FTPTimestampParser implementation, '
+ * passing it the supplied {@link FTPClientConfig FTPClientConfig}
+ * if that is non-null or a default configuration defined by
+ * each concrete subclass.
+ * key
is not recognized as a fully qualified
+ * classname known to the system, this method will then attempt
+ * to see whether it contains a string identifying one of
+ * the known parsers. This comparison is case-insensitive.
+ * The intent here is where possible, to select as keys strings
+ * which are returned by the SYST command on the systems which
+ * the corresponding parser successfully parses. This enables
+ * this factory to be used in the auto-detection system.
+ *
+ *
+ * @param key should be a fully qualified classname corresponding to
+ * a class implementing the FTPFileEntryParser interface
+ * OR
+ * a string containing (case-insensitively) one of the
+ * following keywords:
+ *
+ *
+ * @return the FTPFileEntryParser corresponding to the supplied key.
+ * @throws ParserInitializationException thrown if for any reason the factory cannot resolve
+ * the supplied key into an FTPFileEntryParser.
+ * @see FTPFileEntryParser
+ */
+ public FTPFileEntryParser createFileEntryParser(String key)
+ {
+ if (key == null)
+ throw new ParserInitializationException("Parser key cannot be null");
+
+ Class> parserClass = null;
+ FTPFileEntryParser parser = null;
+ try
+ {
+ parserClass = Class.forName(key);
+ parser = (FTPFileEntryParser) parserClass.newInstance();
+ }
+ catch (ClassNotFoundException e)
+ {
+ try
+ {
+ String ukey = null;
+ if (null != key)
+ {
+ ukey = key.toUpperCase(java.util.Locale.ENGLISH);
+ }
+ if ((ukey.indexOf(FTPClientConfig.SYST_UNIX) >= 0)
+ || (ukey.indexOf(FTPClientConfig.SYST_L8) >= 0))
+ {
+ parser = createUnixFTPEntryParser();
+ }
+ else if (ukey.indexOf(FTPClientConfig.SYST_VMS) >= 0)
+ {
+ parser = createVMSVersioningFTPEntryParser();
+ }
+ else if (ukey.indexOf(FTPClientConfig.SYST_NT) >= 0)
+ {
+ parser = createNTFTPEntryParser();
+ }
+ else if (ukey.indexOf(FTPClientConfig.SYST_OS2) >= 0)
+ {
+ parser = createOS2FTPEntryParser();
+ }
+ else if (ukey.indexOf(FTPClientConfig.SYST_OS400) >= 0 ||
+ ukey.indexOf(FTPClientConfig.SYST_AS400) >= 0)
+ {
+ parser = createOS400FTPEntryParser();
+ }
+ else if (ukey.indexOf(FTPClientConfig.SYST_MVS) >= 0)
+ {
+ parser = createMVSEntryParser();
+ }
+ else if (ukey.indexOf(FTPClientConfig.SYST_NETWARE) >= 0)
+ {
+ parser = createNetwareFTPEntryParser();
+ }
+ else
+ {
+ throw new ParserInitializationException("Unknown parser type: " + key);
+ }
+ }
+ catch (NoClassDefFoundError nf) {
+ throw new ParserInitializationException("Error initializing parser", nf);
+ }
+
+ }
+ catch (NoClassDefFoundError e)
+ {
+ throw new ParserInitializationException("Error initializing parser", e);
+ }
+ catch (ClassCastException e)
+ {
+ throw new ParserInitializationException(parserClass.getName()
+ + " does not implement the interface "
+ + "org.apache.commons.net.ftp.FTPFileEntryParser.", e);
+ }
+ catch (Throwable e)
+ {
+ throw new ParserInitializationException("Error initializing parser", e);
+ }
+
+ if (parser instanceof Configurable) {
+ ((Configurable)parser).configure(this.config);
+ }
+ return parser;
+ }
+
+ /**
+ * FTPFile
instance. If
+ * the file listing line doesn't describe a file, null
is
+ * returned, otherwise a FTPFile
instance representing the
+ * files in the directory is returned.
+ *
+ * @param entry A line of text from the file listing
+ * @return An FTPFile instance corresponding to the supplied entry
+ */
+ public FTPFile parseFTPEntry(String entry)
+ {
+
+ FTPFile file = new FTPFile();
+ file.setRawListing(entry);
+
+ if (matches(entry))
+ {
+ String usr = group(14);
+ String grp = group(15);
+ String filesize = group(16);
+ String mo = group(17);
+ String da = group(18);
+ String yr = group(20);
+ String hr = group(21);
+ String min = group(22);
+ String name = group(23);
+
+ file.setType(FTPFile.FILE_TYPE);
+ file.setUser(usr);
+ file.setGroup(grp);
+ try
+ {
+ file.setSize(Long.parseLong(filesize));
+ }
+ catch (NumberFormatException e)
+ {
+ // intentionally do nothing
+ }
+
+ Calendar cal = Calendar.getInstance();
+ cal.set(Calendar.MILLISECOND, 0);
+ cal.set(Calendar.SECOND,
+ 0);
+ cal.set(Calendar.MINUTE,
+ 0);
+ cal.set(Calendar.HOUR_OF_DAY,
+ 0);
+ try
+ {
+
+ int pos = MONTHS.indexOf(mo);
+ int month = pos / 4;
+ if (yr != null)
+ {
+ // it's a year
+ cal.set(Calendar.YEAR,
+ Integer.parseInt(yr));
+ }
+ else
+ {
+ // it must be hour/minute or we wouldn't have matched
+ int year = cal.get(Calendar.YEAR);
+
+ // if the month we're reading is greater than now, it must
+ // be last year
+ if (cal.get(Calendar.MONTH) < month)
+ {
+ year--;
+ }
+ cal.set(Calendar.YEAR,
+ year);
+ cal.set(Calendar.HOUR_OF_DAY,
+ Integer.parseInt(hr));
+ cal.set(Calendar.MINUTE,
+ Integer.parseInt(min));
+ }
+ cal.set(Calendar.MONTH,
+ month);
+ cal.set(Calendar.DATE,
+ Integer.parseInt(da));
+ file.setTimestamp(cal);
+ }
+ catch (NumberFormatException e)
+ {
+ // do nothing, date will be uninitialized
+ }
+ file.setName(name);
+
+ return file;
+ }
+ return null;
+ }
+}
diff --git a/src/org/apache/commons/net/ftp/parser/FTPFileEntryParserFactory.java b/src/org/apache/commons/net/ftp/parser/FTPFileEntryParserFactory.java
new file mode 100644
index 0000000..e80ec0d
--- /dev/null
+++ b/src/org/apache/commons/net/ftp/parser/FTPFileEntryParserFactory.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.net.ftp.parser;
+import org.apache.commons.net.ftp.FTPClientConfig;
+import org.apache.commons.net.ftp.FTPFileEntryParser;
+
+/**
+ * The interface describes a factory for creating FTPFileEntryParsers.
+ * @since 1.2
+ */
+public interface FTPFileEntryParserFactory
+{
+ /**
+ * Implementation should be a method that decodes the
+ * supplied key and creates an object implementing the
+ * interface FTPFileEntryParser.
+ *
+ * @param key A string that somehow identifies an
+ * FTPFileEntryParser to be created.
+ *
+ * @return the FTPFileEntryParser created.
+ * @exception ParserInitializationException
+ * Thrown on any exception in instantiation
+ */
+ public FTPFileEntryParser createFileEntryParser(String key)
+ throws ParserInitializationException;
+
+ /**
+ *java.util.Calendar
object initialized to the date
+ * parsed by the parser
+ * @throws ParseException if none of the parser mechanisms belonging to
+ * the implementor can parse the input.
+ */
+ public Calendar parseTimestamp(String timestampStr) throws ParseException;
+
+}
diff --git a/src/org/apache/commons/net/ftp/parser/FTPTimestampParserImpl.java b/src/org/apache/commons/net/ftp/parser/FTPTimestampParserImpl.java
new file mode 100644
index 0000000..02a0cc8
--- /dev/null
+++ b/src/org/apache/commons/net/ftp/parser/FTPTimestampParserImpl.java
@@ -0,0 +1,301 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.net.ftp.parser;
+
+import java.text.DateFormatSymbols;
+import java.text.ParseException;
+import java.text.ParsePosition;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.TimeZone;
+
+import org.apache.commons.net.ftp.Configurable;
+import org.apache.commons.net.ftp.FTPClientConfig;
+
+/**
+ * Default implementation of the {@link FTPTimestampParser FTPTimestampParser}
+ * interface also implements the {@link org.apache.commons.net.ftp.Configurable Configurable}
+ * interface to allow the parsing to be configured from the outside.
+ *
+ * @see ConfigurableFTPFileEntryParserImpl
+ * @since 1.4
+ */
+public class FTPTimestampParserImpl implements
+ FTPTimestampParser, Configurable
+{
+
+
+ private SimpleDateFormat defaultDateFormat;
+ private SimpleDateFormat recentDateFormat;
+ private boolean lenientFutureDates = false;
+
+
+ /**
+ * The only constructor for this class.
+ */
+ public FTPTimestampParserImpl() {
+ setDefaultDateFormat(DEFAULT_SDF);
+ setRecentDateFormat(DEFAULT_RECENT_SDF);
+ }
+
+ /**
+ * Implements the one {@link FTPTimestampParser#parseTimestamp(String) method}
+ * in the {@link FTPTimestampParser FTPTimestampParser} interface
+ * according to this algorithm:
+ *
+ * If the recentDateFormat member has been defined, try to parse the
+ * supplied string with that. If that parse fails, or if the recentDateFormat
+ * member has not been defined, attempt to parse with the defaultDateFormat
+ * member. If that fails, throw a ParseException.
+ *
+ * This method allows a {@link Calendar} instance to be passed in which represents the
+ * current (system) time.
+ *
+ * @see org.apache.commons.net.ftp.parser.FTPTimestampParser#parseTimestamp(java.lang.String)
+ *
+ * @param timestampStr The timestamp to be parsed
+ */
+ public Calendar parseTimestamp(String timestampStr) throws ParseException {
+ Calendar now = Calendar.getInstance();
+ return parseTimestamp(timestampStr, now);
+ }
+
+ /**
+ * Implements the one {@link FTPTimestampParser#parseTimestamp(String) method}
+ * in the {@link FTPTimestampParser FTPTimestampParser} interface
+ * according to this algorithm:
+ *
+ * If the recentDateFormat member has been defined, try to parse the
+ * supplied string with that. If that parse fails, or if the recentDateFormat
+ * member has not been defined, attempt to parse with the defaultDateFormat
+ * member. If that fails, throw a ParseException.
+ *
+ * @see org.apache.commons.net.ftp.parser.FTPTimestampParser#parseTimestamp(java.lang.String)
+ * @param timestampStr The timestamp to be parsed
+ * @param serverTime The current time for the server
+ * @since 1.5
+ */
+ public Calendar parseTimestamp(String timestampStr, Calendar serverTime) throws ParseException {
+ Calendar now = (Calendar) serverTime.clone();// Copy this, because we may change it
+ now.setTimeZone(this.getServerTimeZone());
+ Calendar working = (Calendar) now.clone();
+ working.setTimeZone(getServerTimeZone());
+ ParsePosition pp = new ParsePosition(0);
+
+ Date parsed = null;
+ if (recentDateFormat != null) {
+ if (lenientFutureDates) {
+ // add a day to "now" so that "slop" doesn't cause a date
+ // slightly in the future to roll back a full year. (Bug 35181)
+ now.add(Calendar.DATE, 1);
+ }
+ parsed = recentDateFormat.parse(timestampStr, pp);
+ }
+ if (parsed != null && pp.getIndex() == timestampStr.length())
+ {
+ working.setTime(parsed);
+ working.set(Calendar.YEAR, now.get(Calendar.YEAR));
+
+ if (working.after(now)) {
+ working.add(Calendar.YEAR, -1);
+ }
+ } else {
+ // Temporarily add the current year to the short date time
+ // to cope with short-date leap year strings.
+ // e.g. Java's DateFormatter will assume that "Feb 29 12:00" refers to
+ // Feb 29 1970 (an invalid date) rather than a potentially valid leap year date.
+ // This is pretty bad hack to work around the deficiencies of the JDK date/time classes.
+ if (recentDateFormat != null) {
+ pp = new ParsePosition(0);
+ int year = now.get(Calendar.YEAR);
+ String timeStampStrPlusYear = timestampStr + " " + year;
+ SimpleDateFormat hackFormatter = new SimpleDateFormat(recentDateFormat.toPattern() + " yyyy",
+ recentDateFormat.getDateFormatSymbols());
+ hackFormatter.setLenient(false);
+ hackFormatter.setTimeZone(recentDateFormat.getTimeZone());
+ parsed = hackFormatter.parse(timeStampStrPlusYear, pp);
+ }
+ if (parsed != null && pp.getIndex() == timestampStr.length() + 5) {
+ working.setTime(parsed);
+ }
+ else {
+ pp = new ParsePosition(0);
+ parsed = defaultDateFormat.parse(timestampStr, pp);
+ // note, length checks are mandatory for us since
+ // SimpleDateFormat methods will succeed if less than
+ // full string is matched. They will also accept,
+ // despite "leniency" setting, a two-digit number as
+ // a valid year (e.g. 22:04 will parse as 22 A.D.)
+ // so could mistakenly confuse an hour with a year,
+ // if we don't insist on full length parsing.
+ if (parsed != null && pp.getIndex() == timestampStr.length()) {
+ working.setTime(parsed);
+ } else {
+ throw new ParseException(
+ "Timestamp could not be parsed with older or recent DateFormat",
+ pp.getIndex());
+ }
+ }
+ }
+ return working;
+ }
+
+ /**
+ * @return Returns the defaultDateFormat.
+ */
+ public SimpleDateFormat getDefaultDateFormat() {
+ return defaultDateFormat;
+ }
+ /**
+ * @return Returns the defaultDateFormat pattern string.
+ */
+ public String getDefaultDateFormatString() {
+ return defaultDateFormat.toPattern();
+ }
+ /**
+ * @param defaultDateFormat The defaultDateFormat to be set.
+ */
+ private void setDefaultDateFormat(String format) {
+ if (format != null) {
+ this.defaultDateFormat = new SimpleDateFormat(format);
+ this.defaultDateFormat.setLenient(false);
+ }
+ }
+ /**
+ * @return Returns the recentDateFormat.
+ */
+ public SimpleDateFormat getRecentDateFormat() {
+ return recentDateFormat;
+ }
+ /**
+ * @return Returns the recentDateFormat.
+ */
+ public String getRecentDateFormatString() {
+ return recentDateFormat.toPattern();
+ }
+ /**
+ * @param recentDateFormat The recentDateFormat to set.
+ */
+ private void setRecentDateFormat(String format) {
+ if (format != null) {
+ this.recentDateFormat = new SimpleDateFormat(format);
+ this.recentDateFormat.setLenient(false);
+ }
+ }
+
+ /**
+ * @return returns an array of 12 strings representing the short
+ * month names used by this parse.
+ */
+ public String[] getShortMonths() {
+ return defaultDateFormat.getDateFormatSymbols().getShortMonths();
+ }
+
+
+ /**
+ * @return Returns the serverTimeZone used by this parser.
+ */
+ public TimeZone getServerTimeZone() {
+ return this.defaultDateFormat.getTimeZone();
+ }
+ /**
+ * sets a TimeZone represented by the supplied ID string into all
+ * of the parsers used by this server.
+ * @param serverTimeZone Time Id java.util.TimeZone id used by
+ * the ftp server. If null the client's local time zone is assumed.
+ */
+ private void setServerTimeZone(String serverTimeZoneId) {
+ TimeZone serverTimeZone = TimeZone.getDefault();
+ if (serverTimeZoneId != null) {
+ serverTimeZone = TimeZone.getTimeZone(serverTimeZoneId);
+ }
+ this.defaultDateFormat.setTimeZone(serverTimeZone);
+ if (this.recentDateFormat != null) {
+ this.recentDateFormat.setTimeZone(serverTimeZone);
+ }
+ }
+
+ /**
+ * Implementation of the {@link Configurable Configurable}
+ * interface. Configures this FTPTimestampParser
according
+ * to the following logic:
+ *
+ *
config
, use that to parse parse timestamps.config
, use the month names represented
+ * by that {@link FTPClientConfig#lookupDateFormatSymbols(String) language}
+ * to parse timestamps. FTPFile
instance.
+ * If the file listing line doesn't describe a file, then
+ * null
is returned. Otherwise a FTPFile
+ * instance representing the file is returned.
+ *
+ * @param entry
+ * A line of text from the file listing
+ * @return An FTPFile instance corresponding to the supplied entry
+ */
+ public FTPFile parseFTPEntry(String entry) {
+ boolean isParsed = false;
+ FTPFile f = new FTPFile();
+
+ if (isType == FILE_LIST_TYPE)
+ isParsed = parseFileList(f, entry);
+ else if (isType == MEMBER_LIST_TYPE) {
+ isParsed = parseMemberList(f, entry);
+ if (!isParsed)
+ isParsed = parseSimpleEntry(f, entry);
+ } else if (isType == UNIX_LIST_TYPE) {
+ isParsed = parseUnixList(f, entry);
+ } else if (isType == JES_LEVEL_1_LIST_TYPE) {
+ isParsed = parseJeslevel1List(f, entry);
+ } else if (isType == JES_LEVEL_2_LIST_TYPE) {
+ isParsed = parseJeslevel2List(f, entry);
+ }
+
+ if (!isParsed)
+ f = null;
+
+ return f;
+ }
+
+ /**
+ * Parse entries representing a dataset list. Only datasets with DSORG PS or
+ * PO or PO-E and with RECFM F* or V* will be parsed.
+ *
+ * Format of ZOS/MVS file list: 1 2 3 4 5 6 7 8 9 10 Volume Unit Referred
+ * Ext Used Recfm Lrecl BlkSz Dsorg Dsname B10142 3390 2006/03/20 2 31 F 80
+ * 80 PS MDI.OKL.WORK ARCIVE Not Direct Access Device
+ * KJ.IOP998.ERROR.PL.UNITTEST B1N231 3390 2006/03/20 1 15 VB 256 27998 PO
+ * PLU B1N231 3390 2006/03/20 1 15 VB 256 27998 PO-E PLB
+ *
+ * ----------------------------------- Group within Regex [1] Volume [2]
+ * Unit [3] Referred [4] Ext: number of extents [5] Used [6] Recfm: Record
+ * format [7] Lrecl: Logical record length [8] BlkSz: Block size [9] Dsorg:
+ * Dataset organisation. Many exists but only support: PS, PO, PO-E [10]
+ * Dsname: Dataset name
+ *
+ * Note: When volume is ARCIVE, it means the dataset is stored somewhere in
+ * a tape archive. These entries is currently not supported by this parser.
+ * A null value is returned.
+ *
+ * @param file
+ * will be updated with Name, Type, Timestamp if parsed.
+ * @param entry zosDirectoryEntry
+ * @return true: entry was parsed, false: entry was not parsed.
+ */
+ private boolean parseFileList(FTPFile file, String entry) {
+ if (matches(entry)) {
+ file.setRawListing(entry);
+ String name = group(2);
+ String dsorg = group(1);
+ file.setName(name);
+
+ // DSORG
+ if ("PS".equals(dsorg)) {
+ file.setType(FTPFile.FILE_TYPE);
+ }
+ else if ("PO".equals(dsorg) || "PO-E".equals(dsorg)) {
+ // regex already ruled out anything other than PO or PO-E
+ file.setType(FTPFile.DIRECTORY_TYPE);
+ }
+ else {
+ return false;
+ }
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Parse entries within a partitioned dataset.
+ *
+ * Format of a memberlist within a PDS: 1 2 3 4 5 6 7 8 9 Name VV.MM Created
+ * Changed Size Init Mod Id TBSHELF 01.03 2002/09/12 2002/10/11 09:37 11 11
+ * 0 KIL001 TBTOOL 01.12 2002/09/12 2004/11/26 19:54 51 28 0 KIL001
+ *
+ * ------------------------------------------- [1] Name [2] VV.MM: Version .
+ * modification [3] Created: yyyy / MM / dd [4,5] Changed: yyyy / MM / dd
+ * HH:mm [6] Size: number of lines [7] Init: number of lines when first
+ * created [8] Mod: number of modified lines a last save [9] Id: User id for
+ * last update
+ *
+ *
+ * @param file
+ * will be updated with Name, Type and Timestamp if parsed.
+ * @param entry zosDirectoryEntry
+ * @return true: entry was parsed, false: entry was not parsed.
+ */
+ private boolean parseMemberList(FTPFile file, String entry) {
+ if (matches(entry)) {
+ file.setRawListing(entry);
+ String name = group(1);
+ String datestr = group(2) + " " + group(3);
+ file.setName(name);
+ file.setType(FTPFile.FILE_TYPE);
+ try {
+ file.setTimestamp(super.parseTimestamp(datestr));
+ } catch (ParseException e) {
+ e.printStackTrace();
+ // just ignore parsing errors.
+ // TODO check this is ok
+ return false; // this is a parsing failure too.
+ }
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Assigns the name to the first word of the entry. Only to be used from a
+ * safe context, for example from a memberlist, where the regex for some
+ * reason fails. Then just assign the name field of FTPFile.
+ *
+ * @param file
+ * @param entry
+ * @return
+ */
+ private boolean parseSimpleEntry(FTPFile file, String entry) {
+ if (entry != null && entry.length() > 0) {
+ file.setRawListing(entry);
+ String name = entry.split(" ")[0];
+ file.setName(name);
+ file.setType(FTPFile.FILE_TYPE);
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Parse the entry as a standard unix file. Using the UnixFTPEntryParser.
+ *
+ * @param file
+ * @param entry
+ * @return true: entry is parsed, false: entry could not be parsed.
+ */
+ private boolean parseUnixList(FTPFile file, String entry) {
+ file = unixFTPEntryParser.parseFTPEntry(entry);
+ if (file == null)
+ return false;
+ return true;
+ }
+
+ /**
+ * Matches these entries, note: no header: [1] [2] [3] [4] [5] IBMUSER1
+ * JOB01906 OUTPUT 3 Spool Files
+ * 012345678901234567890123456789012345678901234 1 2 3 4
+ * ------------------------------------------- Group in regex [1] Job name
+ * [2] Job number [3] Job status (INPUT,ACTIVE,OUTPUT) [4] Number of sysout
+ * files [5] The string "Spool Files"
+ *
+ *
+ * @param file
+ * will be updated with Name, Type and Timestamp if parsed.
+ * @param entry zosDirectoryEntry
+ * @return true: entry was parsed, false: entry was not parsed.
+ */
+ private boolean parseJeslevel1List(FTPFile file, String entry) {
+ if (matches(entry)) {
+ if (group(3).equalsIgnoreCase("OUTPUT")) {
+ file.setRawListing(entry);
+ String name = group(2); /* Job Number, used by GET */
+ file.setName(name);
+ file.setType(FTPFile.FILE_TYPE);
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Matches these entries, note: no header: [1] [2] [3] [4] [5] JOBNAME JOBID
+ * OWNER STATUS CLASS IBMUSER1 JOB01906 IBMUSER OUTPUT A RC=0000 3 spool
+ * files IBMUSER TSU01830 IBMUSER OUTPUT TSU ABEND=522 3 spool files
+ * 012345678901234567890123456789012345678901234 1 2 3 4
+ * ------------------------------------------- Group in regex [1] Job name
+ * [2] Job number [3] Owner [4] Job status (INPUT,ACTIVE,OUTPUT) [5] Job
+ * Class [6] The rest
+ *
+ *
+ * @param file
+ * will be updated with Name, Type and Timestamp if parsed.
+ * @param entry zosDirectoryEntry
+ * @return true: entry was parsed, false: entry was not parsed.
+ */
+ private boolean parseJeslevel2List(FTPFile file, String entry) {
+ if (matches(entry)) {
+ if (group(4).equalsIgnoreCase("OUTPUT")) {
+ file.setRawListing(entry);
+ String name = group(2); /* Job Number, used by GET */
+ file.setName(name);
+ file.setType(FTPFile.FILE_TYPE);
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * preParse is called as part of the interface. Per definition is is called
+ * before the parsing takes place. Three kind of lists is recognize:
+ * z/OS-MVS File lists z/OS-MVS Member lists unix file lists
+ * @since 2.0
+ */
+ @Override
+ public ListREGEX
is not a valid regular expression.
+ */
+ public NTFTPEntryParser()
+ {
+ this(null);
+ }
+
+ /**
+ * This constructor allows the creation of an NTFTPEntryParser object
+ * with something other than the default configuration.
+ *
+ * @param config The {@link FTPClientConfig configuration} object used to
+ * configure this parser.
+ * @exception IllegalArgumentException
+ * Thrown if the regular expression is unparseable. Should not be seen
+ * under normal conditions. It it is seen, this is a sign that
+ * REGEX
is not a valid regular expression.
+ * @since 1.4
+ */
+ public NTFTPEntryParser(FTPClientConfig config)
+ {
+ super(REGEX);
+ configure(config);
+ }
+
+ /**
+ * Parses a line of an NT FTP server file listing and converts it into a
+ * usable format in the form of an FTPFile
instance. If the
+ * file listing line doesn't describe a file, null
is
+ * returned, otherwise a FTPFile
instance representing the
+ * files in the directory is returned.
+ * REGEX
is not a valid regular expression.
+ */
+ public NetwareFTPEntryParser() {
+ this(null);
+ }
+
+ /**
+ * This constructor allows the creation of an NetwareFTPEntryParser object
+ * with something other than the default configuration.
+ *
+ * @param config The {@link FTPClientConfig configuration} object used to
+ * configure this parser.
+ * @exception IllegalArgumentException
+ * Thrown if the regular expression is unparseable. Should not be seen
+ * under normal conditions. It it is seen, this is a sign that
+ * REGEX
is not a valid regular expression.
+ * @since 1.4
+ */
+ public NetwareFTPEntryParser(FTPClientConfig config) {
+ super(REGEX);
+ configure(config);
+ }
+
+ /**
+ * Parses a line of an NetwareFTP server file listing and converts it into a
+ * usable format in the form of an FTPFile
instance. If the
+ * file listing line doesn't describe a file, null
is
+ * returned, otherwise a FTPFile
instance representing the
+ * files in the directory is returned.
+ *
+ *
+ *
+ * See here
+ * for more details
+ *
+ * @param entry A line of text from the file listing
+ * @return An FTPFile instance corresponding to the supplied entry
+ */
+ public FTPFile parseFTPEntry(String entry) {
+
+ FTPFile f = new FTPFile();
+ if (matches(entry)) {
+ String dirString = group(1);
+ String attrib = group(2);
+ String user = group(3);
+ String size = group(4);
+ String datestr = group(5);
+ String name = group(9);
+
+ try {
+ f.setTimestamp(super.parseTimestamp(datestr));
+ } catch (ParseException e) {
+ // intentionally do nothing
+ }
+
+ //is it a DIR or a file
+ if (dirString.trim().equals("d")) {
+ f.setType(FTPFile.DIRECTORY_TYPE);
+ } else // Should be "-"
+ {
+ f.setType(FTPFile.FILE_TYPE);
+ }
+
+ f.setUser(user);
+
+ //set the name
+ f.setName(name.trim());
+
+ //set the size
+ f.setSize(Long.parseLong(size.trim()));
+
+ // Now set the permissions (or at least a subset thereof - full permissions would probably require
+ // subclassing FTPFile and adding extra metainformation there)
+ if (attrib.indexOf("R") != -1) {
+ f.setPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION,
+ true);
+ }
+ if (attrib.indexOf("W") != -1) {
+ f.setPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION,
+ true);
+ }
+
+ return (f);
+ }
+ return null;
+
+ }
+
+ /**
+ * Defines a default configuration to be used when this class is
+ * instantiated without a {@link FTPClientConfig FTPClientConfig}
+ * parameter being specified.
+ * @return the default configuration for this parser.
+ */
+ @Override
+ protected FTPClientConfig getDefaultConfiguration() {
+ return new FTPClientConfig(FTPClientConfig.SYST_NETWARE,
+ DEFAULT_DATE_FORMAT, DEFAULT_RECENT_DATE_FORMAT, null, null,
+ null);
+ }
+
+}
diff --git a/src/org/apache/commons/net/ftp/parser/OS2FTPEntryParser.java b/src/org/apache/commons/net/ftp/parser/OS2FTPEntryParser.java
new file mode 100644
index 0000000..dc02ffb
--- /dev/null
+++ b/src/org/apache/commons/net/ftp/parser/OS2FTPEntryParser.java
@@ -0,0 +1,147 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.net.ftp.parser;
+import java.text.ParseException;
+
+import org.apache.commons.net.ftp.FTPClientConfig;
+import org.apache.commons.net.ftp.FTPFile;
+
+/**
+ * Implementation of FTPFileEntryParser and FTPFileListParser for OS2 Systems.
+ *
+ * @author Winston Ojeda
+ * @author Steve Cohen
+ * @version $Id: OS2FTPEntryParser.java 658518 2008-05-21 01:04:30Z sebb $
+ * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
+ */
+public class OS2FTPEntryParser extends ConfigurableFTPFileEntryParserImpl
+
+{
+
+ private static final String DEFAULT_DATE_FORMAT
+ = "MM-dd-yy HH:mm"; //11-09-01 12:30
+ /**
+ * this is the regular expression used by this parser.
+ */
+ private static final String REGEX =
+ "\\s*([0-9]+)\\s*"
+ + "(\\s+|[A-Z]+)\\s*"
+ + "(DIR|\\s+)\\s*"
+ + "(\\S+)\\s+(\\S+)\\s+" /* date stuff */
+ + "(\\S.*)";
+
+ /**
+ * The default constructor for a OS2FTPEntryParser object.
+ *
+ * @exception IllegalArgumentException
+ * Thrown if the regular expression is unparseable. Should not be seen
+ * under normal conditions. It it is seen, this is a sign that
+ * REGEX
is not a valid regular expression.
+ */
+ public OS2FTPEntryParser()
+ {
+ this(null);
+ }
+
+ /**
+ * This constructor allows the creation of an OS2FTPEntryParser object
+ * with something other than the default configuration.
+ *
+ * @param config The {@link FTPClientConfig configuration} object used to
+ * configure this parser.
+ * @exception IllegalArgumentException
+ * Thrown if the regular expression is unparseable. Should not be seen
+ * under normal conditions. It it is seen, this is a sign that
+ * REGEX
is not a valid regular expression.
+ * @since 1.4
+ */
+ public OS2FTPEntryParser(FTPClientConfig config)
+ {
+ super(REGEX);
+ configure(config);
+ }
+
+ /**
+ * Parses a line of an OS2 FTP server file listing and converts it into a
+ * usable format in the form of an FTPFile
instance. If the
+ * file listing line doesn't describe a file, null
is
+ * returned, otherwise a FTPFile
instance representing the
+ * files in the directory is returned.
+ * REGEX
is not a valid regular expression.
+ */
+ public OS400FTPEntryParser()
+ {
+ this(null);
+ }
+
+ /**
+ * This constructor allows the creation of an OS400FTPEntryParser object
+ * with something other than the default configuration.
+ *
+ * @param config The {@link FTPClientConfig configuration} object used to
+ * configure this parser.
+ * @exception IllegalArgumentException
+ * Thrown if the regular expression is unparseable. Should not be seen
+ * under normal conditions. It it is seen, this is a sign that
+ * REGEX
is not a valid regular expression.
+ * @since 1.4
+ */
+ public OS400FTPEntryParser(FTPClientConfig config)
+ {
+ super(REGEX);
+ configure(config);
+ }
+
+
+ public FTPFile parseFTPEntry(String entry)
+ {
+
+ FTPFile file = new FTPFile();
+ file.setRawListing(entry);
+ int type;
+
+ if (matches(entry))
+ {
+ String usr = group(1);
+ String filesize = group(2);
+ String datestr = group(3)+" "+group(4);
+ String typeStr = group(5);
+ String name = group(6);
+
+ try
+ {
+ file.setTimestamp(super.parseTimestamp(datestr));
+ }
+ catch (ParseException e)
+ {
+ // intentionally do nothing
+ }
+
+
+ if (typeStr.equalsIgnoreCase("*STMF"))
+ {
+ type = FTPFile.FILE_TYPE;
+ }
+ else if (typeStr.equalsIgnoreCase("*DIR"))
+ {
+ type = FTPFile.DIRECTORY_TYPE;
+ }
+ else
+ {
+ type = FTPFile.UNKNOWN_TYPE;
+ }
+
+ file.setType(type);
+
+ file.setUser(usr);
+
+ try
+ {
+ file.setSize(Long.parseLong(filesize));
+ }
+ catch (NumberFormatException e)
+ {
+ // intentionally do nothing
+ }
+
+ if (name.endsWith("/"))
+ {
+ name = name.substring(0, name.length() - 1);
+ }
+ int pos = name.lastIndexOf('/');
+ if (pos > -1)
+ {
+ name = name.substring(pos + 1);
+ }
+
+ file.setName(name);
+
+ return file;
+ }
+ return null;
+ }
+
+ /**
+ * Defines a default configuration to be used when this class is
+ * instantiated without a {@link FTPClientConfig FTPClientConfig}
+ * parameter being specified.
+ * @return the default configuration for this parser.
+ */
+ @Override
+ protected FTPClientConfig getDefaultConfiguration() {
+ return new FTPClientConfig(
+ FTPClientConfig.SYST_OS400,
+ DEFAULT_DATE_FORMAT,
+ null, null, null, null);
+ }
+
+}
diff --git a/src/org/apache/commons/net/ftp/parser/ParserInitializationException.java b/src/org/apache/commons/net/ftp/parser/ParserInitializationException.java
new file mode 100644
index 0000000..8af9261
--- /dev/null
+++ b/src/org/apache/commons/net/ftp/parser/ParserInitializationException.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.net.ftp.parser;
+
+/**
+ * This class encapsulates all errors that may be thrown by
+ * the process of an FTPFileEntryParserFactory creating and
+ * instantiating an FTPFileEntryParser.
+ */
+public class ParserInitializationException extends RuntimeException {
+
+ /**
+ * Root exception that caused this to be thrown
+ */
+ private final Throwable rootCause;
+
+ /**
+ * Constucts a ParserInitializationException with just a message
+ *
+ * @param message Exception message
+ */
+ public ParserInitializationException(String message) {
+ super(message);
+ this.rootCause = null;
+ }
+
+ /**
+ * Constucts a ParserInitializationException with a message
+ * and a root cause.
+ *
+ * @param message Exception message
+ * @param rootCause root cause throwable that caused
+ * this to be thrown
+ */
+ public ParserInitializationException(String message, Throwable rootCause) {
+ super(message);
+ this.rootCause = rootCause;
+ }
+
+ /**
+ * returns the root cause of this exception or null
+ * if no root cause was specified.
+ *
+ * @return the root cause of this exception being thrown
+ */
+ public Throwable getRootCause() {
+ return this.rootCause;
+ }
+
+}
diff --git a/src/org/apache/commons/net/ftp/parser/RegexFTPFileEntryParserImpl.java b/src/org/apache/commons/net/ftp/parser/RegexFTPFileEntryParserImpl.java
new file mode 100644
index 0000000..5277444
--- /dev/null
+++ b/src/org/apache/commons/net/ftp/parser/RegexFTPFileEntryParserImpl.java
@@ -0,0 +1,155 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package org.apache.commons.net.ftp.parser;
+
+import java.util.regex.MatchResult;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
+
+import org.apache.commons.net.ftp.FTPFileEntryParserImpl;
+
+/**
+ * This abstract class implements both the older FTPFileListParser and
+ * newer FTPFileEntryParser interfaces with default functionality.
+ * All the classes in the parser subpackage inherit from this.
+ *
+ * This is the base for all regular based FTPFileEntryParser
+ *
+ * @author Steve Cohen matchnum'th
group of the internal
+ * match or null if this method is called without a match having
+ * been made.
+ */
+ public String group(int matchnum) {
+ if (this.result == null) {
+ return null;
+ }
+ return this.result.group(matchnum);
+ }
+
+ /**
+ * For debugging purposes - returns a string shows each match group by
+ * number.
+ *
+ * @return a string shows each match group by number.
+ */
+
+ public String getGroupsAsString() {
+ StringBuffer b = new StringBuffer();
+ for (int i = 1; i <= this.result.groupCount(); i++) {
+ b.append(i).append(") ").append(this.result.group(i)).append(
+ System.getProperty("line.separator"));
+ }
+ return b.toString();
+ }
+
+ /**
+ * Alter the current regular expression being utilised for entry parsing
+ * and create a new {@link Pattern} instance.
+ * @param regex The new regular expression
+ * @return
+ * @since 2.0
+ */
+ public boolean setRegex(String regex) {
+ try {
+ pattern = Pattern.compile(regex);
+ } catch (PatternSyntaxException pse) {
+ throw new IllegalArgumentException("Unparseable regex supplied: "
+ + regex);
+ }
+ return (pattern != null);
+ }
+
+}
diff --git a/src/org/apache/commons/net/ftp/parser/UnixFTPEntryParser.java b/src/org/apache/commons/net/ftp/parser/UnixFTPEntryParser.java
new file mode 100644
index 0000000..2a2a909
--- /dev/null
+++ b/src/org/apache/commons/net/ftp/parser/UnixFTPEntryParser.java
@@ -0,0 +1,295 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.net.ftp.parser;
+import java.text.ParseException;
+
+import org.apache.commons.net.ftp.FTPClientConfig;
+import org.apache.commons.net.ftp.FTPFile;
+
+/**
+ * Implementation FTPFileEntryParser and FTPFileListParser for standard
+ * Unix Systems.
+ *
+ * This class is based on the logic of Daniel Savarese's
+ * DefaultFTPListParser, but adapted to use regular expressions and to fit the
+ * new FTPFileEntryParser interface.
+ * @version $Id: UnixFTPEntryParser.java 658518 2008-05-21 01:04:30Z sebb $
+ * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
+ */
+public class UnixFTPEntryParser extends ConfigurableFTPFileEntryParserImpl
+{
+
+ static final String DEFAULT_DATE_FORMAT
+ = "MMM d yyyy"; //Nov 9 2001
+
+ static final String DEFAULT_RECENT_DATE_FORMAT
+ = "MMM d HH:mm"; //Nov 9 20:06
+
+ static final String NUMERIC_DATE_FORMAT
+ = "yyyy-MM-dd HH:mm"; //2001-11-09 20:06
+
+ /**
+ * Some Linux distributions are now shipping an FTP server which formats
+ * file listing dates in an all-numeric format:
+ *
"yyyy-MM-dd HH:mm
.
+ * This is a very welcome development, and hopefully it will soon become
+ * the standard. However, since it is so new, for now, and possibly
+ * forever, we merely accomodate it, but do not make it the default.
+ * UnixFTPEntryParser(FTPClientConfig)
.
+ * Steve Cohen - 2005-04-17
+ */
+ public static final FTPClientConfig NUMERIC_DATE_CONFIG =
+ new FTPClientConfig(
+ FTPClientConfig.SYST_UNIX,
+ NUMERIC_DATE_FORMAT,
+ null, null, null, null);
+
+ /**
+ * this is the regular expression used by this parser.
+ *
+ * Permissions:
+ * r the file is readable
+ * w the file is writable
+ * x the file is executable
+ * - the indicated permission is not granted
+ * L mandatory locking occurs during access (the set-group-ID bit is
+ * on and the group execution bit is off)
+ * s the set-user-ID or set-group-ID bit is on, and the corresponding
+ * user or group execution bit is also on
+ * S undefined bit-state (the set-user-ID bit is on and the user
+ * execution bit is off)
+ * t the 1000 (octal) bit, or sticky bit, is on [see chmod(1)], and
+ * execution is on
+ * T the 1000 bit is turned on, and execution is off (undefined bit-
+ * state)
+ * e z/OS external link bit
+ */
+ private static final String REGEX =
+ "([bcdelfmpSs-])"
+ +"(((r|-)(w|-)([xsStTL-]))((r|-)(w|-)([xsStTL-]))((r|-)(w|-)([xsStTL-])))\\+?\\s+"
+ + "(\\d+)\\s+"
+ + "(?:(\\S+(?:\\s\\S+)*?)\\s+)?" // owner name (optional spaces)
+ + "(?:(\\S+(?:\\s\\S+)*)\\s+)?" // group name (optional spaces)
+ + "(\\d+(?:,\\s*\\d+)?)\\s+"
+
+ /*
+ numeric or standard format date
+ */
+ + "((?:\\d+[-/]\\d+[-/]\\d+)|(?:\\S+\\s+\\S+))\\s+"
+
+ /*
+ year (for non-recent standard format)
+ or time (for numeric or recent standard format
+ */
+ + "(\\d+(?::\\d+)?)\\s+"
+
+ + "(\\S*)(\\s*.*)";
+
+
+ /**
+ * The default constructor for a UnixFTPEntryParser object.
+ *
+ * @exception IllegalArgumentException
+ * Thrown if the regular expression is unparseable. Should not be seen
+ * under normal conditions. It it is seen, this is a sign that
+ * REGEX
is not a valid regular expression.
+ */
+ public UnixFTPEntryParser()
+ {
+ this(null);
+ }
+
+ /**
+ * This constructor allows the creation of a UnixFTPEntryParser object with
+ * something other than the default configuration.
+ *
+ * @param config The {@link FTPClientConfig configuration} object used to
+ * configure this parser.
+ * @exception IllegalArgumentException
+ * Thrown if the regular expression is unparseable. Should not be seen
+ * under normal conditions. It it is seen, this is a sign that
+ * REGEX
is not a valid regular expression.
+ * @since 1.4
+ */
+ public UnixFTPEntryParser(FTPClientConfig config)
+ {
+ super(REGEX);
+ configure(config);
+ }
+
+
+ /**
+ * Parses a line of a unix (standard) FTP server file listing and converts
+ * it into a usable format in the form of an FTPFile
+ * instance. If the file listing line doesn't describe a file,
+ * null
is returned, otherwise a FTPFile
+ * instance representing the files in the directory is returned.
+ * REGEX
is not a valid regular expression.
+ */
+ public VMSFTPEntryParser()
+ {
+ this(null);
+ }
+
+ /**
+ * This constructor allows the creation of a VMSFTPEntryParser object with
+ * something other than the default configuration.
+ *
+ * @param config The {@link FTPClientConfig configuration} object used to
+ * configure this parser.
+ * @exception IllegalArgumentException
+ * Thrown if the regular expression is unparseable. Should not be seen
+ * under normal conditions. It it is seen, this is a sign that
+ * REGEX
is not a valid regular expression.
+ * @since 1.4
+ */
+ public VMSFTPEntryParser(FTPClientConfig config)
+ {
+ super(REGEX);
+ configure(config);
+ }
+
+
+
+ /***
+ * Parses an FTP server file listing and converts it into a usable format
+ * in the form of an array of FTPFile
instances. If the
+ * file list contains no files, null
should be
+ * returned, otherwise an array of FTPFile
instances
+ * representing the files in the directory is returned.
+ * FTPFile
instance. If the
+ * file listing line doesn't describe a file, null
is
+ * returned, otherwise a FTPFile
instance representing the
+ * files in the directory is returned.
+ * REGEX
is not a valid regular expression.
+ */
+ public VMSVersioningFTPEntryParser()
+ {
+ this(null);
+ }
+
+ /**
+ * This constructor allows the creation of a VMSVersioningFTPEntryParser
+ * object with something other than the default configuration.
+ *
+ * @param config The {@link FTPClientConfig configuration} object used to
+ * configure this parser.
+ * @exception IllegalArgumentException
+ * Thrown if the regular expression is unparseable. Should not be seen
+ * under normal conditions. It it is seen, this is a sign that
+ * REGEX
is not a valid regular expression.
+ * @since 1.4
+ */
+ public VMSVersioningFTPEntryParser(FTPClientConfig config)
+ {
+ super();
+ configure(config);
+ try
+ {
+ //_preparse_matcher_ = new Perl5Matcher();
+ _preparse_pattern_ = Pattern.compile(PRE_PARSE_REGEX);
+ }
+ catch (PatternSyntaxException pse)
+ {
+ throw new IllegalArgumentException (
+ "Unparseable regex supplied: " + PRE_PARSE_REGEX);
+ }
+
+ }
+
+
+
+ private static class NameVersion {
+ String name;
+ int versionNumber;
+ NameVersion(String name, String vers) {
+ this.name = name;
+ this.versionNumber = Integer.parseInt(vers);
+ }
+ }
+
+ /**
+ * Implement hook provided for those implementers (such as
+ * VMSVersioningFTPEntryParser, and possibly others) which return
+ * multiple files with the same name to remove the duplicates ..
+ *
+ * @param original Original list
+ *
+ * @return Original list purged of duplicates
+ */
+ @Override
+ public List