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
018package org.apache.commons.net.ftp;
019
020import java.text.DateFormatSymbols;
021import java.util.Collection;
022import java.util.Locale;
023import java.util.Map;
024import java.util.StringTokenizer;
025import 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 * </p>
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 */
142public 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    private final String serverSystemKey;
209    private String defaultDateFormatStr = null;
210    private String recentDateFormatStr = null;
211    private boolean lenientFutureDates = false;
212    private String serverLanguageCode = null;
213    private String shortMonthNames = null;
214    private String serverTimeZoneId = null;
215    
216    
217    /**
218     * The main constructor for an FTPClientConfig object
219     * @param systemKey key representing system type of the  server being 
220     * connected to. See {@link #getServerSystemKey() serverSystemKey}
221     */
222    public FTPClientConfig(String systemKey) {
223        this.serverSystemKey = systemKey;
224    }
225
226    /**
227     * Convenience constructor mainly for use in testing.
228     * Constructs a UNIX configuration. 
229     */
230    public FTPClientConfig() {
231        this(SYST_UNIX);
232    }
233
234    /**
235     * Constructor which allows setting of all member fields
236     * @param systemKey key representing system type of the  server being 
237     * connected to. See 
238     *  {@link #getServerSystemKey() serverSystemKey}
239     * @param defaultDateFormatStr See 
240     *  {@link  #setDefaultDateFormatStr(String)  defaultDateFormatStr}
241     * @param recentDateFormatStr See
242     *  {@link  #setRecentDateFormatStr(String)  recentDateFormatStr}
243     * @param serverLanguageCode See
244     *  {@link  #setServerLanguageCode(String)  serverLanguageCode}
245     * @param shortMonthNames See
246     *  {@link  #setShortMonthNames(String)  shortMonthNames}
247     * @param serverTimeZoneId See
248     *  {@link  #setServerTimeZoneId(String)  serverTimeZoneId}
249     */
250    public FTPClientConfig(String systemKey,
251                           String defaultDateFormatStr,
252                           String recentDateFormatStr,
253                           String serverLanguageCode,
254                           String shortMonthNames,
255                           String serverTimeZoneId)
256    {
257        this(systemKey);
258        this.defaultDateFormatStr = defaultDateFormatStr;
259        this.recentDateFormatStr = recentDateFormatStr;
260        this.serverLanguageCode = serverLanguageCode;
261        this.shortMonthNames = shortMonthNames;
262        this.serverTimeZoneId = serverTimeZoneId;
263    }
264    
265    private static Map<String, Object> LANGUAGE_CODE_MAP = new TreeMap<String, Object>();
266    static {
267        
268        // if there are other commonly used month name encodings which
269        // correspond to particular locales, please add them here.
270        
271        
272        
273        // many locales code short names for months as all three letters
274        // these we handle simply.
275        LANGUAGE_CODE_MAP.put("en", Locale.ENGLISH);
276        LANGUAGE_CODE_MAP.put("de",Locale.GERMAN);
277        LANGUAGE_CODE_MAP.put("it",Locale.ITALIAN);
278        LANGUAGE_CODE_MAP.put("es", new Locale("es", "", "")); // spanish
279        LANGUAGE_CODE_MAP.put("pt", new Locale("pt", "", "")); // portuguese
280        LANGUAGE_CODE_MAP.put("da", new Locale("da", "", "")); // danish
281        LANGUAGE_CODE_MAP.put("sv", new Locale("sv", "", "")); // swedish
282        LANGUAGE_CODE_MAP.put("no", new Locale("no", "", "")); // norwegian
283        LANGUAGE_CODE_MAP.put("nl", new Locale("nl", "", "")); // dutch
284        LANGUAGE_CODE_MAP.put("ro", new Locale("ro", "", "")); // romanian
285        LANGUAGE_CODE_MAP.put("sq", new Locale("sq", "", "")); // albanian
286        LANGUAGE_CODE_MAP.put("sh", new Locale("sh", "", "")); // serbo-croatian
287        LANGUAGE_CODE_MAP.put("sk", new Locale("sk", "", "")); // slovak
288        LANGUAGE_CODE_MAP.put("sl", new Locale("sl", "", "")); // slovenian
289
290
291        // some don't
292        LANGUAGE_CODE_MAP.put("fr",
293                "jan|f\u00e9v|mar|avr|mai|jun|jui|ao\u00fb|sep|oct|nov|d\u00e9c");  //french
294            
295    }
296    
297    /**
298     * Getter for the serverSystemKey property.  This property
299     * specifies the general type of server to which the client connects.
300     * Should be either one of the <code>FTPClientConfig.SYST_*</code> codes
301     * or else the fully qualified class name of a parser implementing both
302     * the <code>FTPFileEntryParser</code> and <code>Configurable</code>
303     * interfaces.
304     * @return Returns the serverSystemKey property.
305     */
306    public String getServerSystemKey() {
307        return serverSystemKey;
308    }
309    
310    /**
311     * getter for the {@link  #setDefaultDateFormatStr(String)  defaultDateFormatStr} 
312     * property.  
313     * @return Returns the defaultDateFormatStr property.
314     */
315    public String getDefaultDateFormatStr() {
316        return defaultDateFormatStr;
317    }
318    
319    /**
320     * getter for the {@link  #setRecentDateFormatStr(String)  recentDateFormatStr} property.
321     * @return Returns the recentDateFormatStr property.
322     */
323
324    public String getRecentDateFormatStr() {
325        return recentDateFormatStr;
326    }
327    
328    /**
329     * getter for the {@link  #setServerTimeZoneId(String)  serverTimeZoneId} property.
330     * @return Returns the serverTimeZoneId property.
331     */
332    public String getServerTimeZoneId() {
333        return serverTimeZoneId;
334    }
335    
336    /**
337     * <p>
338     * getter for the {@link  #setShortMonthNames(String)  shortMonthNames} 
339     * property.  
340     * </p>
341     * @return Returns the shortMonthNames.
342     */
343    public String getShortMonthNames() {
344        return shortMonthNames;
345    }
346    
347    /**
348     * <p>
349     * getter for the {@link  #setServerLanguageCode(String)  serverLanguageCode} property.
350     * </p>  
351     * @return Returns the serverLanguageCode property.
352     */
353    public String getServerLanguageCode() {
354        return serverLanguageCode;
355    }
356    
357    /**
358     * <p>
359     * getter for the {@link  #setLenientFutureDates(boolean)  lenientFutureDates} property.
360     * </p>  
361     * @return Returns the lenientFutureDates.
362     * @since 1.5
363     */
364    public boolean isLenientFutureDates() {
365        return lenientFutureDates;
366    }
367    /**
368     * <p>
369     * setter for the defaultDateFormatStr property.  This property
370     * specifies the main date format that will be used by a parser configured
371     * by this configuration to parse file timestamps.  If this is not
372     * specified, such a parser will use as a default value, the most commonly
373     * used format which will be in as used in <code>en_US</code> locales.
374     * </p><p>
375     * This should be in the format described for 
376     * <code>java.text.SimpleDateFormat</code>. 
377     * property.
378     * </p>
379     * @param defaultDateFormatStr The defaultDateFormatStr to set.
380     */
381    public void setDefaultDateFormatStr(String defaultDateFormatStr) {
382        this.defaultDateFormatStr = defaultDateFormatStr;
383    }
384    
385    /**
386     * <p>
387     * setter for the recentDateFormatStr property.  This property
388     * specifies a secondary date format that will be used by a parser 
389     * configured by this configuration to parse file timestamps, typically 
390     * those less than a year old.  If this is  not specified, such a parser 
391     * will not attempt to parse using an alternate format.
392     * </p>
393     * This is used primarily in unix-based systems.
394     * </p>
395     * This should be in the format described for 
396     * <code>java.text.SimpleDateFormat</code>.
397     * </p>
398     * @param recentDateFormatStr The recentDateFormatStr to set.
399     */
400    public void setRecentDateFormatStr(String recentDateFormatStr) {
401        this.recentDateFormatStr = recentDateFormatStr;
402    }
403    
404    /**
405     * <p>
406     * setter for the lenientFutureDates property.  This boolean property
407     * (default: false) only has meaning when a 
408     * {@link  #setRecentDateFormatStr(String)  recentDateFormatStr} property
409     * has been set.  In that case, if this property is set true, then the
410     * parser, when it encounters a listing parseable with the recent date 
411     * format, will only consider a date to belong to the previous year if
412     * it is more than one day in the future.  This will allow all 
413     * out-of-synch situations (whether based on "slop" - i.e. servers simply 
414     * out of synch with one another or because of time zone differences - 
415     * but in the latter case it is highly recommended to use the 
416     * {@link  #setServerTimeZoneId(String)  serverTimeZoneId} property
417     * instead) to resolve correctly.
418     * </p><p>
419     * This is used primarily in unix-based systems.
420     * </p>
421     * @param lenientFutureDates set true to compensate for out-of-synch 
422     * conditions.
423     */
424    public void setLenientFutureDates(boolean lenientFutureDates) {
425        this.lenientFutureDates = lenientFutureDates;
426    }
427    /**
428     * <p>
429     * setter for the serverTimeZoneId property.  This property
430     * allows a time zone to be specified corresponding to that known to be 
431     * used by an FTP server in file listings.  This might be particularly 
432     * useful to clients such as Ant that try to use these timestamps for 
433     * dependency checking.
434     * </p><p>
435     * This should be one of the identifiers used by 
436     * <code>java.util.TimeZone</code> to refer to time zones, for example, 
437     * <code>America/Chicago</code> or <code>Asia/Rangoon</code>.
438     * </p>
439     * @param serverTimeZoneId The serverTimeZoneId to set.
440     */
441    public void setServerTimeZoneId(String serverTimeZoneId) {
442        this.serverTimeZoneId = serverTimeZoneId;
443    }
444    
445    /**
446     * <p>
447     * setter for the shortMonthNames property.  
448     * This property allows the user to specify a set of month names
449     * used by the server that is different from those that may be 
450     * specified using the {@link  #setServerLanguageCode(String)  serverLanguageCode}
451     * property.
452     * </p><p>
453     * This should be a string containing twelve strings each composed of
454     * three characters, delimited by pipe (|) characters.  Currently, 
455     * only 8-bit ASCII characters are known to be supported.  For example,
456     * a set of month names used by a hypothetical Icelandic FTP server might 
457     * conceivably be specified as 
458     * <code>"jan|feb|mar|apr|ma&#xED;|j&#xFA;n|j&#xFA;l|&#xE1;g&#xFA;|sep|okt|n&#xF3;v|des"</code>.  
459     * </p>
460     * @param shortMonthNames The value to set to the shortMonthNames property.
461     */
462    public void setShortMonthNames(String shortMonthNames) {
463        this.shortMonthNames = shortMonthNames;
464    }
465    
466    /**
467     * <p>
468     * setter for the serverLanguageCode property.  This property allows
469     * user to specify a 
470     * <a href="http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt">
471     * two-letter ISO-639 language code</a> that will be used to 
472     * configure the set of month names used by the file timestamp parser.
473     * If neither this nor the {@link #setShortMonthNames(String) shortMonthNames} 
474     * is specified, parsing will assume English month names, which may or 
475     * may not be significant, depending on whether the date format(s) 
476     * specified via {@link  #setDefaultDateFormatStr(String)  defaultDateFormatStr} 
477     * and/or {@link  #setRecentDateFormatStr(String)  recentDateFormatStr} are using 
478     * numeric or alphabetic month names.
479     * </p>
480     * <p>If the code supplied is not supported here, <code>en_US</code>
481     * month names will be used.  We are supporting here those language 
482     * codes which, when a <code> java.util.Locale</code> is constucted
483     * using it, and a <code>java.text.SimpleDateFormat</code> is 
484     * constructed using that Locale, the array returned by the 
485     * SimpleDateFormat's <code>getShortMonths()</code> method consists
486     * solely of three 8-bit ASCII character strings.  Additionally, 
487     * languages which do not meet this requirement are included if a 
488     * common alternative set of short month names is known to be used.
489     * This means that users who can tell us of additional such encodings
490     * may get them added to the list of supported languages by contacting
491     * the jakarta-commons-net team.
492     * </p>
493     * <p><strong>
494     * Please note that this attribute will NOT be used to determine a 
495     * locale-based date format for the language.  </strong>  
496     * Experience has shown that many if not most FTP servers outside the
497     * United States employ the standard <code>en_US</code> date format 
498     * orderings of <code>MMM d yyyy</code> and <code>MMM d HH:mm</code> 
499     * and attempting to deduce this automatically here would cause more
500     * problems than it would solve.  The date format must be changed 
501     * via the {@link  #setDefaultDateFormatStr(String)  defaultDateFormatStr} and/or 
502     * {@link  #setRecentDateFormatStr(String)  recentDateFormatStr} parameters.
503     * </p>
504     * @param serverLanguageCode The value to set to the serverLanguageCode property.  
505     */
506    public void setServerLanguageCode(String serverLanguageCode) {
507        this.serverLanguageCode = serverLanguageCode;
508    }
509    
510    /**
511     * Looks up the supplied language code in the internally maintained table of 
512     * language codes.  Returns a DateFormatSymbols object configured with 
513     * short month names corresponding to the code.  If there is no corresponding
514     * entry in the table, the object returned will be that for 
515     * <code>Locale.US</code> 
516     * @param languageCode See {@link  #setServerLanguageCode(String)  serverLanguageCode}
517     * @return a DateFormatSymbols object configured with short month names 
518     * corresponding to the supplied code, or with month names for  
519     * <code>Locale.US</code> if there is no corresponding entry in the internal
520     * table.
521     */
522    public static DateFormatSymbols lookupDateFormatSymbols(String languageCode) 
523    {
524        Object lang = LANGUAGE_CODE_MAP.get(languageCode);
525        if (lang != null) {
526            if (lang instanceof Locale) {
527                return new DateFormatSymbols((Locale) lang);
528            } else if (lang instanceof String){
529                return getDateFormatSymbols((String) lang);
530            }
531        }
532        return new DateFormatSymbols(Locale.US);
533    }
534    
535    /**
536     * Returns a DateFormatSymbols object configured with short month names
537     * as in the supplied string
538     * @param shortmonths This  should be as described in 
539     *  {@link  #setShortMonthNames(String)  shortMonthNames}
540     * @return a DateFormatSymbols object configured with short month names
541     * as in the supplied string
542     */
543    public static DateFormatSymbols getDateFormatSymbols(String shortmonths) 
544    {
545        String[] months = splitShortMonthString(shortmonths);
546        DateFormatSymbols dfs = new DateFormatSymbols(Locale.US);
547        dfs.setShortMonths(months);
548        return dfs;
549    }
550    
551    private static String[] splitShortMonthString(String shortmonths) {
552        StringTokenizer st = new StringTokenizer(shortmonths, "|");
553        int monthcnt = st.countTokens();
554        if (12 != monthcnt) {
555            throw new IllegalArgumentException(
556                    "expecting a pipe-delimited string containing 12 tokens");
557        }
558        String[] months = new String[13];
559        int pos = 0;
560        while(st.hasMoreTokens()) {
561            months[pos++] = st.nextToken();
562        }
563        months[pos]="";
564        return months;
565    }
566
567    /**
568     * Returns a Collection of all the language codes currently supported
569     * by this class. See {@link  #setServerLanguageCode(String)  serverLanguageCode}  
570     * for a functional descrption of language codes within this system. 
571     *
572     * @return a Collection of all the language codes currently supported
573     * by this class
574     */
575    public static Collection<String> getSupportedLanguageCodes() {
576        return LANGUAGE_CODE_MAP.keySet();
577    }
578    
579    
580}