956020389abb78e51c55f124d293d30a973caec6
[jFCPlib.git] / src / net / pterodactylus / fcp / ClientPutComplexDir.java
1 /*
2  * jSite2 - ClientPutComplexDir.java -
3  * Copyright © 2008 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package net.pterodactylus.fcp;
21
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;
29 import java.util.Map;
30 import java.util.Map.Entry;
31
32 import net.pterodactylus.fcp.FileEntry.DirectFileEntry;
33
34 /**
35  * The “ClientPutComplexDir” lets you upload a directory with different sources
36  * for each file.
37  * 
38  * @see FileEntry
39  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
40  */
41 public class ClientPutComplexDir extends FcpMessage {
42
43         /** The index for added file entries. */
44         private int fileIndex = 0;
45
46         /** The input streams from {@link DirectFileEntry}s. */
47         private final List<InputStream> directFileInputStreams = new ArrayList<InputStream>();
48
49         /**
50          * Creates a new “ClientPutComplexDir” with the given identifier and URI.
51          * 
52          * @param identifier
53          *            The identifier of the request
54          * @param uri
55          *            The URI to insert the directory to
56          */
57         public ClientPutComplexDir(String identifier, String uri) {
58                 super("ClientPutComplexDir");
59                 setField("Identifier", identifier);
60                 setField("URI", uri);
61         }
62
63         /**
64          * Sets the verbosity of the request.
65          * 
66          * @param verbosity
67          *            The verbosity of the request
68          */
69         public void setVerbosity(Verbosity verbosity) {
70                 setField("Verbosity", String.valueOf(verbosity));
71         }
72
73         /**
74          * Sets the maximum number of retries for failed blocks.
75          * 
76          * @param maxRetries
77          *            The maximum number of retries for failed blocks, or
78          *            <code>-1</code> to retry endlessly
79          */
80         public void setMaxRetries(int maxRetries) {
81                 setField("MaxRetries", String.valueOf(maxRetries));
82         }
83
84         /**
85          * Sets the priority of the request.
86          * 
87          * @param priority
88          *            The priority of the request
89          */
90         public void setPriority(Priority priority) {
91                 setField("PriorityClass", String.valueOf(priority));
92         }
93
94         /**
95          * Sets whether to generate the final URI only.
96          * 
97          * @param getCHKOnly
98          *            <code>true</code> to generate the final CHK only,
99          *            <code>false</code> to complete the insert
100          */
101         public void setGetCHKOnly(boolean getCHKOnly) {
102                 setField("GetCHKOnly", String.valueOf(getCHKOnly));
103         }
104
105         /**
106          * Sets whether the request is on the global queue.
107          * 
108          * @param global
109          *            <code>true</code> to put the request on the global queue,
110          *            <code>false</code> to put it on the client-local queue
111          */
112         public void setGlobal(boolean global) {
113                 setField("Global", String.valueOf(global));
114         }
115
116         /**
117          * Sets whether the node should not try to compress the data.
118          * 
119          * @param dontCompress
120          *            <code>true</code> to skip compression of the data,
121          *            <code>false</code> to try and compress the data
122          */
123         public void setDontCompress(boolean dontCompress) {
124                 setField("DontCompress", String.valueOf(dontCompress));
125         }
126
127         /**
128          * Sets the client token of the request.
129          * 
130          * @param clientToken
131          *            The client token of the request
132          */
133         public void setClientToken(String clientToken) {
134                 setField("ClientToken", clientToken);
135         }
136
137         /**
138          * Sets the persistence of the request.
139          * 
140          * @param persistence
141          *            The persistence of the request
142          */
143         public void setPersistence(Persistence persistence) {
144                 setField("Persistence", String.valueOf(persistence));
145         }
146
147         /**
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.
150          * 
151          * @param targetFilename
152          *            The target filename
153          */
154         public void setTargetFilename(String targetFilename) {
155                 setField("TargetFilename", targetFilename);
156         }
157
158         /**
159          * Sets whether to encode the complete data early to generate the
160          * {@link URIGenerated} message early.
161          * 
162          * @param earlyEncode
163          *            <code>true</code> to encode the complete data early,
164          *            <code>false</code> otherwise
165          */
166         public void setEarlyEncode(boolean earlyEncode) {
167                 setField("EarlyEncode", String.valueOf(earlyEncode));
168         }
169
170         /**
171          * Sets the default name. This is the name of the file that should be shown
172          * if no file was specified.
173          * 
174          * @param defaultName
175          *            The default name
176          */
177         public void setDefaultName(String defaultName) {
178                 setField("DefaultName", defaultName);
179         }
180
181         /**
182          * Adds an entry for a file.
183          * 
184          * @param fileEntry
185          *            The file entry to add
186          */
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());
191                 }
192                 fileIndex++;
193                 if (fileEntry instanceof FileEntry.DirectFileEntry) {
194                         directFileInputStreams.add(((DirectFileEntry) fileEntry).getInputStream());
195                 }
196         }
197
198         /**
199          * {@inheritDoc}
200          * <p>
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!
204          */
205         @Override
206         public void setPayloadInputStream(InputStream payloadInputStream) {
207                 /* do nothing. */
208         }
209
210         /**
211          * {@inheritDoc}
212          */
213         @Override
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);
219         }
220
221 }