2 * jFCPlib - GetFailed.java - Copyright © 2008–2016 David Roden
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 3 of the License, or
7 * (at your option) any later version.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.fcp;
20 import java.util.ArrayList;
21 import java.util.List;
23 import java.util.Map.Entry;
26 * The “GetFailed” message signals the client that a {@link ClientGet} request
27 * has failed. This also means that no further progress messages for that
28 * request will be sent.
30 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
32 public class GetFailed extends BaseMessage implements Identifiable {
35 * Creates a new “GetFailed” message that wraps the received message.
37 * @param receivedMessage
38 * The received message
40 public GetFailed(FcpMessage receivedMessage) {
41 super(receivedMessage);
45 * Returns the code of the error.
47 * @return The code of the error, or <code>-1</code> if the error code
50 public int getCode() {
51 return FcpUtils.safeParseInt(getField("Code"));
55 * Returns the identifier of the request.
57 * @return The identifier of the request
60 public String getIdentifier() {
61 return getField("Identifier");
65 * Returns whether the request is on the global queue.
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
70 public boolean isGlobal() {
71 return Boolean.valueOf(getField("Global"));
75 * Returns the description of the error code.
77 * @return The description of the error code
79 public String getCodeDescription() {
80 return getField("CodeDescription");
84 * Returns the extra description of the error.
86 * @return The extra description of the error
88 public String getExtraDescription() {
89 return getField("ExtraDescription");
93 * Returns the short description of the error.
95 * @return The short description of the error
97 public String getShortCodeDescription() {
98 return getField("ShortCodeDescription");
102 * Returns the expected data length, if already knows.
104 * @return The expected data length, or <code>-1</code> if the length could
107 public long getExpectedDataLength() {
108 return FcpUtils.safeParseLong(getField("ExpectedDataLength"));
112 * Returns the expected content type of the request.
114 * @return The expected content type
116 public String getExpectedMetadataContentType() {
117 return getField("ExpectedMetadata.ContentType");
121 * Returns whether the expected values (see
122 * {@link #getExpectedDataLength()} and
123 * {@link #getExpectedMetadataContentType()}) have already been finalized
124 * and can be trusted. If the values have not been finalized that can
127 * @return <code>true</code> if the expected values have already been
128 * finalized, <code>false</code> otherwise
130 public boolean isFinalizedExpected() {
131 return Boolean.valueOf(getField("FinalizedExpected"));
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.
139 * @return The URI the request was redirected to
141 public String getRedirectURI() {
142 return getField("RedirectURI");
146 * Returns whether the request failed fatally. If a request fails fatally
147 * it can never complete, even with inifinite retries.
149 * @return <code>true</code> if the request failed fatally,
150 * <code>false</code> otherwise
152 public boolean isFatal() {
153 return Boolean.valueOf(getField("Fatal"));
157 * Returns a list of complex error codes with the message. Use
158 * {@link #getComplexErrorDescription(int)} and
159 * {@link #getComplexErrorCount(int)} to get details.
161 * @return A list of complex error codes
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);
171 int errorCode = FcpUtils.safeParseInt(fieldKey.substring(7, nextDot));
172 if (errorCode != -1) {
173 errorCodeList.add(errorCode);
178 int[] errorCodes = new int[errorCodeList.size()];
180 for (int errorCode : errorCodeList) {
181 errorCodes[errorIndex++] = errorCode;
187 * Returns the description of the complex error. You should only hand it
188 * error codes you got from {@link #getComplexErrorCodes()}!
192 * @return The description of the complex error
194 public String getComplexErrorDescription(int errorCode) {
195 return getField("Errors." + errorCode + ".Description");
199 * Returns the count of the complex error. You should only hand it error
200 * codes you got from {@link #getComplexErrorCodes()}!
204 * @return The count of the complex error, or <code>-1</code> if the count
205 * could not be parsed
207 public int getComplexErrorCount(int errorCode) {
208 return FcpUtils.safeParseInt(getField("Errors." + errorCode + ".Count"));