Example usage for org.apache.commons.net ProtocolCommandEvent getMessage

List of usage examples for org.apache.commons.net ProtocolCommandEvent getMessage

Introduction

In this page you can find the example usage for org.apache.commons.net ProtocolCommandEvent getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the entire message sent to or received from the server.

Usage

From source file:de.ipk_gatersleben.ag_nw.graffiti.services.GUIhelper.java

private static ArrayList<String> listFiles(final BackgroundTaskStatusProviderSupportingExternalCall status,
        String downloadURL, String server, String remotePath, final FTPClient ftp) {
    String username;/* w  w w.j av  a 2  s. c om*/
    String password;
    username = "anonymous@" + server;
    password = "anonymous";

    final ObjectRef myoutputstream = new ObjectRef();

    ftp.addProtocolCommandListener(new ProtocolCommandListener() {
        public void protocolCommandSent(ProtocolCommandEvent arg0) {
            status.setCurrentStatusText1("Command: " + arg0.getMessage());
        }

        public void protocolReplyReceived(ProtocolCommandEvent arg0) {
            status.setCurrentStatusText2("Message: " + arg0.getMessage());
            if (myoutputstream.getObject() != null) {
                String msg = arg0.getMessage();
                if (msg.indexOf("Opening BINARY mode") >= 0) {
                    if (msg.indexOf("(") > 0) {
                        msg = msg.substring(msg.indexOf("(") + "(".length());
                        if (msg.indexOf(" ") > 0) {
                            msg = msg.substring(0, msg.indexOf(" "));
                            try {
                                long max = Long.parseLong(msg);
                                MyOutputStream os = (MyOutputStream) myoutputstream.getObject();
                                os.setMaxBytes(max);
                            } catch (Exception e) {
                                System.out.println(
                                        "Could not determine file length for detailed progress information");
                            }
                        }
                    }
                }
            }
        }
    });

    System.out.println("FTP LIST DIRECTORY: " + downloadURL);

    try {
        if (ftp.isConnected()) {
            status.setCurrentStatusText2("Using open FTP connection");
            System.out.println("Reusing open FTP connection");
        } else {
            ftp.connect(server);
            int reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                status.setCurrentStatusText1("Can't connect to FTP server");
                status.setCurrentStatusText2("ERROR");
                return new ArrayList<String>();
            }
            if (!ftp.login("anonymous", "anonymous")) {
                if (!ftp.login(username, password)) {
                    ftp.disconnect();
                    status.setCurrentStatusText1("Can't login to FTP server");
                    status.setCurrentStatusText2("ERROR");
                    return new ArrayList<String>();
                }
            }
            status.setCurrentStatusText1("Set Binary Transfer Mode");
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            status.setCurrentStatusText2("Activate Passive Transfer Mode");
            ftp.enterLocalPassiveMode();
        }
        status.setCurrentStatusText1("Start download...");
        status.setCurrentStatusText2("Please Wait.");

        ftp.setRemoteVerificationEnabled(false);

        FTPFile[] res = ftp.listFiles(remotePath);

        ArrayList<String> result = new ArrayList<String>();

        for (FTPFile r : res) {
            result.add(r.getName());
        }

        return result;
    } catch (Exception err) {
        ErrorMsg.addErrorMessage(err);
        if (ftp != null && ftp.isConnected()) {
            try {
                System.out.println("Disconnect FTP connection (following error condition)");
                ftp.disconnect();
            } catch (Exception err2) {
                ErrorMsg.addErrorMessage(err2);
            }
        }
        return new ArrayList<String>();
    }
}

From source file:de.ipk_gatersleben.ag_nw.graffiti.services.GUIhelper.java

