2 * jSite2 - ClientPutComplexDir.java -
3 * Copyright © 2008 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.fcp;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.io.SequenceInputStream;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
30 import java.util.Map.Entry;
32 import net.pterodactylus.fcp.FileEntry.DirectFileEntry;
35 * The “ClientPutComplexDir” lets you upload a directory with different sources
39 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
42 public class ClientPutComplexDir extends FcpMessage {
44 /** The index for added file entries. */
45 private int fileIndex = 0;
47 /** The input streams from {@link DirectFileEntry}s. */
48 private final List<InputStream> directFileInputStreams = new ArrayList<InputStream>();
51 * Creates a new “ClientPutComplexDir” with the given identifier and URI.
54 * The identifier of the request
56 * The URI to insert the directory to
58 public ClientPutComplexDir(String identifier, String uri) {
59 super("ClientPutComplexDir");
60 setField("Identifier", identifier);
65 * Sets the verbosity of the request.
68 * The verbosity of the request
70 public void setVerbosity(Verbosity verbosity) {
71 setField("Verbosity", String.valueOf(verbosity));
75 * Sets the maximum number of retries for failed blocks.
78 * The maximum number of retries for failed blocks, or
79 * <code>-1</code> to retry endlessly
81 public void setMaxRetries(int maxRetries) {
82 setField("MaxRetries", String.valueOf(maxRetries));
86 * Sets the priority of the request.
89 * The priority of the request
91 public void setPriority(Priority priority) {
92 setField("PriorityClass", String.valueOf(priority));
96 * Sets whether to generate the final URI only.
99 * <code>true</code> to generate the final CHK only,
100 * <code>false</code> to complete the insert
102 public void setGetCHKOnly(boolean getCHKOnly) {
103 setField("GetCHKOnly", String.valueOf(getCHKOnly));
107 * Sets whether the request is on the global queue.
110 * <code>true</code> to put the request on the global queue,
111 * <code>false</code> to put it on the client-local queue
113 public void setGlobal(boolean global) {
114 setField("Global", String.valueOf(global));
118 * Sets whether the node should not try to compress the data.
120 * @param dontCompress
121 * <code>true</code> to skip compression of the data,
122 * <code>false</code> to try and compress the data
124 public void setDontCompress(boolean dontCompress) {
125 setField("DontCompress", String.valueOf(dontCompress));
129 * Sets the client token of the request.
132 * The client token of the request
134 public void setClientToken(String clientToken) {
135 setField("ClientToken", clientToken);
139 * Sets the persistence of the request.
142 * The persistence of the request
144 public void setPersistence(Persistence persistence) {
145 setField("Persistence", String.valueOf(persistence));
149 * Sets the target filename of the request. This is useful for inserts that
150 * go to “CHK@” only and creates a manifest with a single file.
152 * @param targetFilename
153 * The target filename
155 public void setTargetFilename(String targetFilename) {
156 setField("TargetFilename", targetFilename);
160 * Sets whether to encode the complete data early to generate the
161 * {@link URIGenerated} message early.
164 * <code>true</code> to encode the complete data early,
165 * <code>false</code> otherwise
167 public void setEarlyEncode(boolean earlyEncode) {
168 setField("EarlyEncode", String.valueOf(earlyEncode));
172 * Sets the default name. This is the name of the file that should be shown
173 * if no file was specified.
178 public void setDefaultName(String defaultName) {
179 setField("DefaultName", defaultName);
183 * Adds an entry for a file.
186 * The file entry to add
188 public void addFileEntry(FileEntry fileEntry) {
189 Map<String, String> fields = fileEntry.getFields();
190 for (Entry<String, String> fieldEntry: fields.entrySet()) {
191 setField("Files." + fileIndex + "." + fieldEntry.getKey(), fieldEntry.getValue());
194 if (fileEntry instanceof FileEntry.DirectFileEntry) {
195 directFileInputStreams.add(((DirectFileEntry) fileEntry).getInputStream());
202 * Do not call this method to add input streams! The input streams, if any,
203 * will be taken directly from the {@link FileEntry}s and the stream you
204 * set here will be overridden!
207 public void setPayloadInputStream(InputStream payloadInputStream) {
215 public void write(OutputStream outputStream) throws IOException {
216 /* create payload stream. */
217 setPayloadInputStream(new SequenceInputStream(Collections.enumeration(directFileInputStreams)));
218 /* write out all the fields. */
219 super.write(outputStream);