@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()); } }
@Override public InputStream getInputStream(final String remoteFileName, final FlowFile flowFile) throws IOException { final FTPClient client = getClient(flowFile); InputStream in = client.retrieveFileStream(remoteFileName); if (in == null) { throw new IOException(client.getReplyString()); } return in; }
@Override public void deleteDirectory(final FlowFile flowFile, final String remoteDirectoryName) throws IOException { final FTPClient client = getClient(flowFile); final boolean success = client.removeDirectory(remoteDirectoryName); if (!success) { throw new IOException("Failed to remove directory " + remoteDirectoryName + " due to " + client.getReplyString()); } }
/** * utility to create an arbitrary directory hierarchy on the remote ftp server * @param client * @param dirTree the directory tree only delimited with / chars. No file name! * @throws Exception */ private static void ftpCreateDirectoryTree( FTPClient client, String dirTree ) throws IOException { boolean dirExists = true; //tokenize the string and attempt to change into each directory level. If you cannot, then start creating. String[] directories = dirTree.split("/"); for (String dir : directories ) { if (!dir.isEmpty() ) { if (dirExists) { dirExists = client.changeWorkingDirectory(dir); } if (!dirExists) { if (!client.makeDirectory(dir)) { throw new IOException("Unable to create remote directory '" + dir + "'. error='" + client.getReplyString()+"'"); } if (!client.changeWorkingDirectory(dir)) { throw new IOException("Unable to change into newly created remote directory '" + dir + "'. error='" + client.getReplyString()+"'"); } } } } }
@Override public void deleteFile(final FlowFile flowFile, final String path, final String remoteFileName) throws IOException { final FTPClient client = getClient(flowFile); if (path != null) { setWorkingDirectory(path); } if (!client.deleteFile(remoteFileName)) { throw new IOException("Failed to remove file " + remoteFileName + " due to " + client.getReplyString()); } }
public void sendCommands(final List<String> commands, final FlowFile flowFile) throws IOException { if (commands.isEmpty()) { return; } final FTPClient client = getClient(flowFile); for (String cmd : commands) { if (!cmd.isEmpty()) { int result; result = client.sendCommand(cmd); logger.debug(this + " sent command to the FTP server: " + cmd + " for " + flowFile); if (FTPReply.isNegativePermanent(result) || FTPReply.isNegativeTransient(result)) { throw new IOException(this + " negative reply back from FTP server cmd: " + cmd + " reply:" + result + ": " + client.getReplyString() + " for " + flowFile); } } } }
@Override public URL uploadStream(ActiveMQBlobMessage message, InputStream in) throws JMSException, IOException { FTPClient ftp = createFTP(); try { String path = url.getPath(); String workingDir = path.substring(0, path.lastIndexOf("/")); String filename = message.getMessageId().toString().replaceAll(":", "_"); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); String url; if(!ftp.changeWorkingDirectory(workingDir)) { url = this.url.toString().replaceFirst(this.url.getPath(), "")+"/"; } else { url = this.url.toString(); } if (!ftp.storeFile(filename, in)) { throw new JMSException("FTP store failed: " + ftp.getReplyString()); } return new URL(url + filename); } finally { ftp.quit(); ftp.disconnect(); } }
public void deleteFile(ActiveMQBlobMessage message) throws IOException, JMSException { url = message.getURL(); final FTPClient ftp = createFTP(); String path = url.getPath(); try { if (!ftp.deleteFile(path)) { throw new JMSException("Delete file failed: " + ftp.getReplyString()); } } finally { ftp.quit(); ftp.disconnect(); } }
throw new IOException("Failed to store file " + tempFilename + " to " + fullPath + " due to: " + client.getReplyString()); final boolean renameSuccessful = client.rename(tempFilename, filename); if (!renameSuccessful) { throw new IOException("Failed to rename temporary file " + tempFilename + " to " + fullPath + " due to: " + client.getReplyString());
throw new Exception("Can't connect to " + params.serverUrl + ":" + ftpParams.port + ". The server response is: " + ftpClient.getReplyString());
@Override public boolean remove(String path) throws IOException { Assert.hasText(path, "path must not be null"); if (!this.client.deleteFile(path)) { throw new IOException("Failed to delete '" + path + "'. Server replied with: " + this.client.getReplyString()); } else { return true; } }
@Override public void append(InputStream inputStream, String path) throws IOException { Assert.notNull(inputStream, "inputStream must not be null"); Assert.hasText(path, "path must not be null or empty"); boolean completed = this.client.appendFile(path, inputStream); if (!completed) { throw new IOException("Failed to append to '" + path + "'. Server replied with: " + this.client.getReplyString()); } if (this.logger.isInfoEnabled()) { this.logger.info("File has been successfully appended to: " + path); } }
@Override public void read(String path, OutputStream fos) throws IOException { Assert.hasText(path, "path must not be null"); Assert.notNull(fos, "outputStream must not be null"); boolean completed = this.client.retrieveFile(path, fos); if (!completed) { throw new IOException("Failed to copy '" + path + "'. Server replied with: " + this.client.getReplyString()); } this.logger.info("File has been successfully transferred from: " + path); }
@Override public void write(InputStream inputStream, String path) throws IOException { Assert.notNull(inputStream, "inputStream must not be null"); Assert.hasText(path, "path must not be null or empty"); boolean completed = this.client.storeFile(path, inputStream); if (!completed) { throw new IOException("Failed to write to '" + path + "'. Server replied with: " + this.client.getReplyString()); } if (this.logger.isInfoEnabled()) { this.logger.info("File has been successfully transferred to: " + path); } }
@Override public void rename(String pathFrom, String pathTo) throws IOException { this.client.deleteFile(pathTo); boolean completed = this.client.rename(pathFrom, pathTo); if (!completed) { throw new IOException("Failed to rename '" + pathFrom + "' to " + pathTo + "'. Server replied with: " + this.client.getReplyString()); } if (this.logger.isInfoEnabled()) { this.logger.info("File has been successfully renamed from: " + pathFrom + " to " + pathTo); } }
/** * Issue the FTP STAT command to the server. * * @return The status information returned by the server. * @throws 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. * @throws 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. * @param pathname the filename * * @return The status information returned by the server. * @throws 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. * @throws 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; }
/** * 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. * @throws 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. * @throws 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; }
/** * 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. * @throws 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. * @throws 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; }