KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xmpp > packet > PacketError


1 /**
2
3  * $RCSfile: PacketError.java,v $
4
5  * $Revision: 1.7 $
6
7  * $Date: 2005/04/03 19:32:34 $
8
9  *
10
11  * Copyright 2004 Jive Software.
12
13  *
14
15  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
16
17  * you may not use this file except in compliance with the License.
18
19  * You may obtain a copy of the License at
20
21  *
22
23  * http://www.apache.org/licenses/LICENSE-2.0
24
25  *
26
27  * Unless required by applicable law or agreed to in writing, software
28
29  * distributed under the License is distributed on an "AS IS" BASIS,
30
31  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32
33  * See the License for the specific language governing permissions and
34
35  * limitations under the License.
36
37  */

38
39
40
41 package org.xmpp.packet;
42
43
44
45 import org.dom4j.*;
46
47 import org.dom4j.io.XMLWriter;
48
49 import org.dom4j.io.OutputFormat;
50
51
52
53 import java.util.Iterator JavaDoc;
54
55 import java.io.StringWriter JavaDoc;
56
57
58
59 /**
60
61  * A packet error. Errors must have a type and condition. Optionally, they
62
63  * can include explanation text.
64
65  *
66
67  * @author Matt Tucker
68
69  */

70
71 public class PacketError {
72
73
74
75     private static final String JavaDoc ERROR_NAMESPACE = "urn:ietf:params:xml:ns:xmpp-stanzas";
76
77
78
79     private static DocumentFactory docFactory = DocumentFactory.getInstance();
80
81
82
83     private Element element;
84
85
86
87     /**
88
89      * Construcs a new PacketError with the specified condition. The error
90
91      * type will be set to the default for the specified condition.
92
93      *
94
95      * @param condition the error condition.
96
97      */

98
99     public PacketError(Condition condition) {
100
101         this.element = docFactory.createElement("error");
102
103         setCondition(condition);
104
105         setType(condition.getDefaultType());
106
107     }
108
109
110
111     /**
112
113      * Constructs a new PacketError with the specified condition and type.
114
115      *
116
117      * @param condition the error condition.
118
119      * @param type the error type.
120
121      */

122
123     public PacketError(Condition condition, Type type) {
124
125         this.element = docFactory.createElement("error");
126
127         setCondition(condition);
128
129         setType(type);
130
131     }
132
133
134
135     /**
136
137      * Constructs a new PacketError.
138
139      *
140
141      * @param type the error type.
142
143      * @param condition the error condition.
144
145      * @param text the text description of the error.
146
147      */

148
149     public PacketError(Condition condition, Type type, String JavaDoc text) {
150
151         this.element = docFactory.createElement("error");
152
153         setType(type);
154
155         setCondition(condition);
156
157         setText(text, null);
158
159     }
160
161
162
163     /**
164
165      * Constructs a new PacketError.
166
167      *
168
169      * @param type the error type.
170
171      * @param condition the error condition.
172
173      * @param text the text description of the error.
174
175      * @param lang the language code of the error description (e.g. "en").
176
177      */

178
179     public PacketError(Condition condition, Type type, String JavaDoc text, String JavaDoc lang) {
180
181         this.element = docFactory.createElement("error");
182
183         setType(type);
184
185         setCondition(condition);
186
187         setText(text, lang);
188
189     }
190
191
192
193     /**
194
195      * Constructs a new PacketError using an existing Element. This is useful
196
197      * for parsing incoming error Elements into PacketError objects.
198
199      *
200
201      * @param element the error Element.
202
203      */

204
205     public PacketError(Element element) {
206
207         this.element = element;
208
209     }
210
211
212
213     /**
214
215      * Returns the error type.
216
217      *
218
219      * @return the error type.
220
221      * @see Type
222
223      */

224
225     public Type getType() {
226
227         String JavaDoc type = element.attributeValue("type");
228
229         if (type != null) {
230
231             return Type.fromXMPP(type);
232
233         }
234
235         else {
236
237             return null;
238
239         }
240
241     }
242
243
244
245     /**
246
247      * Sets the error type.
248
249      *
250
251      * @param type the error type.
252
253      * @see Type
254
255      */

256
257     public void setType(Type type) {
258
259         element.addAttribute("type", type==null?null:type.toXMPP());
260
261     }
262
263
264
265     /**
266
267      * Returns the error condition.
268
269      *
270
271      * @return the error condition.
272
273      * @see Condition
274
275      */

276
277     public Condition getCondition() {
278
279         for (Iterator JavaDoc i=element.elementIterator(); i.hasNext(); ) {
280
281             Element el = (Element)i.next();
282
283             if (el.getNamespaceURI().equals(ERROR_NAMESPACE) &&
284
285                     !el.getName().equals("text"))
286
287             {
288
289                 return Condition.fromXMPP(el.getName());
290
291             }
292
293         }
294
295         // Looking for XMPP condition failed. See if a legacy error code exists,
296

297         // which can be mapped into an XMPP error condition.
298

299         String JavaDoc code = element.attributeValue("code");
300
301         if (code != null) {
302
303             try {
304
305                 return Condition.fromLegacyCode(Integer.parseInt(code));
306
307             }
308
309             catch (Exception JavaDoc e) {
310
311                 // Ignore -- unable to map legacy code into a valid condition
312

313                 // so return null.
314

315             }
316
317         }
318
319         return null;
320
321     }
322
323
324
325     /**
326
327      * Sets the error condition.
328
329      *
330
331      * @param condition the error condition.
332
333      * @see Condition
334
335      */

336
337     public void setCondition(Condition condition) {
338
339         if (condition == null) {
340
341             throw new NullPointerException JavaDoc("Condition cannot be null");
342
343         }
344
345         // Set the error code for legacy support.
346

347         element.addAttribute("code", Integer.toString(condition.getLegacyCode()));
348
349
350
351         Element conditionElement = null;
352
353         for (Iterator JavaDoc i=element.elementIterator(); i.hasNext(); ) {
354
355             Element el = (Element)i.next();
356
357             if (el.getNamespaceURI().equals(ERROR_NAMESPACE) &&
358
359                     !el.getName().equals("text"))
360
361             {
362
363                 conditionElement = el;
364
365             }
366
367         }
368
369         if (conditionElement != null) {
370
371             element.remove(conditionElement);
372
373         }
374
375
376
377         conditionElement = docFactory.createElement(condition.toXMPP(),
378
379                 ERROR_NAMESPACE);
380
381         element.add(conditionElement);
382
383     }
384
385
386
387     /**
388
389      * Returns a text description of the error, or <tt>null</tt> if there
390
391      * is no text description.
392
393      *
394
395      * @return the text description of the error.
396
397      */

398
399     public String JavaDoc getText() {
400
401         return element.elementText("text");
402
403     }
404
405
406
407     /**
408
409      * Sets the text description of the error.
410
411      *
412
413      * @param text the text description of the error.
414
415      */

416
417     public void setText(String JavaDoc text) {
418
419         setText(text, null);
420
421     }
422
423
424
425     /**
426
427      * Sets the text description of the error. Optionally, a language code
428
429      * can be specified to indicate the language of the description.
430
431      *
432
433      * @param text the text description of the error.
434
435      * @param lang the language code of the description, or <tt>null</tt> to specify
436
437      * no language code.
438
439      */

440
441     public void setText(String JavaDoc text, String JavaDoc lang) {
442
443         Element textElement = element.element("text");
444
445         // If text is null, clear the text.
446

447         if (text == null) {
448
449             if (textElement != null) {
450
451                 element.remove(textElement);
452
453             }
454
455             return;
456
457         }
458
459
460
461         if (textElement == null) {
462
463             textElement = docFactory.createElement("text", ERROR_NAMESPACE);
464
465             if (lang != null) {
466
467                 textElement.addAttribute(QName.get("lang", "xml",
468
469                         "http://www.w3.org/XML/1998/namespace"), lang);
470
471             }
472
473             element.add(textElement);
474
475         }
476
477         textElement.setText(text);
478
479     }
480
481
482
483     /**
484
485      * Returns the text description's language code, or <tt>null</tt> if there
486
487      * is no language code associated with the description text.
488
489      *
490
491      * @return the language code of the text description, if it exists.
492
493      */

494
495     public String JavaDoc getTextLang() {
496
497         Element textElement = element.element("text");
498
499         if (textElement != null) {
500
501             return textElement.attributeValue(QName.get("lang", "xml",
502
503                         "http://www.w3.org/XML/1998/namespace"));
504
505         }
506
507         return null;
508
509     }
510
511
512
513     /**
514
515      * Returns the DOM4J Element that backs the error. The element is the definitive
516
517      * representation of the error and can be manipulated directly to change
518
519      * error contents.
520
521      *
522
523      * @return the DOM4J Element.
524
525      */

526
527     public Element getElement() {
528
529         return element;
530
531     }
532
533
534
535     /**
536
537      * Returns the textual XML representation of this error.
538
539      *
540
541      * @return the textual XML representation of this error.
542
543      */

544
545     public String JavaDoc toXML() {
546
547         return element.asXML();
548
549     }
550
551
552
553     public String JavaDoc toString() {
554
555         StringWriter JavaDoc out = new StringWriter JavaDoc();
556
557         XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint());
558
559         try {
560
561             writer.write(element);
562
563         }
564
565         catch (Exception JavaDoc e) { }
566
567         return out.toString();
568
569     }
570
571
572
573     /**
574
575      * Type-safe enumeration for the error condition.<p>
576
577      *
578
579      * Implementation note: XMPP error conditions use "-" characters in
580
581      * their names such as "bad-request". Because "-" characters are not valid
582
583      * identifier parts in Java, they have been converted to "_" characters in
584
585      * the enumeration names, such as <tt>bad_request</tt>. The {@link #toXMPP()} and
586
587      * {@link #fromXMPP(String)} methods can be used to convert between the
588
589      * enumertation values and XMPP error code strings.
590
591      */

