c01bf2bd8080c154bbaac160889ffd013866f675
[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 {
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 could
49          *         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         public String getIdentifier() {
61                 return getField("Identifier");
62         }
63
64         /**
65          * Returns whether the request is on the global queue.
66          *
67          * @return <code>true</code> if the request is on the global queue,
68          *         <code>false</code> if it is on the client-local queue
69          */
70         public boolean isGlobal() {
71                 return Boolean.valueOf(getField("Global"));
72         }
73
74         /**
75          * Returns the description of the error code.
76          *
77          * @return The description of the error code
78          */
79         public String getCodeDescription() {
80                 return getField("CodeDescription");
81         }
82
83         /**
84          * Returns the extra description of the error.
85          *
86          * @return The extra description of the error
87          */
88         public String getExtraDescription() {
89                 return getField("ExtraDescription");
90         }
91
92         /**
93          * Returns the short description of the error.
94          *
95          * @return The short description of the error
96          */
97         public String getShortCodeDescription() {
98                 return getField("ShortCodeDescription");
99         }
100
101         /**
102          * Returns the expected data length, if already knows.
103          *
104          * @return The expected data length, or <code>-1</code> if the length could
105          *         not be parsed
106          */
107         public long getExpectedDataLength() {
108                 return FcpUtils.safeParseLong(getField("ExpectedDataLength"));
109         }
110
111         /**
112          * Returns the expected content type of the request.
113          *
114          * @return The expected content type
115          */
116         public String getExpectedMetadataContentType() {
117                 return getField("ExpectedMetadata.ContentType");
118         }
119
120         /**
121          * Returns whether the expected values (see {@link #getExpectedDataLength()}
122          * and {@link #getExpectedMetadataContentType()}) have already been
123          * finalized and can be trusted. If the values have not been finalized that
124          * can change over time.
125          *
126          * @return <code>true</code> if the expected values have already been
127          *         finalized, <code>false</code> otherwise
128          */
129         public boolean isFinalizedExpected() {
130                 return Boolean.valueOf(getField("FinalizedExpected"));
131         }
132
133         /**
134          * Returns the URI the request is redirected to (in case of a request for a
135          * USK). This is returned so that client applications know that the URI of
136          * the key has updated.
137          *
138          * @return The URI the request was redirected to
139          */
140         public String getRedirectURI() {
141                 return getField("RedirectURI");
142         }
143
144         /**
145          * Returns whether the request failed fatally. If a request fails fatally it
146          * can never complete, even with inifinite retries.
147          *
148          * @return <code>true</code> if the request failed fatally,
149          *         <code>false</code> otherwise
150          */
151         public boolean isFatal() {
152                 return Boolean.valueOf(getField("Fatal"));
153         }
154
155         /**
156          * Returns a list of complex error codes with the message. Use
157          * {@link #getComplexErrorDescription(int)} and
158          * {@link #getComplexErrorCount(int)} to get details.
159          *
160          * @return A list of complex error codes
161          */
162         public int[] getComplexErrorCodes() {
163                 Map<String, String> allFields = getFields();
164                 List<Integer> errorCodeList = new ArrayList<Integer>();
165                 for (Entry<String, String> field : allFields.entrySet()) {
166                         String fieldKey = field.getKey();
167                         if (fieldKey.startsWith("Errors.")) {
168                                 int nextDot = fieldKey.indexOf('.', 7);
169                                 if (nextDot > -1) {
170                                         int errorCode = FcpUtils.safeParseInt(fieldKey.substring(7, nextDot));
171                                         if (errorCode != -1) {
172                                                 errorCodeList.add(errorCode);
173                                         }
174                                 }
175                         }
176                 }
177                 int[] errorCodes = new int[errorCodeList.size()];
178                 int errorIndex = 0;
179                 for (int errorCode : errorCodeList) {
180                         errorCodes[errorIndex++] = errorCode;
181                 }
182                 return errorCodes;
183         }
184
185         /**
186          * Returns the description of the complex error. You should only hand it
187          * error codes you got from {@link #getComplexErrorCodes()}!
188          *
189          * @param errorCode
190          *            The error code
191          * @return The description of the complex error
192          */
193         public String getComplexErrorDescription(int errorCode) {
194                 return getField("Errors." + errorCode + ".Description");
195         }
196
197         /**
198          * Returns the count of the complex error. You should only hand it error
199          * codes you got from {@link #getComplexErrorCodes()}!
200          *
201          * @param errorCode
202          *            The error code
203          * @return The count of the complex error, or <code>-1</code> if the count
204          *         could not be parsed
205          */
206         public int getComplexErrorCount(int errorCode) {
207                 return FcpUtils.safeParseInt(getField("Errors." + errorCode + ".Count"));
208         }
209
210 }