001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.net.ftp.parser;
019
020import java.text.ParseException;
021
022import org.apache.commons.net.ftp.FTPClientConfig;
023import org.apache.commons.net.ftp.FTPFile;
024
025/**
026 * Implementation of FTPFileEntryParser and FTPFileListParser for Netware Systems. Note that some of the proprietary
027 * extensions for Novell-specific operations are not supported. See
028 * <a href="http://www.novell.com/documentation/nw65/index.html?page=/documentation/nw65/ftp_enu/data/fbhbgcfa.html">
029 * http://www.novell.com/documentation/nw65/index.html?page=/documentation/nw65/ftp_enu/data/fbhbgcfa.html</a>
030 * for more details.
031 *
032 * @see org.apache.commons.net.ftp.FTPFileEntryParser FTPFileEntryParser (for usage instructions)
033 * @since 1.5
034 */
035public class NetwareFTPEntryParser extends ConfigurableFTPFileEntryParserImpl {
036
037    /**
038     * Default date format is e.g. Feb 22 2006
039     */
040    private static final String DEFAULT_DATE_FORMAT = "MMM dd yyyy";
041
042    /**
043     * Default recent date format is e.g. Feb 22 17:32
044     */
045    private static final String DEFAULT_RECENT_DATE_FORMAT = "MMM dd HH:mm";
046
047    /**
048     * this is the regular expression used by this parser.
049     * Example: d [-W---F--] SCION_VOL2                        512 Apr 13 23:12 VOL2
050     */
051    private static final String REGEX = "(d|-){1}\\s+"      // Directory/file flag
052            + "\\[([-A-Z]+)\\]\\s+"                         // Attributes RWCEAFMS or -
053            + "(\\S+)\\s+" + "(\\d+)\\s+"                   // Owner and size
054            + "(\\S+\\s+\\S+\\s+((\\d+:\\d+)|(\\d{4})))"    // Long/short date format
055            + "\\s+(.*)";                                   // Filename (incl. spaces)
056
057    /**
058     * The default constructor for a NetwareFTPEntryParser object.
059     *
060     * @throws IllegalArgumentException
061     * Thrown if the regular expression is unparseable.  Should not be seen
062     * under normal conditions.  It it is seen, this is a sign that
063     * <code>REGEX</code> is  not a valid regular expression.
064     */
065    public NetwareFTPEntryParser() {
066        this(null);
067    }
068
069    /**
070     * This constructor allows the creation of an NetwareFTPEntryParser object
071     * with something other than the default configuration.
072     *
073     * @param config The {@link FTPClientConfig configuration} object used to
074     * configure this parser.
075     * @throws IllegalArgumentException
076     * Thrown if the regular expression is unparseable.  Should not be seen
077     * under normal conditions.  It it is seen, this is a sign that
078     * <code>REGEX</code> is  not a valid regular expression.
079     * @since 1.4
080     */
081    public NetwareFTPEntryParser(final FTPClientConfig config) {
082        super(REGEX);
083        configure(config);
084    }
085
086    /**
087     * Parses a line of an NetwareFTP server file listing and converts it into a
088     * usable format in the form of an <code> FTPFile </code> instance.  If the
089     * file listing line doesn't describe a file, <code> null </code> is
090     * returned, otherwise a <code> FTPFile </code> instance representing the
091     * files in the directory is returned.
092     * <p>
093     * Netware file permissions are in the following format:  RWCEAFMS, and are explained as follows:
094     * <ul>
095     * <li><b>S</b> - Supervisor; All rights.
096     * <li><b>R</b> - Read; Right to open and read or execute.
097     * <li><b>W</b> - Write; Right to open and modify.
098     * <li><b>C</b> - Create; Right to create; when assigned to a file, allows a deleted file to be recovered.
099     * <li><b>E</b> - Erase; Right to delete.
100     * <li><b>M</b> - Modify; Right to rename a file and to change attributes.
101     * <li><b>F</b> - File Scan; Right to see directory or file listings.
102     * <li><b>A</b> - Access Control; Right to modify trustee assignments and the Inherited Rights Mask.
103     * </ul>
104     *
105     * See
106     * <a href="http://www.novell.com/documentation/nfap10/index.html?page=/documentation/nfap10/nfaubook/data/abxraws.html">
107     * 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(final String entry) {
115
116        final FTPFile f = new FTPFile();
117        if (matches(entry)) {
118            final String dirString = group(1);
119            final String attrib = group(2);
120            final String user = group(3);
121            final String size = group(4);
122            final String datestr = group(5);
123            final String name = group(9);
124
125            try {
126                f.setTimestamp(super.parseTimestamp(datestr));
127            } catch (final 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);
174    }
175
176}