private static boolean processDownload(final BackgroundTaskStatusProviderSupportingExternalCallImpl status,
        String downloadURL, String targetFileName, ObjectRef lastStatus, final int thisRun, String server,
        String remote, final FTPClient ftp) {
    String username;//www .  j  a  v  a 2s  . co m
    String password;
    String local;
    username = "anonymous@" + server;
    password = "anonymous";
    local = targetFileName;

    final ObjectRef myoutputstream = new ObjectRef();

    ftp.addProtocolCommandListener(new ProtocolCommandListener() {
        public void protocolCommandSent(ProtocolCommandEvent arg0) {
            // System.out.print("out: " + arg0.getMessage());
            status.setCurrentStatusText1("Command: " + arg0.getMessage());
        }

        public void protocolReplyReceived(ProtocolCommandEvent arg0) {
            // System.out.print("in : " + arg0.getMessage());
            status.setCurrentStatusText2("Message: " + arg0.getMessage());
            if (myoutputstream.getObject() != null) {
                String msg = arg0.getMessage();
                if (msg.indexOf("Opening BINARY mode") >= 0) {
                    if (msg.indexOf("(") > 0) {
                        msg = msg.substring(msg.indexOf("(") + "(".length());
                        if (msg.indexOf(" ") > 0) {
                            msg = msg.substring(0, msg.indexOf(" "));
                            try {
                                long max = Long.parseLong(msg);
                                MyOutputStream os = (MyOutputStream) myoutputstream.getObject();
                                os.setMaxBytes(max);
                            } catch (Exception e) {
                                System.out.println(
                                        "Could not determine file length for detailed progress information");
                            }
                        }
                    }
                }
            }
        }
    });

    System.out.println("FTP DOWNLOAD: " + downloadURL);

    try {
        if (ftp.isConnected()) {
            status.setCurrentStatusText2("Using open FTP connection");
            System.out.println("Reusing open FTP connection");
        } else {
            ftp.connect(server);
            int reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                status.setCurrentStatusText1("Can't connect to FTP server");
                status.setCurrentStatusText2("ERROR");
                return false;
            }
            if (!ftp.login("anonymous", "anonymous")) {
                if (!ftp.login(username, password)) {
                    ftp.disconnect();
                    status.setCurrentStatusText1("Can't login to FTP server");
                    status.setCurrentStatusText2("ERROR");
                    return false;
                }
            }
            status.setCurrentStatusText1("Set Binary Transfer Mode");
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            status.setCurrentStatusText2("Activate Passive Transfer Mode");
            ftp.enterLocalPassiveMode();
        }
        status.setCurrentStatusText1("Start download...");
        status.setCurrentStatusText2("Please Wait.");

        // ftp.listFiles(pathname);

        OutputStream output = new MyOutputStream(lastStatus, status, new FileOutputStream(local));
        myoutputstream.setObject(output);
        ftp.setRemoteVerificationEnabled(false);
        long tA = System.currentTimeMillis();
        boolean result = ftp.retrieveFile(remote, output);
        output.close();
        long tB = System.currentTimeMillis();
        if (!result) {
            new File(local).delete();
            MainFrame.showMessage("Can't download " + downloadURL + ". File not available.", MessageType.INFO);
        } else {
            File f = new File(local);
            System.out.println("Download completed (" + f.getAbsolutePath() + ", " + (f.length() / 1024)
                    + " KB, " + (int) ((f.length() / 1024d / (tB - tA) * 1000d)) + " KB/s).");
        }
        BackgroundTaskHelper.executeLaterOnSwingTask(10000, new Runnable() {
            public void run() {
                try {
                    synchronized (GUIhelper.class) {
                        if (runIdx == thisRun) {
                            System.out.println("Disconnect FTP connection");
                            ftp.disconnect();
                        }
                    }
                } catch (Exception err) {
                    ErrorMsg.addErrorMessage(err);
                }
            }
        });
        return result;
    } catch (Exception err) {
        System.out.println("ERROR: FTP DOWNLOAD ERROR: " + err.getMessage());
        if (ftp != null && ftp.isConnected()) {
            try {
                System.out.println("Disconnect FTP connection (following error condition)");
                ftp.disconnect();
            } catch (Exception err2) {
                ErrorMsg.addErrorMessage(err2);
            }
        }
        return false;
    }
}

From source file:aplicacion.sistema.version.logic.PrintCommandListener.java

public void protocolCommandSent(ProtocolCommandEvent event) {
    __writer.print(event.getMessage());
    __writer.flush();
}

From source file:aplicacion.sistema.version.logic.PrintCommandListener.java

public void protocolReplyReceived(ProtocolCommandEvent event) {
    __writer.print(event.getMessage());
    __writer.flush();
}

From source file:com.isencia.passerelle.actor.ftp.PrintCommandListener.java

public void protocolReplyReceived(ProtocolCommandEvent event) {
    if (event.isReply() && event.getMessage().startsWith("550")) {
        __writer.print(event.getMessage());
        __writer.flush();//from   ww w  .j  a v a2 s . c om
    }
}

From source file:net.sourceforge.dvb.projectx.xinput.ftp.StringCommandListener.java

public void protocolCommandSent(ProtocolCommandEvent event) {
    pWriter.print(event.getMessage());
    pWriter.flush();
}

From source file:net.sourceforge.dvb.projectx.xinput.ftp.StringCommandListener.java

public void protocolReplyReceived(ProtocolCommandEvent event) {
    pWriter.print(event.getMessage());
    pWriter.flush();
}

From source file:ch.cyberduck.core.ftp.LoggingProtocolCommandListener.java

@Override
public void protocolReplyReceived(final ProtocolCommandEvent event) {
    this.log(Type.response, StringUtils.chomp(event.getMessage()));
}

From source file:ch.cyberduck.core.ftp.LoggingProtocolCommandListener.java

@Override
public void protocolCommandSent(final ProtocolCommandEvent event) {
    final String message = StringUtils.chomp(event.getMessage());
    if (message.startsWith(FTPCmd.PASS.name())) {
        this.log(Type.request, String.format("%s %s", FTPCmd.PASS.name(), StringUtils.repeat("*",
                StringUtils.length(StringUtils.removeStart(message, FTPCmd.PASS.name())))));
    } else {//from w  w w  . j a  va 2s .  c  o  m
        this.log(Type.request, message);
    }
}

From source file:com.sos.VirtualFileSystem.FTP.SOSFtpClientLogger.java

@Override
public void protocolReplyReceived(final ProtocolCommandEvent event) {
    logger.debug(clientId + " < " + event.getMessage().trim());
}