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.io.File;
21  import java.text.ParseException;
22  
23  import org.apache.commons.net.ftp.FTPClientConfig;
24  import org.apache.commons.net.ftp.FTPFile;
25  
26  /**
27   * @version $Id: OS400FTPEntryParser.java 1741829 2016-05-01 00:24:44Z sebb $
28   * <pre>
29   * Example *FILE/*MEM FTP entries, when the current
30   * working directory is a file of file system QSYS:
31   * ------------------------------------------------
32   *
33   * $ cwd /qsys.lib/rpgunit.lib/rpgunitc1.file
34   *   250-NAMEFMT set to 1.
35   *   250 "/QSYS.LIB/RPGUNIT.LIB/RPGUNITC1.FILE" is current directory.
36   * $ dir
37   *   227 Entering Passive Mode (10,200,36,33,40,249).
38   *   125 List started.
39   *   QPGMR          135168 22.06.13 13:18:19 *FILE
40   *   QPGMR                                   *MEM       MKCMD.MBR
41   *   QPGMR                                   *MEM       RUCALLTST.MBR
42   *   QPGMR                                   *MEM       RUCMDHLP.MBR
43   *   QPGMR                                   *MEM       RUCRTTST.MBR
44   *   250 List completed.
45   *
46   *
47   * Example *FILE entry of an OS/400 save file:
48   * ---------------------------------------------------
49   *
50   * $ cwd /qsys.lib/rpgunit.lib
51   *   250 "/QSYS.LIB/RPGUNIT.LIB" is current library.
52   * $ dir rpgunit.file
53   *   227 Entering Passive Mode (10,200,36,33,188,106).
54   *   125 List started.
55   *   QPGMR        16347136 29.06.13 15:45:09 *FILE      RPGUNIT.SAVF
56   *   250 List completed.
57   *
58   *
59   * Example *STMF/*DIR FTP entries, when the
60   * current working directory is in file system "root":
61   * ---------------------------------------------------
62   *
63   * $ cwd /home/raddatz
64   *   250 "/home/raddatz" is current directory.
65   * $ dir test*
66   *   227 Entering Passive Mode (10,200,36,33,200,189).
67   *   125 List started.
68   *   RADDATZ           200 21.05.11 12:31:18 *STMF      TEST_RG_02_CRLF.properties
69   *   RADDATZ           187 08.05.11 12:31:40 *STMF      TEST_RG_02_LF.properties
70   *   RADDATZ           187 08.05.11 12:31:52 *STMF      TEST_RG_02_CR.properties
71   *   RADDATZ          8192 04.07.13 09:04:14 *DIR       testDir1/
72   *   RADDATZ          8192 04.07.13 09:04:17 *DIR       testDir2/
73   *   250 List completed.
74   *
75   *
76   * Example 1, using ANT to list specific members of a file:
77   * --------------------------------------------------------
78   *
79   *      &lt;echo/&gt;
80   *      &lt;echo&gt;Listing members of a file:&lt;/echo&gt;
81   *
82   *      &lt;ftp action="list"
83   *           server="${ftp.server}"
84   *           userid="${ftp.user}"
85   *           password="${ftp.password}"
86   *           binary="false"
87   *           verbose="true"
88   *           remotedir="/QSYS.LIB/RPGUNIT.LIB/RPGUNITY1.FILE"
89   *           systemTypeKey="OS/400"
90   *           listing="ftp-listing.txt"
91   *           &gt;
92   *          &lt;fileset dir="./i5-downloads-file" casesensitive="false"&gt;
93   *              &lt;include name="run*.mbr" /&gt;
94   *          &lt;/fileset&gt;
95   *      &lt;/ftp&gt;
96   *
97   * Output:
98   * -------
99   *
100  *   [echo] Listing members of a file:
101  *    [ftp] listing files
102  *    [ftp] listing RUN.MBR
103  *    [ftp] listing RUNNER.MBR
104  *    [ftp] listing RUNNERBND.MBR
105  *    [ftp] 3 files listed
106  *
107  *
108  * Example 2, using ANT to list specific members of all files of a library:
109  * ------------------------------------------------------------------------
110  *
111  *      &lt;echo/&gt;
112  *      &lt;echo&gt;Listing members of all files of a library:&lt;/echo&gt;
113  *
114  *      &lt;ftp action="list"
115  *           server="${ftp.server}"
116  *           userid="${ftp.user}"
117  *           password="${ftp.password}"
118  *           binary="false"
119  *           verbose="true"
120  *           remotedir="/QSYS.LIB/RPGUNIT.LIB"
121  *           systemTypeKey="OS/400"
122  *           listing="ftp-listing.txt"
123  *           &gt;
124  *          &lt;fileset dir="./i5-downloads-lib" casesensitive="false"&gt;
125  *              &lt;include name="**\run*.mbr" /&gt;
126  *          &lt;/fileset&gt;
127  *      &lt;/ftp&gt;
128  *
129  * Output:
130  * -------
131  *
132  *   [echo] Listing members of all files of a library:
133  *    [ftp] listing files
134  *    [ftp] listing RPGUNIT1.FILE\RUN.MBR
135  *    [ftp] listing RPGUNIT1.FILE\RUNRMT.MBR
136  *    [ftp] listing RPGUNITT1.FILE\RUNT.MBR
137  *    [ftp] listing RPGUNITY1.FILE\RUN.MBR
138  *    [ftp] listing RPGUNITY1.FILE\RUNNER.MBR
139  *    [ftp] listing RPGUNITY1.FILE\RUNNERBND.MBR
140  *    [ftp] 6 files listed
141  *
142  *
143  * Example 3, using ANT to download specific members of a file:
144  * ------------------------------------------------------------
145  *
146  *      &lt;echo/&gt;
147  *      &lt;echo&gt;Downloading members of a file:&lt;/echo&gt;
148  *
149  *      &lt;ftp action="get"
150  *           server="${ftp.server}"
151  *           userid="${ftp.user}"
152  *           password="${ftp.password}"
153  *           binary="false"
154  *           verbose="true"
155  *           remotedir="/QSYS.LIB/RPGUNIT.LIB/RPGUNITY1.FILE"
156  *           systemTypeKey="OS/400"
157  *           &gt;
158  *          &lt;fileset dir="./i5-downloads-file" casesensitive="false"&gt;
159  *              &lt;include name="run*.mbr" /&gt;
160  *          &lt;/fileset&gt;
161  *      &lt;/ftp&gt;
162  *
163  * Output:
164  * -------
165  *
166  *   [echo] Downloading members of a file:
167  *    [ftp] getting files
168  *    [ftp] transferring RUN.MBR to C:\workspaces\rdp_080\workspace\ANT - FTP\i5-downloads-file\RUN.MBR
169  *    [ftp] transferring RUNNER.MBR to C:\workspaces\rdp_080\workspace\ANT - FTP\i5-downloads-file\RUNNER.MBR
170  *    [ftp] transferring RUNNERBND.MBR to C:\workspaces\rdp_080\workspace\ANT - FTP\i5-downloads-file\RUNNERBND.MBR
171  *    [ftp] 3 files retrieved
172  *
173  *
174  * Example 4, using ANT to download specific members of all files of a library:
175  * ----------------------------------------------------------------------------
176  *
177  *      &lt;echo/&gt;
178  *      &lt;echo&gt;Downloading members of all files of a library:&lt;/echo&gt;
179  *
180  *      &lt;ftp action="get"
181  *           server="${ftp.server}"
182  *           userid="${ftp.user}"
183  *           password="${ftp.password}"
184  *           binary="false"
185  *           verbose="true"
186  *           remotedir="/QSYS.LIB/RPGUNIT.LIB"
187  *           systemTypeKey="OS/400"
188  *           &gt;
189  *          &lt;fileset dir="./i5-downloads-lib" casesensitive="false"&gt;
190  *              &lt;include name="**\run*.mbr" /&gt;
191  *          &lt;/fileset&gt;
192  *      &lt;/ftp&gt;
193  *
194  * Output:
195  * -------
196  *
197  *   [echo] Downloading members of all files of a library:
198  *    [ftp] getting files
199  *    [ftp] transferring RPGUNIT1.FILE\RUN.MBR to C:\work\rdp_080\space\ANT - FTP\i5-downloads\RPGUNIT1.FILE\RUN.MBR
200  *    [ftp] transferring RPGUNIT1.FILE\RUNRMT.MBR to C:\work\rdp_080\space\ANT - FTP\i5-downloads\RPGUNIT1.FILE\RUNRMT.MBR
201  *    [ftp] transferring RPGUNITT1.FILE\RUNT.MBR to C:\work\rdp_080\space\ANT - FTP\i5-downloads\RPGUNITT1.FILE\RUNT.MBR
202  *    [ftp] transferring RPGUNITY1.FILE\RUN.MBR to C:\work\rdp_080\space\ANT - FTP\i5-downloads\RPGUNITY1.FILE\RUN.MBR
203  *    [ftp] transferring RPGUNITY1.FILE\RUNNER.MBR to C:\work\rdp_080\space\ANT - FTP\i5-downloads\RPGUNITY1.FILE\RUNNER.MBR
204  *    [ftp] transferring RPGUNITY1.FILE\RUNNERBND.MBR to C:\work\rdp_080\space\ANT - FTP\i5-downloads\RPGUNITY1.FILE\RUNNERBND.MBR
205  *    [ftp] 6 files retrieved
206  *
207  *
208  * Example 5, using ANT to download a save file of a library:
209  * ----------------------------------------------------------
210  *
211  *      &lt;ftp action="get"
212  *           server="${ftp.server}"
213  *           userid="${ftp.user}"
214  *           password="${ftp.password}"
215  *           binary="true"
216  *           verbose="true"
217  *           remotedir="/QSYS.LIB/RPGUNIT.LIB"
218  *           systemTypeKey="OS/400"
219  *           &gt;
220  *        &lt;fileset dir="./i5-downloads-savf" casesensitive="false"&gt;
221  *            &lt;include name="RPGUNIT.SAVF" /&gt;
222  *        &lt;/fileset&gt;
223  *      &lt;/ftp&gt;
224  *
225  * Output:
226  * -------
227  *   [echo] Downloading save file:
228  *    [ftp] getting files
229  *    [ftp] transferring RPGUNIT.SAVF to C:\workspaces\rdp_080\workspace\net-Test\i5-downloads-lib\RPGUNIT.SAVF
230  *    [ftp] 1 files retrieved
231  *
232  * </pre>
233  */
234 public class OS400FTPEntryParser extends ConfigurableFTPFileEntryParserImpl
235 {
236     private static final String DEFAULT_DATE_FORMAT
237         = "yy/MM/dd HH:mm:ss"; //01/11/09 12:30:24
238 
239 
240 
241     private static final String REGEX =
242         "(\\S+)\\s+"                  // user
243         + "(?:(\\d+)\\s+)?"           // size, empty for members
244         + "(?:(\\S+)\\s+(\\S+)\\s+)?" // date stuff, empty for members
245         + "(\\*STMF|\\*DIR|\\*FILE|\\*MEM)\\s+"  // *STMF/*DIR/*FILE/*MEM
246         + "(?:(\\S+)\\s*)?";          // filename, missing, when CWD is a *FILE
247 
248 
249     /**
250      * The default constructor for a OS400FTPEntryParser object.
251      *
252      * @exception IllegalArgumentException
253      * Thrown if the regular expression is unparseable.  Should not be seen
254      * under normal conditions.  It it is seen, this is a sign that
255      * <code>REGEX</code> is  not a valid regular expression.
256      */
257     public OS400FTPEntryParser()
258     {
259         this(null);
260     }
261 
262     /**
263      * This constructor allows the creation of an OS400FTPEntryParser object
264      * with something other than the default configuration.
265      *
266      * @param config The {@link FTPClientConfig configuration} object used to
267      * configure this parser.
268      * @exception IllegalArgumentException
269      * Thrown if the regular expression is unparseable.  Should not be seen
270      * under normal conditions.  It it is seen, this is a sign that
271      * <code>REGEX</code> is  not a valid regular expression.
272      * @since 1.4
273      */
274     public OS400FTPEntryParser(FTPClientConfig config)
275     {
276         super(REGEX);
277         configure(config);
278     }
279 
280 
281     @Override
282     public FTPFile parseFTPEntry(String entry)
283     {
284 
285         FTPFile file = new FTPFile();
286         file.setRawListing(entry);
287         int type;
288 
289         if (matches(entry))
290         {
291             String usr = group(1);
292             String filesize = group(2);
293             String datestr = "";
294             if (!isNullOrEmpty(group(3)) || !isNullOrEmpty(group(4)))
295             {
296                 datestr = group(3)+" "+group(4);
297             }
298             String typeStr = group(5);
299             String name = group(6);
300 
301             boolean mustScanForPathSeparator = true;
302 
303             try
304             {
305                 file.setTimestamp(super.parseTimestamp(datestr));
306             }
307             catch (ParseException e)
308             {
309                 // intentionally do nothing
310             }
311 
312 
313             if (typeStr.equalsIgnoreCase("*STMF"))
314             {
315                 type = FTPFile.FILE_TYPE;
316                 if (isNullOrEmpty(filesize) || isNullOrEmpty(name))
317                 {
318                     return null;
319                 }
320             }
321             else if (typeStr.equalsIgnoreCase("*DIR"))
322             {
323                 type = FTPFile.DIRECTORY_TYPE;
324                 if (isNullOrEmpty(filesize) || isNullOrEmpty(name))
325                 {
326                     return null;
327                 }
328             }
329             else if (typeStr.equalsIgnoreCase("*FILE"))
330             {
331                 // File, defines the structure of the data (columns of a row)
332                 // but the data is stored in one or more members. Typically a
333                 // source file contains multiple members whereas it is
334                 // recommended (but not enforced) to use one member per data
335                 // file.
336                 // Save files are a special type of files which are used
337                 // to save objects, e.g. for backups.
338                 if (name != null && name.toUpperCase().endsWith(".SAVF"))
339                 {
340                     mustScanForPathSeparator = false;
341                     type = FTPFile.FILE_TYPE;
342                 }
343                 else
344                 {
345                     return null;
346                 }
347             }
348             else if (typeStr.equalsIgnoreCase("*MEM"))
349             {
350                 mustScanForPathSeparator = false;
351                 type = FTPFile.FILE_TYPE;
352 
353                 if (isNullOrEmpty(name))
354                 {
355                     return null;
356                 }
357                 if (!(isNullOrEmpty(filesize) && isNullOrEmpty(datestr)))
358                 {
359                     return null;
360                 }
361 
362                 // Quick and dirty bug fix to make SelectorUtils work.
363                 // Class SelectorUtils uses 'File.separator' to splitt
364                 // a given path into pieces. But actually it had to
365                 // use the separator of the FTP server, which is a forward
366                 // slash in case of an AS/400.
367                 name = name.replace('/', File.separatorChar);
368             }
369             else
370             {
371                 type = FTPFile.UNKNOWN_TYPE;
372             }
373 
374             file.setType(type);
375 
376             file.setUser(usr);
377 
378             try
379             {
380                 file.setSize(Long.parseLong(filesize));
381             }
382             catch (NumberFormatException e)
383             {
384                 // intentionally do nothing
385             }
386 
387             if (name.endsWith("/"))
388             {
389                 name = name.substring(0, name.length() - 1);
390             }
391             if (mustScanForPathSeparator)
392             {
393                 int pos = name.lastIndexOf('/');
394                 if (pos > -1)
395                 {
396                     name = name.substring(pos + 1);
397                 }
398             }
399 
400             file.setName(name);
401 
402             return file;
403         }
404         return null;
405     }
406 
407     /**
408      *
409      * @param string String value that is checked for <code>null</code>
410      * or empty.
411      * @return <code>true</code> for <code>null</code> or empty values,
412      * else <code>false</code>.
413      */
414     private boolean isNullOrEmpty(String string) {
415         if (string == null || string.length() == 0) {
416             return true;
417         }
418         return false;
419     }
420 
421     /**
422      * Defines a default configuration to be used when this class is
423      * instantiated without a {@link  FTPClientConfig  FTPClientConfig}
424      * parameter being specified.
425      * @return the default configuration for this parser.
426      */
427     @Override
428     protected FTPClientConfig getDefaultConfiguration() {
429         return new FTPClientConfig(
430                 FTPClientConfig.SYST_OS400,
431                 DEFAULT_DATE_FORMAT,
432                 null, null, null, null);
433     }
434 
435 }