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