List of usage examples for org.apache.commons.net.ftp FTPFile getTimestamp
public Calendar getTimestamp()
From source file:org.abstracthorizon.proximity.storage.remote.CommonsNetFtpRemotePeer.java
/** * Construct item properties from get response. * // w ww. ja va 2 s . c om * @param path the path * @param originatingUrlString the originating url string * @param remoteFile the remote file * * @return the item properties * * @throws MalformedURLException the malformed URL exception */ protected ItemProperties constructItemPropertiesFromGetResponse(String path, String originatingUrlString, FTPFile remoteFile) throws MalformedURLException { URL originatingUrl = new URL(originatingUrlString); ItemProperties result = new HashMapItemPropertiesImpl(); result.setDirectoryPath( FilenameUtils.separatorsToUnix(FilenameUtils.getPath(FilenameUtils.getFullPath(path)))); result.setDirectory(remoteFile.isDirectory()); result.setFile(remoteFile.isFile()); result.setLastModified(remoteFile.getTimestamp().getTime()); result.setName(FilenameUtils.getName(originatingUrl.getPath())); if (result.isFile()) { result.setSize(remoteFile.getSize()); } else { result.setSize(0); } result.setRemoteUrl(originatingUrl.toString()); return result; }
From source file:org.apache.camel.component.file.remote.FtpConsumer.java
private RemoteFile<FTPFile> asRemoteFile(String absolutePath, FTPFile file) { RemoteFile<FTPFile> answer = new RemoteFile<FTPFile>(); answer.setEndpointPath(endpointPath); answer.setFile(file);/*w w w .j a v a 2 s .c o m*/ answer.setFileNameOnly(file.getName()); answer.setFileLength(file.getSize()); answer.setDirectory(file.isDirectory()); if (file.getTimestamp() != null) { answer.setLastModified(file.getTimestamp().getTimeInMillis()); } answer.setHostname(((RemoteFileConfiguration) endpoint.getConfiguration()).getHost()); // absolute or relative path boolean absolute = FileUtil.hasLeadingSeparator(absolutePath); answer.setAbsolute(absolute); // create a pseudo absolute name String dir = FileUtil.stripTrailingSeparator(absolutePath); String absoluteFileName = FileUtil.stripLeadingSeparator(dir + "/" + file.getName()); // if absolute start with a leading separator otherwise let it be relative if (absolute) { absoluteFileName = "/" + absoluteFileName; } answer.setAbsoluteFilePath(absoluteFileName); // the relative filename, skip the leading endpoint configured path String relativePath = ObjectHelper.after(absoluteFileName, endpointPath); // skip leading / relativePath = FileUtil.stripLeadingSeparator(relativePath); answer.setRelativeFilePath(relativePath); // the file name should be the relative path answer.setFileName(answer.getRelativeFilePath()); answer.setCharset(endpoint.getCharset()); return answer; }
From source file:org.apache.camel.component.file.remote.strategy.CachedFtpChangedExclusiveReadLockStrategy.java
@Override public boolean acquireExclusiveReadLock(GenericFileOperations<FTPFile> operations, GenericFile<FTPFile> file, Exchange exchange) throws Exception { boolean exclusive = false; FTPFile target = file.getFile();/*from w ww. jav a2 s .c om*/ FTPFile newStats = target; FTPFile oldStats = fileCache.get(target.getName()); if (oldStats != null) { // There is no "created time" available when using FTP. So we can't check the minAge. if (newStats.getSize() >= getMinLength() && (oldStats.getSize() == newStats.getSize() && oldStats.getTimestamp().equals(newStats.getTimestamp())) && ((System.currentTimeMillis() - newStats.getTimestamp().getTimeInMillis()) >= getCheckInterval())) { LOG.debug("File [{}] seems to have stopped changing. Attempting to grab a read lock...", target); exclusive = true; } else { LOG.debug("File [{}] is still changing. Will check it again on the next poll.", target); fileCache.put(target.getName(), newStats); exclusive = false; } } else { LOG.debug("File [{}] is not yet known. Will add it to the cache and check again on the next poll.", target); fileCache.put(target.getName(), newStats); exclusive = false; } if (exclusive) { LOG.debug("Got an exclusive lock for file [{}].", target); fileCache.remove(target.getName()); } return exclusive; }
From source file:org.apache.camel.component.file.remote.strategy.FtpChangedExclusiveReadLockStrategy.java
public boolean acquireExclusiveReadLock(GenericFileOperations<FTPFile> operations, GenericFile<FTPFile> file, Exchange exchange) throws Exception { boolean exclusive = false; LOG.trace("Waiting for exclusive read lock to file: " + file); long lastModified = Long.MIN_VALUE; long length = Long.MIN_VALUE; StopWatch watch = new StopWatch(); while (!exclusive) { // timeout check if (timeout > 0) { long delta = watch.taken(); if (delta > timeout) { CamelLogger.log(LOG, readLockLoggingLevel, "Cannot acquire read lock within " + timeout + " millis. Will skip the file: " + file); // we could not get the lock within the timeout period, so return false return false; }// w ww .jav a2 s.c o m } long newLastModified = 0; long newLength = 0; List<FTPFile> files; if (fastExistsCheck) { // use the absolute file path to only pickup the file we want to check, this avoids expensive // list operations if we have a lot of files in the directory LOG.trace("Using fast exists to update file information for {}", file); files = operations.listFiles(file.getAbsoluteFilePath()); } else { LOG.trace( "Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", file); // fast option not enabled, so list the directory and filter the file name files = operations.listFiles(file.getParent()); } LOG.trace("List files {} found {} files", file.getAbsoluteFilePath(), files.size()); for (FTPFile f : files) { if (f.getName().equals(file.getFileNameOnly())) { newLastModified = f.getTimestamp().getTimeInMillis(); newLength = f.getSize(); } } LOG.trace("Previous last modified: " + lastModified + ", new last modified: " + newLastModified); LOG.trace("Previous length: " + length + ", new length: " + newLength); if (length >= minLength && (newLastModified == lastModified && newLength == length)) { LOG.trace("Read lock acquired."); exclusive = true; } else { // set new base file change information lastModified = newLastModified; length = newLength; boolean interrupted = sleep(); if (interrupted) { // we were interrupted while sleeping, we are likely being shutdown so return false return false; } } } return exclusive; }
From source file:org.apache.camel.component.file.remote.strategy.FtpChangedExclusiveReadLockStrategy.java
public boolean acquireExclusiveReadLock(GenericFileOperations<FTPFile> operations, GenericFile<FTPFile> file, Exchange exchange) throws Exception { boolean exclusive = false; LOG.trace("Waiting for exclusive read lock to file: " + file); long lastModified = Long.MIN_VALUE; long length = Long.MIN_VALUE; StopWatch watch = new StopWatch(); while (!exclusive) { // timeout check if (timeout > 0) { long delta = watch.taken(); if (delta > timeout) { CamelLogger.log(LOG, readLockLoggingLevel, "Cannot acquire read lock within " + timeout + " millis. Will skip the file: " + file); // we could not get the lock within the timeout period, so return false return false; }/*from www . j a v a 2 s .c o m*/ } long newLastModified = 0; long newLength = 0; List<FTPFile> files; if (fastExistsCheck) { // use the absolute file path to only pickup the file we want to check, this avoids expensive // list operations if we have a lot of files in the directory LOG.trace("Using fast exists to update file information for {}", file); files = operations.listFiles(file.getAbsoluteFilePath()); } else { LOG.trace( "Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", file); // fast option not enabled, so list the directory and filter the file name files = operations.listFiles(file.getParent()); } LOG.trace("List files {} found {} files", file.getAbsoluteFilePath(), files.size()); for (FTPFile f : files) { if (f.getName().equals(file.getFileNameOnly())) { newLength = f.getSize(); newLastModified = f.getTimestamp().getTimeInMillis(); } } LOG.trace("Previous last modified: " + lastModified + ", new last modified: " + newLastModified); LOG.trace("Previous length: " + length + ", new length: " + newLength); if (length >= minLength && (newLastModified == lastModified && newLength == length)) { LOG.trace("Read lock acquired."); exclusive = true; } else { // set new base file change information lastModified = newLastModified; length = newLength; boolean interrupted = sleep(); if (interrupted) { // we were interrupted while sleeping, we are likely being shutdown so return false return false; } } } return exclusive; }
From source file:org.apache.camel.component.file.remote.strategy.FtpChangedExclusiveReadLockStrategy.java
public boolean acquireExclusiveReadLock(GenericFileOperations<FTPFile> operations, GenericFile<FTPFile> file, Exchange exchange) throws Exception { boolean exclusive = false; LOG.trace("Waiting for exclusive read lock to file: " + file); long lastModified = Long.MIN_VALUE; long length = Long.MIN_VALUE; StopWatch watch = new StopWatch(); while (!exclusive) { // timeout check if (timeout > 0) { long delta = watch.taken(); if (delta > timeout) { CamelLogger.log(LOG, readLockLoggingLevel, "Cannot acquire read lock within " + timeout + " millis. Will skip the file: " + file); // we could not get the lock within the timeout period, so return false return false; }/* w w w. ja v a2s .com*/ } long newLastModified = 0; long newLength = 0; List<FTPFile> files; if (fastExistsCheck) { // use the absolute file path to only pickup the file we want to check, this avoids expensive // list operations if we have a lot of files in the directory LOG.trace("Using fast exists to update file information for {}", file); files = operations.listFiles(file.getAbsoluteFilePath()); } else { LOG.trace( "Using full directory listing to update file information for {}. Consider enabling fastExistsCheck option.", file); // fast option not enabled, so list the directory and filter the file name files = operations.listFiles(file.getParent()); } LOG.trace("List files {} found {} files", file.getAbsoluteFilePath(), files.size()); for (FTPFile f : files) { if (f.getName().equals(file.getFileNameOnly())) { newLength = f.getSize(); if (f.getTimestamp() != null) { newLastModified = f.getTimestamp().getTimeInMillis(); } } } LOG.trace("Previous last modified: " + lastModified + ", new last modified: " + newLastModified); LOG.trace("Previous length: " + length + ", new length: " + newLength); if (length >= minLength && (newLastModified == lastModified && newLength == length)) { LOG.trace("Read lock acquired."); exclusive = true; } else { // set new base file change information lastModified = newLastModified; length = newLength; boolean interrupted = sleep(); if (interrupted) { // we were interrupted while sleeping, we are likely being shutdown so return false return false; } } } return exclusive; }
From source file:org.apache.ftpserver.clienttests.ListTest.java
public void testListFile() throws Exception { TEST_DIR1.mkdirs();//from w ww . j ava2 s. c om TEST_FILE1.createNewFile(); TEST_FILE2.createNewFile(); FTPFile[] files = client.listFiles(TEST_FILE1.getName()); assertEquals(1, files.length); FTPFile file = getFile(files, TEST_FILE1.getName()); assertEquals(TEST_FILE1.getName(), file.getName()); assertEquals(0, file.getSize()); assertEquals("group", file.getGroup()); assertEquals("user", file.getUser()); assertTrue(file.isFile()); assertFalse(file.isDirectory()); Calendar expectedTimestamp = Calendar.getInstance(); expectedTimestamp.setTimeInMillis(TEST_FILE1.lastModified()); // server does not supply seconds and milliseconds expectedTimestamp.clear(Calendar.SECOND); expectedTimestamp.clear(Calendar.MILLISECOND); assertEquals(expectedTimestamp, file.getTimestamp()); }
From source file:org.apache.hadoop.fs.ftp.FTPFileSystem.java
/** * Convert the file information in FTPFile to a {@link FileStatus} object. * * /* ww w . j av a 2 s. com*/ * @param ftpFile * @param parentPath * @return FileStatus */ private FileStatus getFileStatus(FTPFile ftpFile, Path parentPath) { long length = ftpFile.getSize(); boolean isDir = ftpFile.isDirectory(); int blockReplication = 1; // Using default block size since there is no way in FTP client to know of // block sizes on server. The assumption could be less than ideal. long blockSize = DEFAULT_BLOCK_SIZE; long modTime = ftpFile.getTimestamp().getTimeInMillis(); long accessTime = 0; FsPermission permission = getPermissions(ftpFile); String user = ftpFile.getUser(); String group = ftpFile.getGroup(); Path filePath = new Path(parentPath, ftpFile.getName()); return new FileStatus(length, isDir, blockReplication, blockSize, modTime, accessTime, permission, user, group, filePath.makeQualified(this)); }
From source file:org.apache.nutch.protocol.ftp.FtpResponse.java
private void getFileAsHttpResponse(String path, long lastModified) throws IOException { ByteArrayOutputStream os = null; List<FTPFile> list = null; try {//from w w w . j a v a 2 s .co m // first get its possible attributes list = new LinkedList<FTPFile>(); ftp.client.retrieveList(path, list, ftp.maxContentLength, ftp.parser); FTPFile ftpFile = (FTPFile) list.get(0); this.headers.set(Response.CONTENT_LENGTH, new Long(ftpFile.getSize()).toString()); this.headers.set(Response.LAST_MODIFIED, HttpDateFormat.toString(ftpFile.getTimestamp())); // don't retrieve the file if not changed. if (ftpFile.getTimestamp().getTimeInMillis() <= lastModified) { code = 304; return; } os = new ByteArrayOutputStream(ftp.getBufferSize()); ftp.client.retrieveFile(path, os, ftp.maxContentLength); this.content = os.toByteArray(); // // approximate bytes sent and read // if (this.httpAccounting != null) { // this.httpAccounting.incrementBytesSent(path.length()); // this.httpAccounting.incrementBytesRead(this.content.length); // } this.code = 200; // http OK } catch (FtpExceptionControlClosedByForcedDataClose e) { // control connection is off, clean up // ftp.client.disconnect(); if ((ftp.followTalk) && (Ftp.LOG.isInfoEnabled())) { Ftp.LOG.info("delete client because server cut off control channel: " + e); } ftp.client = null; // in case this FtpExceptionControlClosedByForcedDataClose is // thrown by retrieveList() (not retrieveFile()) above, if (os == null) { // indicating throwing by retrieveList() // throw new FtpException("fail to get attibutes: "+path); if (Ftp.LOG.isWarnEnabled()) { Ftp.LOG.warn("Please try larger maxContentLength for ftp.client.retrieveList(). " + e); } // in a way, this is our request fault this.code = 400; // http Bad request return; } FTPFile ftpFile = (FTPFile) list.get(0); this.headers.set(Response.CONTENT_LENGTH, new Long(ftpFile.getSize()).toString()); // this.headers.put("content-type", "text/html"); this.headers.set(Response.LAST_MODIFIED, HttpDateFormat.toString(ftpFile.getTimestamp())); this.content = os.toByteArray(); if (ftpFile.getTimestamp().getTimeInMillis() <= lastModified) { code = 304; return; } // // approximate bytes sent and read // if (this.httpAccounting != null) { // this.httpAccounting.incrementBytesSent(path.length()); // this.httpAccounting.incrementBytesRead(this.content.length); // } this.code = 200; // http OK } catch (FtpExceptionCanNotHaveDataConnection e) { if (FTPReply.isPositiveCompletion(ftp.client.cwd(path))) { // it is not a file, but dir, so redirect as a dir this.headers.set(Response.LOCATION, path + "/"); this.code = 300; // http redirect // fixme, should we do ftp.client.cwd("/"), back to top dir? } else { // it is not a dir either this.code = 404; // http Not Found } } catch (FtpExceptionUnknownForcedDataClose e) { // Please note control channel is still live. // in a way, this is our request fault if (Ftp.LOG.isWarnEnabled()) { Ftp.LOG.warn("Unrecognized reply after forced close of data channel. " + "If this is acceptable, please modify Client.java accordingly. " + e); } this.code = 400; // http Bad Request } }
From source file:org.apache.nutch.protocol.ftp.FtpResponse.java
private byte[] list2html(List<FTPFile> list, String path, boolean includeDotDot) { // StringBuffer x = new // StringBuffer("<!doctype html public \"-//ietf//dtd html//en\"><html><head>"); StringBuffer x = new StringBuffer("<html><head>"); x.append("<title>Index of " + path + "</title></head>\n"); x.append("<body><h1>Index of " + path + "</h1><pre>\n"); if (includeDotDot) { x.append("<a href='../'>../</a>\t-\t-\t-\n"); }//w w w . j a va2 s .co m for (int i = 0; i < list.size(); i++) { FTPFile f = (FTPFile) list.get(i); String name = f.getName(); String time = HttpDateFormat.toString(f.getTimestamp()); if (f.isDirectory()) { // some ftp server LIST "." and "..", we skip them here if (name.equals(".") || name.equals("..")) continue; x.append("<a href='" + name + "/" + "'>" + name + "/</a>\t"); x.append(time + "\t-\n"); } else if (f.isFile()) { x.append("<a href='" + name + "'>" + name + "</a>\t"); x.append(time + "\t" + f.getSize() + "\n"); } else { // ignore isSymbolicLink() // ignore isUnknown() } } x.append("</pre></body></html>\n"); return new String(x).getBytes(); }