1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.ws.security;
21
22 import java.text.MessageFormat;
23 import java.util.MissingResourceException;
24 import java.util.ResourceBundle;
25 import javax.xml.namespace.QName;
26
27
28
29
30
31
32
33 public class WSSecurityException extends Exception {
34
35
36
37 private static final long serialVersionUID = -6676817793986149732L;
38 public static final int FAILURE = 0;
39 public static final int UNSUPPORTED_SECURITY_TOKEN = 1;
40 public static final int UNSUPPORTED_ALGORITHM = 2;
41 public static final int INVALID_SECURITY = 3;
42 public static final int INVALID_SECURITY_TOKEN = 4;
43 public static final int FAILED_AUTHENTICATION = 5;
44 public static final int FAILED_CHECK = 6;
45 public static final int SECURITY_TOKEN_UNAVAILABLE = 7;
46 public static final int MESSAGE_EXPIRED = 8;
47 public static final int FAILED_ENCRYPTION = 9;
48 public static final int FAILED_SIGNATURE = 10;
49 private static ResourceBundle resources;
50
51
52
53
54
55
56
57 private static final java.util.Map<Integer, QName> FAULT_CODE_MAP =
58 new java.util.HashMap<Integer, QName>();
59
60 static {
61 try {
62 resources = ResourceBundle.getBundle("org.apache.ws.security.errors");
63 } catch (MissingResourceException e) {
64 throw new RuntimeException(e.getMessage(), e);
65 }
66
67 FAULT_CODE_MAP.put(
68 Integer.valueOf(WSSecurityException.UNSUPPORTED_SECURITY_TOKEN),
69 WSConstants.UNSUPPORTED_SECURITY_TOKEN
70 );
71 FAULT_CODE_MAP.put(
72 Integer.valueOf(UNSUPPORTED_ALGORITHM),
73 WSConstants.UNSUPPORTED_ALGORITHM
74 );
75 FAULT_CODE_MAP.put(
76 Integer.valueOf(INVALID_SECURITY),
77 WSConstants.INVALID_SECURITY
78 );
79 FAULT_CODE_MAP.put(
80 Integer.valueOf(INVALID_SECURITY_TOKEN),
81 WSConstants.INVALID_SECURITY_TOKEN
82 );
83 FAULT_CODE_MAP.put(
84 Integer.valueOf(FAILED_AUTHENTICATION),
85 WSConstants.FAILED_AUTHENTICATION
86 );
87 FAULT_CODE_MAP.put(
88 Integer.valueOf(FAILED_CHECK),
89 WSConstants.FAILED_CHECK
90 );
91 FAULT_CODE_MAP.put(
92 Integer.valueOf(SECURITY_TOKEN_UNAVAILABLE),
93 WSConstants.SECURITY_TOKEN_UNAVAILABLE
94 );
95 FAULT_CODE_MAP.put(
96 Integer.valueOf(MESSAGE_EXPIRED),
97 WSConstants.MESSAGE_EXPIRED
98 );
99 }
100
101 private int errorCode;
102
103
104
105
106
107
108
109
110
111
112 public WSSecurityException(int errorCode, String msgId, Object[] args, Throwable exception) {
113 super(getMessage(errorCode, msgId, args), exception);
114 this.errorCode = errorCode;
115 }
116
117
118
119
120
121
122
123
124
125 public WSSecurityException(int errorCode, String msgId, Object[] args) {
126 super(getMessage(errorCode, msgId, args));
127 this.errorCode = errorCode;
128 }
129
130
131
132
133
134
135
136
137 public WSSecurityException(int errorCode, String msgId) {
138 this(errorCode, msgId, null);
139 }
140
141
142
143
144
145
146
147 public WSSecurityException(int errorCode) {
148 this(errorCode, null, null);
149 }
150
151
152
153
154
155
156
157 public WSSecurityException(String errorMessage) {
158 super(errorMessage);
159 }
160
161
162
163
164
165
166
167 public WSSecurityException(String errorMessage, Throwable t) {
168 super(errorMessage, t);
169 }
170
171
172
173
174
175
176
177 public int getErrorCode() {
178 return this.errorCode;
179 }
180
181
182
183
184
185
186
187 public javax.xml.namespace.QName getFaultCode() {
188 Object ret = FAULT_CODE_MAP.get(Integer.valueOf(this.errorCode));
189 if (ret != null) {
190 return (javax.xml.namespace.QName)ret;
191 }
192 return null;
193 }
194
195
196
197
198
199
200
201
202
203
204 private static String getMessage(int errorCode, String msgId, Object[] args) {
205 String msg = null;
206 try {
207 msg = resources.getString(String.valueOf(errorCode));
208 if (msgId != null) {
209 return msg += " (" + MessageFormat.format(resources.getString(msgId), args) + ")";
210 }
211 } catch (MissingResourceException e) {
212 throw new RuntimeException("Undefined '" + msgId + "' resource property", e);
213 }
214 return msg;
215 }
216 }