    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 *      https://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.Configurable;
    023import org.apache.commons.net.ftp.FTPClientConfig;
    024import org.apache.commons.net.ftp.FTPFile;
    025import org.apache.commons.net.ftp.FTPFileEntryParser;
    026
    027/**
    028 * Implements {@link FTPFileEntryParser} and {@link Configurable} for Netware Systems. Note that some proprietary extensions for Novell-specific operations
    029 * are not supported. See <a href="http://www.novell.com/documentation/nw65/index.html?page=/documentation/nw65/ftp_enu/data/fbhbgcfa.html">
    030 * http://www.novell.com/documentation/nw65/index.html?page=/documentation/nw65/ftp_enu/data/fbhbgcfa.html</a> for more details.
    031 *
    032 * @see FTPFileEntryParser 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. Example: d [-W---F--] SCION_VOL2 512 Apr 13 23:12 VOL2
    049     */
    050    private static final String REGEX = "(d|-){1}\\s+" // Directory/file flag
    051            + "\\[([-A-Z]+)\\]\\s+" // Attributes RWCEAFMS or -
    052            + "(\\S+)\\s+" + "(\\d+)\\s+" // Owner and size
    053            + "(\\S+\\s+\\S+\\s+((\\d+:\\d+)|(\\d{4})))" // Long/short date format
    054            + "\\s+(.*)"; // Filename (incl. spaces)
    055
    056    /**
    057     * The default constructor for a NetwareFTPEntryParser object.
    058     *
    059     * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. If it is seen, this is a
    060     *                                  sign that {@code REGEX} is not a valid regular expression.
    061     */
    062    public NetwareFTPEntryParser() {
    063        this(null);
    064    }
    065
    066    /**
    067     * This constructor allows the creation of an NetwareFTPEntryParser object with something other than the default configuration.
    068     *
    069     * @param config The {@link FTPClientConfig configuration} object used to configure this parser.
    070     * @throws IllegalArgumentException Thrown if the regular expression is unparseable. Should not be seen under normal conditions. If it is seen, this is a
    071     *                                  sign that {@code REGEX} is not a valid regular expression.
    072     * @since 1.4
    073     */
    074    public NetwareFTPEntryParser(final FTPClientConfig config) {
    075        super(REGEX);
    076        configure(config);
    077    }
    078
    079    /**
    080     * Gets a new default configuration to be used when this class is instantiated without a {@link FTPClientConfig FTPClientConfig} parameter being specified.
    081     *
    082     * @return the default configuration for this parser.
    083     */
    084    @Override
    085    protected FTPClientConfig getDefaultConfiguration() {
    086        return new FTPClientConfig(FTPClientConfig.SYST_NETWARE, DEFAULT_DATE_FORMAT, DEFAULT_RECENT_DATE_FORMAT);
    087    }
    088
    089    /**
    090     * Parses a line of an NetwareFTP server file listing and converts it into a usable format in the form of an {@code FTPFile} instance. If the file
    091     * listing line doesn't describe a file, {@code null} is returned, otherwise a {@code FTPFile} instance representing the files in the
    092     * directory is returned.
    093     * <p>
    094     * Netware file permissions are in the following format: RWCEAFMS, and are explained as follows:
    095     * <ul>
    096     * <li><strong>S</strong> - Supervisor; All rights.
    097     * <li><strong>R</strong> - Read; Right to open and read or execute.
    098     * <li><strong>W</strong> - Write; Right to open and modify.
    099     * <li><strong>C</strong> - Create; Right to create; when assigned to a file, allows a deleted file to be recovered.
    100     * <li><strong>E</strong> - Erase; Right to delete.
    101     * <li><strong>M</strong> - Modify; Right to rename a file and to change attributes.
    102     * <li><strong>F</strong> - File Scan; Right to see directory or file listings.
    103     * <li><strong>A</strong> - Access Control; Right to modify trustee assignments and the Inherited Rights Mask.
    104     * </ul>
    105     *
    106     * See <a href="http://www.novell.com/documentation/nfap10/index.html?page=/documentation/nfap10/nfaubook/data/abxraws.html"> here</a> for more details
    107     *
    108     * @param entry A line of text from the file listing
    109     * @return An FTPFile instance corresponding to the supplied entry
    110     */
    111    @Override
    112    public FTPFile parseFTPEntry(final String entry) {
    113
    114        final FTPFile f = new FTPFile();
    115        if (matches(entry)) {
    116            final String dirString = group(1);
    117            final String attrib = group(2);
    118            final String user = group(3);
    119            final String size = group(4);
    120            final String datestr = group(5);
    121            final String name = group(9);
    122
    123            try {
    124                f.setTimestamp(super.parseTimestamp(datestr));
    125            } catch (final ParseException e) {
    126                // intentionally do nothing
    127            }
    128
    129            // is it a DIR or a file
    130            if (dirString.trim().equals("d")) {
    131                f.setType(FTPFile.DIRECTORY_TYPE);
    132            } else {
    133                // Should be "-"
    134                f.setType(FTPFile.FILE_TYPE);
    135            }
    136
    137            f.setUser(user);
    138
    139            // set the name
    140            f.setName(name.trim());
    141
    142            // set the size
    143            f.setSize(Long.parseLong(size.trim()));
    144
    145            // Now set the permissions (or at least a subset thereof - full permissions would probably require
    146            // subclassing FTPFile and adding extra metainformation there)
    147            if (attrib.indexOf('R') != -1) {
    148                f.setPermission(FTPFile.USER_ACCESS, FTPFile.READ_PERMISSION, true);
    149            }
    150            if (attrib.indexOf('W') != -1) {
    151                f.setPermission(FTPFile.USER_ACCESS, FTPFile.WRITE_PERMISSION, true);
    152            }
    153
    154            return f;
    155        }
    156        return null;
    157
    158    }
    159
    160}