001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs.provider.ftp;
018:
019: import org.apache.commons.net.ftp.FTP;
020: import org.apache.commons.net.ftp.FTPClient;
021: import org.apache.commons.net.ftp.FTPClientConfig;
022: import org.apache.commons.net.ftp.FTPReply;
023: import org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory;
024: import org.apache.commons.vfs.FileSystemException;
025: import org.apache.commons.vfs.FileSystemOptions;
026: import org.apache.commons.vfs.util.UserAuthenticatorUtils;
027:
028: import java.io.IOException;
029:
030: /**
031: * Create a FtpClient instance
032: *
033: * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
034: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
035: */
036: public class FtpClientFactory {
037: private FtpClientFactory() {
038: }
039:
040: /**
041: * Creates a new connection to the server.
042: */
043: public static FTPClient createConnection(String hostname, int port,
044: char[] username, char[] password, String workingDirectory,
045: FileSystemOptions fileSystemOptions)
046: throws FileSystemException {
047: // Determine the username and password to use
048: if (username == null) {
049: username = "anonymous".toCharArray();
050: }
051:
052: if (password == null) {
053: password = "anonymous".toCharArray();
054: }
055:
056: try {
057: final FTPClient client = new FTPClient();
058:
059: String key = FtpFileSystemConfigBuilder.getInstance()
060: .getEntryParser(fileSystemOptions);
061: if (key != null) {
062: FTPClientConfig config = new FTPClientConfig(key);
063:
064: String serverLanguageCode = FtpFileSystemConfigBuilder
065: .getInstance().getServerLanguageCode(
066: fileSystemOptions);
067: if (serverLanguageCode != null) {
068: config.setServerLanguageCode(serverLanguageCode);
069: }
070: String defaultDateFormat = FtpFileSystemConfigBuilder
071: .getInstance().getDefaultDateFormat(
072: fileSystemOptions);
073: if (defaultDateFormat != null) {
074: config.setDefaultDateFormatStr(defaultDateFormat);
075: }
076: String recentDateFormat = FtpFileSystemConfigBuilder
077: .getInstance().getRecentDateFormat(
078: fileSystemOptions);
079: if (recentDateFormat != null) {
080: config.setRecentDateFormatStr(recentDateFormat);
081: }
082: String serverTimeZoneId = FtpFileSystemConfigBuilder
083: .getInstance().getServerTimeZoneId(
084: fileSystemOptions);
085: if (serverTimeZoneId != null) {
086: config.setServerTimeZoneId(serverTimeZoneId);
087: }
088: String[] shortMonthNames = FtpFileSystemConfigBuilder
089: .getInstance().getShortMonthNames(
090: fileSystemOptions);
091: if (shortMonthNames != null) {
092: StringBuffer shortMonthNamesStr = new StringBuffer(
093: 40);
094: for (int i = 0; i < shortMonthNames.length; i++) {
095: if (shortMonthNamesStr.length() > 0) {
096: shortMonthNamesStr.append("|");
097: }
098: shortMonthNamesStr.append(shortMonthNames[i]);
099: }
100: config.setShortMonthNames(shortMonthNamesStr
101: .toString());
102: }
103:
104: client.configure(config);
105: }
106:
107: FTPFileEntryParserFactory myFactory = FtpFileSystemConfigBuilder
108: .getInstance().getEntryParserFactory(
109: fileSystemOptions);
110: if (myFactory != null) {
111: client.setParserFactory(myFactory);
112: }
113:
114: try {
115: client.connect(hostname, port);
116:
117: int reply = client.getReplyCode();
118: if (!FTPReply.isPositiveCompletion(reply)) {
119: throw new FileSystemException(
120: "vfs.provider.ftp/connect-rejected.error",
121: hostname);
122: }
123:
124: // Login
125: if (!client.login(UserAuthenticatorUtils
126: .toString(username), UserAuthenticatorUtils
127: .toString(password))) {
128: throw new FileSystemException(
129: "vfs.provider.ftp/login.error",
130: new Object[] {
131: hostname,
132: UserAuthenticatorUtils
133: .toString(username) }, null);
134: }
135:
136: // Set binary mode
137: if (!client.setFileType(FTP.BINARY_FILE_TYPE)) {
138: throw new FileSystemException(
139: "vfs.provider.ftp/set-binary.error",
140: hostname);
141: }
142:
143: // Set dataTimeout value
144: Integer dataTimeout = FtpFileSystemConfigBuilder
145: .getInstance()
146: .getDataTimeout(fileSystemOptions);
147: if (dataTimeout != null) {
148: client.setDataTimeout(dataTimeout.intValue());
149: }
150:
151: // Change to root by default
152: // All file operations a relative to the filesystem-root
153: // String root = getRoot().getName().getPath();
154:
155: Boolean userDirIsRoot = FtpFileSystemConfigBuilder
156: .getInstance().getUserDirIsRoot(
157: fileSystemOptions);
158: if (workingDirectory != null
159: && (userDirIsRoot == null || !userDirIsRoot
160: .booleanValue())) {
161: if (!client
162: .changeWorkingDirectory(workingDirectory)) {
163: throw new FileSystemException(
164: "vfs.provider.ftp/change-work-directory.error",
165: workingDirectory);
166: }
167: }
168:
169: Boolean passiveMode = FtpFileSystemConfigBuilder
170: .getInstance()
171: .getPassiveMode(fileSystemOptions);
172: if (passiveMode != null && passiveMode.booleanValue()) {
173: client.enterLocalPassiveMode();
174: }
175: } catch (final IOException e) {
176: if (client.isConnected()) {
177: client.disconnect();
178: }
179: throw e;
180: }
181:
182: return client;
183: } catch (final Exception exc) {
184: throw new FileSystemException(
185: "vfs.provider.ftp/connect.error",
186: new Object[] { hostname }, exc);
187: }
188: }
189: }
|