ca9492712fea90ade89991a0e49a91c106e75470
[jFCPlib.git] / src / net / pterodactylus / fcp / GetFailed.java
1 /*
2  * jSite2 - GetFailed.java -
3  * Copyright © 2008 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 net.pterodactylus.fcp;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Map.Entry;
26
27 /**
28  * The “GetFailed” message signals the client that a {@link ClientGet} request
29  * has failed. This also means that no further progress messages for that
30  * request will be sent.
31  *
32  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
33  */
34 public class GetFailed extends BaseMessage {
35
36         /**
37          * Creates a new “GetFailed” message that wraps the received message.
38          *
39          * @param receivedMessage
40          *            The received message
41          */
42         GetFailed(FcpMessage receivedMessage) {
43                 super(receivedMessage);
44         }
45
46         /**
47          * Returns the code of the error.
48          *
49          * @return The code of the error, or <code>-1</code> if the error code could
50          *         not be parsed
51          */
52         public int getCode() {
53                 return FcpUtils.safeParseInt(getField("Code"));
54         }
55
56         /**
57          * Returns the identifier of the request.
58          *
59          * @return The identifier of the request
60          */
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 {@link #getExpectedDataLength()}
123          * and {@link #getExpectedMetadataContentType()}) have already been
124          * finalized and can be trusted. If the values have not been finalized that
125          * can change over time.
126          *
127          * @return <code>true</code> if the expected values have already been
128          *         finalized, <code>false</code> otherwise
129          */
130         public boolean isFinalizedExpected() {
131                 return Boolean.valueOf(getField("FinalizedExpected"));
132         }
133
134         /**
135          * Returns the URI the request is redirected to (in case of a request for a
136          * USK). This is returned so that client applications know that the URI of
137          * the key has updated.
138          *
139          * @return The URI the request was redirected to
140          */
141         public String getRedirectURI() {
142                 return getField("RedirectURI");
143         }
144
145         /**
146          * Returns whether the request failed fatally. If a request fails fatally it
147          * can never complete, even with inifinite retries.
148          *
149          * @return <code>true</code> if the request failed fatally,
150          *         <code>false</code> otherwise
151          */
152         public boolean isFatal() {
153                 return Boolean.valueOf(getField("Fatal"));
154         }
155
156         /**
157          * Returns a list of complex error codes with the message. Use
158          * {@link #getComplexErrorDescription(int)} and
159          * {@link #getComplexErrorCount(int)} to get details.
160          *
161          * @return A list of complex error codes
162          */
163         public int[] getComplexErrorCodes() {
164                 Map<String, String> allFields = getFields();
165                 List<Integer> errorCodeList = new ArrayList<Integer>();
166                 for (Entry<String, String> field : allFields.entrySet()) {
167                         String fieldKey = field.getKey();
168                         if (fieldKey.startsWith("Errors.")) {
169                                 int nextDot = fieldKey.indexOf('.', 7);
170                                 if (nextDot > -1) {
171                                         int errorCode = FcpUtils.safeParseInt(fieldKey.substring(7, nextDot));
172                                         if (errorCode != -1) {
173                                                 errorCodeList.add(errorCode);
174                                         }
175                                 }
176                         }
177                 }
178                 int[] errorCodes = new int[errorCodeList.size()];
179                 int errorIndex = 0;
180                 for (int errorCode : errorCodeList) {
181                         errorCodes[errorIndex++] = errorCode;
182                 }
183                 return errorCodes;
184         }
185
186         /**
187          * Returns the description of the complex error. You should only hand it
188          * error codes you got from {@link #getComplexErrorCodes()}!
189          *
190          * @param errorCode
191          *            The error code
192          * @return The description of the complex error
193          */
194         public String getComplexErrorDescription(int errorCode) {
195                 return getField("Errors." + errorCode + ".Description");
196         }
197
198         /**
199          * Returns the count of the complex error. You should only hand it error
200          * codes you got from {@link #getComplexErrorCodes()}!
201          *
202          * @param errorCode
203          *            The error code
204          * @return The count of the complex error, or <code>-1</code> if the count
205          *         could not be parsed
206          */
207         public int getComplexErrorCount(int errorCode) {
208                 return FcpUtils.safeParseInt(getField("Errors." + errorCode + ".Count"));
209         }
210
211 }