592
593     public enum Condition {
594
595
596
597         /**
598
599          * The sender has sent XML that is malformed or that cannot be processed
600
601          * (e.g., an IQ stanza that includes an unrecognized value of the 'type'
602
603          * attribute); the associated error type SHOULD be "modify".
604
605          */

606
607         bad_request("bad-request", Type.modify, 400),
608
609
610
611         /**
612
613          * Access cannot be granted because an existing resource or session
614
615          * exists with the same name or address; the associated error type
616
617          * SHOULD be "cancel".
618
619          */

620
621         conflict("conflict", Type.cancel, 409),
622
623
624
625         /**
626
627          * The feature requested is not implemented by the recipient or
628
629          * server and therefore cannot be processed; the associated error
630
631          * type SHOULD be "cancel".
632
633          */

634
635         feature_not_implemented("feature-not-implemented", Type.cancel, 501),
636
637
638
639         /**
640
641          * The requesting entity does not possess the required permissions to
642
643          * perform the action; the associated error type SHOULD be "auth".
644
645          */

646
647         forbidden("forbidden", Type.auth, 403),
648
649
650
651         /**
652
653          * The recipient or server can no longer be contacted at this address
654
655          * (the error stanza MAY contain a new address in the XML character
656
657          * data of the <gone/> element); the associated error type SHOULD be
658
659          * "modify".
660
661          */

662
663         gone("gone", Type.modify, 302),
664
665
666
667         /**
668
669          * The server could not process the stanza because of a misconfiguration
670
671          * or an otherwise-undefined internal server error; the associated error
672
673          * type SHOULD be "wait".
674
675          */

676
677         internal_server_error("internal-server-error", Type.wait, 500),
678
679
680
681         /**
682
683          * The addressed JID or item requested cannot be found; the associated
684
685          * error type SHOULD be "cancel".
686
687          */

688
689         item_not_found("item-not-found", Type.cancel, 404),
690
691
692
693         /**
694
695          * The sending entity has provided or communicated an XMPP address
696
697          * (e.g., a value of the 'to' attribute) or aspect thereof (e.g.,
698
699          * a resource identifier) that does not adhere to the syntax defined
700
701          * in Addressing Scheme (Section 3); the associated error type SHOULD
702
703          * be "modify".
704
705          */

706
707         jid_malformed("jid-malformed", Type.modify, 400),
708
709
710
711         /**
712
713          * The recipient or server understands the request but is refusing
714
715          * to process it because it does not meet criteria defined by the
716
717          * recipient or server (e.g., a local policy regarding acceptable
718
719          * words in messages); the associated error type SHOULD be "modify".
720
721          */

722
723         not_acceptable("not-acceptable", Type.modify, 406),
724
725
726
727         /**
728
729          * The recipient or server does not allow any entity to perform
730
731          * the action; the associated error type SHOULD be "cancel".
732
733          */

734
735         not_allowed("not-allowed", Type.cancel, 405),
736
737
738
739         /**
740
741          * The sender must provide proper credentials before being allowed
742
743          * to perform the action, or has provided improper credentials;
744
745          * the associated error type SHOULD be "auth".
746
747          */

748
749         not_authorized("not-authorized", Type.auth, 401),
750
751
752
753         /**
754
755          * The requesting entity is not authorized to access the requested
756
757          * service because payment is required; the associated error type
758
759          * SHOULD be "auth".
760
761          */

762
763         payment_required("payment-required", Type.auth, 402),
764
765
766
767         /**
768
769          * The intended recipient is temporarily unavailable; the associated
770
771          * error type SHOULD be "wait" (note: an application MUST NOT return
772
773          * this error if doing so would provide information about the intended
774
775          * recipient's network availability to an entity that is not authorized
776
777          * to know such information).
778
779          */

780
781         recipient_unavailable("recipient-unavailable", Type.wait, 404),
782
783
784
785         /**
786
787          * The recipient or server is redirecting requests for this
788
789          * information to another entity, usually temporarily (the error
790
791          * stanza SHOULD contain the alternate address, which MUST be a
792
793          * valid JID, in the XML character data of the &lt;redirect/&gt; element);
794
795          * the associated error type SHOULD be "modify".
796
797          */

798
799         redirect("redirect", Type.modify, 302),
800
801
802
803         /**
804
805          * The requesting entity is not authorized to access the requested
806
807          * service because registration is required; the associated error
808
809          * type SHOULD be "auth".
810
811          */

812
813         registration_required("registration-required", Type.auth, 407),
814
815
816
817         /**
818
819          * A remote server or service specified as part or all of the JID
820
821          * of the intended recipient does not exist; the associated error
822
823          * type SHOULD be "cancel".
824
825          */

826
827         remote_server_not_found("remote-server-not-found", Type.cancel, 404),
828
829
830
831         /**
832
833          * A remote server or service specified as part or all of the JID of
834
835          * the intended recipient (or required to fulfill a request) could not
836
837          * be contacted within a reasonable amount of time; the associated
838
839          * error type SHOULD be "wait".
840
841          */

842
843         remote_server_timeout("remote-server-timeout", Type.wait, 504),
844
845
846
847         /**
848
849          * The server or recipient lacks the system resources necessary to
850
851          * service the request; the associated error type SHOULD be "wait".
852
853          */

854
855         resource_constraint("resource-constraint", Type.wait, 500),
856
857
858
859         /**
860
861          * The server or recipient does not currently provide the requested
862
863          * service; the associated error type SHOULD be "cancel".
864
865          */

866
867         service_unavailable("service-unavailable", Type.cancel, 503),
868
869
870
871         /**
872
873          * The requesting entity is not authorized to access the requested
874
875          * service because a subscription is required; the associated error
876
877          * type SHOULD be "auth".
878
879          */

880
881         subscription_required("subscription-required", Type.auth, 407),
882
883
884
885         /**
886
887          * The error condition is not one of those defined by the other
888
889          * conditions in this list; any error type may be associated with
890
891          * this condition, and it SHOULD be used only in conjunction with
892
893          * an application-specific condition.<p>
894
895          *
896
897          * Implementation note: the default type for this condition is
898
899          * {@link Type#wait}, which is not specified in the XMPP protocol.
900
901          */

902
903         undefined_condition("undefined-condition", Type.wait, 500),
904
905
906
907         /**
908
909          * The recipient or server understood the request but was not
910
911          * expecting it at this time (e.g., the request was out of order);
912
913          * the associated error type SHOULD be "wait".
914
915          */

916
917         unexpected_condition("unexpected-condition", Type.wait, 400);
918
919
920
921         /**
922
923          * Converts a String value into its Condition representation.
924
925          *
926
927          * @param condition the String value.
928
929          * @return the condition corresponding to the String.
930
931          */

932
933         public static Condition fromXMPP(String JavaDoc condition) {
934
935             if (condition == null) {
936
937                 throw new NullPointerException JavaDoc();
938
939             }
940
941             condition = condition.toLowerCase();
942
943             if (bad_request.toXMPP().equals(condition)) {
944
945                 return bad_request;
946
947             }
948
949             else if (conflict.toXMPP().equals(condition)) {
950
951                 return conflict;
952
953             }
954
955             else if (feature_not_implemented.toXMPP().equals(condition)) {
956
957                 return feature_not_implemented;
958
959             }
960
961             else if (forbidden.toXMPP().equals(condition)) {
962
963                 return forbidden;
964
965             }
966
967             else if (gone.toXMPP().equals(condition)) {
968
969                 return gone;
970
971             }
972
973             else if (internal_server_error.toXMPP().equals(condition)) {
974
975                 return internal_server_error;
976
977             }
978
979             else if (item_not_found.toXMPP().equals(condition)) {
980
981                 return item_not_found;
982
983             }
984
985             else if (jid_malformed.toXMPP().equals(condition)) {
986
987                 return jid_malformed;
988
989             }
990
991             else if (not_acceptable.toXMPP().equals(condition)) {
992
993                 return not_acceptable;
994
995             }
996
997             else if (not_allowed.toXMPP().equals(condition)) {
998
999                 return not_allowed;
1000
1001            }
1002
1003            else if (not_authorized.toXMPP().equals(condition)) {
1004
1005                return not_authorized;
1006
1007            }
1008
1009            else if (payment_required.toXMPP().equals(condition)) {
1010
1011                return payment_required;
1012
1013            }
1014
1015            else if (recipient_unavailable.toXMPP().equals(condition)) {
1016
1017                return recipient_unavailable;
1018
1019            }
1020
1021            else if (redirect.toXMPP().equals(condition)) {
1022
1023                return redirect;
1024
1025            }
1026
1027            else if (registration_required.toXMPP().equals(condition)) {
1028
1029                return registration_required;
1030
1031            }
1032
1033            else if (remote_server_not_found.toXMPP().equals(condition)) {
1034
1035                return remote_server_not_found;
1036
1037            }
1038
1039            else if (remote_server_timeout.toXMPP().equals(condition)) {
1040
1041                return remote_server_timeout;
1042
1043            }
1044
1045            else if (resource_constraint.toXMPP().equals(condition)) {
1046
1047                return resource_constraint;
1048
1049            }
1050
1051            else if (service_unavailable.toXMPP().equals(condition)) {
1052
1053                return service_unavailable;
1054
1055            }
1056
1057            else if (subscription_required.toXMPP().equals(condition)) {
1058
1059                return subscription_required;
1060
1061            }
1062
1063            else if (undefined_condition.toXMPP().equals(condition)) {
1064
1065                return undefined_condition;
1066
1067            }
1068
1069            else if (unexpected_condition.toXMPP().equals(condition)) {
1070
1071                return unexpected_condition;
1072
1073            }
1074
1075            else {
1076
1077                throw new IllegalArgumentException JavaDoc("Condition invalid:" + condition);
1078
1079            }
1080
1081        }
1082
1083
1084
1085        public static Condition fromLegacyCode(int code) {
1086
1087            if (bad_request.getLegacyCode() == code) {
1088
1089                return bad_request;
1090
1091            }
1092
1093            else if (conflict.getLegacyCode() == code) {
1094
1095                return conflict;
1096
1097            }
1098
1099            else if (feature_not_implemented.getLegacyCode() == code) {
1100
1101                return feature_not_implemented;
1102
1103            }
1104
1105            else if (forbidden.getLegacyCode() == code) {
1106
1107                return forbidden;
1108
1109            }
1110
1111            else if (gone.getLegacyCode() == code) {
1112
1113                return gone;
1114
1115            }
1116
1117            else if (internal_server_error.getLegacyCode() == code) {
1118
1119                return internal_server_error;
1120
1121            }
1122
1123            else if (item_not_found.getLegacyCode() == code) {
1124
1125                return item_not_found;
1126
1127            }
1128
1129            else if (jid_malformed.getLegacyCode() == code) {
1130
1131                return jid_malformed;
1132
1133            }
1134
1135            else if (not_acceptable.getLegacyCode() == code) {
1136
1137                return not_acceptable;
1138
1139            }
1140
1141            else if (not_allowed.getLegacyCode() == code) {
1142
1143                return not_allowed;
1144
1145            }
1146
1147            else if (not_authorized.getLegacyCode() == code) {
1148
1149                return not_authorized;
1150
1151            }
1152
1153            else if (payment_required.getLegacyCode() == code) {
1154
1155                return payment_required;
1156
1157            }
1158
1159            else if (recipient_unavailable.getLegacyCode() == code) {
1160
1161                return recipient_unavailable;
1162
1163            }
1164
1165            else if (redirect.getLegacyCode() == code) {
1166
1167                return redirect;
1168
1169            }
1170
1171            else if (registration_required.getLegacyCode() == code) {
1172
1173                return registration_required;
1174
1175            }
1176
1177            else if (remote_server_not_found.getLegacyCode() == code) {
1178
1179                return remote_server_not_found;
1180
1181            }
1182
1183            else if (remote_server_timeout.getLegacyCode() == code) {
1184
1185                return remote_server_timeout;
1186
1187            }
1188
1189            else if (resource_constraint.getLegacyCode() == code) {
1190
1191                return resource_constraint;
1192
1193            }
1194
1195            else if (service_unavailable.getLegacyCode() == code) {
1196
1197                return service_unavailable;
1198
1199            }
1200
1201            else if (subscription_required.getLegacyCode() == code) {
1202
1203                return subscription_required;
1204
1205            }
1206
1207            else if (undefined_condition.getLegacyCode() == code) {
1208
1209                return undefined_condition;
1210
1211            }
1212
1213            else if (unexpected_condition.getLegacyCode() == code) {
1214
1215                return unexpected_condition;
1216
1217            }
1218
1219            else {
1220
1221                throw new IllegalArgumentException JavaDoc("Code invalid:" + code);
1222
1223            }
1224
1225        }
1226
1227
1228
1229        private String JavaDoc value;
1230
1231        private int code;
1232
1233        private Type defaultType;
1234
1235
1236
1237        private Condition(String JavaDoc value, Type defaultType, int code) {
1238
1239            this.value = value;
1240
1241            this.defaultType = defaultType;
1242
1243            this.code = code;
1244
1245        }
1246
1247
1248
1249        /**
1250
1251         * Returns the default {@link Type} associated with this condition. Each
1252
1253         * error condition has an error type that it is usually associated with.
1254
1255         *
1256
1257         * @return the default error type.
1258
1259         */

1260
1261        public Type getDefaultType() {
1262
1263            return defaultType;
1264
1265        }
1266
1267
1268
1269        /**
1270
1271         * Returns the legacy error code associated with the error. Error code mappings
1272
1273         * are based on <a HREF="http://www.jabber.org/jeps/jep-0086.html">JEP-0086</a>.
1274
1275         * Support for legacy error codes is necessary since many "Jabber" clients
1276
1277         * do not understand XMPP error codes. The {@link #fromLegacyCode(int)} method
1278
1279         * will convert numeric error codes into Conditions.
1280
1281         *
1282
1283         * @return the legacy error code.
1284
1285         */

1286
1287        public int getLegacyCode() {
1288
1289            return code;
1290
1291        }
1292
1293
1294
1295        /**
1296
1297         * Returns the error code as a valid XMPP error code string.
1298
1299         *
1300
1301         * @return the XMPP error code value.
1302
1303         */

1304
1305        public String JavaDoc toXMPP() {
1306
1307            return value;
1308
1309        }
1310
1311    }
1312
1313
1314
1315    /**
1316
1317     * Error type. Valid types are:<ul>
1318
1319     *
1320
1321     * <li>{@link #cancel Error.Type.cancel} -- do not retry (the error is unrecoverable).
1322
1323     * <li>{@link #continue_processing Error.Type.continue_processing} -- proceed
1324
1325     * (the condition was only a warning). Equivalent to the XMPP error type
1326
1327     * "continue".
1328
1329     * <li>{@link #modify Error.Type.modify} -- retry after changing the data sent.
1330
1331     * <li>{@link #auth Eror.Type.auth} -- retry after providing credentials.
1332
1333     * <li>{@link #wait Error.Type.wait} -- retry after waiting (the error is temporary).
1334
1335     * </ul>
1336
1337     *
1338
1339     * Implementation note: one of the XMPP error types is "continue". Because "continue"
1340
1341     * is a reserved Java keyword, the enum name is <tt>continue_processing</tt>. The
1342
1343     * {@link #toXMPP()} and {@link #fromXMPP(String)} methods can be used to convert
1344
1345     * between the enumertation values and XMPP error type strings.
1346
1347     */

