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    
018    package org.apache.commons.net.ftp;
019    
020    import java.text.DateFormatSymbols;
021    import java.util.Collection;
022    import java.util.Locale;
023    import java.util.Map;
024    import java.util.StringTokenizer;
025    import java.util.TreeMap;
026    
027    /**
028     * <p>
029     * This class implements an alternate means of configuring the
030     * {@link  org.apache.commons.net.ftp.FTPClient  FTPClient} object and
031     * also subordinate objects which it uses.  Any class implementing the
032     * {@link  org.apache.commons.net.ftp.Configurable  Configurable }
033     * interface can be configured by this object.
034     * </p><p>
035     * In particular this class was designed primarily to support configuration
036     * of FTP servers which express file timestamps in formats and languages
037     * other than those for the US locale, which although it is the most common
038     * is not universal.  Unfortunately, nothing in the FTP spec allows this to
039     * be determined in an automated way, so manual configuration such as this
040     * is necessary.
041     * </p><p>
042     * This functionality was designed to allow existing clients to work exactly
043     * as before without requiring use of this component.  This component should
044     * only need to be explicitly invoked by the user of this package for problem
045     * cases that previous implementations could not solve.
046     * </p>
047     * <h3>Examples of use of FTPClientConfig</h3>
048     * Use cases:
049     * You are trying to access a server that
050     * <ul>
051     * <li>lists files with timestamps that use month names in languages other
052     * than English</li>
053     * <li>lists files with timestamps that use date formats other
054     * than the American English "standard" <code>MM dd yyyy</code></li>
055     * <li>is in different timezone and you need accurate timestamps for
056     * dependency checking as in Ant</li>
057     * </ul>
058     * <p>
059     * Unpaged (whole list) access on a UNIX server that uses French month names
060     * but uses the "standard" <code>MMM d yyyy</code> date formatting
061     * <pre>
062     *    FTPClient f=FTPClient();
063     *    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
064     *    conf.setServerLanguageCode("fr");
065     *    f.configure(conf);
066     *    f.connect(server);
067     *    f.login(username, password);
068     *    FTPFile[] files = listFiles(directory);
069     * </pre>
070     * </p>
071     * <p>
072     * Paged access on a UNIX server that uses Danish month names
073     * and "European" date formatting in Denmark's time zone, when you
074     * are in some other time zone.
075     * <pre>
076     *    FTPClient f=FTPClient();
077     *    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
078     *    conf.setServerLanguageCode("da");
079     *    conf.setDefaultDateFormat("d MMM yyyy");
080     *    conf.setRecentDateFormat("d MMM HH:mm");
081     *    conf.setTimeZoneId("Europe/Copenhagen");
082     *    f.configure(conf);
083     *    f.connect(server);
084     *    f.login(username, password);
085     *    FTPListParseEngine engine =
086     *       f.initiateListParsing("com.whatever.YourOwnParser", directory);
087     *
088     *    while (engine.hasNext()) {
089     *       FTPFile[] files = engine.getNext(25);  // "page size" you want
090     *       //do whatever you want with these files, display them, etc.
091     *       //expensive FTPFile objects not created until needed.
092     *    }
093     * </pre>
094     * </p>
095     * <p>
096     * Unpaged (whole list) access on a VMS server that uses month names
097     * in a language not {@link #getSupportedLanguageCodes() supported} by the system.
098     * but uses the "standard" <code>MMM d yyyy</code> date formatting
099     * <pre>
100     *    FTPClient f=FTPClient();
101     *    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_VMS);
102     *    conf.setShortMonthNames(
103     *        "jan|feb|mar|apr|ma\u00ED|j\u00FAn|j\u00FAl|\u00e1g\u00FA|sep|okt|n\u00F3v|des");
104     *    f.configure(conf);
105     *    f.connect(server);
106     *    f.login(username, password);
107     *    FTPFile[] files = listFiles(directory);
108     * </pre>
109     * </p>
110     * <p>
111     * Unpaged (whole list) access on a Windows-NT server in a different time zone.
112     * (Note, since the NT Format uses numeric date formatting, language issues
113     * are irrelevant here).
114     * <pre>
115     *    FTPClient f=FTPClient();
116     *    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
117     *    conf.setTimeZoneId("America/Denver");
118     *    f.configure(conf);
119     *    f.connect(server);
120     *    f.login(username, password);
121     *    FTPFile[] files = listFiles(directory);
122     * </pre>
123     * </p>
124     * Unpaged (whole list) access on a Windows-NT server in a different time zone
125     * but which has been configured to use a unix-style listing format.
126     * <pre>
127     *    FTPClient f=FTPClient();
128     *    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
129     *    conf.setTimeZoneId("America/Denver");
130     *    f.configure(conf);
131     *    f.connect(server);
132     *    f.login(username, password);
133     *    FTPFile[] files = listFiles(directory);
134     * </pre>
135     * 
136     * @since 1.4
137     * @see org.apache.commons.net.ftp.Configurable
138     * @see org.apache.commons.net.ftp.FTPClient
139     * @see org.apache.commons.net.ftp.parser.FTPTimestampParserImpl#configure(FTPClientConfig)
140     * @see org.apache.commons.net.ftp.parser.ConfigurableFTPFileEntryParserImpl
141     */
142    public class FTPClientConfig
143    {
144    
145        /**
146         * Identifier by which a unix-based ftp server is known throughout
147         * the commons-net ftp system.
148         */
149        public static final String SYST_UNIX  = "UNIX";
150    
151        /**
152         * Identifier by which a vms-based ftp server is known throughout
153         * the commons-net ftp system.
154         */
155        public static final String SYST_VMS   = "VMS";
156    
157        /**
158         * Identifier by which a WindowsNT-based ftp server is known throughout
159         * the commons-net ftp system.
160         */
161        public static final String SYST_NT    = "WINDOWS";
162    
163        /**
164         * Identifier by which an OS/2-based ftp server is known throughout
165         * the commons-net ftp system.
166         */
167        public static final String SYST_OS2   = "OS/2";
168    
169        /**
170         * Identifier by which an OS/400-based ftp server is known throughout
171         * the commons-net ftp system.
172         */
173        public static final String SYST_OS400 = "OS/400";
174    
175        /**
176         * Identifier by which an AS/400-based ftp server is known throughout
177         * the commons-net ftp system.
178         */
179        public static final String SYST_AS400 = "AS/400";
180    
181        /**
182         * Identifier by which an MVS-based ftp server is known throughout
183         * the commons-net ftp system.
184         */
185        public static final String SYST_MVS = "MVS";
186    
187        /**
188         * Some servers return an "UNKNOWN Type: L8" message
189         * in response to the SYST command. We set these to be a Unix-type system.
190         * This may happen if the ftpd in question was compiled without system
191         * information.
192         *
193         * NET-230 - Updated to be UPPERCASE so that the check done in
194         * createFileEntryParser will succeed.
195         *
196         * @since 1.5
197         */
198        public static final String SYST_L8 = "TYPE: L8";
199    
200        /**
201         * Identifier by which an Netware-based ftp server is known throughout
202         * the commons-net ftp system.
203         *
204         * @since 1.5
205         */
206        public static final String SYST_NETWARE = "NETWARE";
207    
208        /**
209         * Identifier by which a Mac pre OS-X -based ftp server is known throughout
210         * the commons-net ftp system.
211         *
212         * @since 3.1
213         */
214        // Full string is "MACOS Peter's Server"; the substring below should be enough
215        public static final String SYST_MACOS_PETER  = "MACOS PETER"; // NET-436
216    
217        private final String serverSystemKey;
218        private String defaultDateFormatStr = null;
219        private String recentDateFormatStr = null;
220        private boolean lenientFutureDates = true; // NET-407
221        private String serverLanguageCode = null;
222        private String shortMonthNames = null;
223        private String serverTimeZoneId = null;
224    
225    
226        /**
227         * The main constructor for an FTPClientConfig object
228         * @param systemKey key representing system type of the  server being
229         * connected to. See {@link #getServerSystemKey() serverSystemKey}
230         */
231        public FTPClientConfig(String systemKey) {
232            this.serverSystemKey = systemKey;
233        }
234    
235        /**
236         * Convenience constructor mainly for use in testing.
237         * Constructs a UNIX configuration.
238         */
239        public FTPClientConfig() {
240            this(SYST_UNIX);
241        }
242    
243        /**
244         * Constructor which allows setting of all member fields
245         * @param systemKey key representing system type of the  server being
246         * connected to. See
247         *  {@link #getServerSystemKey() serverSystemKey}
248         * @param defaultDateFormatStr See
249         *  {@link  #setDefaultDateFormatStr(String)  defaultDateFormatStr}
250         * @param recentDateFormatStr See
251         *  {@link  #setRecentDateFormatStr(String)  recentDateFormatStr}
252         * @param serverLanguageCode See
253         *  {@link  #setServerLanguageCode(String)  serverLanguageCode}
254         * @param shortMonthNames See
255         *  {@link  #setShortMonthNames(String)  shortMonthNames}
256         * @param serverTimeZoneId See
257         *  {@link  #setServerTimeZoneId(String)  serverTimeZoneId}
258         */
259        public FTPClientConfig(String systemKey,
260                               String defaultDateFormatStr,
261                               String recentDateFormatStr,
262                               String serverLanguageCode,
263                               String shortMonthNames,
264                               String serverTimeZoneId)
265        {
266            this(systemKey);
267            this.defaultDateFormatStr = defaultDateFormatStr;
268            this.recentDateFormatStr = recentDateFormatStr;
269            this.serverLanguageCode = serverLanguageCode;
270            this.shortMonthNames = shortMonthNames;
271            this.serverTimeZoneId = serverTimeZoneId;
272        }
273    
274        private static final Map<String, Object> LANGUAGE_CODE_MAP = new TreeMap<String, Object>();
275        static {
276    
277            // if there are other commonly used month name encodings which
278            // correspond to particular locales, please add them here.
279    
280    
281    
282            // many locales code short names for months as all three letters
283            // these we handle simply.
284            LANGUAGE_CODE_MAP.put("en", Locale.ENGLISH);
285            LANGUAGE_CODE_MAP.put("de",Locale.GERMAN);
286            LANGUAGE_CODE_MAP.put("it",Locale.ITALIAN);
287            LANGUAGE_CODE_MAP.put("es", new Locale("es", "", "")); // spanish
288            LANGUAGE_CODE_MAP.put("pt", new Locale("pt", "", "")); // portuguese
289            LANGUAGE_CODE_MAP.put("da", new Locale("da", "", "")); // danish
290            LANGUAGE_CODE_MAP.put("sv", new Locale("sv", "", "")); // swedish
291            LANGUAGE_CODE_MAP.put("no", new Locale("no", "", "")); // norwegian
292            LANGUAGE_CODE_MAP.put("nl", new Locale("nl", "", "")); // dutch
293            LANGUAGE_CODE_MAP.put("ro", new Locale("ro", "", "")); // romanian
294            LANGUAGE_CODE_MAP.put("sq", new Locale("sq", "", "")); // albanian
295            LANGUAGE_CODE_MAP.put("sh", new Locale("sh", "", "")); // serbo-croatian
296            LANGUAGE_CODE_MAP.put("sk", new Locale("sk", "", "")); // slovak
297            LANGUAGE_CODE_MAP.put("sl", new Locale("sl", "", "")); // slovenian
298    
299    
300            // some don't
301            LANGUAGE_CODE_MAP.put("fr",
302                    "jan|f\u00e9v|mar|avr|mai|jun|jui|ao\u00fb|sep|oct|nov|d\u00e9c");  //french
303    
304        }
305    
306        /**
307         * Getter for the serverSystemKey property.  This property
308         * specifies the general type of server to which the client connects.
309         * Should be either one of the <code>FTPClientConfig.SYST_*</code> codes
310         * or else the fully qualified class name of a parser implementing both
311         * the <code>FTPFileEntryParser</code> and <code>Configurable</code>
312         * interfaces.
313         * @return Returns the serverSystemKey property.
314         */
315        public String getServerSystemKey() {
316            return serverSystemKey;
317        }
318    
319        /**
320         * getter for the {@link  #setDefaultDateFormatStr(String)  defaultDateFormatStr}
321         * property.
322         * @return Returns the defaultDateFormatStr property.
323         */
324        public String getDefaultDateFormatStr() {
325            return defaultDateFormatStr;
326        }
327    
328        /**
329         * getter for the {@link  #setRecentDateFormatStr(String)  recentDateFormatStr} property.
330         * @return Returns the recentDateFormatStr property.
331         */
332    
333        public String getRecentDateFormatStr() {
334            return recentDateFormatStr;
335        }
336    
337        /**
338         * getter for the {@link  #setServerTimeZoneId(String)  serverTimeZoneId} property.
339         * @return Returns the serverTimeZoneId property.
340         */
341        public String getServerTimeZoneId() {
342            return serverTimeZoneId;
343        }
344    
345        /**
346         * <p>
347         * getter for the {@link  #setShortMonthNames(String)  shortMonthNames}
348         * property.
349         * </p>
350         * @return Returns the shortMonthNames.
351         */
352        public String getShortMonthNames() {
353            return shortMonthNames;
354        }
355    
356        /**
357         * <p>
358         * getter for the {@link  #setServerLanguageCode(String)  serverLanguageCode} property.
359         * </p>
360         * @return Returns the serverLanguageCode property.
361         */
362        public String getServerLanguageCode() {
363            return serverLanguageCode;
364        }
365    
366        /**
367         * <p>
368         * getter for the {@link  #setLenientFutureDates(boolean)  lenientFutureDates} property.
369         * </p>
370         * @return Returns the lenientFutureDates.
371         * @since 1.5
372         */
373        public boolean isLenientFutureDates() {
374            return lenientFutureDates;
375        }
376        /**
377         * <p>
378         * setter for the defaultDateFormatStr property.  This property
379         * specifies the main date format that will be used by a parser configured
380         * by this configuration to parse file timestamps.  If this is not
381         * specified, such a parser will use as a default value, the most commonly
382         * used format which will be in as used in <code>en_US</code> locales.
383         * </p><p>
384         * This should be in the format described for
385         * <code>java.text.SimpleDateFormat</code>.
386         * property.
387         * </p>
388         * @param defaultDateFormatStr The defaultDateFormatStr to set.
389         */
390        public void setDefaultDateFormatStr(String defaultDateFormatStr) {
391            this.defaultDateFormatStr = defaultDateFormatStr;
392        }
393    
394        /**
395         * <p>
396         * setter for the recentDateFormatStr property.  This property
397         * specifies a secondary date format that will be used by a parser
398         * configured by this configuration to parse file timestamps, typically
399         * those less than a year old.  If this is  not specified, such a parser
400         * will not attempt to parse using an alternate format.
401         * </p>
402         * <p>
403         * This is used primarily in unix-based systems.
404         * </p>
405         * <p>
406         * This should be in the format described for
407         * <code>java.text.SimpleDateFormat</code>.
408         * </p>
409         * @param recentDateFormatStr The recentDateFormatStr to set.
410         */
411        public void setRecentDateFormatStr(String recentDateFormatStr) {
412            this.recentDateFormatStr = recentDateFormatStr;
413        }
414    
415        /**
416         * <p>
417         * setter for the lenientFutureDates property.  This boolean property
418         * (default: false) only has meaning when a
419         * {@link  #setRecentDateFormatStr(String)  recentDateFormatStr} property
420         * has been set.  In that case, if this property is set true, then the
421         * parser, when it encounters a listing parseable with the recent date
422         * format, will only consider a date to belong to the previous year if
423         * it is more than one day in the future.  This will allow all
424         * out-of-synch situations (whether based on "slop" - i.e. servers simply
425         * out of synch with one another or because of time zone differences -
426         * but in the latter case it is highly recommended to use the
427         * {@link  #setServerTimeZoneId(String)  serverTimeZoneId} property
428         * instead) to resolve correctly.
429         * </p><p>
430         * This is used primarily in unix-based systems.
431         * </p>
432         * @param lenientFutureDates set true to compensate for out-of-synch
433         * conditions.
434         */
435        public void setLenientFutureDates(boolean lenientFutureDates) {
436            this.lenientFutureDates = lenientFutureDates;
437        }
438        /**
439         * <p>
440         * setter for the serverTimeZoneId property.  This property
441         * allows a time zone to be specified corresponding to that known to be
442         * used by an FTP server in file listings.  This might be particularly
443         * useful to clients such as Ant that try to use these timestamps for
444         * dependency checking.
445         * </p><p>
446         * This should be one of the identifiers used by
447         * <code>java.util.TimeZone</code> to refer to time zones, for example,
448         * <code>America/Chicago</code> or <code>Asia/Rangoon</code>.
449         * </p>
450         * @param serverTimeZoneId The serverTimeZoneId to set.
451         */
452        public void setServerTimeZoneId(String serverTimeZoneId) {
453            this.serverTimeZoneId = serverTimeZoneId;
454        }
455    
456        /**
457         * <p>
458         * setter for the shortMonthNames property.
459         * This property allows the user to specify a set of month names
460         * used by the server that is different from those that may be
461         * specified using the {@link  #setServerLanguageCode(String)  serverLanguageCode}
462         * property.
463         * </p><p>
464         * This should be a string containing twelve strings each composed of
465         * three characters, delimited by pipe (|) characters.  Currently,
466         * only 8-bit ASCII characters are known to be supported.  For example,
467         * a set of month names used by a hypothetical Icelandic FTP server might
468         * conceivably be specified as
469         * <code>"jan|feb|mar|apr|ma&#xED;|j&#xFA;n|j&#xFA;l|&#xE1;g&#xFA;|sep|okt|n&#xF3;v|des"</code>.
470         * </p>
471         * @param shortMonthNames The value to set to the shortMonthNames property.
472         */
473        public void setShortMonthNames(String shortMonthNames) {
474            this.shortMonthNames = shortMonthNames;
475        }
476    
477        /**
478         * <p>
479         * setter for the serverLanguageCode property.  This property allows
480         * user to specify a
481         * <a href="http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt">
482         * two-letter ISO-639 language code</a> that will be used to
483         * configure the set of month names used by the file timestamp parser.
484         * If neither this nor the {@link #setShortMonthNames(String) shortMonthNames}
485         * is specified, parsing will assume English month names, which may or
486         * may not be significant, depending on whether the date format(s)
487         * specified via {@link  #setDefaultDateFormatStr(String)  defaultDateFormatStr}
488         * and/or {@link  #setRecentDateFormatStr(String)  recentDateFormatStr} are using
489         * numeric or alphabetic month names.
490         * </p>
491         * <p>If the code supplied is not supported here, <code>en_US</code>
492         * month names will be used.  We are supporting here those language
493         * codes which, when a <code> java.util.Locale</code> is constucted
494         * using it, and a <code>java.text.SimpleDateFormat</code> is
495         * constructed using that Locale, the array returned by the
496         * SimpleDateFormat's <code>getShortMonths()</code> method consists
497         * solely of three 8-bit ASCII character strings.  Additionally,
498         * languages which do not meet this requirement are included if a
499         * common alternative set of short month names is known to be used.
500         * This means that users who can tell us of additional such encodings
501         * may get them added to the list of supported languages by contacting
502         * the Apache Commons Net team.
503         * </p>
504         * <p><strong>
505         * Please note that this attribute will NOT be used to determine a
506         * locale-based date format for the language.  </strong>
507         * Experience has shown that many if not most FTP servers outside the
508         * United States employ the standard <code>en_US</code> date format
509         * orderings of <code>MMM d yyyy</code> and <code>MMM d HH:mm</code>
510         * and attempting to deduce this automatically here would cause more
511         * problems than it would solve.  The date format must be changed
512         * via the {@link  #setDefaultDateFormatStr(String)  defaultDateFormatStr} and/or
513         * {@link  #setRecentDateFormatStr(String)  recentDateFormatStr} parameters.
514         * </p>
515         * @param serverLanguageCode The value to set to the serverLanguageCode property.
516         */
517        public void setServerLanguageCode(String serverLanguageCode) {
518            this.serverLanguageCode = serverLanguageCode;
519        }
520    
521        /**
522         * Looks up the supplied language code in the internally maintained table of
523         * language codes.  Returns a DateFormatSymbols object configured with
524         * short month names corresponding to the code.  If there is no corresponding
525         * entry in the table, the object returned will be that for
526         * <code>Locale.US</code>
527         * @param languageCode See {@link  #setServerLanguageCode(String)  serverLanguageCode}
528         * @return a DateFormatSymbols object configured with short month names
529         * corresponding to the supplied code, or with month names for
530         * <code>Locale.US</code> if there is no corresponding entry in the internal
531         * table.
532         */
533        public static DateFormatSymbols lookupDateFormatSymbols(String languageCode)
534        {
535            Object lang = LANGUAGE_CODE_MAP.get(languageCode);
536            if (lang != null) {
537                if (lang instanceof Locale) {
538                    return new DateFormatSymbols((Locale) lang);
539                } else if (lang instanceof String){
540                    return getDateFormatSymbols((String) lang);
541                }
542            }
543            return new DateFormatSymbols(Locale.US);
544        }
545    
546        /**
547         * Returns a DateFormatSymbols object configured with short month names
548         * as in the supplied string
549         * @param shortmonths This  should be as described in
550         *  {@link  #setShortMonthNames(String)  shortMonthNames}
551         * @return a DateFormatSymbols object configured with short month names
552         * as in the supplied string
553         */
554        public static DateFormatSymbols getDateFormatSymbols(String shortmonths)
555        {
556            String[] months = splitShortMonthString(shortmonths);
557            DateFormatSymbols dfs = new DateFormatSymbols(Locale.US);
558            dfs.setShortMonths(months);
559            return dfs;
560        }
561    
562        private static String[] splitShortMonthString(String shortmonths) {
563            StringTokenizer st = new StringTokenizer(shortmonths, "|");
564            int monthcnt = st.countTokens();
565            if (12 != monthcnt) {
566                throw new IllegalArgumentException(
567                        "expecting a pipe-delimited string containing 12 tokens");
568            }
569            String[] months = new String[13];
570            int pos = 0;
571            while(st.hasMoreTokens()) {
572                months[pos++] = st.nextToken();
573            }
574            months[pos]="";
575            return months;
576        }
577    
578        /**
579         * Returns a Collection of all the language codes currently supported
580         * by this class. See {@link  #setServerLanguageCode(String)  serverLanguageCode}
581         * for a functional descrption of language codes within this system.
582         *
583         * @return a Collection of all the language codes currently supported
584         * by this class
585         */
586        public static Collection<String> getSupportedLanguageCodes() {
587            return LANGUAGE_CODE_MAP.keySet();
588        }
589    
590    
591    }