use of org.apache.commons.net.ftp.FTPClient in project nifi by apache.
the class FTPTransfer method rename.
@Override
public void rename(final FlowFile flowFile, final String source, final String target) throws IOException {
final FTPClient client = getClient(flowFile);
final boolean renameSuccessful = client.rename(source, target);
if (!renameSuccessful) {
throw new IOException("Failed to rename temporary file " + source + " to " + target + " due to: " + client.getReplyString());
}
}
use of org.apache.commons.net.ftp.FTPClient in project nifi by apache.
the class FTPTransfer method ensureDirectoryExists.
@Override
public void ensureDirectoryExists(final FlowFile flowFile, final File directoryName) throws IOException {
if (directoryName.getParent() != null && !directoryName.getParentFile().equals(new File(File.separator))) {
ensureDirectoryExists(flowFile, directoryName.getParentFile());
}
final String remoteDirectory = directoryName.getAbsolutePath().replace("\\", "/").replaceAll("^.\\:", "");
final FTPClient client = getClient(flowFile);
final boolean cdSuccessful = setWorkingDirectory(remoteDirectory);
if (!cdSuccessful) {
logger.debug("Remote Directory {} does not exist; creating it", new Object[] { remoteDirectory });
if (client.makeDirectory(remoteDirectory)) {
logger.debug("Created {}", new Object[] { remoteDirectory });
} else {
throw new IOException("Failed to create remote directory " + remoteDirectory);
}
}
}
use of org.apache.commons.net.ftp.FTPClient in project nifi by apache.
the class FTPTransfer method getListing.
private List<FileInfo> getListing(final String path, final int depth, final int maxResults) throws IOException {
final List<FileInfo> listing = new ArrayList<>();
if (maxResults < 1) {
return listing;
}
if (depth >= 100) {
logger.warn(this + " had to stop recursively searching directories at a recursive depth of " + depth + " to avoid memory issues");
return listing;
}
final boolean ignoreDottedFiles = ctx.getProperty(FileTransfer.IGNORE_DOTTED_FILES).asBoolean();
final boolean recurse = ctx.getProperty(FileTransfer.RECURSIVE_SEARCH).asBoolean();
final String fileFilterRegex = ctx.getProperty(FileTransfer.FILE_FILTER_REGEX).getValue();
final Pattern pattern = (fileFilterRegex == null) ? null : Pattern.compile(fileFilterRegex);
final String pathFilterRegex = ctx.getProperty(FileTransfer.PATH_FILTER_REGEX).getValue();
final Pattern pathPattern = (!recurse || pathFilterRegex == null) ? null : Pattern.compile(pathFilterRegex);
final String remotePath = ctx.getProperty(FileTransfer.REMOTE_PATH).evaluateAttributeExpressions().getValue();
// check if this directory path matches the PATH_FILTER_REGEX
boolean pathFilterMatches = true;
if (pathPattern != null) {
Path reldir = path == null ? Paths.get(".") : Paths.get(path);
if (remotePath != null) {
reldir = Paths.get(remotePath).relativize(reldir);
}
if (reldir != null && !reldir.toString().isEmpty()) {
if (!pathPattern.matcher(reldir.toString().replace("\\", "/")).matches()) {
pathFilterMatches = false;
}
}
}
final FTPClient client = getClient(null);
int count = 0;
final FTPFile[] files;
if (path == null || path.trim().isEmpty()) {
files = client.listFiles(".");
} else {
files = client.listFiles(path);
}
if (files.length == 0 && path != null && !path.trim().isEmpty()) {
// throw exception if directory doesn't exist
final boolean cdSuccessful = setWorkingDirectory(path);
if (!cdSuccessful) {
throw new IOException("Cannot list files for non-existent directory " + path);
}
}
for (final FTPFile file : files) {
final String filename = file.getName();
if (filename.equals(".") || filename.equals("..")) {
continue;
}
if (ignoreDottedFiles && filename.startsWith(".")) {
continue;
}
final File newFullPath = new File(path, filename);
final String newFullForwardPath = newFullPath.getPath().replace("\\", "/");
if (recurse && file.isDirectory()) {
try {
listing.addAll(getListing(newFullForwardPath, depth + 1, maxResults - count));
} catch (final IOException e) {
logger.error("Unable to get listing from " + newFullForwardPath + "; skipping this subdirectory", e);
}
}
// FILE_FILTER_REGEX - then let's add it
if (!file.isDirectory() && !file.isSymbolicLink() && pathFilterMatches) {
if (pattern == null || pattern.matcher(filename).matches()) {
listing.add(newFileInfo(file, path));
count++;
}
}
if (count >= maxResults) {
break;
}
}
return listing;
}
use of org.apache.commons.net.ftp.FTPClient in project nifi by apache.
the class FTPUtils method connect.
/**
* Creates a new FTPClient connected to an FTP server. The following properties must exist:
* <ul>Required Properties:
* <li>remote.host - The hostname or IP address of the FTP server to connect to</li>
* <li>remote.user - The username of the account to authenticate with</li>
* <li>remote.password = The password for the username to authenticate with</li>
* </ul>
* <ul>Optional Properties:
* <li>remote.port - The port on the FTP server to connect to. Defaults to FTP default.</li>
* <li>transfer.mode - The type of transfer for this connection ('ascii', 'binary'). Defaults to 'binary'</li>
* <li>connection.mode - The type of FTP connection to make ('active_local', 'passive_local'). Defaults to 'active_local'. In active_local the server initiates 'data connections' to the client
* where in passive_local the client initiates 'data connections' to the server.</li>
* <li>network.data.timeout - Default is 0. Sets the timeout in milliseconds for waiting to establish a new 'data connection' (not a control connection) when in ACTIVE_LOCAL mode. Also, this
* establishes the amount of time to wait on read calls on the data connection in either mode. A value of zero means do not timeout. Users should probably set a value here unless using very
* reliable communications links or else risk indefinite hangs that require a restart.</li>
* <li>network.socket.timeout - Default is 0. Sets the timeout in milliseconds to use when creating a new control channel socket and also a timeout to set when reading from a control socket. A
* value of zero means do not timeout. Users should probably set a value here unless using very reliable communications links or else risk indefinite hangs that require a restart.</li>
* </ul>
*
* @param conf conf
* @param monitor if provided will be used to monitor FTP commands processed but may be null
* @return FTPClient connected to FTP server as configured
* @throws NullPointerException if either argument is null
* @throws IllegalArgumentException if a required property is missing
* @throws NumberFormatException if any argument that must be an int cannot be converted to int
* @throws IOException if some problem occurs connecting to FTP server
*/
public static FTPClient connect(final FTPConfiguration conf, final ProtocolCommandListener monitor) throws IOException {
if (null == conf) {
throw new NullPointerException();
}
final String portVal = conf.port;
final int portNum = (null == portVal) ? -1 : Integer.parseInt(portVal);
final String connectionModeVal = conf.connectionMode;
final String connectionMode = (null == connectionModeVal) ? ACTIVE_LOCAL_CONNECTION_MODE : connectionModeVal;
final String transferModeVal = conf.transferMode;
final String transferMode = (null == transferModeVal) ? BINARY_TRANSFER_MODE : transferModeVal;
final String networkDataTimeoutVal = conf.dataTimeout;
final int networkDataTimeout = (null == networkDataTimeoutVal) ? 0 : Integer.parseInt(networkDataTimeoutVal);
final String networkSocketTimeoutVal = conf.connectionTimeout;
final int networkSocketTimeout = (null == networkSocketTimeoutVal) ? 0 : Integer.parseInt(networkSocketTimeoutVal);
final FTPClient client = new FTPClient();
if (networkDataTimeout > 0) {
client.setDataTimeout(networkDataTimeout);
}
if (networkSocketTimeout > 0) {
client.setDefaultTimeout(networkSocketTimeout);
}
client.setRemoteVerificationEnabled(false);
if (null != monitor) {
client.addProtocolCommandListener(monitor);
}
InetAddress inetAddress = null;
try {
inetAddress = InetAddress.getByAddress(conf.remoteHostname, null);
} catch (final UnknownHostException uhe) {
}
if (inetAddress == null) {
inetAddress = InetAddress.getByName(conf.remoteHostname);
}
if (portNum < 0) {
client.connect(inetAddress);
} else {
client.connect(inetAddress, portNum);
}
if (networkDataTimeout > 0) {
client.setDataTimeout(networkDataTimeout);
}
if (networkSocketTimeout > 0) {
client.setSoTimeout(networkSocketTimeout);
}
final boolean loggedIn = client.login(conf.username, conf.password);
if (!loggedIn) {
throw new IOException("Could not login for user '" + conf.username + "'");
}
if (connectionMode.equals(ACTIVE_LOCAL_CONNECTION_MODE)) {
client.enterLocalActiveMode();
} else if (connectionMode.equals(PASSIVE_LOCAL_CONNECTION_MODE)) {
client.enterLocalPassiveMode();
}
boolean transferModeSet = false;
if (transferMode.equals(ASCII_TRANSFER_MODE)) {
transferModeSet = client.setFileType(FTPClient.ASCII_FILE_TYPE);
} else {
transferModeSet = client.setFileType(FTPClient.BINARY_FILE_TYPE);
}
if (!transferModeSet) {
throw new IOException("Unable to set transfer mode to type " + transferMode);
}
return client;
}
use of org.apache.commons.net.ftp.FTPClient in project camel by apache.
the class FtpEndpoint method createFtpClient.
protected FTPClient createFtpClient() throws Exception {
FTPClient client = new FTPClient();
// default ParserFactory
if (isOsgi()) {
ClassResolver cr = getCamelContext().getClassResolver();
OsgiParserFactory opf = new OsgiParserFactory(cr);
client.setParserFactory(opf);
}
return client;
}
Aggregations