2 * jSite2 - FileEntry.java -
3 * Copyright © 2008 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.fcp;
22 import java.io.InputStream;
23 import java.util.HashMap;
27 * Container class for file entry data.
29 * @see ClientPutComplexDir#addFileEntry(FileEntry)
30 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
33 public abstract class FileEntry {
35 /** The name of the file. */
36 protected final String name;
38 /** The upload source of the file. */
39 protected final UploadFrom uploadFrom;
42 * Creates a new file entry with the given name and upload source.
45 * The name of the file
47 * The upload source of the file
49 protected FileEntry(String name, UploadFrom uploadFrom) {
51 this.uploadFrom = uploadFrom;
55 * Creates a new file entry for a file that should be transmitted to the
56 * node in the payload of the message.
59 * The name of the file
61 * The content type of the file, or <code>null</code> to let
62 * the node auto-detect it
64 * The length of the file
65 * @param dataInputStream
66 * The input stream of the file
67 * @return A file entry
69 public static FileEntry createDirectFileEntry(String name, String contentType, long length, InputStream dataInputStream) {
70 return new DirectFileEntry(name, contentType, length, dataInputStream);
74 * Creates a new file entry for a file that should be uploaded from disk.
77 * The name of the file
79 * The name of the file on disk
81 * The content type of the file, or <code>null</code> to let
82 * the node auto-detect it
84 * The length of the file, or <code>-1</code> to not specify a
86 * @return A file entry
88 public static FileEntry createDiskFileEntry(String name, String filename, String contentType, long length) {
89 return new DiskFileEntry(name, filename, contentType, length);
93 * Creates a new file entry for a file that redirects to another URI.
96 * The name of the file
98 * The target URI of the redirect
99 * @return A file entry
101 public static FileEntry createRedirectFileEntry(String name, String targetURI) {
102 return new RedirectFileEntry(name, targetURI);
106 * Returns the fields for this file entry.
108 * @return The fields for this file entry
110 abstract Map<String, String> getFields();
113 * A file entry for a file that should be transmitted in the payload of the
114 * {@link ClientPutComplexDir} message.
116 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
119 static class DirectFileEntry extends FileEntry {
121 /** The content type of the data. */
122 private final String contentType;
124 /** The length of the data. */
125 private final long length;
127 /** The input stream of the data. */
128 private final InputStream inputStream;
131 * Creates a new direct file entry with content type auto-detection.
134 * The name of the file
136 * The length of the file
138 * The input stream of the file
140 public DirectFileEntry(String name, long length, InputStream inputStream) {
141 this(name, null, length, inputStream);
145 * Creates a new direct file entry.
148 * The name of the file
150 * The content type of the file, or <code>null</code> to
151 * let the node auto-detect it
153 * The length of the file
155 * The input stream of the file
157 public DirectFileEntry(String name, String contentType, long length, InputStream inputStream) {
158 super(name, UploadFrom.direct);
159 this.contentType = contentType;
160 this.length = length;
161 this.inputStream = inputStream;
168 Map<String, String> getFields() {
169 Map<String, String> fields = new HashMap<String, String>();
170 fields.put("Name", name);
171 fields.put("UploadFrom", String.valueOf(uploadFrom));
172 fields.put("DataLength", String.valueOf(length));
173 if (contentType != null) {
174 fields.put("Metadata.ContentType", contentType);
180 * Returns the input stream of the file.
182 * @return The input stream of the file
184 InputStream getInputStream() {
191 * A file entry for a file that should be uploaded from the disk.
193 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
196 static class DiskFileEntry extends FileEntry {
198 /** The name of the on-disk file. */
199 private final String filename;
201 /** The content type of the file. */
202 private final String contentType;
204 /** The length of the file. */
205 private final long length;
208 * Creates a new disk file entry.
211 * The name of the file
213 * The name of the on-disk file
215 * The length of the file
217 public DiskFileEntry(String name, String filename, long length) {
218 this(name, filename, null, length);
222 * Creates a new disk file entry.
225 * The name of the file
227 * The name of the on-disk file
229 * The content type of the file, or <code>null</code> to
230 * let the node auto-detect it
232 * The length of the file
234 public DiskFileEntry(String name, String filename, String contentType, long length) {
235 super(name, UploadFrom.disk);
236 this.filename = filename;
237 this.contentType = contentType;
238 this.length = length;
245 Map<String, String> getFields() {
246 Map<String, String> fields = new HashMap<String, String>();
247 fields.put("Name", name);
248 fields.put("UploadFrom", String.valueOf(uploadFrom));
249 fields.put("Filename", filename);
251 fields.put("DataSize", String.valueOf(length));
253 if (contentType != null) {
254 fields.put("Metadata.ContentType", contentType);
262 * A file entry for a file that redirects to another URI.
264 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
267 static class RedirectFileEntry extends FileEntry {
269 /** The target URI of the redirect. */
270 private String targetURI;
273 * Creates a new redirect file entry.
276 * The name of the file
278 * The target URI of the redirect
280 public RedirectFileEntry(String name, String targetURI) {
281 super(name, UploadFrom.redirect);
282 this.targetURI = targetURI;
289 Map<String, String> getFields() {
290 Map<String, String> fields = new HashMap<String, String>();
291 fields.put("Name", name);
292 fields.put("UploadFrom", String.valueOf(uploadFrom));
293 fields.put("TargetURI", targetURI);