View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Exception class for WS-Security.
29   * <p/>
30   *
31   * @author Davanum Srinivas (dims@yahoo.com).
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       * This is an Integer -> QName map. Its function is to map the integer error codes
52       * given above to the QName fault codes as defined in the SOAP Message Security 1.1
53       * specification. A client application can simply call getFaultCode rather than do
54       * any parsing of the error code. Note that there are no mappings for "FAILURE", 
55       * "FAILED_ENCRYPTION" and "FAILED_SIGNATURE" as these are not standard error messages.
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      * Constructor.
105      * <p/>
106      *
107      * @param errorCode
108      * @param msgId
109      * @param args
110      * @param exception
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      * Constructor.
119      * <p/>
120      *
121      * @param errorCode
122      * @param msgId
123      * @param args
124      */
125     public WSSecurityException(int errorCode, String msgId, Object[] args) {
126         super(getMessage(errorCode, msgId, args));
127         this.errorCode = errorCode;
128     }
129 
130     /**
131      * Constructor.
132      * <p/>
133      *
134      * @param errorCode
135      * @param msgId
136      */
137     public WSSecurityException(int errorCode, String msgId) {
138         this(errorCode, msgId, null);
139     }
140 
141     /**
142      * Constructor.
143      * <p/>
144      *
145      * @param errorCode
146      */
147     public WSSecurityException(int errorCode) {
148         this(errorCode, null, null);
149     }
150 
151     /**
152      * Constructor.
153      * <p/>
154      *
155      * @param errorMessage
156      */
157     public WSSecurityException(String errorMessage) {
158         super(errorMessage);
159     }
160 
161     /**
162      * Constructor.
163      * <p/>
164      *
165      * @param errorMessage
166      */
167     public WSSecurityException(String errorMessage, Throwable t) {
168         super(errorMessage, t);
169     }
170 
171     /**
172      * Get the error code.
173      * <p/>
174      *
175      * @return error code of this exception See values above.
176      */
177     public int getErrorCode() {
178         return this.errorCode;
179     }
180     
181     /**
182      * Get the fault code QName for this associated error code.
183      * <p/>
184      * 
185      * @return the fault code QName of this exception
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      * get the message from resource bundle.
197      * <p/>
198      *
199      * @param errorCode
200      * @param msgId
201      * @param args
202      * @return the message translated from the property (message) file.
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 }