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>
32 public abstract class FileEntry {
34 /** The name of the file. */
35 protected final String name;
37 /** The upload source of the file. */
38 protected final UploadFrom uploadFrom;
41 * Creates a new file entry with the given name and upload source.
44 * The name of the file
46 * The upload source of the file
48 protected FileEntry(String name, UploadFrom uploadFrom) {
50 this.uploadFrom = uploadFrom;
54 * Creates a new file entry for a file that should be transmitted to the
55 * node in the payload of the message.
58 * The name of the file
60 * The content type of the file, or <code>null</code> to let
61 * the node auto-detect it
63 * The length of the file
64 * @param dataInputStream
65 * The input stream of the file
66 * @return A file entry
68 public static FileEntry createDirectFileEntry(String name, String contentType, long length, InputStream dataInputStream) {
69 return new DirectFileEntry(name, contentType, length, dataInputStream);
73 * Creates a new file entry for a file that should be uploaded from disk.
76 * The name of the file
78 * The name of the file on disk
80 * The content type of the file, or <code>null</code> to let
81 * the node auto-detect it
83 * The length of the file, or <code>-1</code> to not specify a
85 * @return A file entry
87 public static FileEntry createDiskFileEntry(String name, String filename, String contentType, long length) {
88 return new DiskFileEntry(name, filename, contentType, length);
92 * Creates a new file entry for a file that redirects to another URI.
95 * The name of the file
97 * The target URI of the redirect
98 * @return A file entry
100 public static FileEntry createRedirectFileEntry(String name, String targetURI) {
101 return new RedirectFileEntry(name, targetURI);
105 * Returns the fields for this file entry.
107 * @return The fields for this file entry
109 abstract Map<String, String> getFields();
112 * A file entry for a file that should be transmitted in the payload of the
113 * {@link ClientPutComplexDir} message.
115 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
117 static class DirectFileEntry extends FileEntry {
119 /** The content type of the data. */
120 private final String contentType;
122 /** The length of the data. */
123 private final long length;
125 /** The input stream of the data. */
126 private final InputStream inputStream;
129 * Creates a new direct file entry with content type auto-detection.
132 * The name of the file
134 * The length of the file
136 * The input stream of the file
138 public DirectFileEntry(String name, long length, InputStream inputStream) {
139 this(name, null, length, inputStream);
143 * Creates a new direct file entry.
146 * The name of the file
148 * The content type of the file, or <code>null</code> to
149 * let the node auto-detect it
151 * The length of the file
153 * The input stream of the file
155 public DirectFileEntry(String name, String contentType, long length, InputStream inputStream) {
156 super(name, UploadFrom.direct);
157 this.contentType = contentType;
158 this.length = length;
159 this.inputStream = inputStream;
166 Map<String, String> getFields() {
167 Map<String, String> fields = new HashMap<String, String>();
168 fields.put("Name", name);
169 fields.put("UploadFrom", String.valueOf(uploadFrom));
170 fields.put("DataLength", String.valueOf(length));
171 if (contentType != null) {
172 fields.put("Metadata.ContentType", contentType);
178 * Returns the input stream of the file.
180 * @return The input stream of the file
182 InputStream getInputStream() {
189 * A file entry for a file that should be uploaded from the disk.
191 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
193 static class DiskFileEntry extends FileEntry {
195 /** The name of the on-disk file. */
196 private final String filename;
198 /** The content type of the file. */
199 private final String contentType;
201 /** The length of the file. */
202 private final long length;
205 * Creates a new disk file entry.
208 * The name of the file
210 * The name of the on-disk file
212 * The length of the file
214 public DiskFileEntry(String name, String filename, long length) {
215 this(name, filename, null, length);
219 * Creates a new disk file entry.
222 * The name of the file
224 * The name of the on-disk file
226 * The content type of the file, or <code>null</code> to
227 * let the node auto-detect it
229 * The length of the file
231 public DiskFileEntry(String name, String filename, String contentType, long length) {
232 super(name, UploadFrom.disk);
233 this.filename = filename;
234 this.contentType = contentType;
235 this.length = length;
242 Map<String, String> getFields() {
243 Map<String, String> fields = new HashMap<String, String>();
244 fields.put("Name", name);
245 fields.put("UploadFrom", String.valueOf(uploadFrom));
246 fields.put("Filename", filename);
248 fields.put("DataSize", String.valueOf(length));
250 if (contentType != null) {
251 fields.put("Metadata.ContentType", contentType);
259 * A file entry for a file that redirects to another URI.
261 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
263 static class RedirectFileEntry extends FileEntry {
265 /** The target URI of the redirect. */
266 private String targetURI;
269 * Creates a new redirect file entry.
272 * The name of the file
274 * The target URI of the redirect
276 public RedirectFileEntry(String name, String targetURI) {
277 super(name, UploadFrom.redirect);
278 this.targetURI = targetURI;
285 Map<String, String> getFields() {
286 Map<String, String> fields = new HashMap<String, String>();
287 fields.put("Name", name);
288 fields.put("UploadFrom", String.valueOf(uploadFrom));
289 fields.put("TargetURI", targetURI);