summaryrefslogtreecommitdiff
path: root/ojluni/src/main/java/sun/net/ftp/FtpDirEntry.java
blob: 01ad1ceae35cbd15e44291d1e8f72443f9017902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package sun.net.ftp;

import java.util.Date;
import java.util.HashMap;

/**
 * A {@code FtpDirEntry} is a class agregating all the information that the FTP client
 * can gather from the server by doing a {@code LST} (or {@code NLST}) command and
 * parsing the output. It will typically contain the name, type, size, last modification
 * time, owner and group of the file, although some of these could be unavailable
 * due to specific FTP server limitations.
 *
 * @see sun.net.ftp.FtpDirParser
 * @since 1.7
 */
public class FtpDirEntry {

    public enum Type {

        FILE, DIR, PDIR, CDIR, LINK
    };

    public enum Permission {

        USER(0), GROUP(1), OTHERS(2);
        int value;

        Permission(int v) {
            value = v;
        }
    };
    private final String name;
    private String user = null;
    private String group = null;
    private long size = -1;
    private java.util.Date created = null;
    private java.util.Date lastModified = null;
    private Type type = Type.FILE;
    private boolean[][] permissions = null;
    private HashMap<String, String> facts = new HashMap<String, String>();

    private FtpDirEntry() {
        name = null;
    }

    /**
     * Creates an FtpDirEntry instance with only the name being set.
     *
     * @param name The name of the file
     */
    public FtpDirEntry(String name) {
        this.name = name;
    }

    /**
     * Returns the name of the remote file.
     *
     * @return a {@code String} containing the name of the remote file.
     */
    public String getName() {
        return name;
    }

    /**
     * Returns the user name of the owner of the file as returned by the FTP
     * server, if provided. This could be a name or a user id (number).
     *
     * @return a {@code String} containing the user name or
     *         {@code null} if that information is not available.
     */
    public String getUser() {
        return user;
    }

    /**
     * Sets the user name of the owner of the file. Intended mostly to be
     * used from inside a {@link java.net.FtpDirParser} implementation.
     *
     * @param user The user name of the owner of the file, or {@code null}
     * if that information is not available.
     * @return this FtpDirEntry
     */
    public FtpDirEntry setUser(String user) {
        this.user = user;
        return this;
    }

    /**
     * Returns the group name of the file as returned by the FTP
     * server, if provided. This could be a name or a group id (number).
     *
     * @return a {@code String} containing the group name or
     *         {@code null} if that information is not available.
     */
    public String getGroup() {
        return group;
    }

    /**
     * Sets the name of the group to which the file belong. Intended mostly to be
     * used from inside a {@link java.net.FtpDirParser} implementation.
     *
     * @param group The name of the group to which the file belong, or {@code null}
     * if that information is not available.
     * @return this FtpDirEntry
     */
    public FtpDirEntry setGroup(String group) {
        this.group = group;
        return this;
    }

    /**
     * Returns the size of the remote file as it was returned by the FTP
     * server, if provided.
     *
     * @return the size of the file or -1 if that information is not available.
     */
    public long getSize() {
        return size;
    }

    /**
     * Sets the size of that file. Intended mostly to be used from inside an
     * {@link java.net.FtpDirParser} implementation.
     *
     * @param size The size, in bytes, of that file. or -1 if unknown.
     * @return this FtpDirEntry
     */
    public FtpDirEntry setSize(long size) {
        this.size = size;
        return this;
    }

    /**
     * Returns the type of the remote file as it was returned by the FTP
     * server, if provided.
     * It returns a FtpDirEntry.Type enum and the values can be:
     * - FtpDirEntry.Type.FILE for a normal file
     * - FtpDirEntry.Type.DIR for a directory
     * - FtpDirEntry.Type.LINK for a symbolic link
     *
     * @return a {@code FtpDirEntry.Type} describing the type of the file
     *         or {@code null} if that information is not available.
     */
    public Type getType() {
        return type;
    }

    /**
     * Sets the type of the file. Intended mostly to be used from inside an
     * {@link java.net.FtpDirParser} implementation.
     *
     * @param type the type of this file or {@code null} if that information
     * is not available.
     * @return this FtpDirEntry
     */
    public FtpDirEntry setType(Type type) {
        this.type = type;
        return this;
    }

