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 the request is on the global queue.
108 * <code>true</code> to put the request on the global queue,
109 * <code>false</code> to put it on the client-local queue
111 public void setGlobal(boolean global) {
112 setField("Global", String.valueOf(global));
116 * Sets whether the node should not try to compress the data.
118 * @param dontCompress
119 * <code>true</code> to skip compression of the data,
120 * <code>false</code> to try and compress the data
122 public void setDontCompress(boolean dontCompress) {
123 setField("DontCompress", String.valueOf(dontCompress));
127 * Sets the client token of the request.
130 * The client token of the request
132 public void setClientToken(String clientToken) {
133 setField("ClientToken", clientToken);
137 * Sets the persistence of the request.
140 * The persistence of the request
142 public void setPersistence(Persistence persistence) {
143 setField("Persistence", String.valueOf(persistence));
147 * Sets the target filename of the request. This is useful for inserts that
148 * go to “CHK@” only and creates a manifest with a single file.
150 * @param targetFilename
151 * The target filename
153 public void setTargetFilename(String targetFilename) {
154 setField("TargetFilename", targetFilename);
158 * Sets whether to encode the complete data early to generate the
159 * {@link URIGenerated} message early.
162 * <code>true</code> to encode the complete data early,
163 * <code>false</code> otherwise
165 public void setEarlyEncode(boolean earlyEncode) {
166 setField("EarlyEncode", String.valueOf(earlyEncode));
170 * Sets the default name. This is the name of the file that should be shown
171 * if no file was specified.
176 public void setDefaultName(String defaultName) {
177 setField("DefaultName", defaultName);
181 * Adds an entry for a file.
184 * The file entry to add
186 public void addFileEntry(FileEntry fileEntry) {
187 Map<String, String> fields = fileEntry.getFields();
188 for (Entry<String, String> fieldEntry : fields.entrySet()) {
189 setField("Files." + fileIndex + "." + fieldEntry.getKey(), fieldEntry.getValue());
192 if (fileEntry instanceof FileEntry.DirectFileEntry) {
193 directFileInputStreams.add(((DirectFileEntry) fileEntry).getInputStream());
200 * Do not call this method to add input streams! The input streams, if any,
201 * will be taken directly from the {@link FileEntry}s and the stream you set
202 * here will be overridden!
205 public void setPayloadInputStream(InputStream payloadInputStream) {
213 public void write(OutputStream outputStream) throws IOException {
214 /* create payload stream. */
215 setPayloadInputStream(new SequenceInputStream(Collections.enumeration(directFileInputStreams)));
216 /* write out all the fields. */
217 super.write(outputStream);