809146ae18439d710db9180a44bc0521bd1c4115
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / ClientPut.java
1 /*
2  * jFCPlib - ClientPut.java - Copyright © 2008 David Roden
3  *
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 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package net.pterodactylus.fcp;
20
21 /**
22  * A “ClientPut” requests inserts a single file into freenet, either uploading
23  * it directly with this messge ({@link UploadFrom#direct}), uploading it from
24  * disk ({@link UploadFrom#disk}) or by creating a redirect to another URI (
25  * {@link UploadFrom#redirect}).
26  *
27  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
28  */
29 public class ClientPut extends FcpMessage {
30
31         /**
32          * Creates a new “ClientPut” message that inserts a file to the given URI.
33          * The file data <em>has</em> to be supplied to this message using
34          * {@link #setPayloadInputStream(java.io.InputStream)}! Using this
35          * constructor is the same as using
36          * {@link #ClientPut(String, String, UploadFrom)} with
37          * {@link UploadFrom#direct} as third parameter.
38          *
39          * @param uri
40          *            The URI to insert the file to
41          * @param identifier
42          *            The identifier of the request
43          */
44         public ClientPut(String uri, String identifier) {
45                 this(uri, identifier, UploadFrom.direct);
46         }
47
48         /**
49          * Creates a new “ClientPut” message that inserts a file to the given URI.
50          * Depending on <code>uploadFrom</code> the file data has to be supplied in
51          * different ways: If <code>uploadFrom</code> is {@link UploadFrom#direct},
52          * use {@link #setPayloadInputStream(java.io.InputStream)} to supply the
53          * input data. If <code>uploadFrom</code> is {@link UploadFrom#disk}, use
54          * {@link #setFilename(String)} to supply the file to upload. You have to
55          * test your direct-disk access (see {@link TestDDARequest},
56          * {@link TestDDAReply}, {@link TestDDAResponse}, {@link TestDDAComplete})
57          * before using this option! If <code>uploadFrom</code> is
58          * {@link UploadFrom#redirect}, use {@link #setTargetURI(String)} to set the
59          * target URI of the redirect.
60          *
61          * @param uri
62          *            The URI to insert to
63          * @param identifier
64          *            The identifier of the insert
65          * @param uploadFrom
66          *            The source of the upload
67          */
68         public ClientPut(String uri, String identifier, UploadFrom uploadFrom) {
69                 super("ClientPut");
70                 setField("URI", uri);
71                 setField("Identifier", identifier);
72                 setField("UploadFrom", String.valueOf(uploadFrom));
73         }
74
75         /**
76          * The MIME type of the content.
77          *
78          * @param metadataContentType
79          *            The MIME type of the content
80          */
81         public void setMetadataContentType(String metadataContentType) {
82                 setField("Metadata.ContentType", metadataContentType);
83         }
84
85         /**
86          * The verbosity of the request. Depending on this parameter you will
87          * received only the bare minimum of messages for the request (i.e. “it
88          * completed”) or a whole lot more.
89          *
90          * @see Verbosity
91          * @param verbosity
92          *            The verbosity of the request
93          */
94         public void setVerbosity(Verbosity verbosity) {
95                 setField("Verbosity", String.valueOf(verbosity));
96         }
97
98         /**
99          * The number of retries for a request if the initial try failed.
100          *
101          * @param maxRetries
102          *            The maximum number of retries after failure, or
103          *            <code>-1</code> to retry forever.
104          */
105         public void setMaxRetries(int maxRetries) {
106                 setField("MaxRetries", String.valueOf(maxRetries));
107         }
108
109         /**
110          * Sets the priority of the request.
111          *
112          * @param priority
113          *            The priority of the request
114          */
115         public void setPriority(Priority priority) {
116                 setField("PriorityClass", String.valueOf(priority));
117         }
118
119         /**
120          * Determines whether the node should really insert the data or generate the
121          * final CHK only.
122          *
123          * @param getCHKOnly
124          *            <code>true</code> to generate the final CHK only,
125          *            <code>false</code> to really insert the data
126          */
127         public void setGetCHKOnly(boolean getCHKOnly) {
128                 setField("GetCHKOnly", String.valueOf(getCHKOnly));
129         }
130
131         /**
132          * Sets whether an insert request should be forked when it is cached.
133          *
134          * @param forkOnCacheable
135          *            {@code true} to fork the insert when it is cached, {@code
136          *            false} otherwise
137          */
138         public void setForkOnCacheable(boolean forkOnCacheable) {
139                 setField("ForkOnCacheable", String.valueOf(forkOnCacheable));
140         }
141
142         /**
143          * Determines whether this request appears on the global queue.
144          *
145          * @param global
146          *            <code>true</code> to put the request on the global queue,
147          *            <code>false</code> for the client-local queue.
148          */
149         public void setGlobal(boolean global) {
150                 setField("Global", String.valueOf(global));
151         }
152
153         /**
154          * Determines whether the node should skip compression because the file has
155          * already been compressed.
156          *
157          * @param dontCompress
158          *            <code>true</code> to skip compression of the data in the node,
159          *            <code>false</code> to allow compression
160          */
161         public void setDontCompress(boolean dontCompress) {
162                 setField("DontCompress", String.valueOf(dontCompress));
163         }
164
165         /**
166          * Sets an optional client token. This client token is mentioned in progress
167          * and other request-related messages and can be used to identify this
168          * request.
169          *
170          * @param clientToken
171          *            The client token
172          */
173         public void setClientToken(String clientToken) {
174                 setField("ClientToken", clientToken);
175         }
176
177         /**
178          * Sets the persistence of this request.
179          *
180          * @param persistence
181          *            The persistence of this request
182          */
183         public void setPersistence(Persistence persistence) {
184                 setField("Persistence", String.valueOf(persistence));
185         }
186
187         /**
188          * Sets the target filename of the inserted file. This value is ignored for
189          * all inserts that do not have “CHK@” as a target.
190          *
191          * @param targetFilename
192          *            The filename of the target
193          */
194         public void setTargetFilename(String targetFilename) {
195                 setField("TargetFilename", targetFilename);
196         }
197
198         /**
199          * Determines whether to encode the complete file early in the life of the
200          * request.
201          *
202          * @param earlyEncode
203          *            <code>true</code> to generate the final key long before the
204          *            file is completely fetchable
205          */
206         public void setEarlyEncode(boolean earlyEncode) {
207                 setField("EarlyEncode", String.valueOf(earlyEncode));
208         }
209
210         /**
211          * Sets the length of the data that will be transferred after this message
212          * if <code>uploadFrom</code> is {@link UploadFrom#direct} is used.
213          *
214          * @param dataLength
215          *            The length of the data
216          */
217         public void setDataLength(long dataLength) {
218                 setField("DataLength", String.valueOf(dataLength));
219         }
220
221         /**
222          * Sets the name of the file to upload the data from.
223          *
224          * @param filename
225          *            The filename to upload
226          */
227         public void setFilename(String filename) {
228                 setField("Filename", filename);
229         }
230
231         /**
232          * If <code>uploadFrom</code> is {@link UploadFrom#redirect}, use this
233          * method to determine that target of the redirect.
234          *
235          * @param targetURI
236          *            The target URI to redirect to
237          */
238         public void setTargetURI(String targetURI) {
239                 setField("TargetURI", targetURI);
240         }
241
242 }