rename fcplib to jFCPlib
[jFCPlib.git] / src / net / pterodactylus / fcp / FileEntry.java
1 /*
2  * jSite2 - FileEntry.java -
3  * Copyright © 2008 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package net.pterodactylus.fcp;
21
22 import java.io.InputStream;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 /**
27  * Container class for file entry data.
28  * 
29  * @see ClientPutComplexDir#addFileEntry(FileEntry)
30  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
31  * @version $Id$
32  */
33 public abstract class FileEntry {
34
35         /** The name of the file. */
36         protected final String name;
37
38         /** The upload source of the file. */
39         protected final UploadFrom uploadFrom;
40
41         /**
42          * Creates a new file entry with the given name and upload source.
43          * 
44          * @param name
45          *            The name of the file
46          * @param uploadFrom
47          *            The upload source of the file
48          */
49         protected FileEntry(String name, UploadFrom uploadFrom) {
50                 this.name = name;
51                 this.uploadFrom = uploadFrom;
52         }
53
54         /**
55          * Creates a new file entry for a file that should be transmitted to the
56          * node in the payload of the message.
57          * 
58          * @param name
59          *            The name of the file
60          * @param contentType
61          *            The content type of the file, or <code>null</code> to let
62          *            the node auto-detect it
63          * @param length
64          *            The length of the file
65          * @param dataInputStream
66          *            The input stream of the file
67          * @return A file entry
68          */
69         public static FileEntry createDirectFileEntry(String name, String contentType, long length, InputStream dataInputStream) {
70                 return new DirectFileEntry(name, contentType, length, dataInputStream);
71         }
72
73         /**
74          * Creates a new file entry for a file that should be uploaded from disk.
75          * 
76          * @param name
77          *            The name of the file
78          * @param filename
79          *            The name of the file on disk
80          * @param contentType
81          *            The content type of the file, or <code>null</code> to let
82          *            the node auto-detect it
83          * @param length
84          *            The length of the file, or <code>-1</code> to not specify a
85          *            size
86          * @return A file entry
87          */
88         public static FileEntry createDiskFileEntry(String name, String filename, String contentType, long length) {
89                 return new DiskFileEntry(name, filename, contentType, length);
90         }
91
92         /**
93          * Creates a new file entry for a file that redirects to another URI.
94          * 
95          * @param name
96          *            The name of the file
97          * @param targetURI
98          *            The target URI of the redirect
99          * @return A file entry
100          */
101         public static FileEntry createRedirectFileEntry(String name, String targetURI) {
102                 return new RedirectFileEntry(name, targetURI);
103         }
104
105         /**
106          * Returns the fields for this file entry.
107          * 
108          * @return The fields for this file entry
109          */
110         abstract Map<String, String> getFields();
111
112         /**
113          * A file entry for a file that should be transmitted in the payload of the
114          * {@link ClientPutComplexDir} message.
115          * 
116          * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
117          * @version $Id$
118          */
119         static class DirectFileEntry extends FileEntry {
120
121                 /** The content type of the data. */
122                 private final String contentType;
123
124                 /** The length of the data. */
125                 private final long length;
126
127                 /** The input stream of the data. */
128                 private final InputStream inputStream;
129
130                 /**
131                  * Creates a new direct file entry with content type auto-detection.
132                  * 
133                  * @param name
134                  *            The name of the file
135                  * @param length
136                  *            The length of the file
137                  * @param inputStream
138                  *            The input stream of the file
139                  */
140                 public DirectFileEntry(String name, long length, InputStream inputStream) {
141                         this(name, null, length, inputStream);
142                 }
143
144                 /**
145                  * Creates a new direct file entry.
146                  * 
147                  * @param name
148                  *            The name of the file
149                  * @param contentType
150                  *            The content type of the file, or <code>null</code> to
151                  *            let the node auto-detect it
152                  * @param length
153                  *            The length of the file
154                  * @param inputStream
155                  *            The input stream of the file
156                  */
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;
162                 }
163
164                 /**
165                  * {@inheritDoc}
166                  */
167                 @Override
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);
175                         }
176                         return fields;
177                 }
178
179                 /**
180                  * Returns the input stream of the file.
181                  * 
182                  * @return The input stream of the file
183                  */
184                 InputStream getInputStream() {
185                         return inputStream;
186                 }
187
188         }
189
190         /**
191          * A file entry for a file that should be uploaded from the disk.
192          * 
193          * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
194          * @version $Id$
195          */
196         static class DiskFileEntry extends FileEntry {
197
198                 /** The name of the on-disk file. */
199                 private final String filename;
200
201                 /** The content type of the file. */
202                 private final String contentType;
203
204                 /** The length of the file. */
205                 private final long length;
206
207                 /**
208                  * Creates a new disk file entry.
209                  * 
210                  * @param name
211                  *            The name of the file
212                  * @param filename
213                  *            The name of the on-disk file
214                  * @param length
215                  *            The length of the file
216                  */
217                 public DiskFileEntry(String name, String filename, long length) {
218                         this(name, filename, null, length);
219                 }
220
221                 /**
222                  * Creates a new disk file entry.
223                  * 
224                  * @param name
225                  *            The name of the file
226                  * @param filename
227                  *            The name of the on-disk file
228                  * @param contentType
229                  *            The content type of the file, or <code>null</code> to
230                  *            let the node auto-detect it
231                  * @param length
232                  *            The length of the file
233                  */
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;
239                 }
240
241                 /**
242                  * {@inheritDoc}
243                  */
244                 @Override
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);
250                         if (length > -1) {
251                                 fields.put("DataSize", String.valueOf(length));
252                         }
253                         if (contentType != null) {
254                                 fields.put("Metadata.ContentType", contentType);
255                         }
256                         return fields;
257                 }
258
259         }
260
261         /**
262          * A file entry for a file that redirects to another URI.
263          * 
264          * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
265          * @version $Id$
266          */
267         static class RedirectFileEntry extends FileEntry {
268
269                 /** The target URI of the redirect. */
270                 private String targetURI;
271
272                 /**
273                  * Creates a new redirect file entry.
274                  * 
275                  * @param name
276                  *            The name of the file
277                  * @param targetURI
278                  *            The target URI of the redirect
279                  */
280                 public RedirectFileEntry(String name, String targetURI) {
281                         super(name, UploadFrom.redirect);
282                         this.targetURI = targetURI;
283                 }
284
285                 /**
286                  * {@inheritDoc}
287                  */
288                 @Override
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);
294                         return fields;
295                 }
296
297         }
298
299 }