use of org.apache.commons.net.ftp.FTPFile in project hive by apache.
the class Ftp method retrieveFileList.
/**
* Get the list of files to transfer
*/
void retrieveFileList(String dir) {
if (info) {
if (dir == null || dir.isEmpty()) {
info(null, " Listing the current working FTP directory");
} else {
info(null, " Listing " + dir);
}
}
try {
FTPFile[] files = ftp.listFiles(dir);
ArrayList<FTPFile> dirs = new ArrayList<FTPFile>();
for (FTPFile file : files) {
String name = file.getName();
if (file.isFile()) {
if (filePattern == null || Pattern.matches(filePattern, name)) {
if (dir != null && !dir.isEmpty()) {
if (dir.endsWith("/")) {
name = dir + name;
} else {
name = dir + "/" + name;
}
}
if (!newOnly || !isTargetExists(name)) {
fileCnt++;
ftpSizeInBytes += file.getSize();
filesQueue.add(name);
filesMap.put(name, file);
}
}
} else {
if (subdir && !name.equals(".") && !name.equals("..")) {
dirCnt++;
dirs.add(file);
}
}
}
if (subdir) {
for (FTPFile d : dirs) {
String sd = d.getName();
if (dir != null && !dir.isEmpty()) {
if (dir.endsWith("/")) {
sd = dir + sd;
} else {
sd = dir + "/" + sd;
}
}
retrieveFileList(sd);
}
}
} catch (IOException e) {
exec.signal(e);
}
}
use of org.apache.commons.net.ftp.FTPFile in project hadoop by apache.
the class FTPFileSystem method getFileStatus.
/**
* Convenience method, so that we don't open a new connection when using this
* method from within another method. Otherwise every API invocation incurs
* the overhead of opening/closing a TCP connection.
*/
private FileStatus getFileStatus(FTPClient client, Path file) throws IOException {
FileStatus fileStat = null;
Path workDir = new Path(client.printWorkingDirectory());
Path absolute = makeAbsolute(workDir, file);
Path parentPath = absolute.getParent();
if (parentPath == null) {
// root dir
// Length of root dir on server not known
long length = -1;
boolean isDir = true;
int blockReplication = 1;
// Block Size not known.
long blockSize = DEFAULT_BLOCK_SIZE;
// Modification time of root dir not known.
long modTime = -1;
Path root = new Path("/");
return new FileStatus(length, isDir, blockReplication, blockSize, modTime, root.makeQualified(this));
}
String pathName = parentPath.toUri().getPath();
FTPFile[] ftpFiles = client.listFiles(pathName);
if (ftpFiles != null) {
for (FTPFile ftpFile : ftpFiles) {
if (ftpFile.getName().equals(file.getName())) {
// file found in dir
fileStat = getFileStatus(ftpFile, parentPath);
break;
}
}
if (fileStat == null) {
throw new FileNotFoundException("File " + file + " does not exist.");
}
} else {
throw new FileNotFoundException("File " + file + " does not exist.");
}
return fileStat;
}
use of org.apache.commons.net.ftp.FTPFile in project azure-tools-for-java by Microsoft.
the class WebAppUtils method removeFtpDirectory.
public static void removeFtpDirectory(FTPClient ftpClient, String path, IProgressIndicator pi) throws IOException {
String prefix = "Removing from FTP server: ";
FTPFile[] subFiles = ftpClient.listFiles(path);
if (subFiles.length > 0) {
for (FTPFile ftpFile : subFiles) {
if (pi != null && pi.isCanceled())
break;
String currentFileName = ftpFile.getName();
if (currentFileName.equals(".") || currentFileName.equals("..")) {
// skip
continue;
}
String path1 = path + "/" + currentFileName;
if (ftpFile.isDirectory()) {
// remove the sub directory
removeFtpDirectory(ftpClient, path1, pi);
} else {
// delete the file
if (pi != null)
pi.setText2(prefix + path1);
ftpClient.deleteFile(path1);
}
}
}
if (pi != null)
pi.setText2(prefix + path);
ftpClient.removeDirectory(path);
if (pi != null)
pi.setText2("");
}
use of org.apache.commons.net.ftp.FTPFile in project azure-tools-for-java by Microsoft.
the class WebAppUtils method deployCustomJdk.
public static void deployCustomJdk(WebApp webApp, String jdkDownloadUrl, WebContainer webContainer, IProgressIndicator indicator) throws IOException, InterruptedException, WebAppException {
FTPClient ftp = null;
String customJdkFolderName = null;
try {
PublishingProfile pp = webApp.getPublishingProfile();
ftp = getFtpConnection(pp);
if (indicator != null)
indicator.setText("Deleting custom jdk artifacts, if any (takes a while)...");
removeCustomJdkArtifacts(ftp, indicator);
if (indicator != null)
indicator.setText("Uploading scripts...");
uploadJdkDownloadScript(ftp, jdkDownloadUrl);
// if (indicator != null) indicator.setText("Starting the service...");
// webApp.start();
final String siteUrl = "https://" + webApp.defaultHostName();
// send get to activate the script
sendGet(siteUrl);
// Polling report.txt...
if (indicator != null)
indicator.setText("Checking the JDK gets downloaded and unpacked...");
//int step = 0;
while (!doesRemoteFileExist(ftp, ftpRootPath, reportFilename)) {
if (indicator != null && indicator.isCanceled())
throw new CancellationException("Canceled by user.");
//if (step++ > 3) checkFreeSpaceAvailability(ftp);
Thread.sleep(5000);
sendGet(siteUrl);
}
if (indicator != null)
indicator.setText("Checking status...");
OutputStream reportFileStream = new ByteArrayOutputStream();
ftp.retrieveFile("report.txt", reportFileStream);
String reportFileString = reportFileStream.toString();
if (reportFileString.startsWith("FAIL")) {
String err = reportFileString.substring(reportFileString.indexOf(":" + 1));
throw new WebAppException(err);
}
// get top level jdk folder name (under jdk folder)
FTPFile[] ftpDirs = ftp.listDirectories(ftpJdkPath);
if (ftpDirs.length != 1) {
String err = "Bad JDK archive. Please make sure the JDK archive contains a single JDK folder. For example, 'my-jdk1.7.0_79.zip' archive should contain 'jdk1.7.0_79' folder only";
throw new WebAppException(err);
}
customJdkFolderName = ftpDirs[0].getName();
uploadWebConfigForCustomJdk(ftp, webApp, customJdkFolderName, webContainer, indicator);
} catch (IOException | WebAppException | InterruptedException ex) {
if (doesRemoteFolderExist(ftp, ftpRootPath, jdkFolderName)) {
indicator.setText("Error happened. Cleaning up...");
removeFtpDirectory(ftp, ftpJdkPath, indicator);
}
throw ex;
} finally {
indicator.setText("Removing working data from server...");
cleanupWorkerData(ftp);
if (ftp != null && ftp.isConnected()) {
ftp.disconnect();
}
}
}
use of org.apache.commons.net.ftp.FTPFile in project camel by apache.
the class FtpOperations method retrieveFileToStreamInBody.
@SuppressWarnings("unchecked")
private boolean retrieveFileToStreamInBody(String name, Exchange exchange) throws GenericFileOperationFailedException {
boolean result;
try {
GenericFile<FTPFile> target = (GenericFile<FTPFile>) exchange.getProperty(FileComponent.FILE_EXCHANGE_FILE);
ObjectHelper.notNull(target, "Exchange should have the " + FileComponent.FILE_EXCHANGE_FILE + " set");
String remoteName = name;
String currentDir = null;
if (endpoint.getConfiguration().isStepwise()) {
// remember current directory
currentDir = getCurrentDirectory();
// change directory to path where the file is to be retrieved
// (must do this as some FTP servers cannot retrieve using absolute path)
String path = FileUtil.onlyPath(name);
if (path != null) {
changeCurrentDirectory(path);
}
// remote name is now only the file name as we just changed directory
remoteName = FileUtil.stripPath(name);
}
log.trace("Client retrieveFile: {}", remoteName);
if (endpoint.getConfiguration().isStreamDownload()) {
InputStream is = client.retrieveFileStream(remoteName);
target.setBody(is);
exchange.getIn().setHeader(RemoteFileComponent.REMOTE_FILE_INPUT_STREAM, is);
result = true;
} else {
// read the entire file into memory in the byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
result = client.retrieveFile(remoteName, bos);
// close the stream after done
IOHelper.close(bos);
target.setBody(bos.toByteArray());
}
// store client reply information after the operation
exchange.getIn().setHeader(FtpConstants.FTP_REPLY_CODE, client.getReplyCode());
exchange.getIn().setHeader(FtpConstants.FTP_REPLY_STRING, client.getReplyString());
// change back to current directory
if (endpoint.getConfiguration().isStepwise()) {
changeCurrentDirectory(currentDir);
}
} catch (IOException e) {
throw new GenericFileOperationFailedException(client.getReplyCode(), client.getReplyString(), e.getMessage(), e);
}
return result;
}
Aggregations