2 * jFCPlib - FileEntry.java - Copyright © 2008 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package net.pterodactylus.fcp;
21 import java.io.InputStream;
22 import java.util.HashMap;
26 * Container class for file entry data.
28 * @see ClientPutComplexDir#addFileEntry(FileEntry)
29 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
31 public abstract class FileEntry {
33 /** The name of the file. */
34 protected final String name;
36 /** The upload source of the file. */
37 protected final UploadFrom uploadFrom;
40 * Creates a new file entry with the given name and upload source.
43 * The name of the file
45 * The upload source of the file
47 protected FileEntry(String name, UploadFrom uploadFrom) {
49 this.uploadFrom = uploadFrom;
53 * Creates a new file entry for a file that should be transmitted to the
54 * node in the payload of the message.
57 * The name of the file
59 * The content type of the file, or <code>null</code> to let the
62 * The length of the file
63 * @param dataInputStream
64 * The input stream of the file
65 * @return A file entry
67 public static FileEntry createDirectFileEntry(String name, String contentType, long length, InputStream dataInputStream) {
68 return new DirectFileEntry(name, contentType, length, dataInputStream);
72 * Creates a new file entry for a file that should be uploaded from disk.
75 * The name of the file
77 * The name of the file on disk
79 * The content type of the file, or <code>null</code> to let the
82 * The length of the file, or <code>-1</code> to not specify a
84 * @return A file entry
86 public static FileEntry createDiskFileEntry(String name, String filename, String contentType, long length) {
87 return new DiskFileEntry(name, filename, contentType, length);
91 * Creates a new file entry for a file that redirects to another URI.
94 * The name of the file
96 * The target URI of the redirect
97 * @return A file entry
99 public static FileEntry createRedirectFileEntry(String name, String targetURI) {
100 return new RedirectFileEntry(name, targetURI);
104 * Returns the fields for this file entry.
106 * @return The fields for this file entry
108 abstract Map<String, String> getFields();
111 * A file entry for a file that should be transmitted in the payload of the
112 * {@link ClientPutComplexDir} message.
114 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
116 static class DirectFileEntry extends FileEntry {
118 /** The content type of the data. */
119 private final String contentType;
121 /** The length of the data. */
122 private final long length;
124 /** The input stream of the data. */
125 private final InputStream inputStream;
128 * Creates a new direct file entry with content type auto-detection.
131 * The name of the file
133 * The length of the file
135 * The input stream of the file
137 public DirectFileEntry(String name, long length, InputStream inputStream) {
138 this(name, null, length, inputStream);
142 * Creates a new direct file entry.
145 * The name of the file
147 * The content type of the file, or <code>null</code> to let
148 * the node auto-detect it
150 * The length of the file
152 * The input stream of the file
154 public DirectFileEntry(String name, String contentType, long length, InputStream inputStream) {
155 super(name, UploadFrom.direct);
156 this.contentType = contentType;
157 this.length = length;
158 this.inputStream = inputStream;
165 Map<String, String> getFields() {
166 Map<String, String> fields = new HashMap<String, String>();
167 fields.put("Name", name);
168 fields.put("UploadFrom", String.valueOf(uploadFrom));
169 fields.put("DataLength", String.valueOf(length));
170 if (contentType != null) {
171 fields.put("Metadata.ContentType", contentType);
177 * Returns the input stream of the file.
179 * @return The input stream of the file
181 InputStream getInputStream() {
188 * A file entry for a file that should be uploaded from the disk.
190 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
192 static class DiskFileEntry extends FileEntry {
194 /** The name of the on-disk file. */
195 private final String filename;
197 /** The content type of the file. */
198 private final String contentType;
200 /** The length of the file. */
201 private final long length;
204 * Creates a new disk file entry.
207 * The name of the file
209 * The name of the on-disk file
211 * The length of the file
213 public DiskFileEntry(String name, String filename, long length) {
214 this(name, filename, null, length);
218 * Creates a new disk file entry.
221 * The name of the file
223 * The name of the on-disk file
225 * The content type of the file, or <code>null</code> to let
226 * the node auto-detect it
228 * The length of the file
230 public DiskFileEntry(String name, String filename, String contentType, long length) {
231 super(name, UploadFrom.disk);
232 this.filename = filename;
233 this.contentType = contentType;
234 this.length = length;
241 Map<String, String> getFields() {
242 Map<String, String> fields = new HashMap<String, String>();
243 fields.put("Name", name);
244 fields.put("UploadFrom", String.valueOf(uploadFrom));
245 fields.put("Filename", filename);
247 fields.put("DataSize", String.valueOf(length));
249 if (contentType != null) {
250 fields.put("Metadata.ContentType", contentType);
258 * A file entry for a file that redirects to another URI.
260 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
262 static class RedirectFileEntry extends FileEntry {
264 /** The target URI of the redirect. */
265 private String targetURI;
268 * Creates a new redirect file entry.
271 * The name of the file
273 * The target URI of the redirect
275 public RedirectFileEntry(String name, String targetURI) {
276 super(name, UploadFrom.redirect);
277 this.targetURI = targetURI;
284 Map<String, String> getFields() {
285 Map<String, String> fields = new HashMap<String, String>();
286 fields.put("Name", name);
287 fields.put("UploadFrom", String.valueOf(uploadFrom));
288 fields.put("TargetURI", targetURI);