Example usage for org.apache.commons.net ProtocolCommandListener ProtocolCommandListener

List of usage examples for org.apache.commons.net ProtocolCommandListener ProtocolCommandListener

Introduction

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

Prototype

ProtocolCommandListener

Source Link

Usage

From source file:net.metanotion.emailqueue.SmtpSender.java

@Override
public Connection getConnection() {
    AuthenticatingSMTPClient client = null;
    try {/*from   ww w  .  ja v a2s  . c  o  m*/
        client = new AuthenticatingSMTPClient();
        client.addProtocolCommandListener(new ProtocolCommandListener() {
            @Override
            public void protocolCommandSent(final ProtocolCommandEvent event) {
                logger.trace(COMMAND_LOG_FORMAT, event.getCommand(), event.getMessage());
            }

            @Override
            public void protocolReplyReceived(final ProtocolCommandEvent event) {
                logger.trace(COMMAND_LOG_FORMAT, event.getCommand(), event.getMessage());
            }
        });
        logger.trace("Connecting");
        client.connect(this.server, this.port);
        if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) {
            logger.error("Did not get positive completion.");
            throw new IOException("Could not complete SMTP connection.");
        }
        startTls(client);
        logger.trace("Authenticating");
        client.auth(AuthenticatingSMTPClient.AUTH_METHOD.PLAIN, username, password);
        logger.trace("Login");
        client.login(host);
        return new Connection(client);
    } catch (final IOException | NoSuchAlgorithmException | InvalidKeyException | InvalidKeySpecException ex) {
        logger.debug("Error", ex);
        try {
            if (client != null) {
                client.disconnect();
            }
        } catch (final IOException ex2) {
        }
        throw new RuntimeException(ex);
    }
}

From source file:com.consol.citrus.ftp.client.FtpClient.java

@Override
public void afterPropertiesSet() throws Exception {
    if (ftpClient == null) {
        ftpClient = new FTPClient();
    }//from  w ww  .j a va 2  s. c o  m

    ftpClient.configure(config);

    ftpClient.addProtocolCommandListener(new ProtocolCommandListener() {
        @Override
        public void protocolCommandSent(ProtocolCommandEvent event) {
            log.info("Send FTP command: " + event.getCommand());
        }

        @Override
        public void protocolReplyReceived(ProtocolCommandEvent event) {
            log.info("Received FTP command reply: " + event.getReplyCode());
        }
    });
}

From source file:com.symbian.driver.plugins.ftptelnet.FtpTransfer.java

/**
 * connectFTP : connects to ftp server/*from   w  w  w  . j a va2  s.  c  o m*/
 * 
 * @return boolean success/fail
 */
public boolean connectFTP() {
    if (isFTPConnected()) {
        return true;
    }
    // Connect to FTP
    try {

        // 1. create an apache client
        iFTP = new FTPClient();
        iFTP.addProtocolCommandListener(new ProtocolCommandListener() {

            public void protocolCommandSent(ProtocolCommandEvent aProtocolCommandEvent) {
                LOGGER.fine("FTP Command Send: (" + aProtocolCommandEvent.getCommand() + ") "
                        + aProtocolCommandEvent.getMessage());
            }

            public void protocolReplyReceived(ProtocolCommandEvent aProtocolCommandEvent) {
                LOGGER.fine("FTP Command Recieved: (" + aProtocolCommandEvent.getMessage() + ") Returned Code "
                        + aProtocolCommandEvent.getReplyCode());
            }
        });
        FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
        iFTP.configure(conf);
        // 2. connect
        iFTP.connect(iIP, iPort);

        // 3. check connection done.
        int reply = iFTP.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            disconnectFTP();
            LOGGER.log(Level.SEVERE, "FTP error: " + reply);
            return false;
        }

        // 4. Login
        if (!iFTP.login(iUserName, iPassword)) {
            LOGGER.log(Level.SEVERE, "FTP failed to login, Username: " + iUserName + " Password: " + iPassword);
            disconnectFTP();
            return false;
        } else {
            if (iPassive.equalsIgnoreCase("true")) {
                iFTP.enterLocalPassiveMode();
            }
            iFTP.setFileType(FTP.BINARY_FILE_TYPE);
            iFTP.setBufferSize(iBufferSize);
        }

    } catch (SocketException lSocketException) {
        LOGGER.log(Level.SEVERE, "Socket exception ", lSocketException);
        return false;
    } catch (IOException lIOException) {
        LOGGER.log(Level.SEVERE, "Socket exception ", lIOException);
        return false;
    }

    LOGGER.info("FTP connection established with transport : " + iIP + ":" + iPort);

    return true;

}

From source file:ConnectionInfo.java

/**
 * @param parentShell/*  w w  w .j a  va  2 s.  c  o m*/
 */
public FTPWindow(Shell parentShell) {
    super(parentShell);

    createActions();

    addStatusLine();
    //addCoolBar(SWT.FLAT | SWT.RIGHT);
    addToolBar(SWT.FLAT);
    addMenuBar();

    ftp = new FTPClient();
    ftp.addProtocolCommandListener(new ProtocolCommandListener() {
        public void protocolCommandSent(ProtocolCommandEvent e) {
            logMessage("> " + e.getCommand(), false);
        }

        public void protocolReplyReceived(ProtocolCommandEvent e) {
            logMessage("< " + e.getMessage(), false);
        }
    });

}

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;/*from   w  ww . ja v  a 2 s .  c o 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: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 ww .j  a v a2s  .com*/
    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:org.apache.ftpserver.clienttests.ClientTestTemplate.java

/**
 * @throws Exception/*from   w w  w .  j a  v a 2 s. co m*/
 */
protected void connectClient() throws Exception {
    client = createFTPClient();
    client.addProtocolCommandListener(new ProtocolCommandListener() {

        public void protocolCommandSent(ProtocolCommandEvent event) {
            LOG.debug("> " + event.getMessage().trim());

        }

        public void protocolReplyReceived(ProtocolCommandEvent event) {
            LOG.debug("< " + event.getMessage().trim());
        }
    });

    if (isConnectClient()) {
        doConnect();
    }
}

From source file:org.apache.james.smtpserver.SMTPServerTest.java

protected SMTPClient newSMTPClient() throws IOException {
    SMTPClient smtp = new SMTPClient();
    smtp.connect("127.0.0.1", smtpListenerPort);
    if (log.isDebugEnabled()) {
        smtp.addProtocolCommandListener(new ProtocolCommandListener() {

            @Override/* w  w  w. ja va 2 s  .c  o m*/
            public void protocolCommandSent(ProtocolCommandEvent event) {
                log.debug("> " + event.getMessage().trim());
            }

            @Override
            public void protocolReplyReceived(ProtocolCommandEvent event) {
                log.debug("< " + event.getMessage().trim());
            }
        });
    }
    return smtp;
}