Reformat source code, new line length for comments (79), some trailing whitespace...
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / GetFailed.java
1 /*
2  * jFCPlib - GetFailed.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 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Map.Entry;
25
26 /**
27  * The “GetFailed” message signals the client that a {@link ClientGet} request
28  * has failed. This also means that no further progress messages for that
29  * request will be sent.
30  *
31  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
32  */
33 public class GetFailed extends BaseMessage implements Identifiable {
34
35         /**
36          * Creates a new “GetFailed” message that wraps the received message.
37          *
38          * @param receivedMessage
39          *            The received message
40          */
41         GetFailed(FcpMessage receivedMessage) {
42                 super(receivedMessage);
43         }
44
45         /**
46          * Returns the code of the error.
47          *
48          * @return The code of the error, or <code>-1</code> if the error code
49          *         could not be parsed
50          */
51         public int getCode() {
52                 return FcpUtils.safeParseInt(getField("Code"));
53         }
54
55         /**
56          * Returns the identifier of the request.
57          *
58          * @return The identifier of the request
59          */
60         @Override
61         public String getIdentifier() {
62                 return getField("Identifier");
63         }
64
65         /**
66          * Returns whether the request is on the global queue.
67          *
68          * @return <code>true</code> if the request is on the global queue,
69          *         <code>false</code> if it is on the client-local queue
70          */
71         public boolean isGlobal() {
72                 return Boolean.valueOf(getField("Global"));
73         }
74
75         /**
76          * Returns the description of the error code.
77          *
78          * @return The description of the error code
79          */
80         public String getCodeDescription() {
81                 return getField("CodeDescription");
82         }
83
84         /**
85          * Returns the extra description of the error.
86          *
87          * @return The extra description of the error
88          */
89         public String getExtraDescription() {
90                 return getField("ExtraDescription");
91         }
92
93         /**
94          * Returns the short description of the error.
95          *
96          * @return The short description of the error
97          */
98         public String getShortCodeDescription() {
99                 return getField("ShortCodeDescription");
100         }
101
102         /**
103          * Returns the expected data length, if already knows.
104          *
105          * @return The expected data length, or <code>-1</code> if the length could
106          *         not be parsed
107          */
108         public long getExpectedDataLength() {
109                 return FcpUtils.safeParseLong(getField("ExpectedDataLength"));
110         }
111
112         /**
113          * Returns the expected content type of the request.
114          *
115          * @return The expected content type
116          */
117         public String getExpectedMetadataContentType() {
118                 return getField("ExpectedMetadata.ContentType");
119         }
120
121         /**
122          * Returns whether the expected values (see
123          * {@link #getExpectedDataLength()} and
124          * {@link #getExpectedMetadataContentType()}) have already been finalized
125          * and can be trusted. If the values have not been finalized that can
126          * change over time.
127          *
128          * @return <code>true</code> if the expected values have already been
129          *         finalized, <code>false</code> otherwise
130          */
131         public boolean isFinalizedExpected() {
132                 return Boolean.valueOf(getField("FinalizedExpected"));
133         }
134
135         /**
136          * Returns the URI the request is redirected to (in case of a request for a
137          * USK). This is returned so that client applications know that the URI of
138          * the key has updated.
139          *
140          * @return The URI the request was redirected to
141          */
142         public String getRedirectURI() {
143                 return getField("RedirectURI");
144         }
145
146         /**
147          * Returns whether the request failed fatally. If a request fails fatally
148          * it can never complete, even with inifinite retries.
149          *
150          * @return <code>true</code> if the request failed fatally,
151          *         <code>false</code> otherwise
152          */
153         public boolean isFatal() {
154                 return Boolean.valueOf(getField("Fatal"));
155         }
156
157         /**
158          * Returns a list of complex error codes with the message. Use
159          * {@link #getComplexErrorDescription(int)} and
160          * {@link #getComplexErrorCount(int)} to get details.
161          *
162          * @return A list of complex error codes
163          */
164         public int[] getComplexErrorCodes() {
165                 Map<String, String> allFields = getFields();
166                 List<Integer> errorCodeList = new ArrayList<Integer>();
167                 for (Entry<String, String> field : allFields.entrySet()) {
168                         String fieldKey = field.getKey();
169                         if (fieldKey.startsWith("Errors.")) {
170                                 int nextDot = fieldKey.indexOf('.', 7);
171                                 if (nextDot > -1) {
172                                         int errorCode = FcpUtils.safeParseInt(fieldKey.substring(7, nextDot));
173                                         if (errorCode != -1) {
174                                                 errorCodeList.add(errorCode);
175                                         }
176                                 }
177                         }
178                 }
179                 int[] errorCodes = new int[errorCodeList.size()];
180                 int errorIndex = 0;
181                 for (int errorCode : errorCodeList) {
182                         errorCodes[errorIndex++] = errorCode;
183                 }
184                 return errorCodes;
185         }
186
187         /**
188          * Returns the description of the complex error. You should only hand it
189          * error codes you got from {@link #getComplexErrorCodes()}!
190          *
191          * @param errorCode
192          *            The error code
193          * @return The description of the complex error
194          */
195         public String getComplexErrorDescription(int errorCode) {
196                 return getField("Errors." + errorCode + ".Description");
197         }
198
199         /**
200          * Returns the count of the complex error. You should only hand it error
201          * codes you got from {@link #getComplexErrorCodes()}!
202          *
203          * @param errorCode
204          *            The error code
205          * @return The count of the complex error, or <code>-1</code> if the count
206          *         could not be parsed
207          */
208         public int getComplexErrorCount(int errorCode) {
209                 return FcpUtils.safeParseInt(getField("Errors." + errorCode + ".Count"));
210         }
211
212 }