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