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>
41 public class ClientPutComplexDir extends FcpMessage {
43 /** The index for added file entries. */
44 private int fileIndex = 0;
46 /** The input streams from {@link DirectFileEntry}s. */
47 private final List<InputStream> directFileInputStreams = new ArrayList<InputStream>();
50 * Creates a new “ClientPutComplexDir” with the given identifier and URI.
53 * The identifier of the request
55 * The URI to insert the directory to
57 public ClientPutComplexDir(String identifier, String uri) {
58 super("ClientPutComplexDir");
59 setField("Identifier", identifier);
64 * Sets the verbosity of the request.
67 * The verbosity of the request
69 public void setVerbosity(Verbosity verbosity) {
70 setField("Verbosity", String.valueOf(verbosity));
74 * Sets the maximum number of retries for failed blocks.
77 * The maximum number of retries for failed blocks, or
78 * <code>-1</code> to retry endlessly
80 public void setMaxRetries(int maxRetries) {
81 setField("MaxRetries", String.valueOf(maxRetries));
85 * Sets the priority of the request.
88 * The priority of the request
90 public void setPriority(Priority priority) {
91 setField("PriorityClass", String.valueOf(priority));
95 * Sets whether to generate the final URI only.
98 * <code>true</code> to generate the final CHK only,
99 * <code>false</code> to complete the insert
101 public void setGetCHKOnly(boolean getCHKOnly) {
102 setField("GetCHKOnly", String.valueOf(getCHKOnly));
106 * Sets whether the request is on the global queue.
109 * <code>true</code> to put the request on the global queue,
110 * <code>false</code> to put it on the client-local queue
112 public void setGlobal(boolean global) {
113 setField("Global", String.valueOf(global));
117 * Sets whether the node should not try to compress the data.
119 * @param dontCompress
120 * <code>true</code> to skip compression of the data,
121 * <code>false</code> to try and compress the data
123 public void setDontCompress(boolean dontCompress) {
124 setField("DontCompress", String.valueOf(dontCompress));
128 * Sets the client token of the request.
131 * The client token of the request
133 public void setClientToken(String clientToken) {
134 setField("ClientToken", clientToken);
138 * Sets the persistence of the request.
141 * The persistence of the request
143 public void setPersistence(Persistence persistence) {
144 setField("Persistence", String.valueOf(persistence));
148 * Sets the target filename of the request. This is useful for inserts that
149 * go to “CHK@” only and creates a manifest with a single file.
151 * @param targetFilename
152 * The target filename
154 public void setTargetFilename(String targetFilename) {
155 setField("TargetFilename", targetFilename);
159 * Sets whether to encode the complete data early to generate the
160 * {@link URIGenerated} message early.
163 * <code>true</code> to encode the complete data early,
164 * <code>false</code> otherwise
166 public void setEarlyEncode(boolean earlyEncode) {
167 setField("EarlyEncode", String.valueOf(earlyEncode));
171 * Sets the default name. This is the name of the file that should be shown
172 * if no file was specified.
177 public void setDefaultName(String defaultName) {
178 setField("DefaultName", defaultName);
182 * Adds an entry for a file.
185 * The file entry to add
187 public void addFileEntry(FileEntry fileEntry) {
188 Map<String, String> fields = fileEntry.getFields();
189 for (Entry<String, String> fieldEntry: fields.entrySet()) {
190 setField("Files." + fileIndex + "." + fieldEntry.getKey(), fieldEntry.getValue());
193 if (fileEntry instanceof FileEntry.DirectFileEntry) {
194 directFileInputStreams.add(((DirectFileEntry) fileEntry).getInputStream());
201 * Do not call this method to add input streams! The input streams, if any,
202 * will be taken directly from the {@link FileEntry}s and the stream you
203 * set here will be overridden!
206 public void setPayloadInputStream(InputStream payloadInputStream) {
214 public void write(OutputStream outputStream) throws IOException {
215 /* create payload stream. */
216 setPayloadInputStream(new SequenceInputStream(Collections.enumeration(directFileInputStreams)));
217 /* write out all the fields. */
218 super.write(outputStream);