2 * jFCPlib - ClientPutComplexDir.java - Copyright © 2008 David Roden
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.
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.
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.
19 package net.pterodactylus.fcp;
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;
29 import java.util.Map.Entry;
31 import net.pterodactylus.fcp.FileEntry.DirectFileEntry;
34 * The “ClientPutComplexDir” lets you upload a directory with different sources
38 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
40 public class ClientPutComplexDir extends FcpMessage {
42 /** The index for added file entries. */
43 private int fileIndex = 0;
45 /** The input streams from {@link DirectFileEntry}s. */
46 private final List<InputStream> directFileInputStreams = new ArrayList<InputStream>();
49 * Creates a new “ClientPutComplexDir” with the given identifier and URI.
52 * The identifier of the request
54 * The URI to insert the directory to
56 public ClientPutComplexDir(String identifier, String uri) {
57 super("ClientPutComplexDir");
58 setField("Identifier", identifier);
63 * Sets the verbosity of the request.
66 * The verbosity of the request
68 public void setVerbosity(Verbosity verbosity) {
69 setField("Verbosity", String.valueOf(verbosity));
73 * Sets the maximum number of retries for failed blocks.
76 * The maximum number of retries for failed blocks, or
77 * <code>-1</code> to retry endlessly
79 public void setMaxRetries(int maxRetries) {
80 setField("MaxRetries", String.valueOf(maxRetries));
84 * Sets the priority of the request.
87 * The priority of the request
89 public void setPriority(Priority priority) {
90 setField("PriorityClass", String.valueOf(priority));
94 * Sets whether to generate the final URI only.
97 * <code>true</code> to generate the final CHK only,
98 * <code>false</code> to complete the insert
100 public void setGetCHKOnly(boolean getCHKOnly) {
101 setField("GetCHKOnly", String.valueOf(getCHKOnly));
105 * Sets whether an insert request should be forked when it is cached.
107 * @param forkOnCacheable
108 * {@code true} to fork the insert when it is cached,
109 * {@code false} otherwise
111 public void setForkOnCacheable(boolean forkOnCacheable) {
112 setField("ForkOnCacheable", String.valueOf(forkOnCacheable));
116 * Sets the number of additional inserts of single blocks.
118 * @param extraInsertsSingleBlock
119 * The number of additional inserts
121 public void setExtraInsertsSingleBlock(int extraInsertsSingleBlock) {
122 setField("ExtraInsertsSingleBlock", String.valueOf(extraInsertsSingleBlock));
126 * Sets the number of additional inserts of splitfile header blocks.
128 * @param extraInsertsSplitfileHeaderBlock
129 * The number of additional inserts
131 public void setExtraInsertsSplitfileHeaderBlock(int extraInsertsSplitfileHeaderBlock) {
132 setField("ExtraInsertsSplitfileHeaderBlock", String.valueOf(extraInsertsSplitfileHeaderBlock));
136 * Sets whether the request is on the global queue.
139 * <code>true</code> to put the request on the global queue,
140 * <code>false</code> to put it on the client-local queue
142 public void setGlobal(boolean global) {
143 setField("Global", String.valueOf(global));
147 * Sets whether the node should not try to compress the data.
149 * @param dontCompress
150 * <code>true</code> to skip compression of the data,
151 * <code>false</code> to try and compress the data
153 public void setDontCompress(boolean dontCompress) {
154 setField("DontCompress", String.valueOf(dontCompress));
158 * Sets the client token of the request.
161 * The client token of the request
163 public void setClientToken(String clientToken) {
164 setField("ClientToken", clientToken);
168 * Sets the persistence of the request.
171 * The persistence of the request
173 public void setPersistence(Persistence persistence) {
174 setField("Persistence", String.valueOf(persistence));
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.
181 * @param targetFilename
182 * The target filename
184 public void setTargetFilename(String targetFilename) {
185 setField("TargetFilename", targetFilename);
189 * Sets whether to encode the complete data early to generate the
190 * {@link URIGenerated} message early.
193 * <code>true</code> to encode the complete data early,
194 * <code>false</code> otherwise
196 public void setEarlyEncode(boolean earlyEncode) {
197 setField("EarlyEncode", String.valueOf(earlyEncode));
201 * Sets the default name. This is the name of the file that should be shown
202 * if no file was specified.
207 public void setDefaultName(String defaultName) {
208 setField("DefaultName", defaultName);
212 * Adds an entry for a file.
215 * The file entry to add
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());
223 if (fileEntry instanceof FileEntry.DirectFileEntry) {
224 directFileInputStreams.add(((DirectFileEntry) fileEntry).getInputStream());
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!
236 public void setPayloadInputStream(InputStream payloadInputStream) {
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);