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.net.Proxy;
020
021import org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory;
022import org.apache.commons.vfs2.FileSystem;
023import org.apache.commons.vfs2.FileSystemConfigBuilder;
024import org.apache.commons.vfs2.FileSystemOptions;
025
026/**
027 * The config builder for various ftp configuration options.
028 */
029public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder
030{
031    private static final String _PREFIX = FtpFileSystemConfigBuilder.class.getName();
032
033    private static final FtpFileSystemConfigBuilder BUILDER = new FtpFileSystemConfigBuilder();
034
035    private static final String CONNECT_TIMEOUT = _PREFIX + ".CONNECT_TIMEOUT";
036    private static final String DATA_TIMEOUT = _PREFIX + ".DATA_TIMEOUT";
037    private static final String DEFAULT_DATE_FORMAT = _PREFIX + ".DEFAULT_DATE_FORMAT";
038    private static final String ENCODING = _PREFIX + ".ENCODING";
039    private static final String FACTORY_KEY = FTPFileEntryParserFactory.class.getName() + ".KEY";
040    private static final String FILE_TYPE = _PREFIX + ".FILE_TYPE";
041    private static final String PASSIVE_MODE = _PREFIX + ".PASSIVE";
042    private static final String PROXY = _PREFIX + ".PROXY";
043    private static final String RECENT_DATE_FORMAT = _PREFIX + ".RECENT_DATE_FORMAT";
044    private static final String REMOTE_VERIFICATION = _PREFIX + ".REMOTE_VERIFICATION";
045    private static final String SERVER_LANGUAGE_CODE = _PREFIX + ".SERVER_LANGUAGE_CODE";
046    private static final String SERVER_TIME_ZONE_ID = _PREFIX + ".SERVER_TIME_ZONE_ID";
047    private static final String SHORT_MONTH_NAMES = _PREFIX + ".SHORT_MONTH_NAMES";
048    private static final String SO_TIMEOUT = _PREFIX + ".SO_TIMEOUT";
049    private static final String USER_DIR_IS_ROOT = _PREFIX + ".USER_DIR_IS_ROOT";
050
051    private FtpFileSystemConfigBuilder()
052    {
053        super("ftp.");
054    }
055
056    /**
057     * Create new config builder with specified prefix string.
058     * @param prefix prefix string to use for parameters of this config builder.
059     * @since 2.1
060     */
061    protected FtpFileSystemConfigBuilder(final String prefix)
062    {
063        super(prefix);
064    }
065
066    /**
067     * Gets the singleton instance.
068     *
069     * @return the singleton instance.
070     */
071    public static FtpFileSystemConfigBuilder getInstance()
072    {
073        return BUILDER;
074    }
075
076    @Override
077    protected Class<? extends FileSystem> getConfigClass()
078    {
079        return FtpFileSystem.class;
080    }
081
082    /**
083     * Gets the timeout in milliseconds to use for the socket connection.
084     *
085     * @param opts The FileSystemOptions.
086     * @return The timeout in milliseconds to use for the socket connection.
087     * @since 2.1
088     */
089    public Integer getConnectTimeout(final FileSystemOptions opts)
090    {
091        return getInteger(opts, CONNECT_TIMEOUT);
092    }
093
094    /**
095     * @param opts The FileSystemOptions.
096     * @return The encoding.
097     * @since 2.0
098     * */
099    public String getControlEncoding(final FileSystemOptions opts)
100    {
101        return getString(opts, ENCODING);
102    }
103
104    /**
105     * @param opts The FileSystemOptions.
106     * @return The timeout for opening the data channel in milliseconds.
107     * @see #setDataTimeout
108     */
109    public Integer getDataTimeout(final FileSystemOptions opts)
110    {
111        return getInteger(opts, DATA_TIMEOUT);
112    }
113
114    /**
115     * Get the default date format used by the server. See {@link org.apache.commons.net.ftp.FTPClientConfig}
116     * for details and examples.
117     *
118     * @param opts The FileSystemOptions
119     * @return The default date format.
120     */
121    public String getDefaultDateFormat(final FileSystemOptions opts)
122    {
123        return getString(opts, DEFAULT_DATE_FORMAT);
124    }
125
126    /**
127     * @param opts The FileSystemOptions.
128     * @see #setEntryParser
129     * @return the key to the EntryParser.
130     */
131    public String getEntryParser(final FileSystemOptions opts)
132    {
133        return getString(opts, FACTORY_KEY);
134    }
135
136    /**
137     * @param opts The FlleSystemOptions.
138     * @see #setEntryParserFactory
139     * @return An FTPFileEntryParserFactory.
140     */
141    public FTPFileEntryParserFactory getEntryParserFactory(final FileSystemOptions opts)
142    {
143        return (FTPFileEntryParserFactory) getParam(opts, FTPFileEntryParserFactory.class.getName());
144    }
145
146    /**
147     * Gets the file type parameter.
148     *
149     * @param opts The FileSystemOptions.
150     * @return A FtpFileType
151     * @since 2.1
152     */
153    public FtpFileType getFileType(final FileSystemOptions opts)
154    {
155        return getEnum(FtpFileType.class, opts, FILE_TYPE);
156    }
157
158    /**
159     * @param opts The FileSystemOptions.
160     * @return true if passive mode is set.
161     * @see #setPassiveMode
162     */
163    public Boolean getPassiveMode(final FileSystemOptions opts)
164    {
165        return getBoolean(opts, PASSIVE_MODE);
166    }
167
168    /**
169     * Gets the Proxy.
170     *
171     * @param opts The FileSystemOptions.
172     * @return the Proxy
173     * @since 2.1
174     */
175    public Proxy getProxy(final FileSystemOptions opts)
176    {
177        return (Proxy) this.getParam(opts, PROXY);
178    }
179
180    /**
181     * See {@link org.apache.commons.net.ftp.FTPClientConfig} for details and examples.
182     *
183     * @param opts The FileSystemOptions.
184     * @return The recent date format.
185     */
186    public String getRecentDateFormat(final FileSystemOptions opts)
187    {
188        return getString(opts, RECENT_DATE_FORMAT);
189    }
190
191    /**
192     * Gets whether to use remote verification.
193     *
194     * @param opts The FileSystemOptions.
195     * @return True if remote verification should be done.
196     */
197    public Boolean getRemoteVerification(FileSystemOptions opts)
198    {
199        return getBoolean(opts, REMOTE_VERIFICATION);
200    }
201
202    /**
203     * Get the language code used by the server. See {@link org.apache.commons.net.ftp.FTPClientConfig}
204     * for details and examples.
205     *
206     * @param opts The FilesystemOptions.
207     * @return The language code of the server.
208     */
209    public String getServerLanguageCode(final FileSystemOptions opts)
210    {
211        return getString(opts, SERVER_LANGUAGE_CODE);
212    }
213
214    /**
215     * See {@link org.apache.commons.net.ftp.FTPClientConfig} for details and examples.
216     *
217     * @param opts The FileSystemOptions.
218     * @return The server timezone id.
219     */
220    public String getServerTimeZoneId(final FileSystemOptions opts)
221    {
222        return getString(opts, SERVER_TIME_ZONE_ID);
223    }
224
225    /**
226     * See {@link org.apache.commons.net.ftp.FTPClientConfig} for details and examples.
227     *
228     * @param opts The FileSystemOptions.
229     * @return An array of short month names.
230     */
231    public String[] getShortMonthNames(final FileSystemOptions opts)
232    {
233        return (String[]) getParam(opts, SHORT_MONTH_NAMES);
234    }
235
236    /**
237     * @param opts The FileSystem options.
238     * @return The timeout value in milliseconds.
239     * @see #getDataTimeout
240     * @since 2.0
241     */
242    public Integer getSoTimeout(final FileSystemOptions opts)
243    {
244        return getInteger(opts, SO_TIMEOUT);
245    }
246
247    /**
248     * Returns {@link Boolean#TRUE} if VFS should treat the user directory as the root directory. Defaults to
249     * <code>Boolean.TRUE</code> if the method {@link #setUserDirIsRoot(FileSystemOptions, boolean)} has not been
250     * invoked.
251     *
252     * @param opts
253     *            The FileSystemOptions.
254     * @return <code>Boolean.TRUE</code> if VFS treats the user directory as the root directory.
255     * @see #setUserDirIsRoot
256     */
257    public Boolean getUserDirIsRoot(final FileSystemOptions opts)
258    {
259        return getBoolean(opts, USER_DIR_IS_ROOT, Boolean.TRUE);
260    }
261
262    /**
263     * Sets the timeout for the initial control connection.
264     * <p>
265     * If you set the connectTimeout to {@code null} no connectTimeout will be set.
266     *
267     * @param opts The FileSystemOptions.
268     * @param connectTimeout the timeout value in milliseconds
269     * @since 2.1
270     */
271    public void setConnectTimeout(final FileSystemOptions opts, final Integer connectTimeout)
272    {
273        setParam(opts, CONNECT_TIMEOUT, connectTimeout);
274    }
275
276    /**
277     * See {@link org.apache.commons.net.ftp.FTP#setControlEncoding} for details and examples.
278     *
279     * @param opts The FileSystemOptions.
280     * @param encoding the encoding to use
281     * @since 2.0
282     */
283    public void setControlEncoding(final FileSystemOptions opts, final String encoding)
284    {
285        setParam(opts, ENCODING, encoding);
286    }
287
288    /**
289     * Set the data timeout for the ftp client.
290     * <p>
291     * If you set the {@code dataTimeout} to {@code null}, no dataTimeout will be set on the
292     * ftp client.
293     *
294     * @param opts The FileSystemOptions.
295     * @param dataTimeout The timeout value.
296     */
297    public void setDataTimeout(final FileSystemOptions opts, final Integer dataTimeout)
298    {
299        setParam(opts, DATA_TIMEOUT, dataTimeout);
300    }
301
302    /**
303     * Set the default date format used by the server. See {@link org.apache.commons.net.ftp.FTPClientConfig}
304     * for details and examples.
305     *
306     * @param opts The FileSystemOptions.
307     * @param defaultDateFormat The default date format.
308     */
309    public void setDefaultDateFormat(final FileSystemOptions opts, final String defaultDateFormat)
310    {
311        setParam(opts, DEFAULT_DATE_FORMAT, defaultDateFormat);
312    }
313
314    /**
315     * Set the FQCN of your FileEntryParser used to parse the directory listing from your server.
316     * <p>
317     * If you do not use the default commons-net FTPFileEntryParserFactory e.g. by using
318     * {@link #setEntryParserFactory} this is the "key" parameter passed as argument into your
319     * custom factory.
320     *
321     * @param opts The FileSystemOptions.
322     * @param key The key.
323     */
324    public void setEntryParser(final FileSystemOptions opts, final String key)
325    {
326        setParam(opts, FACTORY_KEY, key);
327    }
328
329    /**
330     * FTPFileEntryParserFactory which will be used for ftp-entry parsing.
331     *
332     * @param opts The FileSystemOptions.
333     * @param factory instance of your factory
334     */
335    public void setEntryParserFactory(final FileSystemOptions opts, final FTPFileEntryParserFactory factory)
336    {
337        setParam(opts, FTPFileEntryParserFactory.class.getName(), factory);
338    }
339
340    /**
341     * Sets the file type parameter.
342     *
343     * @param opts The FileSystemOptions.
344     * @param ftpFileType A FtpFileType
345     * @since 2.1
346     */
347    public void setFileType(final FileSystemOptions opts, final FtpFileType ftpFileType)
348    {
349        setParam(opts, FILE_TYPE, ftpFileType);
350    }
351
352    /**
353     * Enter into passive mode.
354     *
355     * @param opts The FileSystemOptions.
356     * @param passiveMode true if passive mode should be used.
357     */
358    public void setPassiveMode(final FileSystemOptions opts, final boolean passiveMode)
359    {
360        setParam(opts, PASSIVE_MODE, passiveMode ? Boolean.TRUE : Boolean.FALSE);
361    }
362
363    /**
364     * Sets the Proxy.
365     * <p>
366     * You might need to make sure that {@link #setPassiveMode(FileSystemOptions, boolean) passive mode}
367     * is activated.
368     *
369     * @param opts the FileSystem options.
370     * @param proxy the Proxy
371     * @since 2.1
372     */
373    public void setProxy(final FileSystemOptions opts, Proxy proxy)
374    {
375        setParam(opts, PROXY, proxy);
376    }
377
378    /**
379     * See {@link org.apache.commons.net.ftp.FTPClientConfig} for details and examples.
380     *
381     * @param opts The FileSystemOptions.
382     * @param recentDateFormat The recent date format.
383     */
384    public void setRecentDateFormat(final FileSystemOptions opts, final String recentDateFormat)
385    {
386        setParam(opts, RECENT_DATE_FORMAT, recentDateFormat);
387    }
388
389    /**
390     * Sets whether to use remote verification.
391     *
392     * @param opts The FileSystemOptions.
393     * @param remoteVerification True if verification should be done.
394     */
395    public void setRemoteVerification(FileSystemOptions opts, boolean remoteVerification)
396    {
397        setParam(opts, REMOTE_VERIFICATION, remoteVerification);
398    }
399
400    /**
401     * Set the language code used by the server. See {@link org.apache.commons.net.ftp.FTPClientConfig}
402     * for details and examples.
403     *
404     * @param opts The FileSystemOptions.
405     * @param serverLanguageCode The servers language code.
406     */
407    public void setServerLanguageCode(final FileSystemOptions opts, final String serverLanguageCode)
408    {
409        setParam(opts, SERVER_LANGUAGE_CODE, serverLanguageCode);
410    }
411
412    /**
413     * See {@link org.apache.commons.net.ftp.FTPClientConfig} for details and examples.
414     *
415     * @param opts The FileSystemOptions.
416     * @param serverTimeZoneId The server timezone id.
417     */
418    public void setServerTimeZoneId(final FileSystemOptions opts, final String serverTimeZoneId)
419    {
420        setParam(opts, SERVER_TIME_ZONE_ID, serverTimeZoneId);
421    }
422
423    /**
424     * See {@link org.apache.commons.net.ftp.FTPClientConfig} for details and examples.
425     *
426     * @param opts The FileSystemOptions.
427     * @param shortMonthNames an array of short month name Strings.
428     */
429    public void setShortMonthNames(final FileSystemOptions opts, final String[] shortMonthNames)
430    {
431        String[] clone = null;
432        if (shortMonthNames != null)
433        {
434            clone = new String[shortMonthNames.length];
435            System.arraycopy(shortMonthNames, 0, clone, 0, shortMonthNames.length);
436        }
437
438        setParam(opts, SHORT_MONTH_NAMES, clone);
439    }
440
441    /**
442     * Sets the socket timeout for the FTP client.
443     * <p>
444     * If you set the {@code soTimeout} to {@code null}, no socket timeout will be set on the
445     * ftp client.
446     *
447     * @param opts The FileSystem options.
448     * @param soTimeout The timeout value in milliseconds.
449     * @since 2.0
450     */
451    public void setSoTimeout(final FileSystemOptions opts, final Integer soTimeout)
452    {
453        setParam(opts, SO_TIMEOUT, soTimeout);
454    }
455
456    /**
457     * Use user directory as root (do not change to fs root).
458     *
459     * @param opts The FileSystemOptions.
460     * @param userDirIsRoot true if the user directory should be treated as the root.
461     */
462    public void setUserDirIsRoot(final FileSystemOptions opts, final boolean userDirIsRoot)
463    {
464        setParam(opts, USER_DIR_IS_ROOT, userDirIsRoot ? Boolean.TRUE : Boolean.FALSE);
465    }
466
467}