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.vfs2.provider.ftp;
018    
019    import org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory;
020    import org.apache.commons.vfs2.FileSystem;
021    import org.apache.commons.vfs2.FileSystemConfigBuilder;
022    import org.apache.commons.vfs2.FileSystemOptions;
023    
024    /**
025     * The config BUILDER for various ftp configuration options.
026     *
027     * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
028     */
029    public final class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder
030    {
031        private static final FtpFileSystemConfigBuilder BUILDER = new FtpFileSystemConfigBuilder();
032    
033        private static final String FACTORY_KEY = FTPFileEntryParserFactory.class.getName() + ".KEY";
034        private static final String PASSIVE_MODE = FtpFileSystemConfigBuilder.class.getName() + ".PASSIVE";
035        private static final String USER_DIR_IS_ROOT = FtpFileSystemConfigBuilder.class.getName() + ".USER_DIR_IS_ROOT";
036        private static final String DATA_TIMEOUT = FtpFileSystemConfigBuilder.class.getName() + ".DATA_TIMEOUT";
037        private static final String SO_TIMEOUT = FtpFileSystemConfigBuilder.class.getName() + ".SO_TIMEOUT";
038    
039        private static final String SERVER_LANGUAGE_CODE =
040                FtpFileSystemConfigBuilder.class.getName() + ".SERVER_LANGUAGE_CODE";
041        private static final String DEFAULT_DATE_FORMAT =
042                FtpFileSystemConfigBuilder.class.getName() + ".DEFAULT_DATE_FORMAT";
043        private static final String RECENT_DATE_FORMAT =
044                FtpFileSystemConfigBuilder.class.getName() + ".RECENT_DATE_FORMAT";
045        private static final String SERVER_TIME_ZONE_ID =
046                FtpFileSystemConfigBuilder.class.getName() + ".SERVER_TIME_ZONE_ID";
047        private static final String SHORT_MONTH_NAMES =
048                FtpFileSystemConfigBuilder.class.getName() + ".SHORT_MONTH_NAMES";
049        private static final String ENCODING =
050                FtpFileSystemConfigBuilder.class.getName() + ".ENCODING";
051    
052        private FtpFileSystemConfigBuilder()
053        {
054            super("ftp.");
055        }
056    
057        public static FtpFileSystemConfigBuilder getInstance()
058        {
059            return BUILDER;
060        }
061    
062        /**
063         * FTPFileEntryParserFactory which will be used for ftp-entry parsing.
064         *
065         * @param opts The FileSystemOptions.
066         * @param factory instance of your factory
067         */
068        public void setEntryParserFactory(FileSystemOptions opts, FTPFileEntryParserFactory factory)
069        {
070            setParam(opts, FTPFileEntryParserFactory.class.getName(), factory);
071        }
072    
073        /**
074         * @param opts The FlleSystemOptions.
075         * @see #setEntryParserFactory
076         * @return An FTPFileEntryParserFactory.
077         */
078        public FTPFileEntryParserFactory getEntryParserFactory(FileSystemOptions opts)
079        {
080            return (FTPFileEntryParserFactory) getParam(opts, FTPFileEntryParserFactory.class.getName());
081        }
082    
083        /**
084         * set the FQCN of your FileEntryParser used to parse the directory listing from your server.<br />
085         * <br />
086         * <i>If you do not use the default commons-net FTPFileEntryParserFactory e.g. by using
087         * {@link #setEntryParserFactory}
088         * this is the "key" parameter passed as argument into your custom factory</i>
089         *
090         * @param opts The FileSystemOptions.
091         * @param key The key.
092         */
093        public void setEntryParser(FileSystemOptions opts, String key)
094        {
095            setParam(opts, FACTORY_KEY, key);
096        }
097    
098        /**
099         * @param opts The FileSystemOptions.
100         * @see #setEntryParser
101         * @return the key to the EntryParser.
102         */
103        public String getEntryParser(FileSystemOptions opts)
104        {
105            return getString(opts, FACTORY_KEY);
106        }
107    
108        @Override
109        protected Class<? extends FileSystem> getConfigClass()
110        {
111            return FtpFileSystem.class;
112        }
113    
114        /**
115         * enter into passive mode.
116         *
117         * @param opts The FileSystemOptions.
118         * @param passiveMode true if passive mode should be used.
119         */
120        public void setPassiveMode(FileSystemOptions opts, boolean passiveMode)
121        {
122            setParam(opts, PASSIVE_MODE, passiveMode ? Boolean.TRUE : Boolean.FALSE);
123        }
124    
125        /**
126         * @param opts The FileSystemOptions.
127         * @return true if passive mode is set.
128         * @see #setPassiveMode
129         */
130        public Boolean getPassiveMode(FileSystemOptions opts)
131        {
132            return getBoolean(opts, PASSIVE_MODE);
133        }
134    
135        /**
136         * use user directory as root (do not change to fs root).
137         *
138         * @param opts The FileSystemOptions.
139         * @param userDirIsRoot true if the user directory should be treated as the root.
140         */
141        public void setUserDirIsRoot(FileSystemOptions opts, boolean userDirIsRoot)
142        {
143            setParam(opts, USER_DIR_IS_ROOT, userDirIsRoot ? Boolean.TRUE : Boolean.FALSE);
144        }
145    
146        /**
147         * @param opts The FileSystemOptions.
148         * @return true if the user directory is treated as the root.
149         * @see #setUserDirIsRoot
150         */
151        public Boolean getUserDirIsRoot(FileSystemOptions opts)
152        {
153            return getBoolean(opts, USER_DIR_IS_ROOT);
154        }
155    
156        /**
157         * @param opts The FileSystemOptions.
158         * @return The timeout as an Integer.
159         * @see #setDataTimeout
160         */
161        public Integer getDataTimeout(FileSystemOptions opts)
162        {
163            return getInteger(opts, DATA_TIMEOUT);
164        }
165    
166        /**
167         * set the data timeout for the ftp client.<br />
168         * If you set the dataTimeout to <code>null</code> no dataTimeout will be set on the
169         * ftp client.
170         *
171         * @param opts The FileSystemOptions.
172         * @param dataTimeout The timeout value.
173         */
174        public void setDataTimeout(FileSystemOptions opts, Integer dataTimeout)
175        {
176            setParam(opts, DATA_TIMEOUT, dataTimeout);
177        }
178    
179        /**
180         * @param opts The FileSystem options.
181         * @return The timeout value.
182         * @see #getDataTimeout
183         * @since 2.0
184         */
185        public Integer getSoTimeout(FileSystemOptions opts)
186        {
187            return (Integer) getParam(opts, SO_TIMEOUT);
188        }
189    
190        /**
191         * set the socket timeout for the ftp client.<br />
192         * If you set the socketTimeout to <code>null</code> no socketTimeout will be set on the
193         * ftp client.
194         *
195         * @param opts The FileSystem options.
196         * @param soTimeout The timeout value.
197         * @since 2.0
198         */
199        public void setSoTimeout(FileSystemOptions opts, Integer soTimeout)
200        {
201            setParam(opts, SO_TIMEOUT, soTimeout);
202        }
203    
204        /**
205         * get the language code used by the server. see {@link org.apache.commons.net.ftp.FTPClientConfig}
206         * for details and examples.
207         * @param opts The FilesystemOptions.
208         * @return The language code of the server.
209         */
210        public String getServerLanguageCode(FileSystemOptions opts)
211        {
212            return getString(opts, SERVER_LANGUAGE_CODE);
213        }
214    
215        /**
216         * set the language code used by the server. see {@link org.apache.commons.net.ftp.FTPClientConfig}
217         * for details and examples.
218         * @param opts The FileSystemOptions.
219         * @param serverLanguageCode The servers language code.
220         */
221        public void setServerLanguageCode(FileSystemOptions opts, String serverLanguageCode)
222        {
223            setParam(opts, SERVER_LANGUAGE_CODE, serverLanguageCode);
224        }
225    
226        /**
227         * get the language code used by the server. see {@link org.apache.commons.net.ftp.FTPClientConfig}
228         * for details and examples.
229         * @param opts The FileSystemOptions
230         * @return The default date format.
231         */
232        public String getDefaultDateFormat(FileSystemOptions opts)
233        {
234            return getString(opts, DEFAULT_DATE_FORMAT);
235        }
236    
237        /**
238         * set the language code used by the server. see {@link org.apache.commons.net.ftp.FTPClientConfig}
239         * for details and examples.
240         * @param opts The FileSystemOptions.
241         * @param defaultDateFormat The default date format.
242         */
243        public void setDefaultDateFormat(FileSystemOptions opts, String defaultDateFormat)
244        {
245            setParam(opts, DEFAULT_DATE_FORMAT, defaultDateFormat);
246        }
247    
248        /**
249         * see {@link org.apache.commons.net.ftp.FTPClientConfig} for details and examples.
250         * @param opts The FileSystemOptions.
251         * @return The recent date format.
252         */
253        public String getRecentDateFormat(FileSystemOptions opts)
254        {
255            return getString(opts, RECENT_DATE_FORMAT);
256        }
257    
258        /**
259         * see {@link org.apache.commons.net.ftp.FTPClientConfig} for details and examples.
260         * @param opts The FileSystemOptions.
261         * @param recentDateFormat The recent date format.
262         */
263        public void setRecentDateFormat(FileSystemOptions opts, String recentDateFormat)
264        {
265            setParam(opts, RECENT_DATE_FORMAT, recentDateFormat);
266        }
267    
268        /**
269         * see {@link org.apache.commons.net.ftp.FTPClientConfig} for details and examples.
270         * @param opts The FileSystemOptions.
271         * @return The server timezone id.
272         */
273        public String getServerTimeZoneId(FileSystemOptions opts)
274        {
275            return getString(opts, SERVER_TIME_ZONE_ID);
276        }
277    
278        /**
279         * see {@link org.apache.commons.net.ftp.FTPClientConfig} for details and examples.
280         * @param opts The FileSystemOptions.
281         * @param serverTimeZoneId The server timezone id.
282         */
283        public void setServerTimeZoneId(FileSystemOptions opts, String serverTimeZoneId)
284        {
285            setParam(opts, SERVER_TIME_ZONE_ID, serverTimeZoneId);
286        }
287    
288        /**
289         * see {@link org.apache.commons.net.ftp.FTPClientConfig} for details and examples.
290         * @param opts The FileSystemOptions.
291         * @return An array of short month names.
292         */
293        public String[] getShortMonthNames(FileSystemOptions opts)
294        {
295            return (String[]) getParam(opts, SHORT_MONTH_NAMES);
296        }
297    
298        /**
299         * see {@link org.apache.commons.net.ftp.FTPClientConfig} for details and examples.
300         * @param opts The FileSystemOptions.
301         * @param shortMonthNames an array of short month name Strings.
302         */
303        public void setShortMonthNames(FileSystemOptions opts, String[] shortMonthNames)
304        {
305            String[] clone = null;
306            if (shortMonthNames != null)
307            {
308                clone = new String[shortMonthNames.length];
309                System.arraycopy(shortMonthNames, 0, clone, 0, shortMonthNames.length);
310            }
311    
312            setParam(opts, SHORT_MONTH_NAMES, clone);
313        }
314    
315        /**
316         * see {@link org.apache.commons.net.ftp.FTP#setControlEncoding} for details and examples.
317         * @param opts The FileSystemOptions.
318         * @param encoding the encoding to use
319         * @since 2.0
320         */
321        public void setControlEncoding(FileSystemOptions opts, String encoding)
322        {
323            setParam(opts, ENCODING, encoding);
324        }
325    
326        /**
327         * @param opts The FileSystemOptions.
328         * @return The encoding.
329         * @since 2.0
330         * */
331        public String getControlEncoding(FileSystemOptions opts)
332        {
333            return  (String) getParam(opts, ENCODING);
334        }
335    }