Пример #1
0
  @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;
  }
Пример #2
0
 @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();
           }
         }
       });
 }
Пример #3
0
 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;
 }
Пример #4
0
 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;
         }
       });
 }
Пример #5
0
 private void login() {
   LOGGER.debug("login");
   executionHandler(
       new Callable<Void>() {
         @Override
         public Void call() throws Exception {
           ftpClient.login(username(), String.valueOf(password()));
           return null;
         }
       });
 }
Пример #6
0
 @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);
         }
       });
 }
Пример #7
0
 @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;
         }
       });
 }
Пример #8
0
 @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;
         }
       });
 }
Пример #9
0
  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();
          }
        });
  }
Пример #10
0
 @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;
         }
       });
 }
Пример #11
0
 private void init() {
   LOGGER.debug("init");
   ftpClient = new FTPClient();
   ftpClient.setControlEncoding("UTF-8");
 }