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;
019
020import java.io.BufferedReader;
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.InputStreamReader;
024import java.util.ArrayList;
025import java.util.Iterator;
026import java.util.LinkedList;
027import java.util.List;
028import java.util.ListIterator;
029
030import org.apache.commons.net.util.Charsets;
031
032
033/**
034 * This class handles the entire process of parsing a listing of
035 * file entries from the server.
036 * <p>
037 * This object defines a two-part parsing mechanism.
038 * <p>
039 * The first part is comprised of reading the raw input into an internal
040 * list of strings.  Every item in this list corresponds to an actual
041 * file.  All extraneous matter emitted by the server will have been
042 * removed by the end of this phase.  This is accomplished in conjunction
043 * with the FTPFileEntryParser associated with this engine, by calling
044 * its methods <code>readNextEntry()</code> - which handles the issue of
045 * what delimits one entry from another, usually but not always a line
046 * feed and <code>preParse()</code> - which handles removal of
047 * extraneous matter such as the preliminary lines of a listing, removal
048 * of duplicates on versioning systems, etc.
049 * <p>
050 * The second part is composed of the actual parsing, again in conjunction
051 * with the particular parser used by this engine.  This is controlled
052 * by an iterator over the internal list of strings.  This may be done
053 * either in block mode, by calling the <code>getNext()</code> and
054 * <code>getPrevious()</code> methods to provide "paged" output of less
055 * than the whole list at one time, or by calling the
056 * <code>getFiles()</code> method to return the entire list.
057 * <p>
058 * Examples:
059 * <p>
060 * Paged access:
061 * <pre>
062 *    FTPClient f=FTPClient();
063 *    f.connect(server);
064 *    f.login(username, password);
065 *    FTPListParseEngine engine = f.initiateListParsing(directory);
066 *
067 *    while (engine.hasNext()) {
068 *       FTPFile[] files = engine.getNext(25);  // "page size" you want
069 *       //do whatever you want with these files, display them, etc.
070 *       //expensive FTPFile objects not created until needed.
071 *    }
072 * </pre>
073 * <p>
074 * For unpaged access, simply use FTPClient.listFiles().  That method
075 * uses this class transparently.
076 */
077public class FTPListParseEngine {
078    private List<String> entries = new LinkedList<>();
079    private ListIterator<String> internalIterator = entries.listIterator();
080
081    private final FTPFileEntryParser parser;
082    // Should invalid files (parse failures) be allowed?
083    private final boolean saveUnparseableEntries;
084
085    public FTPListParseEngine(final FTPFileEntryParser parser) {
086        this(parser, null);
087    }
088
089    /**
090     * Intended for use by FTPClient only
091     * @since 3.4
092     */
093    FTPListParseEngine(final FTPFileEntryParser parser, final FTPClientConfig configuration) {
094        this.parser = parser;
095        if (configuration != null) {
096            this.saveUnparseableEntries = configuration.getUnparseableEntries();
097        } else {
098            this.saveUnparseableEntries = false;
099        }
100    }
101
102    /**
103     * handle the initial reading and preparsing of the list returned by
104     * the server.  After this method has completed, this object will contain
105     * a list of unparsed entries (Strings) each referring to a unique file
106     * on the server.
107     *
108     * @param stream input stream provided by the server socket.
109     * @param encoding the encoding to be used for reading the stream
110     *
111     * @throws IOException
112     *                   thrown on any failure to read from the sever.
113     */
114    public void readServerList(final InputStream stream, final String encoding)
115    throws IOException
116    {
117        this.entries = new LinkedList<>();
118        readStream(stream, encoding);
119        this.parser.preParse(this.entries);
120        resetIterator();
121    }
122
123    /**
124     * Internal method for reading the input into the <code>entries</code> list.
125     * After this method has completed, <code>entries</code> will contain a
126     * collection of entries (as defined by
127     * <code>FTPFileEntryParser.readNextEntry()</code>), but this may contain
128     * various non-entry preliminary lines from the server output, duplicates,
129     * and other data that will not be part of the final listing.
130     *
131     * @param stream The socket stream on which the input will be read.
132     * @param encoding The encoding to use.
133     *
134     * @throws IOException
135     *                   thrown on any failure to read the stream
136     */
137    private void readStream(final InputStream stream, final String encoding) throws IOException
138    {
139        try (final BufferedReader reader = new BufferedReader(
140                new InputStreamReader(stream, Charsets.toCharset(encoding)))) {
141
142            String line = this.parser.readNextEntry(reader);
143
144            while (line != null) {
145                this.entries.add(line);
146                line = this.parser.readNextEntry(reader);
147            }
148        }
149    }
150
151    /**
152     * Returns an array of at most <code>quantityRequested</code> FTPFile
153     * objects starting at this object's internal iterator's current position.
154     * If fewer than <code>quantityRequested</code> such
155     * elements are available, the returned array will have a length equal
156     * to the number of entries at and after after the current position.
157     * If no such entries are found, this array will have a length of 0.
158     *
159     * After this method is called this object's internal iterator is advanced
160     * by a number of positions equal to the size of the array returned.
161     *
162     * @param quantityRequested
163     * the maximum number of entries we want to get.
164     *
165     * @return an array of at most <code>quantityRequested</code> FTPFile
166     * objects starting at the current position of this iterator within its
167     * list and at least the number of elements which  exist in the list at
168     * and after its current position.
169     * <p><b>
170     * NOTE:</b> This array may contain null members if any of the
171     * individual file listings failed to parse.  The caller should
172     * check each entry for null before referencing it.
173     */
174    public FTPFile[] getNext(final int quantityRequested) {
175        final List<FTPFile> tmpResults = new LinkedList<>();
176        int count = quantityRequested;
177        while (count > 0 && this.internalIterator.hasNext()) {
178            final String entry = this.internalIterator.next();
179            FTPFile temp = this.parser.parseFTPEntry(entry);
180            if (temp == null && saveUnparseableEntries) {
181                temp = new FTPFile(entry);
182            }
183            tmpResults.add(temp);
184            count--;
185        }
186        return tmpResults.toArray(new FTPFile[tmpResults.size()]);
187
188    }
189
190    /**
191     * Returns an array of at most <code>quantityRequested</code> FTPFile
192     * objects starting at this object's internal iterator's current position,
193     * and working back toward the beginning.
194     *
195     * If fewer than <code>quantityRequested</code> such
196     * elements are available, the returned array will have a length equal
197     * to the number of entries at and after after the current position.
198     * If no such entries are found, this array will have a length of 0.
199     *
200     * After this method is called this object's internal iterator is moved
201     * back by a number of positions equal to the size of the array returned.
202     *
203     * @param quantityRequested
204     * the maximum number of entries we want to get.
205     *
206     * @return an array of at most <code>quantityRequested</code> FTPFile
207     * objects starting at the current position of this iterator within its
208     * list and at least the number of elements which  exist in the list at
209     * and after its current position.  This array will be in the same order
210     * as the underlying list (not reversed).
211     * <p><b>
212     * NOTE:</b> This array may contain null members if any of the
213     * individual file listings failed to parse.  The caller should
214     * check each entry for null before referencing it.
215     */
216    public FTPFile[] getPrevious(final int quantityRequested) {
217        final List<FTPFile> tmpResults = new LinkedList<>();
218        int count = quantityRequested;
219        while (count > 0 && this.internalIterator.hasPrevious()) {
220            final String entry = this.internalIterator.previous();
221            FTPFile temp = this.parser.parseFTPEntry(entry);
222            if (temp == null && saveUnparseableEntries) {
223                temp = new FTPFile(entry);
224            }
225            tmpResults.add(0,temp);
226            count--;
227        }
228        return tmpResults.toArray(new FTPFile[tmpResults.size()]);
229    }
230
231    /**
232     * Returns an array of FTPFile objects containing the whole list of
233     * files returned by the server as read by this object's parser.
234     *
235     * @return an array of FTPFile objects containing the whole list of
236     *         files returned by the server as read by this object's parser.
237     * None of the entries will be null
238     * @throws IOException - not ever thrown, may be removed in a later release
239     */
240    public FTPFile[] getFiles()
241    throws IOException // TODO remove; not actually thrown
242    {
243        return getFiles(FTPFileFilters.NON_NULL);
244    }
245
246    /**
247     * Returns an array of FTPFile objects containing the whole list of
248     * files returned by the server as read by this object's parser.
249     * The files are filtered before being added to the array.
250     *
251     * @param filter FTPFileFilter, must not be <code>null</code>.
252     *
253     * @return an array of FTPFile objects containing the whole list of
254     *         files returned by the server as read by this object's parser.
255     * <p><b>
256     * NOTE:</b> This array may contain null members if any of the
257     * individual file listings failed to parse.  The caller should
258     * check each entry for null before referencing it, or use the
259     * a filter such as {@link FTPFileFilters#NON_NULL} which does not
260     * allow null entries.
261     * @since 2.2
262     * @throws IOException - not ever thrown, may be removed in a later release
263     */
264    public FTPFile[] getFiles(final FTPFileFilter filter)
265    throws IOException // TODO remove; not actually thrown
266    {
267        final List<FTPFile> tmpResults = new ArrayList<>();
268        final Iterator<String> iter = this.entries.iterator();
269        while (iter.hasNext()) {
270            final String entry = iter.next();
271            FTPFile temp = this.parser.parseFTPEntry(entry);
272            if (temp == null && saveUnparseableEntries) {
273                temp = new FTPFile(entry);
274            }
275            if (filter.accept(temp)){
276                tmpResults.add(temp);
277            }
278        }
279        return tmpResults.toArray(new FTPFile[tmpResults.size()]);
280
281    }
282
283    /**
284     * convenience method to allow clients to know whether this object's
285     * internal iterator's current position is at the end of the list.
286     *
287     * @return true if internal iterator is not at end of list, false
288     * otherwise.
289     */
290    public boolean hasNext() {
291        return internalIterator.hasNext();
292    }
293
294    /**
295     * convenience method to allow clients to know whether this object's
296     * internal iterator's current position is at the beginning of the list.
297     *
298     * @return true if internal iterator is not at beginning of list, false
299     * otherwise.
300     */
301    public boolean hasPrevious() {
302        return internalIterator.hasPrevious();
303    }
304
305    /**
306     * resets this object's internal iterator to the beginning of the list.
307     */
308    public void resetIterator() {
309        this.internalIterator = this.entries.listIterator();
310    }
311
312    // DEPRECATED METHODS - for API compatibility only - DO NOT USE
313
314    /**
315     * Do not use.
316     * @param stream the stream from which to read
317     * @throws IOException on error
318     * @deprecated use {@link #readServerList(InputStream, String)} instead
319    */
320    @Deprecated
321    public void readServerList(final InputStream stream)
322    throws IOException
323    {
324        readServerList(stream, null);
325    }
326
327}