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