1 /* 2 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * 28 * (C) Copyright IBM Corp. 1999 All Rights Reserved. 29 * Copyright 1997 The Open Group Research Institute. All rights reserved. 30 */ 31 32 package sun.security.krb5; 33 34 import sun.security.krb5.internal.Krb5; 35 import sun.security.krb5.internal.KRBError; 36 37 public class KrbException extends Exception { 38 39 private static final long serialVersionUID = -4993302876451928596L; 40 41 private int returnCode; 42 private KRBError error; 43 44 public KrbException(String s) { 45 super(s); 46 } 47 48 public KrbException(Throwable cause) { 49 super(cause); 50 } 51 52 public KrbException(int i) { 53 returnCode = i; 54 } 55 56 public KrbException(int i, String s) { 57 this(s); 58 returnCode = i; 59 } 60 61 public KrbException(KRBError e) { 62 returnCode = e.getErrorCode(); 63 error = e; 64 } 65 66 public KrbException(KRBError e, String s) { 67 this(s); 68 returnCode = e.getErrorCode(); 69 error = e; 70 } 71 72 public KRBError getError() { 73 return error; 74 } 75 76 77 public int returnCode() { 78 return returnCode; 79 } 80 81 public String returnCodeSymbol() { 82 return returnCodeSymbol(returnCode); 83 } 84 85 public static String returnCodeSymbol(int i) { 86 return "not yet implemented"; 87 } 88 89 public String returnCodeMessage() { 90 return Krb5.getErrorMessage(returnCode); 91 } 92 93 public static String errorMessage(int i) { 94 return Krb5.getErrorMessage(i); 95 } 96 97 98 public String krbErrorMessage() { 99 StringBuilder strbuf = new StringBuilder("krb_error " + returnCode); 100 String msg = getMessage(); 101 if (msg != null) { 102 strbuf.append(" "); 103 strbuf.append(msg); 104 } 105 return strbuf.toString(); 106 } 107 108 /** 109 * Returns messages like: 110 * "Integrity check on decrypted field failed (31) - \ 111 * Could not decrypt service ticket" 112 * If the error code is 0 then the first half is skipped. 113 */ 114 public String getMessage() { 115 StringBuilder message = new StringBuilder(); 116 int returnCode = returnCode(); 117 if (returnCode != 0) { 118 message.append(returnCodeMessage()); 119 message.append(" (").append(returnCode()).append(')'); 120 } 121 String consMessage = super.getMessage(); 122 if (consMessage != null && consMessage.length() != 0) { 123 if (returnCode != 0) 124 message.append(" - "); 125 message.append(consMessage); 126 } 127 return message.toString(); 128 } 129 130 public String toString() { 131 return ("KrbException: " + getMessage()); 132 } 133 134 @Override public int hashCode() { 135 int result = 17; 136 result = 37 * result + returnCode; 137 if (error != null) { 138 result = 37 * result + error.hashCode(); 139 } 140 return result; 141 } 142 143 @Override public boolean equals(Object obj) { 144 if (this == obj) { 145 return true; 146 } 147 148 if (!(obj instanceof KrbException)) { 149 return false; 150 } 151 152 KrbException other = (KrbException)obj; 153 if (returnCode != other.returnCode) { 154 return false; 155 } 156 return (error == null)?(other.error == null): 157 (error.equals(other.error)); 158 } 159 }