Modify groupId for ems driver
[vfc/nfvo/driver/ems.git] / ems / boco / src / main / java / org / onap / nfvo / emsdriver / commons / ftp / ExtendsDefaultFTPFileEntryParserFactory.java
1 /**
2  * Copyright 2017 BOCO Corporation.  CMCC Technologies Co., Ltd
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.nfvo.emsdriver.commons.ftp;
17
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;
30         /**
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.
36          *
37          * @see org.apache.commons.net.ftp.FTPClient#listFiles
38          * @see org.apache.commons.net.ftp.FTPClient#setParserFactory
39          */
40 public class ExtendsDefaultFTPFileEntryParserFactory extends
41                 DefaultFTPFileEntryParserFactory {
42
43                 private FTPClientConfig config = null;
44
45                 /**
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.
54              * <p/>
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.
63              * <p/>
64              *
65              * @param key    should be a fully qualified classname corresponding to
66              *               a class implementing the FTPFileEntryParser interface<br/>
67              *               OR<br/>
68              *               a string containing (case-insensitively) one of the
69              *               following keywords:
70              *               <ul>
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>
77              *               </ul>
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
82              */
83             public FTPFileEntryParser createFileEntryParser(String key)
84             {
85                 @SuppressWarnings("rawtypes")
86                         Class parserClass = null;
87                 FTPFileEntryParser parser = null;
88                 try
89                 {
90                     parserClass = Class.forName(key);
91                     parser = (FTPFileEntryParser) parserClass.newInstance();
92                 }
93                 catch (ClassNotFoundException e)
94                 {
95                     String ukey = null;
96                     if (null != key)
97                     {
98                         ukey = key.toUpperCase();
99                     }
100                     if (ukey.indexOf(FTPClientConfig.SYST_UNIX) >= 0)
101                     {
102                         parser = createUnixFTPEntryParser();
103                     }
104                     else if (ukey.indexOf(FTPClientConfig.SYST_VMS) >= 0)
105                     {
106                         parser = createVMSVersioningFTPEntryParser();
107                     }
108                     else if (ukey.indexOf(FTPClientConfig.SYST_NT) >= 0 || ukey.indexOf("DOPRA") >= 0 || ukey.indexOf("MSDOS") >= 0)
109                     {
110                         parser = createNTFTPEntryParser();
111                     }
112                     else if (ukey.indexOf(FTPClientConfig.SYST_OS2) >= 0)
113                     {
114                         parser = createOS2FTPEntryParser();
115                     }
116                     else if (ukey.indexOf(FTPClientConfig.SYST_OS400) >= 0)
117                     {
118                         parser = createOS400FTPEntryParser();
119                     }
120                     else if (ukey.indexOf(FTPClientConfig.SYST_MVS) >= 0)
121                     {
122                         parser = createMVSEntryParser();
123                         }
124                     else
125                     {
126                         throw new ParserInitializationException("Unknown parser type: " + key);
127                     }
128                 }
129                 catch (ClassCastException e)
130                 {
131                     throw new ParserInitializationException(parserClass.getName()
132                         + " does not implement the interface "
133                         + "org.apache.commons.net.ftp.FTPFileEntryParser.", e);
134                 }
135                 catch (Throwable e)
136                 {
137                     throw new ParserInitializationException("Error initializing parser", e);
138                 }
139
140                 if (parser instanceof Configurable) {
141                     ((Configurable)parser).configure(this.config);
142                 }    
143                 return parser;
144             }
145             
146             /**
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
151              * to configure it.
152              * </p><p>
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.
157              * </p>
158              * @param config  A {@link  FTPClientConfig FTPClientConfig}  
159              * used to configure the parser created
160              *
161              * @return the @link  FTPFileEntryParser FTPFileEntryParser} so created.
162              * @exception ParserInitializationException
163              *                   Thrown on any exception in instantiation
164              * @since 1.4
165              */
166                 public FTPFileEntryParser createFileEntryParser(FTPClientConfig config) 
167                 throws ParserInitializationException 
168                 {
169                     this.config = config;
170                         String key = config.getServerSystemKey();
171                         return createFileEntryParser(key);
172                 }
173
174
175             public FTPFileEntryParser createUnixFTPEntryParser()
176             {
177                 return (FTPFileEntryParser) new UnixFTPEntryParser();
178             }
179
180             public FTPFileEntryParser createVMSVersioningFTPEntryParser()
181             {
182                 return (FTPFileEntryParser) new VMSVersioningFTPEntryParser();
183             }
184
185             public FTPFileEntryParser createNTFTPEntryParser()
186             {
187                 if (config != null && FTPClientConfig.SYST_NT.equals(
188                         config.getServerSystemKey())) 
189                 {
190                     return new NTFTPEntryParser();
191                 } else {
192                     return new CompositeFileEntryParser(new FTPFileEntryParser[]
193                                 {
194                                     new NTFTPEntryParser(),
195                                     new UnixFTPEntryParser()
196                                 });
197                 }
198             }
199             
200              public FTPFileEntryParser createOS2FTPEntryParser()
201             {
202                 return (FTPFileEntryParser) new OS2FTPEntryParser();
203             }
204
205             public FTPFileEntryParser createOS400FTPEntryParser()
206             {
207                 if (config != null && 
208                         FTPClientConfig.SYST_OS400.equals(config.getServerSystemKey())) 
209                 {
210                     return new OS400FTPEntryParser();
211                 } else {
212                         return new CompositeFileEntryParser(new FTPFileEntryParser[]
213                             {
214                                 new OS400FTPEntryParser(),
215                                 new UnixFTPEntryParser()
216                             });
217                 }
218             }
219
220             public FTPFileEntryParser createMVSEntryParser()
221             {
222                 return new MVSFTPEntryParser();
223             }
224 }