DefaultFTPFileEntryParserFactory.java

  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. package org.apache.commons.net.ftp.parser;

  18. import java.util.regex.Pattern;

  19. import org.apache.commons.net.ftp.Configurable;
  20. import org.apache.commons.net.ftp.FTPClientConfig;
  21. import org.apache.commons.net.ftp.FTPFileEntryParser;

  22. /**
  23.  * This is the default implementation of the FTPFileEntryParserFactory interface. This is the implementation that will be used by
  24.  * org.apache.commons.net.ftp.FTPClient.listFiles() if no other implementation has been specified.
  25.  *
  26.  * @see org.apache.commons.net.ftp.FTPClient#listFiles
  27.  * @see org.apache.commons.net.ftp.FTPClient#setParserFactory
  28.  */
  29. public class DefaultFTPFileEntryParserFactory implements FTPFileEntryParserFactory {

  30.     // Match a plain Java Identifier
  31.     private static final String JAVA_IDENTIFIER = "\\p{javaJavaIdentifierStart}(\\p{javaJavaIdentifierPart})*";
  32.     // Match a qualified name, e.g. a.b.c.Name - but don't allow the default package as that would allow "VMS"/"UNIX" etc.
  33.     private static final String JAVA_QUALIFIED_NAME = "(" + JAVA_IDENTIFIER + "\\.)+" + JAVA_IDENTIFIER;
  34.     // Create the pattern, as it will be reused many times
  35.     private static final Pattern JAVA_QUALIFIED_NAME_PATTERN = Pattern.compile(JAVA_QUALIFIED_NAME);

  36.     /**
  37.      * <p>
  38.      * Implementation extracts a key from the supplied {@link FTPClientConfig FTPClientConfig} parameter and creates an object implementing the interface
  39.      * FTPFileEntryParser and uses the supplied configuration to configure it.
  40.      * </p>
  41.      * <p>
  42.      * Note that this method will generally not be called in scenarios that call for autodetection of parser type but rather, for situations where the user
  43.      * knows that the server uses a non-default configuration and knows what that configuration is.
  44.      * </p>
  45.      *
  46.      * @param config A {@link FTPClientConfig FTPClientConfig} used to configure the parser created
  47.      *
  48.      * @return the {@link FTPFileEntryParser} so created.
  49.      * @throws ParserInitializationException Thrown on any exception in instantiation
  50.      * @throws NullPointerException          if {@code config} is {@code null}
  51.      * @since 1.4
  52.      */
  53.     @Override
  54.     public FTPFileEntryParser createFileEntryParser(final FTPClientConfig config) throws ParserInitializationException {
  55.         final String key = config.getServerSystemKey();
  56.         return createFileEntryParser(key, config);
  57.     }

  58.     /**
  59.      * This default implementation of the FTPFileEntryParserFactory interface works according to the following logic: First it attempts to interpret the
  60.      * supplied key as a fully qualified class name (default package is not allowed) of a class implementing the FTPFileEntryParser interface. If that succeeds,
  61.      * a parser object of this class is instantiated and is returned; otherwise it attempts to interpret the key as an identifier commonly used by the FTP SYST
  62.      * command to identify systems.
  63.      * <p>
  64.      * If <code>key</code> is not recognized as a fully qualified class name known to the system, this method will then attempt to see whether it
  65.      * <b>contains</b> a string identifying one of the known parsers. This comparison is <b>case-insensitive</b>. The intent here is where possible, to select
  66.      * as keys strings which are returned by the SYST command on the systems which the corresponding parser successfully parses. This enables this factory to be
  67.      * used in the auto-detection system.
  68.      * </p>
  69.      *
  70.      * @param key should be a fully qualified class name corresponding to a class implementing the FTPFileEntryParser interface<br>
  71.      *            OR<br>
  72.      *            a string containing (case-insensitively) one of the following keywords:
  73.      *            <ul>
  74.      *            <li>{@link FTPClientConfig#SYST_UNIX UNIX}</li>
  75.      *            <li>{@link FTPClientConfig#SYST_NT WINDOWS}</li>
  76.      *            <li>{@link FTPClientConfig#SYST_OS2 OS/2}</li>
  77.      *            <li>{@link FTPClientConfig#SYST_OS400 OS/400}</li>
  78.      *            <li>{@link FTPClientConfig#SYST_AS400 AS/400}</li>
  79.      *            <li>{@link FTPClientConfig#SYST_VMS VMS}</li>
  80.      *            <li>{@link FTPClientConfig#SYST_MVS MVS}</li>
  81.      *            <li>{@link FTPClientConfig#SYST_NETWARE NETWARE}</li>
  82.      *            <li>{@link FTPClientConfig#SYST_L8 TYPE:L8}</li>
  83.      *            </ul>
  84.      * @return the FTPFileEntryParser corresponding to the supplied key.
  85.      * @throws ParserInitializationException thrown if for any reason the factory cannot resolve the supplied key into an FTPFileEntryParser.
  86.      * @see FTPFileEntryParser
  87.      */
  88.     @Override
  89.     public FTPFileEntryParser createFileEntryParser(final String key) {
  90.         if (key == null) {
  91.             throw new ParserInitializationException("Parser key cannot be null");
  92.         }
  93.         return createFileEntryParser(key, null);
  94.     }

  95.     // Common method to process both key and config parameters.
  96.     private FTPFileEntryParser createFileEntryParser(final String key, final FTPClientConfig config) {
  97.         FTPFileEntryParser parser = null;

  98.         // Is the key a possible class name?
  99.         if (JAVA_QUALIFIED_NAME_PATTERN.matcher(key).matches()) {
  100.             try {
  101.                 final Class<?> parserClass = Class.forName(key);
  102.                 try {
  103.                     parser = (FTPFileEntryParser) parserClass.getConstructor().newInstance();
  104.                 } catch (final ClassCastException e) {
  105.                     throw new ParserInitializationException(
  106.                             parserClass.getName() + " does not implement the interface " + "org.apache.commons.net.ftp.FTPFileEntryParser.", e);
  107.                 } catch (final Exception | ExceptionInInitializerError e) {
  108.                     throw new ParserInitializationException("Error initializing parser", e);
  109.                 }
  110.             } catch (final ClassNotFoundException e) {
  111.                 // OK, assume it is an alias
  112.             }
  113.         }

  114.         if (parser == null) { // Now try for aliases
  115.             final String ukey = key.toUpperCase(java.util.Locale.ENGLISH);
  116.             if (ukey.contains(FTPClientConfig.SYST_UNIX_TRIM_LEADING)) {
  117.                 parser = new UnixFTPEntryParser(config, true);
  118.             }
  119.             // must check this after SYST_UNIX_TRIM_LEADING as it is a substring of it
  120.             else if (ukey.contains(FTPClientConfig.SYST_UNIX)) {
  121.                 parser = new UnixFTPEntryParser(config, false);
  122.             } else if (ukey.contains(FTPClientConfig.SYST_VMS)) {
  123.                 parser = new VMSVersioningFTPEntryParser(config);
  124.             } else if (ukey.contains(FTPClientConfig.SYST_NT)) {
  125.                 parser = createNTFTPEntryParser(config);
  126.             } else if (ukey.contains(FTPClientConfig.SYST_OS2)) {
  127.                 parser = new OS2FTPEntryParser(config);
  128.             } else if (ukey.contains(FTPClientConfig.SYST_OS400) || ukey.contains(FTPClientConfig.SYST_AS400)) {
  129.                 parser = createOS400FTPEntryParser(config);
  130.             } else if (ukey.contains(FTPClientConfig.SYST_MVS)) {
  131.                 parser = new MVSFTPEntryParser(); // Does not currently support config parameter
  132.             } else if (ukey.contains(FTPClientConfig.SYST_NETWARE)) {
  133.                 parser = new NetwareFTPEntryParser(config);
  134.             } else if (ukey.contains(FTPClientConfig.SYST_MACOS_PETER)) {
  135.                 parser = new MacOsPeterFTPEntryParser(config);
  136.             } else if (ukey.contains(FTPClientConfig.SYST_L8)) {
  137.                 // L8 normally means Unix, but move it to the end for some L8 systems that aren't.
  138.                 // This check should be last!
  139.                 parser = new UnixFTPEntryParser(config);
  140.             } else {
  141.                 throw new ParserInitializationException("Unknown parser type: " + key);
  142.             }
  143.         }

  144.         if (parser instanceof Configurable) {
  145.             ((Configurable) parser).configure(config);
  146.         }
  147.         return parser;
  148.     }

  149.     public FTPFileEntryParser createMVSEntryParser() {
  150.         return new MVSFTPEntryParser();
  151.     }

  152.     public FTPFileEntryParser createNetwareFTPEntryParser() {
  153.         return new NetwareFTPEntryParser();
  154.     }

  155.     public FTPFileEntryParser createNTFTPEntryParser() {
  156.         return createNTFTPEntryParser(null);
  157.     }

  158.     /**
  159.      * Creates an NT FTP parser: if the config exists, and the system key equals {@link FTPClientConfig#SYST_NT} then a plain {@link NTFTPEntryParser} is used,
  160.      * otherwise a composite of {@link NTFTPEntryParser} and {@link UnixFTPEntryParser} is used.
  161.      *
  162.      * @param config the config to use, may be {@code null}
  163.      * @return the parser
  164.      */
  165.     private FTPFileEntryParser createNTFTPEntryParser(final FTPClientConfig config) {
  166.         if (config != null && FTPClientConfig.SYST_NT.equals(config.getServerSystemKey())) {
  167.             return new NTFTPEntryParser(config);
  168.         }
  169.         // clone the config as it may be changed by the parsers (NET-602)
  170.         final FTPClientConfig config2 = config != null ? new FTPClientConfig(config) : null;
  171.         return new CompositeFileEntryParser(new FTPFileEntryParser[] { new NTFTPEntryParser(config),
  172.                 new UnixFTPEntryParser(config2, config2 != null && FTPClientConfig.SYST_UNIX_TRIM_LEADING.equals(config2.getServerSystemKey())) });
  173.     }

  174.     public FTPFileEntryParser createOS2FTPEntryParser() {
  175.         return new OS2FTPEntryParser();
  176.     }

  177.     public FTPFileEntryParser createOS400FTPEntryParser() {
  178.         return createOS400FTPEntryParser(null);
  179.     }

  180.     /**
  181.      * Creates an OS400 FTP parser: if the config exists, and the system key equals {@link FTPClientConfig#SYST_OS400} then a plain {@link OS400FTPEntryParser}
  182.      * is used, otherwise a composite of {@link OS400FTPEntryParser} and {@link UnixFTPEntryParser} is used.
  183.      *
  184.      * @param config the config to use, may be {@code null}
  185.      * @return the parser
  186.      */
  187.     private FTPFileEntryParser createOS400FTPEntryParser(final FTPClientConfig config) {
  188.         if (config != null && FTPClientConfig.SYST_OS400.equals(config.getServerSystemKey())) {
  189.             return new OS400FTPEntryParser(config);
  190.         }
  191.         // clone the config as it may be changed by the parsers (NET-602)
  192.         final FTPClientConfig config2 = config != null ? new FTPClientConfig(config) : null;
  193.         return new CompositeFileEntryParser(new FTPFileEntryParser[] { new OS400FTPEntryParser(config),
  194.                 new UnixFTPEntryParser(config2, config2 != null && FTPClientConfig.SYST_UNIX_TRIM_LEADING.equals(config2.getServerSystemKey())) });
  195.     }

  196.     public FTPFileEntryParser createUnixFTPEntryParser() {
  197.         return new UnixFTPEntryParser();
  198.     }

  199.     public FTPFileEntryParser createVMSVersioningFTPEntryParser() {
  200.         return new VMSVersioningFTPEntryParser();
  201.     }

  202. }