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 */
017package org.apache.commons.vfs2.provider.ftp;
018
019import java.io.IOException;
020import java.io.PrintWriter;
021import java.io.StringWriter;
022import java.io.Writer;
023import java.net.Proxy;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.apache.commons.net.PrintCommandListener;
028import org.apache.commons.net.ftp.FTPClient;
029import org.apache.commons.net.ftp.FTPClientConfig;
030import org.apache.commons.net.ftp.FTPReply;
031import org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory;
032import org.apache.commons.vfs2.FileSystemException;
033import org.apache.commons.vfs2.FileSystemOptions;
034import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
035
036/**
037 * Create a FtpClient instance.
038 */
039public final class FtpClientFactory
040{
041    private FtpClientFactory()
042    {
043    }
044
045    /**
046     * Creates a new connection to the server.
047     *
048     * @param hostname          The host name of the server.
049     * @param port              The port to connect to.
050     * @param username          The name of the user for authentication.
051     * @param password          The user's password.
052     * @param workingDirectory  The base directory.
053     * @param fileSystemOptions The FileSystemOptions.
054     * @return An FTPClient.
055     * @throws FileSystemException if an error occurs while connecting.
056     */
057    public static FTPClient createConnection(final String hostname, final int port,
058                                             final char[] username, final char[] password,
059                                             final String workingDirectory, final FileSystemOptions fileSystemOptions)
060        throws FileSystemException
061    {
062        final FtpConnectionFactory factory = new FtpConnectionFactory(FtpFileSystemConfigBuilder.getInstance());
063        return factory.createConnection(hostname, port, username, password, workingDirectory, fileSystemOptions);
064    }
065
066    /** Connection Factory, used to configure the FTPClient. */
067    public static final class FtpConnectionFactory extends ConnectionFactory<FTPClient, FtpFileSystemConfigBuilder>
068    {
069        private FtpConnectionFactory(final FtpFileSystemConfigBuilder builder)
070        {
071            super(builder);
072        }
073
074        @Override
075        protected FTPClient createClient(final FileSystemOptions fileSystemOptions)
076        {
077            return new FTPClient();
078        }
079
080        @Override
081        protected void setupOpenConnection(final FTPClient client, final FileSystemOptions fileSystemOptions)
082        {
083            // nothing to do for FTP
084        }
085    }
086
087    /** Abstract Factory, used to configure different FTPClients. */
088    public abstract static class ConnectionFactory<C extends FTPClient, B extends FtpFileSystemConfigBuilder>
089    {
090        private static final char[] ANON_CHAR_ARRAY = "anonymous".toCharArray();
091        private static final int BUFSZ = 40;
092        private final Log log = LogFactory.getLog(getClass());
093
094        protected B builder;
095
096        protected ConnectionFactory(final B builder)
097        {
098            this.builder = builder;
099        }
100
101        public C createConnection(final String hostname, final int port, char[] username, char[] password,
102            final String workingDirectory, final FileSystemOptions fileSystemOptions) throws FileSystemException
103        {
104            // Determine the username and password to use
105            if (username == null)
106            {
107                username = ANON_CHAR_ARRAY;
108            }
109
110            if (password == null)
111            {
112                password = ANON_CHAR_ARRAY;
113            }
114
115            try
116            {
117                final C client = createClient(fileSystemOptions);
118
119                if (log.isDebugEnabled())
120                {
121                    final Writer writer = new StringWriter(1024)
122                    {
123                        @Override
124                        public void flush()
125                        {
126                            final StringBuffer buffer = getBuffer();
127                            String message = buffer.toString();
128                            if (message.toUpperCase().startsWith("PASS ") && message.length() > 5)
129                            {
130                                message = "PASS ***";
131                            }
132                            log.debug(message);
133                            buffer.setLength(0);
134                        }
135                    };
136                    client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(writer)));
137                }
138
139                configureClient(fileSystemOptions, client);
140
141                final FTPFileEntryParserFactory myFactory =
142                    builder.getEntryParserFactory(fileSystemOptions);
143                if (myFactory != null)
144                {
145                    client.setParserFactory(myFactory);
146                }
147
148                Boolean remoteVerification = builder.getRemoteVerification(fileSystemOptions);
149                if (remoteVerification != null)
150                {
151                    client.setRemoteVerificationEnabled(remoteVerification.booleanValue());
152                }
153
154                try
155                {
156                    // Set connect timeout
157                    final Integer connectTimeout = builder.getConnectTimeout(fileSystemOptions);
158                    if (connectTimeout != null)
159                    {
160                        client.setDefaultTimeout(connectTimeout.intValue());
161                    }
162
163                    final String controlEncoding = builder.getControlEncoding(fileSystemOptions);
164                    if (controlEncoding != null)
165                    {
166                        client.setControlEncoding(controlEncoding);
167                    }
168
169                    final Proxy proxy = builder.getProxy(fileSystemOptions);
170                    if (proxy != null)
171                    {
172                        client.setProxy(proxy);
173                    }
174
175                    client.connect(hostname, port);
176
177                    final int reply = client.getReplyCode();
178                    if (!FTPReply.isPositiveCompletion(reply))
179                    {
180                        throw new FileSystemException("vfs.provider.ftp/connect-rejected.error", hostname);
181                    }
182
183                    // Login
184                    if (!client.login(
185                        UserAuthenticatorUtils.toString(username),
186                        UserAuthenticatorUtils.toString(password)))
187                    {
188                        throw new FileSystemException("vfs.provider.ftp/login.error",
189                            hostname, UserAuthenticatorUtils.toString(username));
190                    }
191
192                    FtpFileType fileType = builder.getFileType(fileSystemOptions);
193                    if (fileType == null)
194                    {
195                        fileType = FtpFileType.BINARY;
196                    }
197                    // Set binary mode
198                    if (!client.setFileType(fileType.getValue()))
199                    {
200                        throw new FileSystemException("vfs.provider.ftp/set-file-type.error", fileType);
201                    }
202
203                    // Set dataTimeout value
204                    final Integer dataTimeout = builder.getDataTimeout(fileSystemOptions);
205                    if (dataTimeout != null)
206                    {
207                        client.setDataTimeout(dataTimeout.intValue());
208                    }
209
210                    final Integer socketTimeout = builder.getSoTimeout(fileSystemOptions);
211                    if (socketTimeout != null)
212                    {
213                        client.setSoTimeout(socketTimeout.intValue());
214                    }
215
216                    final Boolean userDirIsRoot = builder.getUserDirIsRoot(fileSystemOptions);
217                    if (workingDirectory != null && (userDirIsRoot == null || !userDirIsRoot.booleanValue()))
218                    {
219                        if (!client.changeWorkingDirectory(workingDirectory))
220                        {
221                            throw new FileSystemException("vfs.provider.ftp/change-work-directory.error",
222                                                          workingDirectory);
223                        }
224                    }
225
226                    final Boolean passiveMode = builder.getPassiveMode(fileSystemOptions);
227                    if (passiveMode != null && passiveMode.booleanValue())
228                    {
229                        client.enterLocalPassiveMode();
230                    }
231
232                    setupOpenConnection(client, fileSystemOptions);
233                }
234                catch (final IOException e)
235                {
236                    if (client.isConnected())
237                    {
238                        client.disconnect();
239                    }
240                    throw e;
241                }
242
243                return client;
244            }
245            catch (final Exception exc)
246            {
247                throw new FileSystemException("vfs.provider.ftp/connect.error", exc, hostname);
248            }
249        }
250
251        protected abstract C createClient(FileSystemOptions fileSystemOptions) throws FileSystemException;
252        protected abstract void setupOpenConnection(C client, FileSystemOptions fileSystemOptions) throws IOException;
253
254        private void configureClient(final FileSystemOptions fileSystemOptions, final C client)
255        {
256            final String key = builder.getEntryParser(fileSystemOptions);
257            if (key != null)
258            {
259                final FTPClientConfig config = new FTPClientConfig(key);
260
261                final String serverLanguageCode =
262                    builder.getServerLanguageCode(fileSystemOptions);
263                if (serverLanguageCode != null)
264                {
265                    config.setServerLanguageCode(serverLanguageCode);
266                }
267                final String defaultDateFormat =
268                    builder.getDefaultDateFormat(fileSystemOptions);
269                if (defaultDateFormat != null)
270                {
271                    config.setDefaultDateFormatStr(defaultDateFormat);
272                }
273                final String recentDateFormat =
274                    builder.getRecentDateFormat(fileSystemOptions);
275                if (recentDateFormat != null)
276                {
277                    config.setRecentDateFormatStr(recentDateFormat);
278                }
279                final String serverTimeZoneId =
280                    builder.getServerTimeZoneId(fileSystemOptions);
281                if (serverTimeZoneId != null)
282                {
283                    config.setServerTimeZoneId(serverTimeZoneId);
284                }
285                final String[] shortMonthNames =
286                    builder.getShortMonthNames(fileSystemOptions);
287                if (shortMonthNames != null)
288                {
289                    final StringBuilder shortMonthNamesStr = new StringBuilder(BUFSZ);
290                    for (final String shortMonthName : shortMonthNames)
291                    {
292                        if (shortMonthNamesStr.length() > 0)
293                        {
294                            shortMonthNamesStr.append("|");
295                        }
296                        shortMonthNamesStr.append(shortMonthName);
297                    }
298                    config.setShortMonthNames(shortMonthNamesStr.toString());
299                }
300
301                client.configure(config);
302            }
303        }
304    }
305}