    /**
     * Returns the last modification time of the remote file as it was returned
     * by the FTP server, if provided, {@code null} otherwise.
     *
     * @return a <code>Date</code> representing the last time the file was
     *         modified on the server, or {@code null} if that
     *         information is not available.
     */
    public java.util.Date getLastModified() {
        return this.lastModified;
    }

    /**
     * Sets the last modification time of the file. Intended mostly to be used
     * from inside an {@link java.net.FtpDirParser} implementation.
     *
     * @param lastModified The Date representing the last modification time, or
     * {@code null} if that information is not available.
     * @return this FtpDirEntry
     */
    public FtpDirEntry setLastModified(Date lastModified) {
        this.lastModified = lastModified;
        return this;
    }

    /**
     * Returns whether read access is granted for a specific permission.
     *
     * @param p the Permission (user, group, others) to check.
     * @return {@code true} if read access is granted.
     */
    public boolean canRead(Permission p) {
        if (permissions != null) {
            return permissions[p.value][0];
        }
        return false;
    }

    /**
     * Returns whether write access is granted for a specific permission.
     *
     * @param p the Permission (user, group, others) to check.
     * @return {@code true} if write access is granted.
     */
    public boolean canWrite(Permission p) {
        if (permissions != null) {
            return permissions[p.value][1];
        }
        return false;
    }

    /**
     * Returns whether execute access is granted for a specific permission.
     *
     * @param p the Permission (user, group, others) to check.
     * @return {@code true} if execute access is granted.
     */
    public boolean canExexcute(Permission p) {
        if (permissions != null) {
            return permissions[p.value][2];
        }
        return false;
    }

    /**
     * Sets the permissions for that file. Intended mostly to be used
     * from inside an {@link java.net.FtpDirParser} implementation.
     * The permissions array is a 3x3 {@code boolean} array, the first index being
     * the User, group or owner (0, 1 and 2 respectively) while the second
     * index is read, write or execute (0, 1 and 2 respectively again).
     * <p>E.G.: {@code permissions[1][2]} is the group/execute permission.</p>
     *
     * @param permissions a 3x3 {@code boolean} array
     * @return this {@code FtpDirEntry}
     */
    public FtpDirEntry setPermissions(boolean[][] permissions) {
        this.permissions = permissions;
        return this;
    }

    /**
     * Adds a 'fact', as defined in RFC 3659, to the list of facts of this file.
     * Intended mostly to be used from inside a {@link java.net.FtpDirParser}
     * implementation.
     *
     * @param fact the name of the fact (e.g. "Media-Type"). It is not case-sensitive.
     * @param value the value associated with this fact.
     * @return this {@code FtpDirEntry}
     */
    public FtpDirEntry addFact(String fact, String value) {
        facts.put(fact.toLowerCase(), value);
        return this;
    }

    /**
     * Returns the requested 'fact', as defined in RFC 3659, if available.
     *
     * @param fact The name of the fact *e.g. "Media-Type"). It is not case sensitive.
     * @return The value of the fact or, {@code null} if that fact wasn't
     * provided by the server.
     */
    public String getFact(String fact) {
        return facts.get(fact.toLowerCase());
    }

    /**
     * Returns the creation time of the file, when provided by the server.
     *
     * @return The Date representing the creation time, or {@code null}
     * if the server didn't provide that information.
     */
    public Date getCreated() {
        return created;
    }

    /**
     * Sets the creation time for that file. Intended mostly to be used from
     * inside a {@link java.net.FtpDirParser} implementation.
     *
     * @param created the Date representing the creation time for that file, or
     * {@code null} if that information is not available.
     * @return this FtpDirEntry
     */
    public FtpDirEntry setCreated(Date created) {
        this.created = created;
        return this;
    }

    /**
     * Returns a string representation of the object.
     * The {@code toString} method for class {@code FtpDirEntry}
     * returns a string consisting of the name of the file, followed by its
     * type between brackets, followed by the user and group between
     * parenthesis, then size between '{', and, finally, the lastModified of last
     * modification if it's available.
     *
     * @return  a string representation of the object.
     */
    @Override
    public String toString() {
        if (lastModified == null) {
            return name + " [" + type + "] (" + user + " / " + group + ") " + size;
        }
        return name + " [" + type + "] (" + user + " / " + group + ") {" + size + "} " + java.text.DateFormat.getDateInstance().format(lastModified);
    }
}