Package org.globus.ftp

Examples of org.globus.ftp.GridFTPClient


     * @param gsCredential
     * @param localFile
     * @throws GFacException
     */
    public void downloadFile(URI destURI, GSSCredential gsCredential, File localFile) throws ToolsException {
        GridFTPClient ftpClient = null;
        GridFTPContactInfo contactInfo = new GridFTPContactInfo(destURI.getHost(), destURI.getPort());
        try {
            String remoteFile = destURI.getPath();

            log.info("The local temp file is " + localFile);
            log.info("the remote file is " + remoteFile);

            log.debug("Setup GridFTP Client");

            ftpClient = new GridFTPClient(contactInfo.hostName, contactInfo.port);
            ftpClient.setAuthorization(new HostAuthorization(GridFtp.HOST));
            ftpClient.authenticate(gsCredential);
            ftpClient.setDataChannelAuthentication(DataChannelAuthentication.SELF);
            makeFileTransferExternalConfigurations(ftpClient, null);

            log.debug("Downloading file");
            if (checkBinaryExtensions(remoteFile)) {
                log.debug("Transfer mode is set to Binary to download a file");
                ftpClient.setType(Session.TYPE_IMAGE);
            }

            ftpClient.get(remoteFile, localFile);

            log.info("Download file to:" + localFile + " is done");

        } catch (ServerException e) {
            throw new ToolsException("Cannot download file from GridFTP:" + contactInfo.toString(), e);
        } catch (IOException e) {
            throw new ToolsException("Cannot download file from GridFTP:" + contactInfo.toString(), e);
        } catch (ClientException e) {
            throw new ToolsException("Cannot download file from GridFTP:" + contactInfo.toString(), e);
        } finally {
            if (ftpClient != null) {
                try {
                    //ftpClient.close();
                    ftpClient.close(false);
                } catch (Exception e) {
                    log.warn("Cannot close GridFTP client connection",e);
                }
            }
        }
View Full Code Here


     * @throws ServerException
     * @throws ClientException
     * @throws IOException
     */
    public void transfer(URI srchost, URI desthost, GSSCredential gssCred, boolean srcActive) throws ToolsException {
        GridFTPClient destClient = null;
        GridFTPClient srcClient = null;

        try {
            destClient = new GridFTPClient(desthost.getHost(), desthost.getPort());
            destClient.setAuthorization(new HostAuthorization(GridFtp.HOST));
            destClient.authenticate(gssCred);

            if (checkBinaryExtensions(desthost.getPath())) {
                log.debug("Transfer mode is set to Binary");
                destClient.setType(Session.TYPE_IMAGE);
            }

            srcClient = new GridFTPClient(srchost.getHost(), srchost.getPort());
            srcClient.setAuthorization(new HostAuthorization(GridFtp.HOST));
            srcClient.authenticate(gssCred);
            makeFileTransferExternalConfigurations(srcClient, destClient);

            if (checkBinaryExtensions(srchost.getPath())) {
                log.debug("Transfer mode is set to Binary");
                srcClient.setType(Session.TYPE_IMAGE);
            }

            if (srcActive) {
                log.debug("Set src active");
                HostPort hp = destClient.setPassive();
                srcClient.setActive(hp);
            } else {
                log.debug("Set dst active");
                HostPort hp = srcClient.setPassive();
                destClient.setActive(hp);
            }

            log.debug("Start transfer file from GridFTP:" + srchost.toString() + " to " + desthost.toString());

            /**
             * Transfer a file. The transfer() function blocks until the transfer is complete.
             */
            srcClient.transfer(srchost.getPath(), destClient, desthost.getPath(), false, null);
            if (srcClient.getSize(srchost.getPath()) == destClient.getSize(desthost.getPath())) {
                log.debug("CHECK SUM OK");
            } else {
                log.debug("****CHECK SUM FAILED****");
            }

        } catch (ServerException e) {
            throw new ToolsException("Cannot transfer file from GridFTP:" + srchost.toString() + " to "
                    + desthost.toString(), e);
        } catch (IOException e) {
            throw new ToolsException("Cannot transfer file from GridFTP:" + srchost.toString() + " to "
                    + desthost.toString(), e);
        } catch (ClientException e) {
            throw new ToolsException("Cannot transfer file from GridFTP:" + srchost.toString() + " to "
                    + desthost.toString(), e);
        } finally {
            if (destClient != null) {
                try {
                    destClient.close();
                } catch (Exception e) {
                    log.warn("Cannot close GridFTP client connection at Desitnation:" + desthost.toString());
                }
            }
            if (srcClient != null) {
                try {
                    srcClient.close();
                } catch (Exception e) {
                    log.warn("Cannot close GridFTP client connection at Source:" + srchost.toString(),e);
                }
            }
        }
View Full Code Here

   * @throws ToolsException
   */
    @SuppressWarnings("unchecked")
  public List<String> listDir(URI dirURI, GSSCredential gssCred) throws ToolsException {
      List<String> files = new  ArrayList<String>();
      GridFTPClient srcClient = null;
      try {
        GridFTPContactInfo contactInfo = new GridFTPContactInfo(dirURI.getHost(), dirURI.getPort());

        srcClient = new GridFTPClient(contactInfo.hostName, contactInfo.port);
        srcClient.setAuthorization(new HostAuthorization(GridFtp.HOST));
        srcClient.authenticate(gssCred);
        srcClient.setDataChannelAuthentication(DataChannelAuthentication.SELF);
        srcClient.setType(Session.TYPE_ASCII);
        srcClient.changeDir(dirURI.getPath());
        makelistDirExternalConfigurations(srcClient, srcClient.getCurrentDir());
       
        Vector<Object> fileInfo = null;
        try {
          fileInfo = srcClient.mlsd();
        } catch (Throwable e) {
          fileInfo = srcClient.list();
        }

        if (!fileInfo.isEmpty()) {
          for (int j = 0; j < fileInfo.size(); ++j) {
            String name = null;
            if (fileInfo.get(j) instanceof MlsxEntry) {
              name = ((MlsxEntry) fileInfo.get(j)).getFileName();
            } else if (fileInfo.get(j) instanceof FileInfo) {
              name = ((FileInfo) fileInfo.get(j)).getName();
            } else {
              throw new ToolsException("Unsupported type returned by gridftp " + fileInfo.get(j));
            }

            if (!name.equals(".") && !name.equals("..")) {
              URI uri = GFacUtils.createGsiftpURI(contactInfo.hostName, dirURI.getPath() + File.separator + name);
              files.add(uri.getPath());
            }
          }
        }
        return files;
      } catch (IOException e) {
        throw new ToolsException("Could not list directory: " + dirURI.toString() ,e);
      } catch (ServerException e) {
        throw new ToolsException("Could not list directory: " + dirURI.toString() ,e);
      } catch (ClientException e) {
        throw new ToolsException("Could not list directory: " + dirURI.toString() ,e);
      } catch (URISyntaxException e) {
        throw new ToolsException("Error creating URL of listed files: " + dirURI.toString() ,e);
      } finally {
        if (srcClient != null) {
                  try {
                      srcClient.close();
                  } catch (Exception e) {
                      log.warn("Cannot close GridFTP client connection", e);
                  }
              }
    }
View Full Code Here

TOP

Related Classes of org.globus.ftp.GridFTPClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.