예제 #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
 @Override
 public void disconnect() {
   LOGGER.info("Disconnecting from " + username() + "@" + host() + ":" + String.valueOf(port()));
   executionHandler(
       new Callable<Void>() {
         @Override
         public Void call() throws Exception {
           reconnect = false;
           if (ftpClient.isConnected()) {
             ftpClient.logout();
             ftpClient.disconnect();
           }
           return null;
         }
       });
 }
예제 #10
0
 @Override
 public void connect() {
   LOGGER.info("Connecting to " + username() + "@" + host() + ":" + String.valueOf(port()));
   reconnect = true;
   try {
     ftpClient.connect(host(), port());
   } catch (IOException e) {
     throw new VirtualFileException(e);
   }
   if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
     disconnect();
     return;
   }
   login();
   setFileType(FTP.BINARY_FILE_TYPE);
 }
예제 #11
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();
          }
        });
  }
예제 #12
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;
         }
       });
 }
예제 #13
0
 private void init() {
   LOGGER.debug("init");
   ftpClient = new FTPClient();
   ftpClient.setControlEncoding("UTF-8");
 }