View Javadoc

1   /*
2    * Copyright  2003-2004 The Apache Software Foundation.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  
18  package org.apache.ws.security;
19  
20  import java.rmi.RemoteException;
21  import java.text.MessageFormat;
22  import java.util.MissingResourceException;
23  import java.util.ResourceBundle;
24  
25  /***
26   * Exception class for WS-Security.
27   * <p/>
28   *
29   * @author Davanum Srinivas (dims@yahoo.com).
30   */
31  public class WSSecurityException extends RemoteException {
32      public static final int FAILURE = 0;
33      public static final int UNSUPPORTED_SECURITY_TOKEN = 1;
34      public static final int UNSUPPORTED_ALGORITHM = 2;
35      public static final int INVALID_SECURITY = 3;
36      public static final int INVALID_SECURITY_TOKEN = 4;
37      public static final int FAILED_AUTHENTICATION = 5;
38      public static final int FAILED_CHECK = 6;
39      public static final int SECURITY_TOKEN_UNAVAILABLE = 7;
40      public static final int FAILED_ENC_DEC = 8;
41      public static final int FAILED_SIGNATURE = 9;
42      private static ResourceBundle resources;
43  
44      static {
45          try {
46              resources = ResourceBundle.getBundle("org.apache.ws.security.errors");
47          } catch (MissingResourceException e) {
48              throw new RuntimeException(e.getMessage());
49          }
50      }
51  
52      private int errorCode;
53  
54      /***
55       * Constructor.
56       * <p/>
57       *
58       * @param errorCode
59       * @param msgId
60       * @param args
61       * @param exception
62       */
63      public WSSecurityException(int errorCode, String msgId, Object[] args, Throwable exception) {
64          super(getMessage(errorCode, msgId, args), exception);
65          this.errorCode = errorCode;
66      }
67  
68      /***
69       * Constructor.
70       * <p/>
71       *
72       * @param errorCode
73       * @param msgId
74       * @param args
75       */
76      public WSSecurityException(int errorCode, String msgId, Object[] args) {
77          super(getMessage(errorCode, msgId, args));
78          this.errorCode = errorCode;
79      }
80  
81      /***
82       * Constructor.
83       * <p/>
84       *
85       * @param errorCode
86       * @param msgId
87       */
88      public WSSecurityException(int errorCode, String msgId) {
89          this(errorCode, msgId, null);
90      }
91  
92      /***
93       * Constructor.
94       * <p/>
95       *
96       * @param errorCode
97       */
98      public WSSecurityException(int errorCode) {
99          this(errorCode, null, null);
100     }
101 
102     /***
103      * Constructor.
104      * <p/>
105      *
106      * @param errorMessage
107      */
108     public WSSecurityException(String errorMessage) {
109         super(errorMessage);
110     }
111 
112     /***
113      * Constructor.
114      * <p/>
115      *
116      * @param errorMessage
117      */
118     public WSSecurityException(String errorMessage, Throwable t) {
119         super(errorMessage, t);
120     }
121 
122     /***
123      * Get the error code.
124      * <p/>
125      *
126      * @return error code of this exception See values above.
127      */
128     public int getErrorCode() {
129         return this.errorCode;
130     }
131 
132     /***
133      * get the message from resource bundle.
134      * <p/>
135      *
136      * @param errorCode
137      * @param msgId
138      * @param args
139      * @return the message translated from the property (message) file.
140      */
141     private static String getMessage(int errorCode, String msgId, Object[] args) {
142         String msg = null;
143         try {
144             msg = resources.getString(String.valueOf(errorCode));
145             if (msgId != null) {
146                 return msg += (" (" + MessageFormat.format(resources.getString(msgId), args) + ")");
147             }
148         } catch (MissingResourceException e) {
149             throw new RuntimeException("Undefined '" + msgId + "' resource property");
150         }
151         return msg;
152     }
153 }