47c3d8d0da19d9bb18bfb500b020235e6997e694
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / PersistentPutDir.java
1 /*
2  * jFCPlib - PersistentPutDir.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 “PersistentPutDir” is the response to a {@link ClientPutDiskDir} message.
23  * It is also sent as a possible response to a {@link ListPersistentRequests}
24  * message.
25  *
26  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
27  */
28 public class PersistentPutDir extends BaseMessage {
29
30         /**
31          * Creates a new “PersistentPutDir” message that wraps the received message.
32          *
33          * @param receivedMessage
34          *            The received message
35          */
36         PersistentPutDir(FcpMessage receivedMessage) {
37                 super(receivedMessage);
38         }
39
40         /**
41          * Returns the identifier of the request.
42          *
43          * @return The identifier of the request
44          */
45         public String getIdentifier() {
46                 return getField("Identifier");
47         }
48
49         /**
50          * Returns the URI of the request.
51          *
52          * @return The URI of the request
53          */
54         public String getURI() {
55                 return getField("URI");
56         }
57
58         /**
59          * Returns the verbosity of the request.
60          *
61          * @return The verbosity of the request
62          */
63         public Verbosity getVerbosity() {
64                 return Verbosity.valueOf(getField("Verbosity"));
65         }
66
67         /**
68          * Returns the priority of the request.
69          *
70          * @return The priority of the request
71          */
72         public Priority getPriority() {
73                 return Priority.values()[FcpUtils.safeParseInt(getField("PriorityClass"), Priority.unknown.ordinal())];
74         }
75
76         /**
77          * Returns whether the request is on the global queue.
78          *
79          * @return <code>true</code> if the request is on the global queue,
80          *         <code>false</code> if it is on the client-local queue
81          */
82         public boolean isGlobal() {
83                 return Boolean.valueOf(getField("Global"));
84         }
85
86         /**
87          * Returns the maximum number of retries for failed blocks.
88          *
89          * @return The maximum number of retries, or <code>-1</code> for endless
90          *         retries, or <code>-2</code> if the number could not be parsed
91          */
92         public int getMaxRetries() {
93                 return FcpUtils.safeParseInt(getField("MaxRetries"), -2);
94         }
95
96         /**
97          * Returns the number of files in the request.
98          *
99          * @return The number of files in the request
100          */
101         public int getFileCount() {
102                 int fileCount = -1;
103                 while (getField("Files." + ++fileCount + ".UploadFrom") != null) {
104                         /* do nothing. */
105                 }
106                 return fileCount;
107         }
108
109         /**
110          * Returns the name of the file at the given index. The index is counted
111          * from <code>0</code>.
112          *
113          * @param fileIndex
114          *            The index of the file
115          * @return The name of the file at the given index
116          */
117         public String getFileName(int fileIndex) {
118                 return getField("Files." + fileIndex + ".Name");
119         }
120
121         /**
122          * Returns the length of the file at the given index. The index is counted
123          * from <code>0</code>.
124          *
125          * @param fileIndex
126          *            The index of the file
127          * @return The length of the file at the given index
128          */
129         public long getFileDataLength(int fileIndex) {
130                 return FcpUtils.safeParseLong(getField("Files." + fileIndex + ".DataLength"));
131         }
132
133         /**
134          * Returns the upload source of the file at the given index. The index is
135          * counted from <code>0</code>.
136          *
137          * @param fileIndex
138          *            The index of the file
139          * @return The upload source of the file at the given index
140          */
141         public UploadFrom getFileUploadFrom(int fileIndex) {
142                 return UploadFrom.valueOf(getField("Files." + fileIndex + ".UploadFrom"));
143         }
144
145         /**
146          * Returns the content type of the file at the given index. The index is
147          * counted from <code>0</code>.
148          *
149          * @param fileIndex
150          *            The index of the file
151          * @return The content type of the file at the given index
152          */
153         public String getFileMetadataContentType(int fileIndex) {
154                 return getField("Files." + fileIndex + ".Metadata.ContentType");
155         }
156
157         /**
158          * Returns the filename of the file at the given index. This value is only
159          * returned if {@link #getFileUploadFrom(int)} is returning
160          * {@link UploadFrom#disk}. The index is counted from <code>0</code>.
161          *
162          * @param fileIndex
163          *            The index of the file
164          * @return The filename of the file at the given index
165          */
166         public String getFileFilename(int fileIndex) {
167                 return getField("Files." + fileIndex + ".Filename");
168         }
169
170 }