Update license to GPLv3, fix header comments
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / PutFailed.java
1 /*
2  * jFCPlib - PutFailed.java - Copyright © 2008–2016 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 3 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, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.fcp;
19
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Map.Entry;
24
25 /**
26  * The “PutFailed” message signals the client that a {@link ClientPut} request
27  * has failed. This also means that no further progress messages for that
28  * request will be sent.
29  *
30  * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
31  */
32 public class PutFailed extends BaseMessage implements Identifiable {
33
34         /**
35          * Creates a new “PutFailed” message that wraps the received message.
36          *
37          * @param receivedMessage
38          *            The received message
39          */
40         public PutFailed(FcpMessage receivedMessage) {
41                 super(receivedMessage);
42         }
43
44         /**
45          * Returns the code of the error.
46          *
47          * @return The code of the error, or <code>-1</code> if the error code
48          *         could not be parsed
49          */
50         public int getCode() {
51                 return FcpUtils.safeParseInt(getField("Code"));
52         }
53
54         /**
55          * Returns the identifier of the request.
56          *
57          * @return The identifier of the request
58          */
59         @Override
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 URI of the request.
103          *
104          * @return The expected URI
105          */
106         public String getExpectedURI() {
107                 return getField("ExpectedURI");
108         }
109
110         /**
111          * Returns whether the request failed fatally. If a request fails fatally
112          * it can never complete, even with inifinite retries.
113          *
114          * @return <code>true</code> if the request failed fatally,
115          *         <code>false</code> otherwise
116          */
117         public boolean isFatal() {
118                 return Boolean.valueOf(getField("Fatal"));
119         }
120
121         /**
122          * Returns a list of complex error codes with the message. Use
123          * {@link #getComplexErrorDescription(int)} and
124          * {@link #getComplexErrorCount(int)} to get details.
125          *
126          * @return A list of complex error codes
127          */
128         public int[] getComplexErrorCodes() {
129                 Map<String, String> allFields = getFields();
130                 List<Integer> errorCodeList = new ArrayList<Integer>();
131                 for (Entry<String, String> field : allFields.entrySet()) {
132                         String fieldKey = field.getKey();
133                         if (fieldKey.startsWith("Errors.")) {
134                                 int nextDot = fieldKey.indexOf('.', 7);
135                                 if (nextDot > -1) {
136                                         int errorCode = FcpUtils.safeParseInt(fieldKey.substring(7, nextDot));
137                                         if (errorCode != -1) {
138                                                 errorCodeList.add(errorCode);
139                                         }
140                                 }
141                         }
142                 }
143                 int[] errorCodes = new int[errorCodeList.size()];
144                 int errorIndex = 0;
145                 for (int errorCode : errorCodeList) {
146                         errorCodes[errorIndex++] = errorCode;
147                 }
148                 return errorCodes;
149         }
150
151         /**
152          * Returns the description of the complex error. You should only hand it
153          * error codes you got from {@link #getComplexErrorCodes()}!
154          *
155          * @param errorCode
156          *            The error code
157          * @return The description of the complex error
158          */
159         public String getComplexErrorDescription(int errorCode) {
160                 return getField("Errors." + errorCode + ".Description");
161         }
162
163         /**
164          * Returns the count of the complex error. You should only hand it error
165          * codes you got from {@link #getComplexErrorCodes()}!
166          *
167          * @param errorCode
168          *            The error code
169          * @return The count of the complex error, or <code>-1</code> if the count
170          *         could not be parsed
171          */
172         public int getComplexErrorCount(int errorCode) {
173                 return FcpUtils.safeParseInt(getField("Errors." + errorCode + ".Count"));
174         }
175
176 }