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