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