Update license to GPLv3, fix header comments
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / ClientPutComplexDir.java
1 /*
2  * jFCPlib - ClientPutComplexDir.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.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.io.SequenceInputStream;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29
30 import net.pterodactylus.fcp.FileEntry.DirectFileEntry;
31
32 /**
33  * The “ClientPutComplexDir” lets you upload a directory with different sources
34  * for each file.
35  *
36  * @see FileEntry
37  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
38  */
39 public class ClientPutComplexDir extends FcpMessage {
40
41         /** The index for added file entries. */
42         private int fileIndex = 0;
43
44         /** The input streams from {@link DirectFileEntry}s. */
45         private final List<InputStream> directFileInputStreams = new ArrayList<InputStream>();
46
47         /**
48          * Creates a new “ClientPutComplexDir” with the given identifier and URI.
49          *
50          * @param identifier
51          *            The identifier of the request
52          * @param uri
53          *            The URI to insert the directory to
54          */
55         public ClientPutComplexDir(String identifier, String uri) {
56                 super("ClientPutComplexDir");
57                 setField("Identifier", identifier);
58                 setField("URI", uri);
59         }
60
61         /**
62          * Sets the verbosity of the request.
63          *
64          * @param verbosity
65          *            The verbosity of the request
66          */
67         public void setVerbosity(Verbosity verbosity) {
68                 setField("Verbosity", String.valueOf(verbosity));
69         }
70
71         /**
72          * Sets the maximum number of retries for failed blocks.
73          *
74          * @param maxRetries
75          *            The maximum number of retries for failed blocks, or
76          *            <code>-1</code> to retry endlessly
77          */
78         public void setMaxRetries(int maxRetries) {
79                 setField("MaxRetries", String.valueOf(maxRetries));
80         }
81
82         /**
83          * Sets the priority of the request.
84          *
85          * @param priority
86          *            The priority of the request
87          */
88         public void setPriority(Priority priority) {
89                 setField("PriorityClass", String.valueOf(priority));
90         }
91
92         /**
93          * Sets whether to generate the final URI only.
94          *
95          * @param getCHKOnly
96          *            <code>true</code> to generate the final CHK only,
97          *            <code>false</code> to complete the insert
98          */
99         public void setGetCHKOnly(boolean getCHKOnly) {
100                 setField("GetCHKOnly", String.valueOf(getCHKOnly));
101         }
102
103         /**
104          * Sets whether an insert request should be forked when it is cached.
105          *
106          * @param forkOnCacheable
107          *            {@code true} to fork the insert when it is cached,
108          *            {@code false} otherwise
109          */
110         public void setForkOnCacheable(boolean forkOnCacheable) {
111                 setField("ForkOnCacheable", String.valueOf(forkOnCacheable));
112         }
113
114         /**
115          * Sets the number of additional inserts of single blocks.
116          *
117          * @param extraInsertsSingleBlock
118          *            The number of additional inserts
119          */
120         public void setExtraInsertsSingleBlock(int extraInsertsSingleBlock) {
121                 setField("ExtraInsertsSingleBlock", String.valueOf(extraInsertsSingleBlock));
122         }
123
124         /**
125          * Sets the number of additional inserts of splitfile header blocks.
126          *
127          * @param extraInsertsSplitfileHeaderBlock
128          *            The number of additional inserts
129          */
130         public void setExtraInsertsSplitfileHeaderBlock(int extraInsertsSplitfileHeaderBlock) {
131                 setField("ExtraInsertsSplitfileHeaderBlock", String.valueOf(extraInsertsSplitfileHeaderBlock));
132         }
133
134         /**
135          * Sets whether the request is on the global queue.
136          *
137          * @param global
138          *            <code>true</code> to put the request on the global queue,
139          *            <code>false</code> to put it on the client-local queue
140          */
141         public void setGlobal(boolean global) {
142                 setField("Global", String.valueOf(global));
143         }
144
145         /**
146          * Sets whether the node should not try to compress the data.
147          *
148          * @param dontCompress
149          *            <code>true</code> to skip compression of the data,
150          *            <code>false</code> to try and compress the data
151          */
152         public void setDontCompress(boolean dontCompress) {
153                 setField("DontCompress", String.valueOf(dontCompress));
154         }
155
156         /**
157          * Sets the client token of the request.
158          *
159          * @param clientToken
160          *            The client token of the request
161          */
162         public void setClientToken(String clientToken) {
163                 setField("ClientToken", clientToken);
164         }
165
166         /**
167          * Sets the persistence of the request.
168          *
169          * @param persistence
170          *            The persistence of the request
171          */
172         public void setPersistence(Persistence persistence) {
173                 setField("Persistence", String.valueOf(persistence));
174         }
175
176         /**
177          * Sets the target filename of the request. This is useful for inserts that
178          * go to “CHK@” only and creates a manifest with a single file.
179          *
180          * @param targetFilename
181          *            The target filename
182          */
183         public void setTargetFilename(String targetFilename) {
184                 setField("TargetFilename", targetFilename);
185         }
186
187         /**
188          * Sets whether to encode the complete data early to generate the
189          * {@link URIGenerated} message early.
190          *
191          * @param earlyEncode
192          *            <code>true</code> to encode the complete data early,
193          *            <code>false</code> otherwise
194          */
195         public void setEarlyEncode(boolean earlyEncode) {
196                 setField("EarlyEncode", String.valueOf(earlyEncode));
197         }
198
199         /**
200          * Sets the default name. This is the name of the file that should be shown
201          * if no file was specified.
202          *
203          * @param defaultName
204          *            The default name
205          */
206         public void setDefaultName(String defaultName) {
207                 setField("DefaultName", defaultName);
208         }
209
210         /**
211          * Adds an entry for a file.
212          *
213          * @param fileEntry
214          *            The file entry to add
215          */
216         public void addFileEntry(FileEntry fileEntry) {
217                 Map<String, String> fields = fileEntry.getFields();
218                 for (Entry<String, String> fieldEntry : fields.entrySet()) {
219                         setField("Files." + fileIndex + "." + fieldEntry.getKey(), fieldEntry.getValue());
220                 }
221                 fileIndex++;
222                 if (fileEntry instanceof FileEntry.DirectFileEntry) {
223                         directFileInputStreams.add(((DirectFileEntry) fileEntry).getInputStream());
224                 }
225         }
226
227         /**
228          * {@inheritDoc}
229          * <p>
230          * Do not call this method to add input streams! The input streams, if any,
231          * will be taken directly from the {@link FileEntry}s and the stream you
232          * set here will be overridden!
233          */
234         @Override
235         public void setPayloadInputStream(InputStream payloadInputStream) {
236                 /* do nothing. */
237         }
238
239         /**
240          * {@inheritDoc}
241          */
242         @Override
243         public void write(OutputStream outputStream) throws IOException {
244                 /* create payload stream. */
245                 setPayloadInputStream(new SequenceInputStream(Collections.enumeration(directFileInputStreams)));
246                 /* write out all the fields. */
247                 super.write(outputStream);
248         }
249
250 }