1348
1349    public enum Type {
1350
1351
1352
1353        /**
1354
1355         * Do not retry (the error is unrecoverable).
1356
1357         */

1358
1359        cancel("cancel"),
1360
1361
1362
1363        /**
1364
1365         * Proceed (the condition was only a warning). This represents
1366
1367         * the "continue" error code in XMPP; because "continue" is a
1368
1369         * reserved keyword in Java the enum name has been changed.
1370
1371         */

1372
1373        continue_processing("continue"),
1374
1375
1376
1377        /**
1378
1379         * Retry after changing the data sent.
1380
1381         */

1382
1383        modify("modify"),
1384
1385
1386
1387        /**
1388
1389         * Retry after providing credentials.
1390
1391         */

1392
1393        auth("auth"),
1394
1395
1396
1397        /**
1398
1399         * Retry after waiting (the error is temporary).
1400
1401         */

1402
1403        wait("wait");
1404
1405
1406
1407        /**
1408
1409         * Converts a String value into its Type representation.
1410
1411         *
1412
1413         * @param type the String value.
1414
1415         * @return the condition corresponding to the String.
1416
1417         */

1418
1419        public static Type fromXMPP(String JavaDoc type) {
1420
1421            if (type == null) {
1422
1423                throw new NullPointerException JavaDoc();
1424
1425            }
1426
1427            type = type.toLowerCase();
1428
1429            if (cancel.toXMPP().equals(type)) {
1430
1431                return cancel;
1432
1433            }
1434
1435            else if (continue_processing.toXMPP().equals(type)) {
1436
1437                return continue_processing;
1438
1439            }
1440
1441            else if (modify.toXMPP().equals(type)) {
1442
1443                return modify;
1444
1445            }
1446
1447            else if (auth.toXMPP().equals(type)) {
1448
1449                return auth;
1450
1451            }
1452
1453            else if (wait.toXMPP().equals(type)) {
1454
1455                return wait;
1456
1457            }
1458
1459            else {
1460
1461                throw new IllegalArgumentException JavaDoc("Type invalid:" + type);
1462
1463            }
1464
1465        }
1466
1467
1468
1469        private String JavaDoc value;
1470
1471
1472
1473        private Type(String JavaDoc value) {
1474
1475            this.value = value;
1476
1477        }
1478
1479
1480
1481        /**
1482
1483         * Returns the error code as a valid XMPP error code string.
1484
1485         *
1486
1487         * @return the XMPP error code value.
1488
1489         */

1490
1491        public String JavaDoc toXMPP() {
1492
1493            return value;
1494
1495        }
1496
1497    }
1498
1499}
Popular Tags