@Override public boolean exists(final String path) { LOGGER.debug("exists (path : {})", path); Boolean exists = executionHandler( new Callable<Boolean>() { @Override public Boolean call() throws Exception { int replyCode = ftpClient.stat(path); String replyText = ftpClient.getReplyString(); if (!FTPReply.isPositiveCompletion(replyCode)) { // this replyText code is set when file doesn't exist on the server if (FTPReply.FILE_ACTION_NOT_TAKEN == replyCode) return false; else { LOGGER.warn("Unexpected Reply (Code: {}, Text: '{}'", replyCode, replyText); } } String[] replyTextParts = replyText.split("\n"); if (replyTextParts.length <= 2) { if (ftpClient.changeWorkingDirectory(path)) ftpClient.changeToParentDirectory(); else return false; } return true; } }); LOGGER.debug("Returns: {}", exists); return exists; }
@Override public FTPFile getFileInfo(final String path) { LOGGER.debug("getFileInfo (path: {})", path); return executionHandler( new Callable<FTPFile>() { @Override public FTPFile call() throws Exception { if ("/".equals(path)) { FTPFile rootFile = new FTPFile(); rootFile.setName("/"); rootFile.setTimestamp(GregorianCalendar.getInstance()); return rootFile; } else { String lastPathPart = UrlUtils.getLastPathPart(path); String parentPath = UrlUtils.getParentPath(path); ftpClient.changeWorkingDirectory(parentPath); FTPFile[] ftpFiles = ftpClient.listFiles(); for (FTPFile ftpFile : ftpFiles) { if (ftpFile.getName().equals(lastPathPart)) { return ftpFile; } } return new FTPFile(); } } }); }
private <T> T executionHandler(Callable<T> action) { int connectionAttempts = MAX_CONNECTION_ATTEMPTS; while (connectionAttempts > 0) { try { checkConnection(); return action.call(); } catch (FTPConnectionClosedException e) { LOGGER.debug("Exception", e); if (reconnect) { LOGGER.warn("Server closed connection. Reconnecting."); connectionAttempts--; connect(); } } catch (Exception e) { LOGGER.debug("Exception", e); throw new VirtualFileException(e); } } return null; }
private void setFileType(final int fileType) { LOGGER.debug("setFileType (fileType: {})", fileType); executionHandler( new Callable<Void>() { @Override public Void call() throws Exception { ftpClient.setFileType(fileType); return null; } }); }
private void login() { LOGGER.debug("login"); executionHandler( new Callable<Void>() { @Override public Void call() throws Exception { ftpClient.login(username(), String.valueOf(password())); return null; } }); }
@Override public OutputStream getOutputStream(final String path) { LOGGER.debug("getOutputStream (path : {})", path); return executionHandler( new Callable<FtpOutputStream>() { @Override public FtpOutputStream call() throws Exception { return new FtpOutputStream(ftpClient.storeFileStream(path), ftpClient); } }); }
@Override public void deleteFile(final String path) { LOGGER.debug("deleteFile (path : {})", path); executionHandler( new Callable<Void>() { @Override public Void call() throws Exception { int replyCode = ftpClient.dele(path); String replyText = ftpClient.getReplyString(); if (!FTPReply.isPositiveCompletion(replyCode)) LOGGER.warn("Unexpected Reply (Code: {}, Text: '{}'", replyCode, replyText); return null; } }); }
@Override public void createFile(final String path) { LOGGER.debug("createFile (path : {})", path); executionHandler( new Callable<Void>() { @Override public Void call() throws Exception { try (ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {})) { checkConnection(); ftpClient.storeFile(path, inputStream); } return null; } }); }
public List<FTPFile> list(final String path) { LOGGER.debug("list (path: {})", path); return executionHandler( new Callable<List<FTPFile>>() { @Override public List<FTPFile> call() throws Exception { int replyCode = ftpClient.cwd(path); String replyText = ftpClient.getReplyString(); if (FTPReply.isPositiveCompletion(replyCode)) { return Arrays.asList(ftpClient.listFiles()); } else { LOGGER.warn("Unexpected Reply (Code: {}, Text: '{}'", replyCode, replyText); } return Collections.emptyList(); } }); }
@Override public void createDirectory(final String path) { LOGGER.debug("createDirectory (path : {})", path); executionHandler( new Callable<Void>() { @Override public Void call() throws Exception { int replyCode = ftpClient.mkd(path); String replyText = ftpClient.getReplyString(); if (!FTPReply.isPositiveCompletion(replyCode)) { // this replyText code is set when file already exists on the server if (FTPReply.FILE_UNAVAILABLE == replyCode) throw new FileAlreadyExistsException(path); else LOGGER.warn("Unexpected Reply (Code: {}, Text: '{}'", replyCode, replyText); } return null; } }); }
private void init() { LOGGER.debug("init"); ftpClient = new FTPClient(); ftpClient.setControlEncoding("UTF-8"); }