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