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.text.ParseException;
21  
22  import org.apache.commons.net.ftp.FTPClientConfig;
23  import org.apache.commons.net.ftp.FTPFile;
24  
25  /**
26   * Implementation of FTPFileEntryParser and FTPFileListParser for Netware Systems. Note that some of the proprietary
27   * extensions for Novell-specific operations are not supported. See
28   * <a href="http://www.novell.com/documentation/nw65/index.html?page=/documentation/nw65/ftp_enu/data/fbhbgcfa.html">http://www.novell.com/documentation/nw65/index.html?page=/documentation/nw65/ftp_enu/data/fbhbgcfa.html</a>
29   * for more details.
30   *
31   * @author <a href="rwinston@apache.org">Rory Winston</a>
32   * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
33   * @version $Id: NetwareFTPEntryParser.java 1489361 2013-06-04 09:48:36Z sebb $
34   * @since 1.5
35   */
36  public class NetwareFTPEntryParser extends ConfigurableFTPFileEntryParserImpl {
37  
38      /**
39       * Default date format is e.g. Feb 22 2006
40       */
41      private static final String DEFAULT_DATE_FORMAT = "MMM dd yyyy";
42  
43      /**
44       * Default recent date format is e.g. Feb 22 17:32
45       */
46      private static final String DEFAULT_RECENT_DATE_FORMAT = "MMM dd HH:mm";
47  
48      /**
49       * this is the regular expression used by this parser.
50       * Example: d [-W---F--] SCION_VOL2                        512 Apr 13 23:12 VOL2
51       */
52      private static final String REGEX = "(d|-){1}\\s+"      // Directory/file flag
53              + "\\[(.*)\\]\\s+"                              // Attributes
54              + "(\\S+)\\s+" + "(\\d+)\\s+"                   // Owner and size
55              + "(\\S+\\s+\\S+\\s+((\\d+:\\d+)|(\\d{4})))"    // Long/short date format
56              + "\\s+(.*)";                                   // Filename (incl. spaces)
57  
58      /**
59       * The default constructor for a NetwareFTPEntryParser object.
60       *
61       * @exception IllegalArgumentException
62       * Thrown if the regular expression is unparseable.  Should not be seen
63       * under normal conditions.  It it is seen, this is a sign that
64       * <code>REGEX</code> is  not a valid regular expression.
65       */
66      public NetwareFTPEntryParser() {
67          this(null);
68      }
69  
70      /**
71       * This constructor allows the creation of an NetwareFTPEntryParser object
72       * with something other than the default configuration.
73       *
74       * @param config The {@link FTPClientConfig configuration} object used to
75       * configure this parser.
76       * @exception IllegalArgumentException
77       * Thrown if the regular expression is unparseable.  Should not be seen
78       * under normal conditions.  It it is seen, this is a sign that
79       * <code>REGEX</code> is  not a valid regular expression.
80       * @since 1.4
81       */
82      public NetwareFTPEntryParser(FTPClientConfig config) {
83          super(REGEX);
84          configure(config);
85      }
86  
87      /**
88       * Parses a line of an NetwareFTP server file listing and converts it into a
89       * usable format in the form of an <code> FTPFile </code> instance.  If the
90       * file listing line doesn't describe a file, <code> null </code> is
91       * returned, otherwise a <code> FTPFile </code> instance representing the
92       * files in the directory is returned.
93       * <p>
94       * <p>
95       * Netware file permissions are in the following format:  RWCEAFMS, and are explained as follows:
96       * <ul>
97       * <li><b>S</b> - Supervisor; All rights.
98       * <li><b>R</b> - Read; Right to open and read or execute.
99       * <li><b>W</b> - Write; Right to open and modify.
100      * <li><b>C</b> - Create; Right to create; when assigned to a file, allows a deleted file to be recovered.
101      * <li><b>E</b> - Erase; Right to delete.
102      * <li><b>M</b> - Modify; Right to rename a file and to change attributes.
103      * <li><b>F</b> - File Scan; Right to see directory or file listings.
104      * <li><b>A</b> - Access Control; Right to modify trustee assignments and the Inherited Rights Mask.
105      * </ul>
106      *
107      * See <a href="http://www.novell.com/documentation/nfap10/index.html?page=/documentation/nfap10/nfaubook/data/abxraws.html">here</a>
108      * for more details
109      *
110      * @param entry A line of text from the file listing
111      * @return An FTPFile instance corresponding to the supplied entry
112      */
113 //    @Override
114     public FTPFile parseFTPEntry(String entry) {
115 
116         FTPFile f = new FTPFile();
117         if (matches(entry)) {
118             String dirString = group(1);
119             String attrib = group(2);
120             String user = group(3);
121             String size = group(4);
122             String datestr = group(5);
123             String name = group(9);
124 
125             try {
126                 f.setTimestamp(super.parseTimestamp(datestr));
127             } catch (ParseException e) {
128                  // intentionally do nothing
129             }
130 
131             //is it a DIR or a file
132             if (dirString.trim().equals("d")) {
133                 f.setType(FTPFile.DIRECTORY_TYPE);
134             } else // Should be "-"
135             {
136                 f.setType(FTPFile.FILE_TYPE);
137             }
138 
139             f.setUser(user);
140 
141             //set the name
142             f.setName(name.trim());
143 
144             //set the size
145             f.setSize(Long.parseLong(size.trim()));
146 
147             // Now set the permissions (or at least a subset thereof - full permissions would probably require
148             // subclassing FTPFile and adding extra metainformation there)
149             if (attrib.indexOf("R") != -1) {
150                 f.setPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION,
151                         true);
152             }
153             if (attrib.indexOf("W") != -1) {
154                 f.setPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION,
155                         true);
156             }
157 
158             return (f);
159         }
160         return null;
161 
162     }
163 
164     /**
165      * Defines a default configuration to be used when this class is
166      * instantiated without a {@link  FTPClientConfig  FTPClientConfig}
167      * parameter being specified.
168      * @return the default configuration for this parser.
169      */
170     @Override
171     protected FTPClientConfig getDefaultConfiguration() {
172         return new FTPClientConfig(FTPClientConfig.SYST_NETWARE,
173                 DEFAULT_DATE_FORMAT, DEFAULT_RECENT_DATE_FORMAT, null, null,
174                 null);
175     }
176 
177 }