List of usage examples for org.apache.commons.net PrintCommandListener PrintCommandListener
public PrintCommandListener(PrintWriter writer, boolean suppressLogin, char eolMarker, boolean showDirection)
From source file:ch.sdi.core.impl.ftp.FTPClientExample.java
/** * *///www . ja v a2 s . c om private void run() throws Throwable { if (myProtocol == null) { if (myProxyHost != null) { myLog.debug("Using HTTP proxy server: " + myProxyHost); myFtp = new FTPHTTPClient(myProxyHost, myProxyPort, myProxyUser, myProxyPassword); } else { myFtp = new FTPClient(); } } else { FTPSClient ftps; if (myProtocol.equals("true")) { ftps = new FTPSClient(true); } else if (myProtocol.equals("false")) { ftps = new FTPSClient(false); } else { String prot[] = myProtocol.split(","); if (prot.length == 1) { // Just protocol ftps = new FTPSClient(myProtocol); } else { // protocol,true|false ftps = new FTPSClient(prot[0], Boolean.parseBoolean(prot[1])); } } myFtp = ftps; if ("all".equals(myTrustmgr)) { ftps.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager()); } else if ("valid".equals(myTrustmgr)) { ftps.setTrustManager(TrustManagerUtils.getValidateServerCertificateTrustManager()); } else if ("none".equals(myTrustmgr)) { ftps.setTrustManager(null); } } if (myPrintHash) { myFtp.setCopyStreamListener(createListener()); } if (myKeepAliveTimeout >= 0) { myFtp.setControlKeepAliveTimeout(myKeepAliveTimeout); } if (myControlKeepAliveReplyTimeout >= 0) { myFtp.setControlKeepAliveReplyTimeout(myControlKeepAliveReplyTimeout); } myFtp.setListHiddenFiles(myHidden); // intercept commands and write it to our logger myPrintCommandToLoggerListener = new PrintCommandToLoggerListener(myLog); PrintWriter writer = myPrintCommandToLoggerListener.getPrintWriter(); myFtp.addProtocolCommandListener(new PrintCommandListener(writer, true, '\n', true)); // myFtp.addProtocolCommandListener( new PrintCommandListener( new PrintWriter( System.out ), true ) ); try { int reply; if (myPort > 0) { myFtp.connect(myServer, myPort); } else { myFtp.connect(myServer); } myLog.debug("Connected to " + myServer + " on " + (myPort > 0 ? myPort : myFtp.getDefaultPort())); // After connection attempt, you should check the reply code to verify // success. reply = myFtp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { myFtp.disconnect(); throw new Exception("FTP server refused connection."); } } catch (IOException e) { if (myFtp.isConnected()) { try { myFtp.disconnect(); } catch (IOException f) { // do nothing } } throw new Exception("Could not connect to server.", e); } try { if (!myFtp.login(myUsername, myPassword)) { myFtp.logout(); throw createFtpException("Problems on login"); } myLog.debug("Remote system is " + myFtp.getSystemType()); if (myBinaryTransfer) { myFtp.setFileType(FTP.BINARY_FILE_TYPE); } else { // in theory this should not be necessary as servers should default to ASCII // but they don't all do so - see NET-500 myFtp.setFileType(FTP.ASCII_FILE_TYPE); } // Use passive mode as default because most of us are // behind firewalls these days. if (myLocalActive) { myFtp.enterLocalActiveMode(); } else { myFtp.enterLocalPassiveMode(); } myFtp.setUseEPSVwithIPv4(myUseEpsvWithIPv4); if (myStoreFile) { InputStream input; input = new FileInputStream(myLocal); myFtp.storeFile(myRemote, input); input.close(); } else if (myListFiles) { if (myLenient) { FTPClientConfig config = new FTPClientConfig(); config.setLenientFutureDates(true); myFtp.configure(config); } for (FTPFile f : myFtp.listFiles(myRemote)) { myLog.debug(f.getRawListing()); myLog.debug(f.toFormattedString()); } } else if (myMlsd) { for (FTPFile f : myFtp.mlistDir(myRemote)) { myLog.debug(f.getRawListing()); myLog.debug(f.toFormattedString()); } } else if (myMlst) { FTPFile f = myFtp.mlistFile(myRemote); if (f != null) { myLog.debug(f.toFormattedString()); } } else if (myListNames) { for (String s : myFtp.listNames(myRemote)) { myLog.debug(s); } } else if (myFeat) { // boolean feature check if (myRemote != null) { // See if the command is present if (myFtp.hasFeature(myRemote)) { myLog.debug("Has feature: " + myRemote); } else { if (FTPReply.isPositiveCompletion(myFtp.getReplyCode())) { myLog.debug("FEAT " + myRemote + " was not detected"); } else { throw createFtpException("Command failed"); } } // Strings feature check String[] features = myFtp.featureValues(myRemote); if (features != null) { for (String f : features) { myLog.debug("FEAT " + myRemote + "=" + f + "."); } } else { if (FTPReply.isPositiveCompletion(myFtp.getReplyCode())) { myLog.warn("FEAT " + myRemote + " is not present"); } else { throw createFtpException("Command failed"); } } } else { if (myFtp.features()) { // Command listener has already printed the output } else { throw createFtpException("Command failed"); } } } else if (myDoCommand != null) { if (myFtp.doCommand(myDoCommand, myRemote)) { // Command listener has already printed the output } else { throw createFtpException("Command failed"); } } else { OutputStream output; output = new FileOutputStream(myLocal); myFtp.retrieveFile(myRemote, output); output.close(); } myFtp.noop(); // check that control connection is working OK myFtp.logout(); } catch (FTPConnectionClosedException e) { throw createFtpException("Server closed connection."); } catch (IOException e) { throw createFtpException("IOException caught"); } finally { myPrintCommandToLoggerListener.flushRest(); if (myFtp.isConnected()) { try { myFtp.disconnect(); } catch (IOException f) { // do nothing } } } }
From source file:ch.sdi.core.impl.ftp.FtpExecutor.java
/** * @throws SdiException//from w w w .ja v a2 s. c o m * @throws IOException */ public void connectAndLogin() throws SdiException, IOException { if (!myInitialized) { initBySpringContext(); } // if !initialized try { if (myProtocol == null) { if (myProxyHost != null) { myLog.debug("Using HTTP proxy server: " + myProxyHost); myFtp = new FTPHTTPClient(myProxyHost, myProxyPort, myProxyUser, myProxyPassword); } else { myLog.debug("Using simple FTPClient"); myFtp = new FTPClient(); } } else { FTPSClient ftps; if (myProtocol.equals("true")) { myLog.debug("Using FTPSClient with implicite SSL"); ftps = new FTPSClient(true); } else if (myProtocol.equals("false")) { myLog.debug("Using FTPSClient with no implicite SSL"); ftps = new FTPSClient(false); } else { String prot[] = myProtocol.split(","); if (prot.length == 1) { // Just protocol myLog.debug("Using FTPSClient with protocol " + myProtocol); ftps = new FTPSClient(myProtocol); } else { // protocol,true|false myLog.debug("Using FTPSClient with " + prot[0] + " and " + prot[1]); ftps = new FTPSClient(prot[0], Boolean.parseBoolean(prot[1])); } } myFtp = ftps; if ("all".equals(myTrustmgr)) { myLog.debug("Using AcceptAllTrustManager"); ftps.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager()); } else if ("valid".equals(myTrustmgr)) { myLog.debug("Using ValidateServerCertificateTrustManager"); ftps.setTrustManager(TrustManagerUtils.getValidateServerCertificateTrustManager()); } else if ("none".equals(myTrustmgr)) { myLog.debug("Setting TrustManager to null"); ftps.setTrustManager(null); } else { myLog.debug("Using no TrustManager at all"); } } if (myKeepAliveTimeout >= 0) { myLog.debug("Setting KeepAliveTimeout to " + myKeepAliveTimeout); myFtp.setControlKeepAliveTimeout(myKeepAliveTimeout); } if (myControlKeepAliveReplyTimeout >= 0) { myLog.debug("Setting ControlKeepAliveReplyTimeout to " + myControlKeepAliveReplyTimeout); myFtp.setControlKeepAliveReplyTimeout(myControlKeepAliveReplyTimeout); } // intercept commands and write it to our logger myPrintCommandToLoggerListener = new PrintCommandToLoggerListener(myLog); PrintWriter writer = myPrintCommandToLoggerListener.getPrintWriter(); myFtp.addProtocolCommandListener(new PrintCommandListener(writer, true, '\n', true)); // myFtp.addProtocolCommandListener( new PrintCommandListener( new PrintWriter( System.out ), true ) ); try { int reply; if (myPort > 0) { myLog.debug("Connecting to " + myServer + ":" + myPort); myFtp.connect(myServer, myPort); } else { myLog.debug("Connecting to " + myServer + " (default port)"); myFtp.connect(myServer); } myLog.debug("Connected to " + myServer + " on " + (myPort > 0 ? myPort : myFtp.getDefaultPort())); // After connection attempt, you should check the reply code to verify success. reply = myFtp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { myFtp.disconnect(); throw createFtpException("FTP server refused connection."); } } catch (IOException e) { throw createFtpException("Could not connect to server.", e); } if (!myFtp.login(myUsername, myPassword)) { myFtp.logout(); throw createFtpException("Problems on login"); } myLog.debug("Remote system is " + myFtp.getSystemType()); myFtp.setFileType(FTP.BINARY_FILE_TYPE); // Use passive mode as default because most of us are behind firewalls these days. if (myLocalActive) { myFtp.enterLocalActiveMode(); } else { myFtp.enterLocalPassiveMode(); } myFtp.setUseEPSVwithIPv4(myUseEpsvWithIPv4); } finally { myPrintCommandToLoggerListener.flushRest(); } }