cd14e96df979430cb223ae28cb2692834b52f2af
[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  * @version $Id$
34  */
35 public class GetFailed extends BaseMessage {
36
37         /**
38          * Creates a new “GetFailed” message that wraps the received message.
39          * 
40          * @param receivedMessage
41          *            The received message
42          */
43         GetFailed(FcpMessage receivedMessage) {
44                 super(receivedMessage);
45         }
46
47         /**
48          * Returns the code of the error.
49          * 
50          * @return The code of the error, or <code>-1</code> if the error code
51          *         could not be parsed
52          */
53         public int getCode() {
54                 return FcpUtils.safeParseInt(getField("Code"));
55         }
56
57         /**
58          * Returns the identifier of the request.
59          * 
60          * @return The identifier of the request
61          */
62         public String getIdentifier() {
63                 return getField("Identifier");
64         }
65
66         /**
67          * Returns whether the request is on the global queue.
68          * 
69          * @return <code>true</code> if the request is on the global queue,
70          *         <code>false</code> if it is on the client-local queue
71          */
72         public boolean isGlobal() {
73                 return Boolean.valueOf(getField("Global"));
74         }
75
76         /**
77          * Returns the description of the error code.
78          * 
79          * @return The description of the error code
80          */
81         public String getCodeDescription() {
82                 return getField("CodeDescription");
83         }
84
85         /**
86          * Returns the extra description of the error.
87          * 
88          * @return The extra description of the error
89          */
90         public String getExtraDescription() {
91                 return getField("ExtraDescription");
92         }
93
94         /**
95          * Returns the short description of the error.
96          * 
97          * @return The short description of the error
98          */
99         public String getShortCodeDescription() {
100                 return getField("ShortCodeDescription");
101         }
102
103         /**
104          * Returns the expected data length, if already knows.
105          * 
106          * @return The expected data length, or <code>-1</code> if the length
107          *         could not be parsed
108          */
109         public long getExpectedDataLength() {
110                 return FcpUtils.safeParseLong(getField("ExpectedDataLength"));
111         }
112
113         /**
114          * Returns the expected content type of the request.
115          * 
116          * @return The expected content type
117          */
118         public String getExpectedMetadataContentType() {
119                 return getField("ExpectedMetadata.ContentType");
120         }
121
122         /**
123          * Returns whether the expected values (see {@link #getExpectedDataLength()}
124          * and {@link #getExpectedMetadataContentType()}) have already been
125          * finalized and can be trusted. If the values have not been finalized that
126          * can 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 a list of complex error codes with the message. Use
148          * {@link #getComplexErrorDescription(int)} and
149          * {@link #getComplexErrorCount(int)} to get details.
150          * 
151          * @return A list of complex error codes
152          */
153         public int[] getComplexErrorCodes() {
154                 Map<String, String> allFields = getFields();
155                 List<Integer> errorCodeList = new ArrayList<Integer>();
156                 for (Entry<String, String> field: allFields.entrySet()) {
157                         String fieldKey = field.getKey();
158                         if (fieldKey.startsWith("Errors.")) {
159                                 int nextDot = fieldKey.indexOf('.', 7);
160                                 if (nextDot > -1) {
161                                         int errorCode = FcpUtils.safeParseInt(fieldKey.substring(7, nextDot));
162                                         if (errorCode != -1) {
163                                                 errorCodeList.add(errorCode);
164                                         }
165                                 }
166                         }
167                 }
168                 int[] errorCodes = new int[errorCodeList.size()];
169                 int errorIndex = 0;
170                 for (int errorCode: errorCodeList) {
171                         errorCodes[errorIndex++] = errorCode;
172                 }
173                 return errorCodes;
174         }
175
176         /**
177          * Returns the description of the complex error. You should only hand it
178          * error codes you got from {@link #getComplexErrorCodes()}!
179          * 
180          * @param errorCode
181          *            The error code
182          * @return The description of the complex error
183          */
184         public String getComplexErrorDescription(int errorCode) {
185                 return getField("Errors." + errorCode + ".Description");
186         }
187
188         /**
189          * Returns the count of the complex error. You should only hand it error
190          * codes you got from {@link #getComplexErrorCodes()}!
191          * 
192          * @param errorCode
193          *            The error code
194          * @return The count of the complex error, or <code>-1</code> if the count
195          *         could not be parsed
196          */
197         public int getComplexErrorCount(int errorCode) {
198                 return FcpUtils.safeParseInt(getField("Errors." + errorCode + ".Count"));
199         }
200
201 }