2 * jFCPlib - ClientPutComplexDir.java - Copyright © 2008–2016 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 3 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, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.fcp;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.io.SequenceInputStream;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
28 import java.util.Map.Entry;
30 import net.pterodactylus.fcp.FileEntry.DirectFileEntry;
33 * The “ClientPutComplexDir” lets you upload a directory with different sources
37 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
39 public class ClientPutComplexDir extends FcpMessage {
41 /** The index for added file entries. */
42 private int fileIndex = 0;
44 /** The input streams from {@link DirectFileEntry}s. */
45 private final List<InputStream> directFileInputStreams = new ArrayList<InputStream>();
48 * Creates a new “ClientPutComplexDir” with the given identifier and URI.
51 * The identifier of the request
53 * The URI to insert the directory to
55 public ClientPutComplexDir(String identifier, String uri) {
56 super("ClientPutComplexDir");
57 setField("Identifier", identifier);
62 * Sets the verbosity of the request.
65 * The verbosity of the request
67 public void setVerbosity(Verbosity verbosity) {
68 setField("Verbosity", String.valueOf(verbosity));
72 * Sets the maximum number of retries for failed blocks.
75 * The maximum number of retries for failed blocks, or
76 * <code>-1</code> to retry endlessly
78 public void setMaxRetries(int maxRetries) {
79 setField("MaxRetries", String.valueOf(maxRetries));
83 * Sets the priority of the request.
86 * The priority of the request
88 public void setPriority(Priority priority) {
89 setField("PriorityClass", String.valueOf(priority));
93 * Sets whether to generate the final URI only.
96 * <code>true</code> to generate the final CHK only,
97 * <code>false</code> to complete the insert
99 public void setGetCHKOnly(boolean getCHKOnly) {
100 setField("GetCHKOnly", String.valueOf(getCHKOnly));
104 * Sets whether an insert request should be forked when it is cached.
106 * @param forkOnCacheable
107 * {@code true} to fork the insert when it is cached,
108 * {@code false} otherwise
110 public void setForkOnCacheable(boolean forkOnCacheable) {
111 setField("ForkOnCacheable", String.valueOf(forkOnCacheable));
115 * Sets the number of additional inserts of single blocks.
117 * @param extraInsertsSingleBlock
118 * The number of additional inserts
120 public void setExtraInsertsSingleBlock(int extraInsertsSingleBlock) {
121 setField("ExtraInsertsSingleBlock", String.valueOf(extraInsertsSingleBlock));
125 * Sets the number of additional inserts of splitfile header blocks.
127 * @param extraInsertsSplitfileHeaderBlock
128 * The number of additional inserts
130 public void setExtraInsertsSplitfileHeaderBlock(int extraInsertsSplitfileHeaderBlock) {
131 setField("ExtraInsertsSplitfileHeaderBlock", String.valueOf(extraInsertsSplitfileHeaderBlock));
135 * Sets whether the request is on the global queue.
138 * <code>true</code> to put the request on the global queue,
139 * <code>false</code> to put it on the client-local queue
141 public void setGlobal(boolean global) {
142 setField("Global", String.valueOf(global));
146 * Sets whether the node should not try to compress the data.
148 * @param dontCompress
149 * <code>true</code> to skip compression of the data,
150 * <code>false</code> to try and compress the data
152 public void setDontCompress(boolean dontCompress) {
153 setField("DontCompress", String.valueOf(dontCompress));
157 * Sets the client token of the request.
160 * The client token of the request
162 public void setClientToken(String clientToken) {
163 setField("ClientToken", clientToken);
167 * Sets the persistence of the request.
170 * The persistence of the request
172 public void setPersistence(Persistence persistence) {
173 setField("Persistence", String.valueOf(persistence));
177 * Sets the target filename of the request. This is useful for inserts that
178 * go to “CHK@” only and creates a manifest with a single file.
180 * @param targetFilename
181 * The target filename
183 public void setTargetFilename(String targetFilename) {
184 setField("TargetFilename", targetFilename);
188 * Sets whether to encode the complete data early to generate the
189 * {@link URIGenerated} message early.
192 * <code>true</code> to encode the complete data early,
193 * <code>false</code> otherwise
195 public void setEarlyEncode(boolean earlyEncode) {
196 setField("EarlyEncode", String.valueOf(earlyEncode));
200 * Sets the default name. This is the name of the file that should be shown
201 * if no file was specified.
206 public void setDefaultName(String defaultName) {
207 setField("DefaultName", defaultName);
211 * Adds an entry for a file.
214 * The file entry to add
216 public void addFileEntry(FileEntry fileEntry) {
217 Map<String, String> fields = fileEntry.getFields();
218 for (Entry<String, String> fieldEntry : fields.entrySet()) {
219 setField("Files." + fileIndex + "." + fieldEntry.getKey(), fieldEntry.getValue());
222 if (fileEntry instanceof FileEntry.DirectFileEntry) {
223 directFileInputStreams.add(((DirectFileEntry) fileEntry).getInputStream());
230 * Do not call this method to add input streams! The input streams, if any,
231 * will be taken directly from the {@link FileEntry}s and the stream you
232 * set here will be overridden!
235 public void setPayloadInputStream(InputStream payloadInputStream) {
243 public void write(OutputStream outputStream) throws IOException {
244 /* create payload stream. */
245 setPayloadInputStream(new SequenceInputStream(Collections.enumeration(directFileInputStreams)));
246 /* write out all the fields. */
247 super.write(outputStream);