View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.net.ftp.parser;
19  
20  import java.util.regex.Pattern;
21  
22  import org.apache.commons.net.ftp.Configurable;
23  import org.apache.commons.net.ftp.FTPClientConfig;
24  import org.apache.commons.net.ftp.FTPFileEntryParser;
25  
26  
27  /**
28   * This is the default implementation of the
29   * FTPFileEntryParserFactory interface.  This is the
30   * implementation that will be used by
31   * org.apache.commons.net.ftp.FTPClient.listFiles()
32   * if no other implementation has been specified.
33   *
34   * @see org.apache.commons.net.ftp.FTPClient#listFiles
35   * @see org.apache.commons.net.ftp.FTPClient#setParserFactory
36   */
37  public class DefaultFTPFileEntryParserFactory
38      implements FTPFileEntryParserFactory
39  {
40  
41      // Match a plain Java Identifier
42      private static final String JAVA_IDENTIFIER = "\\p{javaJavaIdentifierStart}(\\p{javaJavaIdentifierPart})*";
43      // Match a qualified name, e.g. a.b.c.Name - but don't allow the default package as that would allow "VMS"/"UNIX" etc.
44      private static final String JAVA_QUALIFIED_NAME  = "("+JAVA_IDENTIFIER+"\\.)+"+JAVA_IDENTIFIER;
45      // Create the pattern, as it will be reused many times
46      private static final Pattern JAVA_QUALIFIED_NAME_PATTERN = Pattern.compile(JAVA_QUALIFIED_NAME);
47  
48      /**
49       * This default implementation of the FTPFileEntryParserFactory
50       * interface works according to the following logic:
51       * First it attempts to interpret the supplied key as a fully
52       * qualified classname (default package is not allowed) of a class implementing the
53       * FTPFileEntryParser interface.  If that succeeds, a parser
54       * object of this class is instantiated and is returned;
55       * otherwise it attempts to interpret the key as an identirier
56       * commonly used by the FTP SYST command to identify systems.
57       * <p>
58       * If <code>key</code> is not recognized as a fully qualified
59       * classname known to the system, this method will then attempt
60       * to see whether it <b>contains</b> a string identifying one of
61       * the known parsers.  This comparison is <b>case-insensitive</b>.
62       * The intent here is where possible, to select as keys strings
63       * which are returned by the SYST command on the systems which
64       * the corresponding parser successfully parses.  This enables
65       * this factory to be used in the auto-detection system.
66       *
67       * @param key    should be a fully qualified classname corresponding to
68       *               a class implementing the FTPFileEntryParser interface<br>
69       *               OR<br>
70       *               a string containing (case-insensitively) one of the
71       *               following keywords:
72       *               <ul>
73       *               <li>{@link FTPClientConfig#SYST_UNIX UNIX}</li>
74       *               <li>{@link FTPClientConfig#SYST_NT WINDOWS}</li>
75       *               <li>{@link FTPClientConfig#SYST_OS2 OS/2}</li>
76       *               <li>{@link FTPClientConfig#SYST_OS400 OS/400}</li>
77       *               <li>{@link FTPClientConfig#SYST_AS400 AS/400}</li>
78       *               <li>{@link FTPClientConfig#SYST_VMS VMS}</li>
79       *               <li>{@link FTPClientConfig#SYST_MVS MVS}</li>
80       *               <li>{@link FTPClientConfig#SYST_NETWARE NETWARE}</li>
81       *               <li>{@link FTPClientConfig#SYST_L8 TYPE:L8}</li>
82       *               </ul>
83       * @return the FTPFileEntryParser corresponding to the supplied key.
84       * @throws ParserInitializationException thrown if for any reason the factory cannot resolve
85       *                   the supplied key into an FTPFileEntryParser.
86       * @see FTPFileEntryParser
87       */
88      @Override
89      public FTPFileEntryParser createFileEntryParser(String key)
90      {
91          if (key == null) {
92              throw new ParserInitializationException("Parser key cannot be null");
93          }
94          return createFileEntryParser(key, null);
95      }
96  
97      // Common method to process both key and config parameters.
98      private FTPFileEntryParser createFileEntryParser(String key, FTPClientConfig config) {
99          FTPFileEntryParser parser = null;
100 
101         // Is the key a possible class name?
102         if (JAVA_QUALIFIED_NAME_PATTERN.matcher(key).matches()) {
103             try
104             {
105                 Class<?> parserClass = Class.forName(key);
106                 try {
107                     parser = (FTPFileEntryParser) parserClass.newInstance();
108                 } catch (ClassCastException e) {
109                     throw new ParserInitializationException(parserClass.getName()
110                         + " does not implement the interface "
111                         + "org.apache.commons.net.ftp.FTPFileEntryParser.", e);
112                 } catch (Exception e) {
113                     throw new ParserInitializationException("Error initializing parser", e);
114                 } catch (ExceptionInInitializerError e) {
115                     throw new ParserInitializationException("Error initializing parser", e);
116                 }
117             } catch (ClassNotFoundException e) {
118                 // OK, assume it is an alias
119             }
120         }
121 
122         if (parser == null) { // Now try for aliases
123             String ukey = key.toUpperCase(java.util.Locale.ENGLISH);
124             if (ukey.indexOf(FTPClientConfig.SYST_UNIX_TRIM_LEADING) >= 0)
125             {
126                 parser = new UnixFTPEntryParser(config, true);
127             }
128             // must check this after SYST_UNIX_TRIM_LEADING as it is a substring of it
129             else if (ukey.indexOf(FTPClientConfig.SYST_UNIX) >= 0)
130             {
131                 parser = new UnixFTPEntryParser(config, false);
132             }
133             else if (ukey.indexOf(FTPClientConfig.SYST_VMS) >= 0)
134             {
135                 parser = new VMSVersioningFTPEntryParser(config);
136             }
137             else if (ukey.indexOf(FTPClientConfig.SYST_NT) >= 0)
138             {
139                 parser = createNTFTPEntryParser(config);
140             }
141             else if (ukey.indexOf(FTPClientConfig.SYST_OS2) >= 0)
142             {
143                 parser = new OS2FTPEntryParser(config);
144             }
145             else if (ukey.indexOf(FTPClientConfig.SYST_OS400) >= 0 ||
146                     ukey.indexOf(FTPClientConfig.SYST_AS400) >= 0)
147             {
148                 parser = createOS400FTPEntryParser(config);
149             }
150             else if (ukey.indexOf(FTPClientConfig.SYST_MVS) >= 0)
151             {
152                 parser = new MVSFTPEntryParser(); // Does not currently support config parameter
153             }
154             else if (ukey.indexOf(FTPClientConfig.SYST_NETWARE) >= 0)
155             {
156                 parser = new NetwareFTPEntryParser(config);
157             }
158             else if (ukey.indexOf(FTPClientConfig.SYST_MACOS_PETER) >= 0)
159             {
160                 parser = new MacOsPeterFTPEntryParser(config);
161             }
162             else if (ukey.indexOf(FTPClientConfig.SYST_L8) >= 0)
163             {
164                 // L8 normally means Unix, but move it to the end for some L8 systems that aren't.
165                 // This check should be last!
166                 parser = new UnixFTPEntryParser(config);
167             }
168             else
169             {
170                 throw new ParserInitializationException("Unknown parser type: " + key);
171             }
172         }
173 
174         if (parser instanceof Configurable) {
175             ((Configurable)parser).configure(config);
176         }
177         return parser;
178     }
179 
180     /**
181      * <p>Implementation extracts a key from the supplied
182      * {@link  FTPClientConfig FTPClientConfig}
183      * parameter and creates an object implementing the
184      * interface FTPFileEntryParser and uses the supplied configuration
185      * to configure it.
186      * </p><p>
187      * Note that this method will generally not be called in scenarios
188      * that call for autodetection of parser type but rather, for situations
189      * where the user knows that the server uses a non-default configuration
190      * and knows what that configuration is.
191      * </p>
192      * @param config  A {@link  FTPClientConfig FTPClientConfig}
193      * used to configure the parser created
194      *
195      * @return the @link  FTPFileEntryParser FTPFileEntryParser} so created.
196      * @throws ParserInitializationException
197      *                   Thrown on any exception in instantiation
198      * @throws NullPointerException if {@code config} is {@code null}
199      * @since 1.4
200      */
201     @Override
202     public FTPFileEntryParser createFileEntryParser(FTPClientConfig config)
203     throws ParserInitializationException
204     {
205         String key = config.getServerSystemKey();
206         return createFileEntryParser(key, config);
207     }
208 
209 
210     public FTPFileEntryParser createUnixFTPEntryParser()
211     {
212         return new UnixFTPEntryParser();
213     }
214 
215     public FTPFileEntryParser createVMSVersioningFTPEntryParser()
216     {
217         return new VMSVersioningFTPEntryParser();
218     }
219 
220     public FTPFileEntryParser createNetwareFTPEntryParser() {
221         return new NetwareFTPEntryParser();
222     }
223 
224     public FTPFileEntryParser createNTFTPEntryParser()
225     {
226         return createNTFTPEntryParser(null);
227     }
228 
229     /**
230      * Creates an NT FTP parser: if the config exists, and the system key equals
231      * {@link FTPClientConfig.SYST_NT} then a plain {@link NTFTPEntryParser} is used,
232      * otherwise a composite of {@link NTFTPEntryParser} and {@link UnixFTPEntryParser} is used.
233      * @param config the config to use, may be {@code null}
234      * @return the parser
235      */
236     private FTPFileEntryParser createNTFTPEntryParser(FTPClientConfig config)
237     {
238         if (config != null && FTPClientConfig.SYST_NT.equals(
239                 config.getServerSystemKey()))
240         {
241             return new NTFTPEntryParser(config);
242         } else {
243             // clone the config as it may be changed by the parsers (NET-602)
244             final FTPClientConfig config2 =  (config != null) ? new FTPClientConfig(config) : null;
245             return new CompositeFileEntryParser(new FTPFileEntryParser[]
246                    {
247                        new NTFTPEntryParser(config),
248                        new UnixFTPEntryParser(config2,
249                                config2 != null && FTPClientConfig.SYST_UNIX_TRIM_LEADING.equals(config2.getServerSystemKey()))
250                    });
251         }
252     }
253 
254      public FTPFileEntryParser createOS2FTPEntryParser()
255     {
256         return new OS2FTPEntryParser();
257     }
258 
259     public FTPFileEntryParser createOS400FTPEntryParser()
260     {
261         return createOS400FTPEntryParser(null);
262     }
263 
264     /**
265      * Creates an OS400 FTP parser: if the config exists, and the system key equals
266      * {@link FTPClientConfig.SYST_OS400} then a plain {@link OS400FTPEntryParser} is used,
267      * otherwise a composite of {@link OS400FTPEntryParser} and {@link UnixFTPEntryParser} is used.
268      * @param config the config to use, may be {@code null}
269      * @return the parser
270      */
271     private FTPFileEntryParser createOS400FTPEntryParser(FTPClientConfig config)
272         {
273         if (config != null &&
274                 FTPClientConfig.SYST_OS400.equals(config.getServerSystemKey()))
275         {
276             return new OS400FTPEntryParser(config);
277         } else {
278             // clone the config as it may be changed by the parsers (NET-602)
279             final FTPClientConfig config2 =  (config != null) ? new FTPClientConfig(config) : null;
280             return new CompositeFileEntryParser(new FTPFileEntryParser[]
281                 {
282                     new OS400FTPEntryParser(config),
283                     new UnixFTPEntryParser(config2,
284                             config2 != null && FTPClientConfig.SYST_UNIX_TRIM_LEADING.equals(config2.getServerSystemKey()))
285                 });
286         }
287     }
288 
289     public FTPFileEntryParser createMVSEntryParser()
290     {
291         return new MVSFTPEntryParser();
292     }
293 
294 }
295