Debian Codegraph @

gss/src/main/java/org/globus/gsi/gssapi/GlobusGSSException.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15 package org.globus.gsi.gssapi;
16
17 import java.io.PrintStream;
18 import java.io.PrintWriter;
19 import java.text.MessageFormat;
20 import java.util.ResourceBundle;
21 import java.util.MissingResourceException;
22
23 import org.ietf.jgss.GSSException;
24
25 import javax.net.ssl.SSLException;
26
27 public class GlobusGSSException extends GSSException {
28
29 private static final long serialVersionUID = 1366868883920091438L;
30
31 public static final int
32 PROXY_VIOLATION = 5,
33 BAD_ARGUMENT = 7,
34 BAD_NAME = 25,
35 CREDENTIAL_ERROR = 27,
36 TOKEN_FAIL = 29,
37 DELEGATION_ERROR = 30,
38 BAD_MIC = 33,
39 UNKNOWN_OPTION = 37;
40
41 public static final int
42 BAD_OPTION_TYPE = 100,
43 BAD_OPTION = 101,
44 UNKNOWN = 102;
45
46 private static ResourceBundle resources;
47
48 static {
49 try {
50 resources = ResourceBundle.getBundle("org.globus.gsi.gssapi.errors");
51 } catch (MissingResourceException e) {
52 throw new RuntimeException(e.getMessage());
53 }
54 }
55
56 private final boolean hasCustomMessage;
57
58 public GlobusGSSException(int majorCode,
59 Throwable cause) {
60 super(majorCode);
61 initCause(cause);
62 hasCustomMessage = false;
63 }
64
65 public GlobusGSSException(int majorCode,
66 int minorCode,
67 String minorString,
68 Throwable cause) {
69 super(majorCode, minorCode, minorString);
70 initCause(cause);
71 hasCustomMessage = true;
72 }
73
74 public GlobusGSSException(int majorCode,
75 int minorCode,
76 String key) {
77 this(majorCode, minorCode, key, (Object[])null);
78 }
79
80 public GlobusGSSException(int majorCode,
81 int minorCode,
82 String key,
83 Object [] args) {
84 super(majorCode);
85
86 String msg = null;
87 try {
88 msg = MessageFormat.format(resources.getString(key), args);
89 } catch (MissingResourceException e) {
90 //msg = "No msg text defined for '" + key + "'";
91 throw new RuntimeException("bad" + key);
92 }
93
94 setMinor(minorCode, msg);
95 initCause(null);
96 hasCustomMessage = true;
97 }
98
99
100 /**
101 * Prints this exception's stack trace to <tt>System.err</tt>.
102 * If this exception has a root exception; the stack trace of the
103 * root exception is printed to <tt>System.err</tt> instead.
104 */
105 @Override
106 public void printStackTrace() {
107 printStackTrace( System.err );
108 }
109
110 /**
111 * Prints this exception's stack trace to a print stream.
112 * If this exception has a root exception; the stack trace of the
113 * root exception is printed to the print stream instead.
114 * @param ps The non-null print stream to which to print.
115 */
116 @Override
117 public void printStackTrace(PrintStream ps) {
118 if ( getCause() != null ) {
119 String superString = getLocalMessage();
120 synchronized ( ps ) {
121 ps.print(superString);
122 ps.print((superString.endsWith(".") ?
123 " Caused by " : ". Caused by "));
124 getCause().printStackTrace( ps );
125 }
126 } else {
127 super.printStackTrace( ps );
128 }
129 }
130
131 /**
132 * Prints this exception's stack trace to a print writer.
133 * If this exception has a root exception; the stack trace of the
134 * root exception is printed to the print writer instead.
135 * @param pw The non-null print writer to which to print.
136 */
137 @Override
138 public void printStackTrace(PrintWriter pw) {
139 if ( getCause() != null ) {
140 String superString = getLocalMessage();
141 synchronized (pw) {
142 pw.print(superString);
143 pw.print((superString.endsWith(".") ?
144 " Caused by " : ". Caused by "));
145 getCause().printStackTrace( pw );
146 }
147 } else {
148 super.printStackTrace( pw );
149 }
150 }
151
152 @Override
153 public String getMessage() {
154 Throwable cause = getCause();
155
156 if (isBoring(this)) {
157 return getUsefulMessage(cause);
158 } else {
159 StringBuilder message = new StringBuilder(super.getMessage());
160 if (cause != null) {
161 message.append(" [Caused by: ").append(getUsefulMessage(cause)).append("]");
162 }
163 return message.toString();
164 }
165 }
166
167 /**
168 * Wrapper around getMessage method that tries to provide a meaningful
169 * message. This is needed because many GSSException objects provide no
170 * useful information and the actual useful information is in the Throwable
171 * that caused the exception.
172 */
173 private static String getUsefulMessage(Throwable throwable) {
174 while(isBoring(throwable)) {
175 throwable = throwable.getCause();
176 }
177
178 String message = throwable.getMessage();
179 if (message == null) {
180 message = throwable.getClass().getName();
181 }
182 return message;
183 }
184
185 /**
186 * Use heuristics to determine whether the supplied Throwable has any
187 * semantic content (i.e., does it provide any additional information).
188 *
189 * It seems that many GSSException objects are created with no information.
190 * Instead, the useful information is contained within the causing
191 * Throwable.
192 *
193 * Also, an SSLException may be thrown by SSLEngine that wraps some more
194 * interesting exception but the message has no information.
195 *
196 * As part of a work-around for this problem, this method tries to guess
197 * whether the supplied Throwable contains useful information.
198 *
199 * @return true if the Throwable contains no useful information, false
200 * otherwise.
201 */
202 private static boolean isBoring(Throwable t) {
203
204 // Last throwable in the causal chain is never boring.
205 if (t.getCause() == null) {
206 return false;
207 }
208
209 // Some GSSExceptions have no semantic content, therefore boring.
210 if (t instanceof GSSException) {
211 GSSException g = (GlobusGSSException) t;
212
213 if (g.getMajor() == GSSException.FAILURE && g.getMinor() == 0) {
214 if (g instanceof GlobusGSSException) {
215 return !((GlobusGSSException)g).hasCustomMessage;
216 } else {
217 // Unfortunately, for GSSException, we must compare the
218 // actual message.
219 return g.getMessage().equals("Failure unspecified at GSS-API level");
220 }
221 }
222 }
223
224 // SSLEngine can return a message with no meaning, therefore boring.
225 if (t instanceof SSLException &&
226 t.getMessage().equals("General SSLEngine problem")) {
227 return true;
228 }
229
230 return false;
231 }
232
233 private String getLocalMessage() {
234 String message = super.getMessage();
235 return (message == null) ? getClass().getName() : message;
236 }
237 }