Steffen,

I tried it with a simple 2-party transfer using the jglobus API and I
can't reproduce what you describe. Try the attached code.
Does it show the same results for you?
Adjust the values of the variables in TwoPartyTransferMain.java.
If you don't have a MyProxy server available, replace the MyProxy code.

Compile and run:

source ${GLOBUS_LOCATION}/etc/globus-devel-env.sh
javac TwoPartyTransferMain.java
java TwoPartyTransferMain

Martin

Steffen Limmer wrote:
> Hello Martin,
> 
> thanks for your answer.
> 
>> Does setting the type to binary (Session.TYPE_IMAGE) help?
> 
> Unfortunately this doesn't help.
> 
> Regards,
> Steffen
> 
>> Martin
>>
>> Steffen Limmer wrote:
>>> Hello,
>>>
>>> i want to transfer files with the put method of the
>> org.globus.ftp.GridFTPClient class.
>>>  
>>> Some of the files are self extracting tar archives that contain newline
>> characters in the form of ^M. When i transfer such a file for some reason
>> the ^M will be erased or replaced by \n and so the archiv becomes useless.
>>> I tried to copy the files locally with java and everything works fine.
>> So it should not be a problem with the java file i/o. Also with
>> globus-url-copy everything works as expected.
>>> Only with the GridFTPClient appears the problem.
>>> Has anybody an idea what i can do to fix this?
>>>
>>> Thanks in advance 
>>> and regards,
>>> Steffen 
>>>   
> 

import java.io.File;
import java.util.List;
import java.util.LinkedList;
import java.util.Map;
import java.util.HashMap;
import java.util.Vector;
import org.ietf.jgss.GSSCredential;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.globus.ftp.FileInfo;
import org.globus.ftp.GridFTPClient;
import org.globus.ftp.Session;

public class TwoPartyTransfer {

    private Log log = LogFactory.getLog(TwoPartyTransfer.class);
    
    /**
     * Download a file from a GridFTP server.
     */
    public void downloadFile(
        String host,
        int port,
        GSSCredential credential,        
        String serverFile,
        String localFile)
        throws Exception {

        GridFTPClient client = null;
        
        try {
            log.debug("Creating GridFTP client");
            client = this.createClient(host, port, credential);
            log.debug("Downloading file " + serverFile + " from " + host);
            this.downloadFile(client, serverFile, localFile);
            log.debug("Downloaded file " + localFile);
        } finally {
            this.closeClient(client);
        }
     } 

    /**
     * Upload a file to a GridFTP server.
     */
    public void uploadFile(
        String host,
        int port,
        GSSCredential credential,        
        String serverFile,
        String localFile)
        throws Exception {

        GridFTPClient client = null;
        
        try {
            log.debug("Creating GridFTP client");
            client = this.createClient(host, port, credential);
            log.debug("Uploading file " + localFile);
            this.uploadFile(client, localFile, serverFile);
            log.debug("Uploaded file to " + serverFile + " on " + host);
        } catch (Exception e) {
            log.error("Error downloading file.", e);
            throw e;
        } finally {
            this.closeClient(client);
        }
     } 

    /**
     * Recursively download the content of a remote directory into a local 
directory.
     * The local directory will be created if it does not exist.
     */
    public void downloadDir(
        String host,
        int port,
        GSSCredential credential,
        String serverDir,
        String localDir)
        throws Exception {

        GridFTPClient listClient = null;
        GridFTPClient transferClient = null;
        LinkedList<String> dirs = new LinkedList<String>();
        HashMap<String,String> files = new HashMap<String,String>();
        
        try {
            log.debug("Create GridFTP client for listing");
            listClient = this.createClient(host, port, credential);
            log.debug("Get information of dirs and files from server");
            this.createInfoFromRemote(listClient, serverDir, localDir, dirs, 
files);
            log.debug("Create local directories");
            this.createLocalDirs(dirs);
        } finally {
            this.closeClient(listClient);
        }
        
        try {
            log.debug("Create GridFTP client for file transfers");
            transferClient = this.createClient(host, port, credential);
            log.debug("Download files from server");
            this.downloadFiles(transferClient, files);
        } finally {
            this.closeClient(transferClient);
        }
     } 

