99931736e7d794c8e381250ad240111c89f1cfe1
[jSite.git] / src / main / java / de / todesbaum / util / freenet / fcp2 / ClientPut.java
1 /*
2  * jSite - ClientPut.java - Copyright © 2006–2012 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 de.todesbaum.util.freenet.fcp2;
20
21 import java.io.IOException;
22 import java.io.Writer;
23
24 /**
25  * Abstract base class for all put requests. It contains all parameters that put
26  * requests have in common.
27  *
28  * @author David Roden <droden@gmail.com>
29  * @version $Id$
30  */
31 public abstract class ClientPut extends Command {
32
33         /** The URI of this request. */
34         protected final String uri;
35
36         /** The client token of this request. */
37         protected String clientToken = null;
38
39         /** Whether this request should only create a CHK. */
40         protected boolean getCHKOnly = false;
41
42         /** Whether this request is a global request. */
43         protected boolean global = false;
44
45         /** Whether the node should not try to compress the file. */
46         protected boolean dontCompress = false;
47
48         /** The maximum number of retries of this command. */
49         protected int maxRetries = 0;
50
51         /** Whether to generate the keys early. */
52         protected boolean earlyEncode = false;
53
54         /** The persistence of this request. */
55         protected Persistence persistence = Persistence.CONNECTION;
56
57         /** The priority class of this request. */
58         protected PriorityClass priorityClass = PriorityClass.INTERACTIVE;
59
60         /** The verbosiry of this request. */
61         protected Verbosity verbosity = Verbosity.NONE;
62
63         /**
64          * Creates a new put request with the specified name, identifier and URI.
65          *
66          * @param name
67          *            The name of this request
68          * @param identifier
69          *            The identifier of this request
70          * @param uri
71          *            The URI of this request
72          */
73         protected ClientPut(String name, String identifier, String uri) {
74                 super(name, identifier);
75                 this.uri = uri;
76         }
77
78         /**
79          * Returns whether the node should not try to compress the data.
80          *
81          * @return <code>true</code> if the node should <strong>not</strong> try
82          *         to compress the data
83          */
84         public boolean isDontCompress() {
85                 return dontCompress;
86         }
87
88         /**
89          * Sets whether the node should not try to compress the data. A client might
90          * set this hint on data that is clearly not compressible, like MPEG audio
91          * files, JPEG or PNG images, highly compressed movies, or compressed
92          * archives like ZIP files. Otherwise the node will try to compress the file
93          * which -- depending on the size of the data -- might take a lot of time
94          * and memory.
95          *
96          * @param dontCompress
97          *            <code>true</code> if the node should <strong>not</strong>
98          *            try to compress the data
99          */
100         public void setDontCompress(boolean dontCompress) {
101                 this.dontCompress = dontCompress;
102         }
103
104         /**
105          * Returns whether this request should only return the CHK of the data.
106          * @return Whether this request should only return the CHK of the data
107          */
108         public boolean isGetCHKOnly() {
109                 return getCHKOnly;
110         }
111
112         /**
113          * Sets whether this request should only return the CHK of the data.
114          * @param getCHKOnly
115          *            <code>true</code> if this request should only return the CHK of the data
116          */
117         public void setGetCHKOnly(boolean getCHKOnly) {
118                 this.getCHKOnly = getCHKOnly;
119         }
120
121         /**
122          * Returns whether this request is a global request.
123          * @return <code>true</code> if this request is a global request, <code>false</code> otherwise
124          */
125         public boolean isGlobal() {
126                 return global;
127         }
128
129         /**
130          * Sets whether this request is a global request.
131          * @param global
132          *            <code>true</code> if this request is a global request, <code>false</code> otherwise
133          */
134         public void setGlobal(boolean global) {
135                 this.global = global;
136         }
137
138         /**
139          * Returns the maximum number of retries of this request.
140          * @return The maximum number of retries of this request
141          */
142         public int getMaxRetries() {
143                 return maxRetries;
144         }
145
146         /**
147          * Sets the maximum number of retries of this request
148          * @param maxRetries
149          *            The maximum number of retries of this request
150          */
151         public void setMaxRetries(int maxRetries) {
152                 this.maxRetries = maxRetries;
153         }
154
155         /**
156          * Returns whether the data should be encoded early to generate the final
157          * key as fast as possible.
158          *
159          * @return {@code true} if the key should be generated early, {@code false}
160          *         otherwise
161          */
162         public boolean isEarlyEncode() {
163                 return earlyEncode;
164         }
165
166         /**
167          * Sets whether the data should be encoded early to generate the final key
168          * as fast as possible.
169          *
170          * @param earlyEncode
171          *            {@code true} if the key should be generated early, {@code
172          *            false} otherwise
173          */
174         public void setEarlyEncode(boolean earlyEncode) {
175                 this.earlyEncode = earlyEncode;
176         }
177
178         /**
179          * Returns the priority class of this request.
180          * @return The priority class of this request
181          */
182         public PriorityClass getPriorityClass() {
183                 return priorityClass;
184         }
185
186         /**
187          * Sets the priority class of this request.
188          * @param priorityClass
189          *            The priority class of this request
190          */
191         public void setPriorityClass(PriorityClass priorityClass) {
192                 this.priorityClass = priorityClass;
193         }
194
195         /**
196          * Returns the verbosity of this request.
197          * @return The verbosity of this request
198          */
199         public Verbosity getVerbosity() {
200                 return verbosity;
201         }
202
203         /**
204          * Sets the verbosity of this request.
205          * @param verbosity
206          *            The verbosity of this request
207          */
208         public void setVerbosity(Verbosity verbosity) {
209                 this.verbosity = verbosity;
210         }
211
212         /**
213          * Returns the URI of this request
214          * @return The URI of this request.
215          */
216         public String getUri() {
217                 return uri;
218         }
219
220         /**
221          * {@inheritDoc}
222          */
223         @Override
224         protected void write(Writer writer) throws IOException {
225                 super.write(writer);
226                 writer.write("URI=" + uri + LINEFEED);
227                 if (verbosity != null)
228                         writer.write("Verbosity=" + verbosity.getValue() + LINEFEED);
229                 if (maxRetries != 0)
230                         writer.write("MaxRetries=" + maxRetries + LINEFEED);
231                 writer.write("EarlyEncode=" + earlyEncode);
232                 if (priorityClass != null)
233                         writer.write("PriorityClass=" + priorityClass.getValue() + LINEFEED);
234                 writer.write("GetCHKOnly=" + getCHKOnly + LINEFEED);
235                 writer.write("Global=" + global + LINEFEED);
236                 writer.write("DontCompress=" + dontCompress + LINEFEED);
237                 if (clientToken != null)
238                         writer.write("ClientToken=" + clientToken + LINEFEED);
239                 if (persistence != null)
240                         writer.write("Persistence=" + persistence.getName() + LINEFEED);
241         }
242
243 }