List of usage examples for org.apache.commons.net.ftp FTPClient setDataTimeout
public void setDataTimeout(int timeout)
From source file:ch.cyberduck.core.ftp.FTPSession.java
protected void configure(final FTPClient client) throws IOException { client.setProtocol(host.getProtocol()); client.setSocketFactory(socketFactory); client.setControlEncoding(host.getEncoding()); final int timeout = preferences.getInteger("connection.timeout.seconds") * 1000; client.setConnectTimeout(timeout);/*from www . ja va 2 s.c o m*/ client.setDefaultTimeout(timeout); client.setDataTimeout(timeout); client.setDefaultPort(host.getProtocol().getDefaultPort()); client.setParserFactory(new FTPParserFactory()); client.setRemoteVerificationEnabled(preferences.getBoolean("ftp.datachannel.verify")); final int buffer = preferences.getInteger("ftp.socket.buffer"); client.setBufferSize(buffer); if (preferences.getInteger("connection.buffer.receive") > 0) { client.setReceiveBufferSize(preferences.getInteger("connection.buffer.receive")); } if (preferences.getInteger("connection.buffer.send") > 0) { client.setSendBufferSize(preferences.getInteger("connection.buffer.send")); } if (preferences.getInteger("connection.buffer.receive") > 0) { client.setReceieveDataSocketBufferSize(preferences.getInteger("connection.buffer.receive")); } if (preferences.getInteger("connection.buffer.send") > 0) { client.setSendDataSocketBufferSize(preferences.getInteger("connection.buffer.send")); } client.setStrictMultilineParsing(preferences.getBoolean("ftp.parser.multiline.strict")); client.setStrictReplyParsing(preferences.getBoolean("ftp.parser.reply.strict")); }
From source file:fr.ibp.nifi.processors.IBPFTPTransfer.java
private FTPClient getClient(final FlowFile flowFile) throws IOException { if (client != null) { String desthost = ctx.getProperty(HOSTNAME).evaluateAttributeExpressions(flowFile).getValue(); if (remoteHostName.equals(desthost)) { // destination matches so we can keep our current session resetWorkingDirectory();/* w ww . j av a 2 s. com*/ return client; } else { // this flowFile is going to a different destination, reset // session close(); } } final Proxy.Type proxyType = Proxy.Type.valueOf(ctx.getProperty(PROXY_TYPE).getValue()); final String proxyHost = ctx.getProperty(PROXY_HOST).getValue(); final Integer proxyPort = ctx.getProperty(PROXY_PORT).asInteger(); FTPClient client; if (proxyType == Proxy.Type.HTTP) { client = new FTPHTTPClient(proxyHost, proxyPort, ctx.getProperty(HTTP_PROXY_USERNAME).getValue(), ctx.getProperty(HTTP_PROXY_PASSWORD).getValue()); } else { client = new FTPClient(); if (proxyType == Proxy.Type.SOCKS) { client.setSocketFactory(new SocksProxySocketFactory( new Proxy(proxyType, new InetSocketAddress(proxyHost, proxyPort)))); } } this.client = client; client.setDataTimeout(ctx.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); client.setDefaultTimeout( ctx.getProperty(CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); client.setRemoteVerificationEnabled(false); final String remoteHostname = ctx.getProperty(HOSTNAME).evaluateAttributeExpressions(flowFile).getValue(); this.remoteHostName = remoteHostname; InetAddress inetAddress = null; try { inetAddress = InetAddress.getByAddress(remoteHostname, null); } catch (final UnknownHostException uhe) { } if (inetAddress == null) { inetAddress = InetAddress.getByName(remoteHostname); } client.connect(inetAddress, ctx.getProperty(PORT).evaluateAttributeExpressions(flowFile).asInteger()); this.closed = false; client.setDataTimeout(ctx.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); client.setSoTimeout(ctx.getProperty(CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); final String username = ctx.getProperty(USERNAME).evaluateAttributeExpressions(flowFile).getValue(); final String password = ctx.getProperty(PASSWORD).evaluateAttributeExpressions(flowFile).getValue(); final boolean loggedIn = client.login(username, password); if (!loggedIn) { throw new IOException("Could not login for user '" + username + "'"); } final String connectionMode = ctx.getProperty(CONNECTION_MODE).getValue(); if (connectionMode.equalsIgnoreCase(CONNECTION_MODE_ACTIVE)) { client.enterLocalActiveMode(); } else { client.enterLocalPassiveMode(); } final String transferMode = ctx.getProperty(TRANSFER_MODE).evaluateAttributeExpressions(flowFile) .getValue(); final int fileType = (transferMode.equalsIgnoreCase(TRANSFER_MODE_ASCII)) ? FTPClient.ASCII_FILE_TYPE : FTPClient.BINARY_FILE_TYPE; if (!client.setFileType(fileType)) { throw new IOException("Unable to set transfer mode to type " + transferMode); } this.homeDirectory = client.printWorkingDirectory(); return client; }
From source file:com.clickha.nifi.processors.util.FTPTransferV2.java
private FTPClient getClient(final FlowFile flowFile) throws IOException { if (client != null) { String desthost = ctx.getProperty(HOSTNAME).evaluateAttributeExpressions(flowFile).getValue(); if (remoteHostName.equals(desthost)) { // destination matches so we can keep our current session resetWorkingDirectory();//from ww w .j a v a 2 s.c om return client; } else { // this flowFile is going to a different destination, reset session close(); } } final Proxy.Type proxyType = Proxy.Type.valueOf(ctx.getProperty(PROXY_TYPE).getValue()); final String proxyHost = ctx.getProperty(PROXY_HOST).getValue(); final Integer proxyPort = ctx.getProperty(PROXY_PORT).asInteger(); FTPClient client; if (proxyType == Proxy.Type.HTTP) { client = new FTPHTTPClient(proxyHost, proxyPort, ctx.getProperty(HTTP_PROXY_USERNAME).getValue(), ctx.getProperty(HTTP_PROXY_PASSWORD).getValue()); } else { client = new FTPClient(); if (proxyType == Proxy.Type.SOCKS) { client.setSocketFactory(new SocksProxySocketFactory( new Proxy(proxyType, new InetSocketAddress(proxyHost, proxyPort)))); } } this.client = client; client.setDataTimeout(ctx.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); client.setDefaultTimeout( ctx.getProperty(CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); client.setRemoteVerificationEnabled(false); final String remoteHostname = ctx.getProperty(HOSTNAME).evaluateAttributeExpressions(flowFile).getValue(); this.remoteHostName = remoteHostname; InetAddress inetAddress = null; try { inetAddress = InetAddress.getByAddress(remoteHostname, null); } catch (final UnknownHostException uhe) { } if (inetAddress == null) { inetAddress = InetAddress.getByName(remoteHostname); } client.connect(inetAddress, ctx.getProperty(PORT).evaluateAttributeExpressions(flowFile).asInteger()); this.closed = false; client.setDataTimeout(ctx.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); client.setSoTimeout(ctx.getProperty(CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); final String username = ctx.getProperty(USERNAME).evaluateAttributeExpressions(flowFile).getValue(); final String password = ctx.getProperty(PASSWORD).evaluateAttributeExpressions(flowFile).getValue(); final boolean loggedIn = client.login(username, password); if (!loggedIn) { throw new IOException("Could not login for user '" + username + "'"); } final String connectionMode = ctx.getProperty(CONNECTION_MODE).getValue(); if (connectionMode.equalsIgnoreCase(CONNECTION_MODE_ACTIVE)) { client.enterLocalActiveMode(); } else { client.enterLocalPassiveMode(); } // additional FTPClientConfig ftpConfig = new FTPClientConfig(FTPClientConfig.SYST_UNIX); final String ftpClientConfig = ctx.getProperty(FTP_CLIENT_CONFIG_SYST).getValue(); if (ftpClientConfig.equalsIgnoreCase(FTP_CLIENT_CONFIG_SYST_UNIX)) { this.ftpConfig = ftpConfig; client.configure(ftpConfig); } else if (ftpClientConfig.equalsIgnoreCase(FTP_CLIENT_CONFIG_SYST_NT)) { this.ftpConfig = ftpConfig; client.configure(ftpConfig); } final String transferMode = ctx.getProperty(TRANSFER_MODE).getValue(); final int fileType = (transferMode.equalsIgnoreCase(TRANSFER_MODE_ASCII)) ? FTPClient.ASCII_FILE_TYPE : FTPClient.BINARY_FILE_TYPE; if (!client.setFileType(fileType)) { throw new IOException("Unable to set transfer mode to type " + transferMode); } this.homeDirectory = client.printWorkingDirectory(); return client; }
From source file:fr.acxio.tools.agia.ftp.DefaultFtpClientFactory.java
public FTPClient getFtpClient() throws IOException { FTPClient aClient = new FTPClient(); // Debug output // aClient.addProtocolCommandListener(new PrintCommandListener(new // PrintWriter(System.out), true)); if (activeExternalIPAddress != null) { aClient.setActiveExternalIPAddress(activeExternalIPAddress); }/*from www . j a va2 s .c o m*/ if (activeMinPort != null && activeMaxPort != null) { aClient.setActivePortRange(activeMinPort, activeMaxPort); } if (autodetectUTF8 != null) { aClient.setAutodetectUTF8(autodetectUTF8); } if (bufferSize != null) { aClient.setBufferSize(bufferSize); } if (charset != null) { aClient.setCharset(charset); } if (connectTimeout != null) { aClient.setConnectTimeout(connectTimeout); } if (controlEncoding != null) { aClient.setControlEncoding(controlEncoding); } if (controlKeepAliveReplyTimeout != null) { aClient.setControlKeepAliveReplyTimeout(controlKeepAliveReplyTimeout); } if (controlKeepAliveTimeout != null) { aClient.setControlKeepAliveTimeout(controlKeepAliveTimeout); } if (dataTimeout != null) { aClient.setDataTimeout(dataTimeout); } if (defaultPort != null) { aClient.setDefaultPort(defaultPort); } if (defaultTimeout != null) { aClient.setDefaultTimeout(defaultTimeout); } if (fileStructure != null) { aClient.setFileStructure(fileStructure); } if (keepAlive != null) { aClient.setKeepAlive(keepAlive); } if (listHiddenFiles != null) { aClient.setListHiddenFiles(listHiddenFiles); } if (parserFactory != null) { aClient.setParserFactory(parserFactory); } if (passiveLocalIPAddress != null) { aClient.setPassiveLocalIPAddress(passiveLocalIPAddress); } if (passiveNatWorkaround != null) { aClient.setPassiveNatWorkaround(passiveNatWorkaround); } if (proxy != null) { aClient.setProxy(proxy); } if (receieveDataSocketBufferSize != null) { aClient.setReceieveDataSocketBufferSize(receieveDataSocketBufferSize); } if (receiveBufferSize != null) { aClient.setReceiveBufferSize(receiveBufferSize); } if (remoteVerificationEnabled != null) { aClient.setRemoteVerificationEnabled(remoteVerificationEnabled); } if (reportActiveExternalIPAddress != null) { aClient.setReportActiveExternalIPAddress(reportActiveExternalIPAddress); } if (sendBufferSize != null) { aClient.setSendBufferSize(sendBufferSize); } if (sendDataSocketBufferSize != null) { aClient.setSendDataSocketBufferSize(sendDataSocketBufferSize); } if (strictMultilineParsing != null) { aClient.setStrictMultilineParsing(strictMultilineParsing); } if (tcpNoDelay != null) { aClient.setTcpNoDelay(tcpNoDelay); } if (useEPSVwithIPv4 != null) { aClient.setUseEPSVwithIPv4(useEPSVwithIPv4); } if (systemKey != null) { FTPClientConfig aClientConfig = new FTPClientConfig(systemKey); if (defaultDateFormat != null) { aClientConfig.setDefaultDateFormatStr(defaultDateFormat); } if (recentDateFormat != null) { aClientConfig.setRecentDateFormatStr(recentDateFormat); } if (serverLanguageCode != null) { aClientConfig.setServerLanguageCode(serverLanguageCode); } if (shortMonthNames != null) { aClientConfig.setShortMonthNames(shortMonthNames); } if (serverTimeZoneId != null) { aClientConfig.setServerTimeZoneId(serverTimeZoneId); } aClient.configure(aClientConfig); } if (LOGGER.isInfoEnabled()) { LOGGER.info("Connecting to : {}", host); } if (port == null) { aClient.connect(host); } else { aClient.connect(host, port); } int aReplyCode = aClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(aReplyCode)) { aClient.disconnect(); throw new IOException("Cannot connect to " + host + ". Returned code : " + aReplyCode); } try { if (localPassiveMode) { aClient.enterLocalPassiveMode(); } boolean aIsLoggedIn = false; if (account == null) { aIsLoggedIn = aClient.login(username, password); } else { aIsLoggedIn = aClient.login(username, password, account); } if (!aIsLoggedIn) { throw new IOException(aClient.getReplyString()); } } catch (IOException e) { aClient.disconnect(); throw e; } if (fileTransferMode != null) { aClient.setFileTransferMode(fileTransferMode); } if (fileType != null) { aClient.setFileType(fileType); } return aClient; }
From source file:net.seedboxer.camel.component.file.remote.ftp2.Ftp2Endpoint.java
@Override public RemoteFileOperations<FTPFile> createRemoteFileOperations() throws Exception { // configure ftp client FTPClient client = ftpClient; if (client == null) { // must use a new client if not explicit configured to use a custom client client = createFtpClient();//w w w. j a va 2 s .c om } // set any endpoint configured timeouts if (getConfiguration().getConnectTimeout() > -1) { client.setConnectTimeout(getConfiguration().getConnectTimeout()); } if (getConfiguration().getSoTimeout() > -1) { soTimeout = getConfiguration().getSoTimeout(); } dataTimeout = getConfiguration().getTimeout(); // then lookup ftp client parameters and set those if (ftpClientParameters != null) { // setting soTimeout has to be done later on FTPClient (after it has connected) Object timeout = ftpClientParameters.remove("soTimeout"); if (timeout != null) { soTimeout = getCamelContext().getTypeConverter().convertTo(int.class, timeout); } // and we want to keep data timeout so we can log it later timeout = ftpClientParameters.remove("dataTimeout"); if (timeout != null) { dataTimeout = getCamelContext().getTypeConverter().convertTo(int.class, dataTimeout); } IntrospectionSupport.setProperties(client, ftpClientParameters); } if (ftpClientConfigParameters != null) { // client config is optional so create a new one if we have parameter for it if (ftpClientConfig == null) { ftpClientConfig = new FTPClientConfig(); } IntrospectionSupport.setProperties(ftpClientConfig, ftpClientConfigParameters); } if (dataTimeout > 0) { client.setDataTimeout(dataTimeout); } if (log.isDebugEnabled()) { log.debug("Created FTPClient [connectTimeout: {}, soTimeout: {}, dataTimeout: {}]: {}", new Object[] { client.getConnectTimeout(), getSoTimeout(), dataTimeout, client }); } Ftp2Operations operations = new Ftp2Operations(client, getFtpClientConfig()); operations.setEndpoint(this); return operations; }
From source file:com.github.goldin.org.apache.tools.ant.taskdefs.optional.net.FTP.java
/** * Runs the task./*from ww w . j a v a 2s . co m*/ * * @throws BuildException if the task fails or is not configured * correctly. */ public void execute() throws BuildException { checkAttributes(); FTPClient ftp = null; try { log("Opening FTP connection to " + server, Project.MSG_VERBOSE); /** * "verbose" version of <code>FTPClient</code> prints progress indicator * See http://evgeny-goldin.com/blog/2010/08/18/ant-ftp-task-progress-indicator-timeout/ */ ftp = (verbose ? new com.github.goldin.org.apache.tools.ant.taskdefs.optional.net.FTPClient(getProject()) : new org.apache.commons.net.ftp.FTPClient()); ftp.setDataTimeout(5 * 60 * 1000); // 5 minutes if (this.isConfigurationSet) { ftp = FTPConfigurator.configure(ftp, this); } ftp.setRemoteVerificationEnabled(enableRemoteVerification); ftp.connect(server, port); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("FTP connection failed: " + ftp.getReplyString()); } log("connected", Project.MSG_VERBOSE); log("logging in to FTP server", Project.MSG_VERBOSE); if ((this.account != null && !ftp.login(userid, password, account)) || (this.account == null && !ftp.login(userid, password))) { throw new BuildException("Could not login to FTP server"); } log("login succeeded", Project.MSG_VERBOSE); if (binary) { ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not set transfer type: " + ftp.getReplyString()); } } else { ftp.setFileType(org.apache.commons.net.ftp.FTP.ASCII_FILE_TYPE); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not set transfer type: " + ftp.getReplyString()); } } if (passive) { log("entering passive mode", Project.MSG_VERBOSE); ftp.enterLocalPassiveMode(); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not enter into passive " + "mode: " + ftp.getReplyString()); } } // If an initial command was configured then send it. // Some FTP servers offer different modes of operation, // E.G. switching between a UNIX file system mode and // a legacy file system. if (this.initialSiteCommand != null) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { doSiteCommand(lftp, FTP.this.initialSiteCommand); } }, "initial site command: " + this.initialSiteCommand); } // For a unix ftp server you can set the default mask for all files // created. if (umask != null) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { doSiteCommand(lftp, "umask " + umask); } }, "umask " + umask); } // If the action is MK_DIR, then the specified remote // directory is the directory to create. if (action == MK_DIR) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { makeRemoteDir(lftp, remotedir); } }, remotedir); } else if (action == SITE_CMD) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { doSiteCommand(lftp, FTP.this.siteCommand); } }, "Site Command: " + this.siteCommand); } else { if (remotedir != null) { log("changing the remote directory to " + remotedir, Project.MSG_VERBOSE); ftp.changeWorkingDirectory(remotedir); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not change remote " + "directory: " + ftp.getReplyString()); } } if (newerOnly && timeDiffAuto) { // in this case we want to find how much time span there is between local // and remote timeDiffMillis = getTimeDiff(ftp); } log(ACTION_STRS[action] + " " + ACTION_TARGET_STRS[action]); transferFiles(ftp); } } catch (IOException ex) { throw new BuildException("error during FTP transfer: " + ex, ex); } finally { if (ftp != null && ftp.isConnected()) { try { log("disconnecting", Project.MSG_VERBOSE); ftp.logout(); ftp.disconnect(); } catch (IOException ex) { // ignore it } } } }
From source file:com.github.carlosrubio.org.apache.tools.ant.taskdefs.optional.net.FTP.java
/** * Runs the task.//from ww w . j ava 2 s .c o m * * @throws BuildException if the task fails or is not configured * correctly. */ public void execute() throws BuildException { checkAttributes(); FTPClient ftp = null; try { log("Opening FTP connection to " + server, Project.MSG_VERBOSE); /** * "verbose" version of <code>FTPClient</code> prints progress indicator * See http://evgeny-goldin.com/blog/2010/08/18/ant-ftp-task-progress-indicator-timeout/ */ ftp = (verbose ? new com.github.carlosrubio.org.apache.tools.ant.taskdefs.optional.net.FTPClient(getProject()) : new org.apache.commons.net.ftp.FTPClient()); ftp.setDataTimeout(5 * 60 * 1000); // 5 minutes if (this.isConfigurationSet) { ftp = FTPConfigurator.configure(ftp, this); } ftp.setRemoteVerificationEnabled(enableRemoteVerification); ftp.connect(server, port); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("FTP connection failed: " + ftp.getReplyString()); } log("connected", Project.MSG_VERBOSE); log("logging in to FTP server", Project.MSG_VERBOSE); if ((this.account != null && !ftp.login(userid, password, account)) || (this.account == null && !ftp.login(userid, password))) { throw new BuildException("Could not login to FTP server"); } log("login succeeded", Project.MSG_VERBOSE); if (binary) { ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not set transfer type: " + ftp.getReplyString()); } } else { ftp.setFileType(org.apache.commons.net.ftp.FTP.ASCII_FILE_TYPE); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not set transfer type: " + ftp.getReplyString()); } } if (passive) { log("entering passive mode", Project.MSG_VERBOSE); ftp.enterLocalPassiveMode(); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not enter into passive " + "mode: " + ftp.getReplyString()); } } // If an initial command was configured then send it. // Some FTP servers offer different modes of operation, // E.G. switching between a UNIX file system mode and // a legacy file system. if (this.initialSiteCommand != null) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { doSiteCommand(lftp, FTP.this.initialSiteCommand); } }, "initial site command: " + this.initialSiteCommand); } // For a unix ftp server you can set the default mask for all files // created. if (umask != null) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { doSiteCommand(lftp, "umask " + umask); } }, "umask " + umask); } // If the action is MK_DIR, then the specified remote // directory is the directory to create. if (action == MK_DIR) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { makeRemoteDir(lftp, remotedir); } }, remotedir); } else if (action == SITE_CMD) { RetryHandler h = new RetryHandler(this.retriesAllowed, this); final FTPClient lftp = ftp; executeRetryable(h, new Retryable() { public void execute() throws IOException { doSiteCommand(lftp, FTP.this.siteCommand); } }, "Site Command: " + this.siteCommand); } else { if (remotedir != null) { log("changing the remote directory to " + remotedir, Project.MSG_VERBOSE); ftp.changeWorkingDirectory(remotedir); if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { throw new BuildException("could not change remote " + "directory: " + ftp.getReplyString()); } } if (newerOnly && timeDiffAuto) { // in this case we want to find how much time span there is between local // and remote timeDiffMillis = getTimeDiff(ftp); } log(ACTION_STRS[action] + " " + ACTION_TARGET_STRS[action]); transferFiles(ftp); } } catch (IOException ex) { throw new BuildException("error during FTP transfer: " + ex, ex); } finally { if (ftp != null && ftp.isConnected()) { try { log("disconnecting", Project.MSG_VERBOSE); ftp.logout(); ftp.disconnect(); } catch (IOException ex) { // ignore it } } } }
From source file:com.ibm.cics.ca1y.Emit.java
/** * Get the contents of a file from an FTP server. * /* w ww . jav a 2s.c o m*/ * @param filename * - * @param server * - * @param useraname * - * @param userpassword * - * @param transfer * - * @param mode * - * @param epsv * - * @param protocol * - * @param trustmgr * - * @param datatimeout * - * @param proxyserver * - * @param proxyusername * - * @param proxypassword * - * @param anonymouspassword * - * @return byte[] representing the retrieved file, null otherwise. */ private static byte[] getFileUsingFTP(String filename, String server, String username, String userpassword, String transfer, String mode, String epsv, String protocol, String trustmgr, String datatimeout, String proxyserver, String proxyusername, String proxypassword, String anonymouspassword) { FTPClient ftp; if (filename == null || server == null) return null; int port = 0; if (server != null) { String parts[] = server.split(":"); if (parts.length == 2) { server = parts[0]; try { port = Integer.parseInt(parts[1]); } catch (Exception _ex) { } } } int proxyport = 0; if (proxyserver != null) { String parts[] = proxyserver.split(":"); if (parts.length == 2) { proxyserver = parts[0]; try { proxyport = Integer.parseInt(parts[1]); } catch (Exception _ex) { } } } if (username == null) { username = "anonymous"; if (userpassword == null) userpassword = anonymouspassword; } if (protocol == null) { if (proxyserver != null) ftp = new FTPHTTPClient(proxyserver, proxyport, proxyusername, proxypassword); else ftp = new FTPClient(); } else { FTPSClient ftps = null; if ("true".equalsIgnoreCase(protocol)) { ftps = new FTPSClient(true); } else if ("false".equalsIgnoreCase(protocol)) { ftps = new FTPSClient(false); } else if (protocol != null) { String parts[] = protocol.split(","); if (parts.length == 1) ftps = new FTPSClient(protocol); else ftps = new FTPSClient(parts[0], Boolean.parseBoolean(parts[1])); } ftp = ftps; if ("all".equalsIgnoreCase(trustmgr)) { ftps.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager()); } else if ("valid".equalsIgnoreCase(trustmgr)) { ftps.setTrustManager(TrustManagerUtils.getValidateServerCertificateTrustManager()); } else if ("none".equalsIgnoreCase(trustmgr)) { ftps.setTrustManager(null); } } if (datatimeout != null) { try { ftp.setDataTimeout(Integer.parseInt(datatimeout)); } catch (Exception _ex) { ftp.setDataTimeout(-1); } } if (port <= 0) { port = ftp.getDefaultPort(); } if (logger.isLoggable(Level.FINE)) { StringBuilder sb = new StringBuilder(); sb.append(Emit.messages.getString("FTPAboutToGetFileFromServer")).append(" - ").append("file name:") .append(filename).append(",server:").append(server).append(":").append(port) .append(",username:").append(username).append(",userpassword:") .append(userpassword != null && !"anonymous".equals(username) ? "<obscured>" : userpassword) .append(",transfer:").append(transfer).append(",mode:").append(mode).append(",epsv:") .append(epsv).append(",protocol:").append(protocol).append(",trustmgr:").append(trustmgr) .append(",datatimeout:").append(datatimeout).append(",proxyserver:").append(proxyserver) .append(":").append(proxyport).append(",proxyusername:").append(proxyusername) .append(",proxypassword:").append(proxypassword != null ? "<obscured>" : null); logger.fine(sb.toString()); } try { if (port > 0) { ftp.connect(server, port); } else { ftp.connect(server); } int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); logger.warning(Emit.messages.getString("FTPCouldNotConnectToServer")); return null; } } catch (IOException _ex) { logger.warning(Emit.messages.getString("FTPCouldNotConnectToServer")); if (ftp.isConnected()) try { ftp.disconnect(); } catch (IOException _ex2) { } return null; } ByteArrayOutputStream output; try { if (!ftp.login(username, userpassword)) { ftp.logout(); logger.warning(Emit.messages.getString("FTPServerRefusedLoginCredentials")); return null; } if ("binary".equalsIgnoreCase(transfer)) { ftp.setFileType(2); } else { ftp.setFileType(0); } if ("localactive".equalsIgnoreCase(mode)) { ftp.enterLocalActiveMode(); } else { ftp.enterLocalPassiveMode(); } ftp.setUseEPSVwithIPv4("true".equalsIgnoreCase(epsv)); output = new ByteArrayOutputStream(); ftp.retrieveFile(filename, output); output.close(); ftp.noop(); ftp.logout(); } catch (IOException _ex) { logger.warning(Emit.messages.getString("FTPFailedToTransferFile")); if (ftp.isConnected()) try { ftp.disconnect(); } catch (IOException _ex2) { } return null; } return output.toByteArray(); }
From source file:net.yacy.grid.io.assets.FTPStorageFactory.java
public FTPStorageFactory(String server, int port, String username, String password, boolean deleteafterread) throws IOException { this.server = server; this.username = username == null ? "" : username; this.password = password == null ? "" : password; this.port = port; this.deleteafterread = deleteafterread; this.ftpClient = new Storage<byte[]>() { @Override/*from ww w. j av a2 s . com*/ public void checkConnection() throws IOException { return; } private FTPClient initConnection() throws IOException { FTPClient ftp = new FTPClient(); ftp.setDataTimeout(3000); ftp.setConnectTimeout(20000); if (FTPStorageFactory.this.port < 0 || FTPStorageFactory.this.port == DEFAULT_PORT) { ftp.connect(FTPStorageFactory.this.server); } else { ftp.connect(FTPStorageFactory.this.server, FTPStorageFactory.this.port); } ftp.enterLocalPassiveMode(); // the server opens a data port to which the client conducts data transfers int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { if (ftp != null) try { ftp.disconnect(); } catch (Throwable ee) { } throw new IOException("bad connection to ftp server: " + reply); } if (!ftp.login(FTPStorageFactory.this.username, FTPStorageFactory.this.password)) { if (ftp != null) try { ftp.disconnect(); } catch (Throwable ee) { } throw new IOException("login failure"); } ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.setBufferSize(8192); return ftp; } @Override public StorageFactory<byte[]> store(String path, byte[] asset) throws IOException { long t0 = System.currentTimeMillis(); FTPClient ftp = initConnection(); try { long t1 = System.currentTimeMillis(); String file = cdPath(ftp, path); long t2 = System.currentTimeMillis(); ftp.enterLocalPassiveMode(); boolean success = ftp.storeFile(file, new ByteArrayInputStream(asset)); long t3 = System.currentTimeMillis(); if (!success) throw new IOException("storage to path " + path + " was not successful (storeFile=false)"); Data.logger.debug("FTPStorageFactory.store ftp store successfull: check connection = " + (t1 - t0) + ", cdPath = " + (t2 - t1) + ", store = " + (t3 - t2)); } catch (IOException e) { throw e; } finally { if (ftp != null) try { ftp.disconnect(); } catch (Throwable ee) { } } return FTPStorageFactory.this; } @Override public Asset<byte[]> load(String path) throws IOException { FTPClient ftp = initConnection(); ByteArrayOutputStream baos = null; byte[] b = null; try { String file = cdPath(ftp, path); baos = new ByteArrayOutputStream(); ftp.retrieveFile(file, baos); b = baos.toByteArray(); if (FTPStorageFactory.this.deleteafterread) try { boolean deleted = ftp.deleteFile(file); FTPFile[] remaining = ftp.listFiles(); if (remaining.length == 0) { ftp.cwd("/"); if (path.startsWith("/")) path = path.substring(1); int p = path.indexOf('/'); if (p > 0) path = path.substring(0, p); ftp.removeDirectory(path); } } catch (Throwable e) { Data.logger.warn("FTPStorageFactory.load failed to remove asset " + path, e); } } catch (IOException e) { throw e; } finally { if (ftp != null) try { ftp.disconnect(); } catch (Throwable ee) { } } return new Asset<byte[]>(FTPStorageFactory.this, b); } @Override public void close() { } private String cdPath(FTPClient ftp, String path) throws IOException { int success_code = ftp.cwd("/"); if (success_code >= 300) throw new IOException("cannot cd into " + path + ": " + success_code); if (path.length() == 0 || path.equals("/")) return ""; if (path.charAt(0) == '/') path = path.substring(1); // we consider that all paths are absolute to / (home) int p; while ((p = path.indexOf('/')) > 0) { String dir = path.substring(0, p); int code = ftp.cwd(dir); if (code >= 300) { // path may not exist, try to create the path boolean success = ftp.makeDirectory(dir); if (!success) throw new IOException("unable to create directory " + dir + " for path " + path); code = ftp.cwd(dir); if (code >= 300) throw new IOException("unable to cwd into directory " + dir + " for path " + path); } path = path.substring(p + 1); } return path; } }; }
From source file:org.apache.camel.component.file.remote.FtpEndpoint.java
public RemoteFileOperations<FTPFile> createRemoteFileOperations() throws Exception { // configure ftp client FTPClient client = ftpClient; if (client == null) { // must use a new client if not explicit configured to use a custom client client = createFtpClient();//from ww w . jav a2 s. com } // set any endpoint configured timeouts if (getConfiguration().getConnectTimeout() > -1) { client.setConnectTimeout(getConfiguration().getConnectTimeout()); } if (getConfiguration().getSoTimeout() > -1) { soTimeout = getConfiguration().getSoTimeout(); } dataTimeout = getConfiguration().getTimeout(); // then lookup ftp client parameters and set those if (ftpClientParameters != null) { Map<String, Object> localParameters = new HashMap<String, Object>(ftpClientParameters); // setting soTimeout has to be done later on FTPClient (after it has connected) Object timeout = localParameters.remove("soTimeout"); if (timeout != null) { soTimeout = getCamelContext().getTypeConverter().convertTo(int.class, timeout); } // and we want to keep data timeout so we can log it later timeout = localParameters.remove("dataTimeout"); if (timeout != null) { dataTimeout = getCamelContext().getTypeConverter().convertTo(int.class, dataTimeout); } setProperties(client, localParameters); } if (ftpClientConfigParameters != null) { // client config is optional so create a new one if we have parameter for it if (ftpClientConfig == null) { ftpClientConfig = new FTPClientConfig(); } Map<String, Object> localConfigParameters = new HashMap<String, Object>(ftpClientConfigParameters); setProperties(ftpClientConfig, localConfigParameters); } if (dataTimeout > 0) { client.setDataTimeout(dataTimeout); } if (log.isDebugEnabled()) { log.debug("Created FTPClient [connectTimeout: {}, soTimeout: {}, dataTimeout: {}]: {}", new Object[] { client.getConnectTimeout(), getSoTimeout(), dataTimeout, client }); } FtpOperations operations = new FtpOperations(client, getFtpClientConfig()); operations.setEndpoint(this); return operations; }