    /**
     * Recursively upload the content of a local directory into a remote 
directory.
     * The remote directory will be created if it does not exist.
     */
    public void uploadDir(
        String host,
        int port,
        GSSCredential credential,
        String serverDir,
        String localDir)
        throws Exception {

        GridFTPClient listClient = null;
        GridFTPClient transferClient = null;
        LinkedList<String> dirs = new LinkedList<String>();
        HashMap<String,String> files = new HashMap<String,String>();

        try {
            log.debug("Create GridFTP client for listings and directory 
creation");
            listClient = this.createClient(host, port, credential);
            log.debug("Get information of dirs and files on local machine");
            this.createInfoFromLocal(serverDir, localDir, dirs, files);
            log.debug("Create directories on server");
            this.createRemoteDirs(listClient, dirs);
        } finally {
            this.closeClient(listClient);
        }
        
        try {
            log.debug("Create GridFTP client for file transfers");
            transferClient = this.createClient(host, port, credential);
            log.debug("Upload files to server");
            this.uploadFiles(transferClient, files);
        } finally {
            this.closeClient(transferClient);
        }
    }

    /**
     * Create a list of dirs that must be created on the remote side
     * and a map of files that have to be transferred.
     */
    private void createInfoFromLocal(
        String remoteDir,
        String localDir,
        List<String> dirs,
        Map<String,String> files) 
        throws Exception {
        
        File baseDir = new File(localDir);
        File[] fs = baseDir.listFiles();
        dirs.add(remoteDir);
        for (File tmp : fs) {
            String localFile = new File(localDir, 
tmp.getName()).getAbsolutePath();
            String remoteFile = new File(remoteDir, 
tmp.getName()).getAbsolutePath();
            if (tmp.isDirectory()) {
                dirs.add(remoteFile);
                createInfoFromLocal(remoteFile, localFile, dirs, files);
            } else {
                files.put(remoteFile, localFile);
            }
        }
    }

    /**
     * Create a list of dirs that must be created on the local side
     * and a map of files that have to be transferred.
     */
    private void createInfoFromRemote(
        GridFTPClient client,
        String remoteDir,
        String localDir,
        List<String> dirs,
        Map<String,String> files) 
        throws Exception {

        client.setPassive();
        client.setLocalActive();
        client.changeDir(remoteDir);
        Vector<FileInfo> remDir = client.list();
        dirs.add(localDir);
        for (FileInfo info : remDir) {
            if (!(info.getName().equalsIgnoreCase(".")) && 
!(info.getName().equalsIgnoreCase(".."))) {
                String remoteFile = new File(remoteDir, 
info.getName()).getAbsolutePath();
                String localFile = new File(localDir, 
info.getName()).getAbsolutePath();
                if (info.isDirectory()) {
                    dirs.add(localFile);
                    createInfoFromRemote(client, remoteFile, localFile, dirs, 
files);
                } else {
                    files.put(remoteFile, localFile);                        
                }
            }
        }
    }

    private void createRemoteDirs(
        GridFTPClient client,
        List<String> dirs)
        throws Exception {
     
        client.setPassive();
        client.setLocalActive();
        for (String dir : dirs) {
            if (!client.exists(dir)) {
                client.makeDir(dir);
            }
        }
    }

    private void createLocalDirs(
        List<String> dirs)
        throws Exception {
     
        for (String dir : dirs) {
            new File(dir).mkdirs();
        }
    }
    
    private void downloadFiles(
        GridFTPClient client,
        Map<String,String> files)
        throws Exception {
     
        for (Map.Entry<String, String> entry : files.entrySet()) {
            this.downloadFile(client, entry.getKey(), entry.getValue());
        }
    }

    private void uploadFiles(
        GridFTPClient client,
        Map<String,String> files)
        throws Exception {
     
        for (Map.Entry<String, String> entry : files.entrySet()) {
            this.uploadFile(client, entry.getValue(), entry.getKey());
        }
    }

    private void downloadFile(
        GridFTPClient client,
        String sourceFile,
        String destFile)
        throws Exception {
        
        client.setPassive();
        client.setLocalActive();
        client.get(sourceFile, new File(destFile));
    }

    private void uploadFile(
        GridFTPClient client,
        String sourceFile,
        String destFile)
        throws Exception {
        
        client.setPassive();
        client.setLocalActive();
        client.put(new File(sourceFile), destFile, false);
    }

    private GridFTPClient createClient(
        String host,
        int port,
        GSSCredential credential)
        throws Exception {
        
        GridFTPClient client = new GridFTPClient(host, port);
        client.authenticate(credential);
        client.setType(Session.TYPE_IMAGE);
        return client;
    }

    private void closeClient(
        GridFTPClient client)
        throws Exception {
     
        if (client != null) {
            client.close();
        }
    }
}
import org.ietf.jgss.GSSCredential;
import org.globus.myproxy.MyProxy;

public class TwoPartyTransferMain {

    public static void main(String[] args)
        throws Exception {

        String myproxyHost = "...";
        int myproxyPort = 7512;
        String myproxyUser = "...";
        String myproxyPassword = "...";
        String gridftpHost = "...";
        int gridftpPort = 2811;
        
        String serverFile = "/tmp/example.sh";
        String localFile = "/tmp/test.sh";
        
        try {
            MyProxy myproxy = new MyProxy(myproxyHost, myproxyPort);
            GSSCredential credential = myproxy.get(myproxyUser, 
myproxyPassword, 0);
            TwoPartyTransfer tpt = new TwoPartyTransfer();
            tpt.downloadFile(gridftpHost, gridftpPort, credential, serverFile, 
localFile);
         } catch (Exception e) {
             e.printStackTrace();
         }
    }

}

Reply via email to