View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.openr66.context;
21  
22  import com.fasterxml.jackson.annotation.JsonValue;
23  import org.waarp.openr66.protocol.configuration.Messages;
24  
25  /**
26   * This enum class keeps all code that will be returned into the result and
27   * store (char representation) into
28   * the runner.
29   */
30  public enum ErrorCode {
31    /**
32     * Code stands for initialization ok (internal connection, authentication)
33     */
34    InitOk('i'),
35    /**
36     * Code stands for pre processing ok
37     */
38    PreProcessingOk('B'),
39    /**
40     * Code stands for transfer OK
41     */
42    TransferOk('X'),
43    /**
44     * Code stands for post processing ok
45     */
46    PostProcessingOk('P'),
47    /**
48     * Code stands for All action are completed ok
49     */
50    CompleteOk('O'),
51    /**
52     * Code stands for connection is impossible (remote or local reason)
53     */
54    ConnectionImpossible('C'),
55    /**
56     * Code stands for connection is impossible now due to limits(remote or
57     * local
58     * reason)
59     */
60    ServerOverloaded('l'),
61    /**
62     * Code stands for bad authentication (remote or local)
63     */
64    BadAuthent('A'),
65    /**
66     * Code stands for External operation in error (pre, post or error
67     * processing)
68     */
69    ExternalOp('E'),
70    /**
71     * Code stands for Transfer is in error
72     */
73    TransferError('T'),
74    /**
75     * Code stands for Transfer in error due to MD5
76     */
77    MD5Error('M'),
78    /**
79     * Code stands for Network disconnection
80     */
81    Disconnection('D'),
82    /**
83     * Code stands for Remote Shutdown
84     */
85    RemoteShutdown('r'),
86    /**
87     * Code stands for final action (like moving file) is in error
88     */
89    FinalOp('F'),
90    /**
91     * Code stands for unimplemented feature
92     */
93    Unimplemented('U'),
94    /**
95     * Code stands for shutdown is in progress
96     */
97    Shutdown('S'),
98    /**
99     * Code stands for a remote error is received
100    */
101   RemoteError('R'),
102   /**
103    * Code stands for an internal error
104    */
105   Internal('I'),
106   /**
107    * Code stands for a request of stopping transfer
108    */
109   StoppedTransfer('H'),
110   /**
111    * Code stands for a request of canceling transfer
112    */
113   CanceledTransfer('K'),
114   /**
115    * Warning in execution
116    */
117   Warning('W'),
118   /**
119    * Code stands for unknown type of error
120    */
121   Unknown('-'),
122   /**
123    * Code stands for a request that is already remotely finished
124    */
125   QueryAlreadyFinished('Q'),
126   /**
127    * Code stands for request that is still running
128    */
129   QueryStillRunning('s'),
130   /**
131    * Code stands for not known host
132    */
133   NotKnownHost('N'),
134   /**
135    * Code stands for self requested host starting request is invalid
136    */
137   LoopSelfRequestedHost('L'),
138   /**
139    * Code stands for request should exist but is not found on remote host
140    */
141   QueryRemotelyUnknown('u'),
142   /**
143    * Code stands for File not found error
144    */
145   FileNotFound('f'),
146   /**
147    * Code stands for Command not found error
148    */
149   CommandNotFound('c'),
150   /**
151    * Code stands for a request in PassThroughMode and required action is
152    * incompatible with this mode
153    */
154   PassThroughMode('p'),
155   /**
156    * Code stands for running step
157    */
158   Running('z'),
159   /**
160    * Code stands for Incorrect command
161    */
162   IncorrectCommand('n'),
163   /**
164    * Code stands for File not allowed
165    */
166   FileNotAllowed('a'),
167   /**
168    * Code stands for Size not allowed
169    */
170   SizeNotAllowed('d');
171 
172   /**
173    * Code could be used to switch case operations
174    */
175   public final char code;
176 
177   ErrorCode(final char code) {
178     this.code = code;
179   }
180 
181   @JsonValue
182   public final String getJsonRepr() {
183     return code + "  ";
184   }
185 
186   public final String getCode() {
187     return String.valueOf(code);
188   }
189 
190   public final String getMesg() {
191     return Messages.getString("ErrorCode." + code);
192   }
193 
194   /**
195    * Code is either the 1 char code or the exact name in Enum
196    *
197    * @param code
198    *
199    * @return the ErrorCode according to the code
200    */
201   public static ErrorCode getFromCode(final String code) {
202     if (code.isEmpty()) {
203       return Unknown;
204     }
205     switch (code.charAt(0)) {
206       case 'i':
207         return InitOk;
208       case 'B':
209         return PreProcessingOk;
210       case 'P':
211         return PostProcessingOk;
212       case 'X':
213         return TransferOk;
214       case 'O':
215         return CompleteOk;
216       case 'C':
217         return ConnectionImpossible;
218       case 'A':
219         return BadAuthent;
220       case 'E':
221         return ExternalOp;
222       case 'T':
223         return TransferError;
224       case 'M':
225         return MD5Error;
226       case 'D':
227         return Disconnection;
228       case 'r':
229         return RemoteShutdown;
230       case 'F':
231         return FinalOp;
232       case 'U':
233         return Unimplemented;
234       case 'S':
235         return Shutdown;
236       case 'R':
237         return RemoteError;
238       case 'I':
239         return Internal;
240       case 'H':
241         return StoppedTransfer;
242       case 'K':
243         return CanceledTransfer;
244       case 'W':
245         return Warning;
246       case '-':
247         return Unknown;
248       case 'Q':
249         return QueryAlreadyFinished;
250       case 's':
251         return QueryStillRunning;
252       case 'N':
253         return NotKnownHost;
254       case 'L':
255         return LoopSelfRequestedHost;
256       case 'u':
257         return QueryRemotelyUnknown;
258       case 'f':
259         return FileNotFound;
260       case 'z':
261         return Running;
262       case 'c':
263         return CommandNotFound;
264       case 'p':
265         return PassThroughMode;
266       case 'l':
267         return ServerOverloaded;
268       case 'n':
269         return IncorrectCommand;
270       case 'a':
271         return FileNotAllowed;
272       case 'd':
273         return SizeNotAllowed;
274       default:
275         final ErrorCode ecode;
276         try {
277           ecode = valueOf(code.trim());
278         } catch (final IllegalArgumentException e) {
279           return Unknown;
280         }
281         return ecode;
282     }
283   }
284 
285   public static boolean isErrorCode(final ErrorCode code) {
286     switch (code) {
287       case BadAuthent:
288       case CanceledTransfer:
289       case CommandNotFound:
290       case ConnectionImpossible:
291       case Disconnection:
292       case ExternalOp:
293       case FileNotFound:
294       case FinalOp:
295       case Internal:
296       case LoopSelfRequestedHost:
297       case MD5Error:
298       case NotKnownHost:
299       case PassThroughMode:
300       case QueryAlreadyFinished:
301       case QueryRemotelyUnknown:
302       case QueryStillRunning:
303       case RemoteError:
304       case RemoteShutdown:
305       case ServerOverloaded:
306       case Shutdown:
307       case StoppedTransfer:
308       case TransferError:
309       case Unimplemented:
310       case IncorrectCommand:
311       case FileNotAllowed:
312       case SizeNotAllowed:
313         return true;
314       case CompleteOk:
315       case InitOk:
316       case PostProcessingOk:
317       case PreProcessingOk:
318       case Running:
319       case TransferOk:
320       case Unknown:
321       case Warning:
322         return false;
323       default:
324         break;
325     }
326     return true;
327   }
328 }