2  * Copyright 2017 BOCO Corporation.  CMCC Technologies Co., Ltd
 
   4  * Licensed under the Apache License, Version 2.0 (the "License");
 
   5  * you may not use this file except in compliance with the License.
 
   6  * You may obtain a copy of the License at
 
   8  *     http://www.apache.org/licenses/LICENSE-2.0
 
  10  * Unless required by applicable law or agreed to in writing, software
 
  11  * distributed under the License is distributed on an "AS IS" BASIS,
 
  12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  13  * See the License for the specific language governing permissions and
 
  14  * limitations under the License.
 
  16 package org.onap.vfc.nfvo.emsdriver.commons.ftp;
 
  18 import org.apache.commons.net.ftp.Configurable;
 
  19 import org.apache.commons.net.ftp.FTPClientConfig;
 
  20 import org.apache.commons.net.ftp.FTPFileEntryParser;
 
  21 import org.apache.commons.net.ftp.parser.CompositeFileEntryParser;
 
  22 import org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory;
 
  23 import org.apache.commons.net.ftp.parser.MVSFTPEntryParser;
 
  24 import org.apache.commons.net.ftp.parser.NTFTPEntryParser;
 
  25 import org.apache.commons.net.ftp.parser.OS2FTPEntryParser;
 
  26 import org.apache.commons.net.ftp.parser.OS400FTPEntryParser;
 
  27 import org.apache.commons.net.ftp.parser.ParserInitializationException;
 
  28 import org.apache.commons.net.ftp.parser.UnixFTPEntryParser;
 
  29 import org.apache.commons.net.ftp.parser.VMSVersioningFTPEntryParser;
 
  31          * This is the default implementation of the
 
  32          * FTPFileEntryParserFactory interface.  This is the
 
  33          * implementation that will be used by
 
  34          * org.apache.commons.net.ftp.FTPClient.listFiles()
 
  35          * if no other implementation has been specified.
 
  37          * @see org.apache.commons.net.ftp.FTPClient#listFiles
 
  38          * @see org.apache.commons.net.ftp.FTPClient#setParserFactory
 
  40 public class ExtendsDefaultFTPFileEntryParserFactory extends
 
  41                 DefaultFTPFileEntryParserFactory {
 
  43                 private FTPClientConfig config = null;
 
  46              * This default implementation of the FTPFileEntryParserFactory
 
  47              * interface works according to the following logic:
 
  48              * First it attempts to interpret the supplied key as a fully
 
  49              * qualified classname of a class implementing the
 
  50              * FTPFileEntryParser interface.  If that succeeds, a parser
 
  51              * object of this class is instantiated and is returned; 
 
  52              * otherwise it attempts to interpret the key as an identirier
 
  53              * commonly used by the FTP SYST command to identify systems.
 
  55              * If <code>key</code> is not recognized as a fully qualified
 
  56              * classname known to the system, this method will then attempt
 
  57              * to see whether it <b>contains</b> a string identifying one of
 
  58              * the known parsers.  This comparison is <b>case-insensitive</b>.
 
  59              * The intent here is where possible, to select as keys strings
 
  60              * which are returned by the SYST command on the systems which
 
  61              * the corresponding parser successfully parses.  This enables
 
  62              * this factory to be used in the auto-detection system.
 
  65              * @param key    should be a fully qualified classname corresponding to
 
  66              *               a class implementing the FTPFileEntryParser interface<br/>
 
  68              *               a string containing (case-insensitively) one of the
 
  71              *               <li>{@link FTPClientConfig#SYST_UNIX UNIX}</li>
 
  72              *               <li>{@link FTPClientConfig#SYST_NT WINDOWS}</li>
 
  73              *               <li>{@link FTPClientConfig#SYST_OS2 OS/2}</li>
 
  74              *               <li>{@link FTPClientConfig#SYST_OS400 OS/400}</li>
 
  75              *               <li>{@link FTPClientConfig#SYST_VMS VMS}</li>
 
  76              *               <li>{@link FTPClientConfig#SYST_MVS MVS}</li>
 
  78              * @return the FTPFileEntryParser corresponding to the supplied key.
 
  79              * @throws ParserInitializationException thrown if for any reason the factory cannot resolve
 
  80              *                   the supplied key into an FTPFileEntryParser.
 
  81              * @see FTPFileEntryParser
 
  83             public FTPFileEntryParser createFileEntryParser(String key)
 
  85                 @SuppressWarnings("rawtypes")
 
  86                         Class parserClass = null;
 
  87                 FTPFileEntryParser parser = null;
 
  90                     parserClass = Class.forName(key);
 
  91                     parser = (FTPFileEntryParser) parserClass.newInstance();
 
  93                 catch (ClassNotFoundException e)
 
  98                         ukey = key.toUpperCase();
 
 100                     if (ukey.indexOf(FTPClientConfig.SYST_UNIX) >= 0)
 
 102                         parser = createUnixFTPEntryParser();
 
 104                     else if (ukey.indexOf(FTPClientConfig.SYST_VMS) >= 0)
 
 106                         parser = createVMSVersioningFTPEntryParser();
 
 108                     else if (ukey.indexOf(FTPClientConfig.SYST_NT) >= 0 || ukey.indexOf("DOPRA") >= 0 || ukey.indexOf("MSDOS") >= 0)
 
 110                         parser = createNTFTPEntryParser();
 
 112                     else if (ukey.indexOf(FTPClientConfig.SYST_OS2) >= 0)
 
 114                         parser = createOS2FTPEntryParser();
 
 116                     else if (ukey.indexOf(FTPClientConfig.SYST_OS400) >= 0)
 
 118                         parser = createOS400FTPEntryParser();
 
 120                     else if (ukey.indexOf(FTPClientConfig.SYST_MVS) >= 0)
 
 122                         parser = createMVSEntryParser();
 
 126                         throw new ParserInitializationException("Unknown parser type: " + key);
 
 129                 catch (ClassCastException e)
 
 131                     throw new ParserInitializationException(parserClass.getName()
 
 132                         + " does not implement the interface "
 
 133                         + "org.apache.commons.net.ftp.FTPFileEntryParser.", e);
 
 137                     throw new ParserInitializationException("Error initializing parser", e);
 
 140                 if (parser instanceof Configurable) {
 
 141                     ((Configurable)parser).configure(this.config);
 
 147              * <p>Implementation extracts a key from the supplied 
 
 148              * {@link  FTPClientConfig FTPClientConfig}
 
 149              * parameter and creates an object implementing the
 
 150              * interface FTPFileEntryParser and uses the supplied configuration
 
 153              * Note that this method will generally not be called in scenarios
 
 154              * that call for autodetection of parser type but rather, for situations
 
 155              * where the user knows that the server uses a non-default configuration
 
 156              * and knows what that configuration is.
 
 158              * @param config  A {@link  FTPClientConfig FTPClientConfig}  
 
 159              * used to configure the parser created
 
 161              * @return the @link  FTPFileEntryParser FTPFileEntryParser} so created.
 
 162              * @exception ParserInitializationException
 
 163              *                   Thrown on any exception in instantiation
 
 166                 public FTPFileEntryParser createFileEntryParser(FTPClientConfig config) 
 
 167                 throws ParserInitializationException 
 
 169                     this.config = config;
 
 170                         String key = config.getServerSystemKey();
 
 171                         return createFileEntryParser(key);
 
 175             public FTPFileEntryParser createUnixFTPEntryParser()
 
 177                 return (FTPFileEntryParser) new UnixFTPEntryParser();
 
 180             public FTPFileEntryParser createVMSVersioningFTPEntryParser()
 
 182                 return (FTPFileEntryParser) new VMSVersioningFTPEntryParser();
 
 185             public FTPFileEntryParser createNTFTPEntryParser()
 
 187                 if (config != null && FTPClientConfig.SYST_NT.equals(
 
 188                         config.getServerSystemKey())) 
 
 190                     return new NTFTPEntryParser();
 
 192                     return new CompositeFileEntryParser(new FTPFileEntryParser[]
 
 194                                     new NTFTPEntryParser(),
 
 195                                     new UnixFTPEntryParser()
 
 200              public FTPFileEntryParser createOS2FTPEntryParser()
 
 202                 return (FTPFileEntryParser) new OS2FTPEntryParser();
 
 205             public FTPFileEntryParser createOS400FTPEntryParser()
 
 207                 if (config != null && 
 
 208                         FTPClientConfig.SYST_OS400.equals(config.getServerSystemKey())) 
 
 210                     return new OS400FTPEntryParser();
 
 212                         return new CompositeFileEntryParser(new FTPFileEntryParser[]
 
 214                                 new OS400FTPEntryParser(),
 
 215                                 new UnixFTPEntryParser()
 
 220             public FTPFileEntryParser createMVSEntryParser()
 
 222                 return new MVSFTPEntryParser();