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