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.conversation;
21  
22  import java.text.MessageFormat;
23  import java.util.MissingResourceException;
24  import java.util.ResourceBundle;
25  
26  public class ConversationException extends Exception {
27      
28      private static final long serialVersionUID = 970894530660804319L;
29      
30      public static final String BAD_CONTEXT_TOKEN = "BadContextToken";
31      public static final String UNSUPPORTED_CONTEXT_TOKEN = "UnsupportedContextToken";
32      public static final String UNKNOWN_DERIVATION_SOURCE = "UnknownDerivationSource";
33      public static final String RENEW_NEEDED = "RenewNeeded";
34      public static final String UNABLE_TO_REVIEW = "UnableToRenew";
35      
36      private static ResourceBundle resources;
37  
38      private String faultCode;
39      private String faultString;
40      
41      static {
42          try {
43              resources = ResourceBundle.getBundle("org.apache.ws.security.conversation.errors");
44          } catch (MissingResourceException e) {
45              throw new RuntimeException(e.getMessage(), e);
46          }
47      }
48      
49      public ConversationException(String faultCode, Object[] args) {
50          super(getMessage(faultCode, args));
51          this.faultCode = getFaultCode(faultCode);
52          this.faultString = getMessage(faultCode, args);
53      }
54      
55      /**
56       * Construct the fault properly code for the standard faults
57       * @param faultCode2
58       * @return
59       */
60      private String getFaultCode(String code) {
61          if(BAD_CONTEXT_TOKEN.equals(code) ||
62             UNABLE_TO_REVIEW.equals(code) ||
63             UNKNOWN_DERIVATION_SOURCE.equals(code) ||
64             UNSUPPORTED_CONTEXT_TOKEN.equals(code) ||
65             RENEW_NEEDED.equals(code)) {
66              return ConversationConstants.WSC_PREFIX+ ":" + code;
67          } else {
68              return code;
69          }
70      }
71  
72      public ConversationException(String faultCode) {
73          this(faultCode, (Object[])null);
74      }
75      
76      public ConversationException(String faultCode, Object[] args, Throwable e) {
77          super(getMessage(faultCode, args),e);
78          this.faultCode = faultCode;
79          this.faultString = getMessage(faultCode, args);
80      }
81      
82      public ConversationException(String faultCode, Throwable e) {
83          this(faultCode, null, e);
84      }
85  
86      /**
87       * get the message from resource bundle.
88       * <p/>
89       *
90       * @return the message translated from the property (message) file.
91       */
92      protected static String getMessage(String faultCode, Object[] args) {
93          String msg = null;
94          try {
95              msg = MessageFormat.format(resources.getString(faultCode), args);
96          } catch (MissingResourceException e) {
97              throw new RuntimeException("Undefined '" + faultCode + "' resource property", e);
98          }
99          if (msg != null) {
100             return msg;
101         } else {
102             return faultCode;
103         }
104     }
105 
106     /**
107      * @return Returns the faultCode.
108      */
109     protected String getFaultCode() {
110         return faultCode;
111     }
112 
113     /**
114      * @return Returns the faultString.
115      */
116     protected String getFaultString() {
117         return faultString;
118     }
119     
120 
121 }