Java Code Examples for org.apache.commons.net.ftp.FTPReply#isPositiveIntermediate()

The following examples show how to use org.apache.commons.net.ftp.FTPReply#isPositiveIntermediate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public ReadableByteChannel read() throws IOException {
	FTPClient ftp = null;
	try {
		ftp = connect(uri);
		InputStream is = ftp.retrieveFileStream(getPath(uri));
		if (is == null) {
			throw new IOException(ftp.getReplyString());
		}
		int replyCode = ftp.getReplyCode();
		if (!FTPReply.isPositiveIntermediate(replyCode) && !FTPReply.isPositivePreliminary(replyCode)) {
			is.close();
			throw new IOException(ftp.getReplyString());
		}
		return Channels.newChannel(new FTPInputStream(is, ftp));
	} catch (Throwable t) {
		disconnect(ftp);
		if (t instanceof IOException) {
			throw (IOException) t;
		} else {
			throw new IOException(t);
		}
	}
}
 
Example 2
Source File: NetUtils.java    From ApprovalTests.Java with Apache License 2.0 6 votes vote down vote up
private static void assertValidReplyCode(int code, FTPClient ftp)
{
  if (FTPReply.isPositiveCompletion(code))
  {
    //good
    SimpleLogger.variable("Good Completion code " + code);
  }
  else if (FTPReply.isPositiveIntermediate(code))
  {
    // do nothing
    SimpleLogger.variable("Good Intermediate code " + code);
  }
  else if (FTPReply.isPositivePreliminary(code))
  {
    // do nothing
    SimpleLogger.variable("Good Preliminary code " + code);
  }
  else
  {
    // bad
    throw new Error("Problem encountered with FTP Server, returned Code " + code + ", replied '"
        + ftp.getReplyString() + "'");
  }
}
 
Example 3
Source File: Client.java    From anthelion with Apache License 2.0 5 votes vote down vote up
/***
 * Login to the FTP server using the provided username and password.
 * <p>
 * @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(getReplyCode()))
        return true;

    // If we get here, we either have an error code, or an intermmediate
    // reply requesting password.
    if (!FTPReply.isPositiveIntermediate(getReplyCode()))
        return false;

    return FTPReply.isPositiveCompletion(pass(password));
}
 
Example 4
Source File: PooledFTPConnection.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * If the stream cannot be opened, the connection is closed
 * (returned to the pool).
 * 
 * @param path
 * @param mode
 * @return
 * @throws IOException
 */
public OutputStream getOutputStream(String path, WriteMode mode) throws IOException {
	try {
		OutputStream os = (mode == WriteMode.APPEND) ? ftp.appendFileStream(path) : ftp.storeFileStream(path);
		if (os == null) {
			throw new IOException(ftp.getReplyString());
		}
		int replyCode = ftp.getReplyCode();
		if (!FTPReply.isPositiveIntermediate(replyCode) && !FTPReply.isPositivePreliminary(replyCode)) {
			os.close();
			throw new IOException(ftp.getReplyString());
		}
		os = new CloseOnceOutputStream (new BufferedOutputStream(os) {
			@Override
			public void close() throws IOException {
				try {
					super.close();
				} finally {
					try {
						if (!ftp.completePendingCommand()) {
							throw new IOException(FileOperationMessages.getString("FTPOperationHandler.failed_to_close_stream")); //$NON-NLS-1$
						}
					} finally {
						returnToPool();
					}
				}
			}
		}, null);
		return os;
	} catch (Exception e) {
		returnToPool();
		throw getIOException(e);
	}
}
 
Example 5
Source File: FTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public WritableByteChannel write() throws IOException {
	FTPClient ftp = null;
	try {
		ftp = connect(uri);
		Info info = info(uri, ftp);
		if ((info != null) && info.isDirectory()) {
			throw new IOException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.exists_not_file"), uri)); //$NON-NLS-1$
		}
		OutputStream os = ftp.storeFileStream(getPath(uri));
		if (os == null) {
			throw new IOException(ftp.getReplyString());
		}
		int replyCode = ftp.getReplyCode();
		if (!FTPReply.isPositiveIntermediate(replyCode) && !FTPReply.isPositivePreliminary(replyCode)) {
			os.close();
			throw new IOException(ftp.getReplyString());
		}
		return Channels.newChannel(new FTPOutputStream(os, ftp));
	} catch (Throwable t) {
		disconnect(ftp);
		if (t instanceof IOException) {
			throw (IOException) t;
		} else {
			throw new IOException(t);
		}
	}
}
 
Example 6
Source File: FTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public WritableByteChannel append() throws IOException {
	FTPClient ftp = null;
	try {
		ftp = connect(uri);
		Info info = info(uri, ftp);
		if ((info != null) && info.isDirectory()) {
			throw new IOException(MessageFormat.format(FileOperationMessages.getString("IOperationHandler.exists_not_file"), uri)); //$NON-NLS-1$
		}
		OutputStream os = ftp.appendFileStream(getPath(uri));
		if (os == null) {
			throw new IOException(ftp.getReplyString());
		}
		int replyCode = ftp.getReplyCode();
		if (!FTPReply.isPositiveIntermediate(replyCode) && !FTPReply.isPositivePreliminary(replyCode)) {
			os.close();
			throw new IOException(ftp.getReplyString());
		}
		return Channels.newChannel(new FTPOutputStream(os, ftp));
	} catch (Throwable t) {
		disconnect(ftp);
		if (t instanceof IOException) {
			throw (IOException) t;
		} else {
			throw new IOException(t);
		}
	}
}
 
Example 7
Source File: PooledFTPOperationHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
private boolean createFile(FTPClient ftp, String path) throws IOException {
	log.debug(MessageFormat.format("Creating {0}", path));
	ftp.storeFile(path, new ByteArrayInputStream(new byte[0]));
	int replyCode = ftp.getReplyCode();
	boolean result = FTPReply.isPositiveCompletion(replyCode) || FTPReply.isPositiveIntermediate(replyCode) || FTPReply.isPositivePreliminary(replyCode);
	if (!result) {
		result = FTPReply.isPositiveCompletion(replyCode);
		log.debug(MessageFormat.format("Failed to create {0}: {1}", path, ftp.getReplyString()));
	}
	return result;
}
 
Example 8
Source File: Client.java    From nutch-htmlunit with Apache License 2.0 5 votes vote down vote up
/***
 * Login to the FTP server using the provided username and password.
 * <p>
 * @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(getReplyCode()))
        return true;

    // If we get here, we either have an error code, or an intermmediate
    // reply requesting password.
    if (!FTPReply.isPositiveIntermediate(getReplyCode()))
        return false;

    return FTPReply.isPositiveCompletion(pass(password));
}