Add “fork on cacheable” flag to all ClientPut* requests.
[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, {@code
109          *            false} otherwise
110          */
111         public void setForkOnCacheable(boolean forkOnCacheable) {
112                 setField("ForkOnCacheable", String.valueOf(forkOnCacheable));
113         }
114
115         /**
116          * Sets whether the request is on the global queue.
117          *
118          * @param global
119          *            <code>true</code> to put the request on the global queue,
120          *            <code>false</code> to put it on the client-local queue
121          */
122         public void setGlobal(boolean global) {
123                 setField("Global", String.valueOf(global));
124         }
125
126         /**
127          * Sets whether the node should not try to compress the data.
128          *
129          * @param dontCompress
130          *            <code>true</code> to skip compression of the data,
131          *            <code>false</code> to try and compress the data
132          */
133         public void setDontCompress(boolean dontCompress) {
134                 setField("DontCompress", String.valueOf(dontCompress));
135         }
136
137         /**
138          * Sets the client token of the request.
139          *
140          * @param clientToken
141          *            The client token of the request
142          */
143         public void setClientToken(String clientToken) {
144                 setField("ClientToken", clientToken);
145         }
146
147         /**
148          * Sets the persistence of the request.
149          *
150          * @param persistence
151          *            The persistence of the request
152          */
153         public void setPersistence(Persistence persistence) {
154                 setField("Persistence", String.valueOf(persistence));
155         }
156
157         /**
158          * Sets the target filename of the request. This is useful for inserts that
159          * go to “CHK@” only and creates a manifest with a single file.
160          *
161          * @param targetFilename
162          *            The target filename
163          */
164         public void setTargetFilename(String targetFilename) {
165                 setField("TargetFilename", targetFilename);
166         }
167
168         /**
169          * Sets whether to encode the complete data early to generate the
170          * {@link URIGenerated} message early.
171          *
172          * @param earlyEncode
173          *            <code>true</code> to encode the complete data early,
174          *            <code>false</code> otherwise
175          */
176         public void setEarlyEncode(boolean earlyEncode) {
177                 setField("EarlyEncode", String.valueOf(earlyEncode));
178         }
179
180         /**
181          * Sets the default name. This is the name of the file that should be shown
182          * if no file was specified.
183          *
184          * @param defaultName
185          *            The default name
186          */
187         public void setDefaultName(String defaultName) {
188                 setField("DefaultName", defaultName);
189         }
190
191         /**
192          * Adds an entry for a file.
193          *
194          * @param fileEntry
195          *            The file entry to add
196          */
197         public void addFileEntry(FileEntry fileEntry) {
198                 Map<String, String> fields = fileEntry.getFields();
199                 for (Entry<String, String> fieldEntry : fields.entrySet()) {
200                         setField("Files." + fileIndex + "." + fieldEntry.getKey(), fieldEntry.getValue());
201                 }
202                 fileIndex++;
203                 if (fileEntry instanceof FileEntry.DirectFileEntry) {
204                         directFileInputStreams.add(((DirectFileEntry) fileEntry).getInputStream());
205                 }
206         }
207
208         /**
209          * {@inheritDoc}
210          * <p>
211          * Do not call this method to add input streams! The input streams, if any,
212          * will be taken directly from the {@link FileEntry}s and the stream you set
213          * here will be overridden!
214          */
215         @Override
216         public void setPayloadInputStream(InputStream payloadInputStream) {
217                 /* do nothing. */
218         }
219
220         /**
221          * {@inheritDoc}
222          */
223         @Override
224         public void write(OutputStream outputStream) throws IOException {
225                 /* create payload stream. */
226                 setPayloadInputStream(new SequenceInputStream(Collections.enumeration(directFileInputStreams)));
227                 /* write out all the fields. */
228                 super.write(outputStream);
229         }
230
231 }