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 sb = new StringBuilder();
 100         sb.append("krb_error ").append(returnCode);
 101         String msg =  getMessage();
 102         if (msg != null) {
 103             sb.append(" ");
 104             sb.append(msg);
 105         }
 106         return sb.toString();
 107     }
 108 
 109     /**
 110      * Returns messages like:
 111      * "Integrity check on decrypted field failed (31) - \
 112      *                         Could not decrypt service ticket"
 113      * If the error code is 0 then the first half is skipped.
 114      */
 115     public String getMessage() {
 116         StringBuilder message = new StringBuilder();
 117         int returnCode = returnCode();
 118         if (returnCode != 0) {
 119             message.append(returnCodeMessage());
 120             message.append(" (").append(returnCode()).append(')');
 121         }
 122         String consMessage = super.getMessage();
 123         if (consMessage != null && consMessage.length() != 0) {
 124             if (returnCode != 0)
 125                 message.append(" - ");
 126             message.append(consMessage);
 127         }
 128         return message.toString();
 129     }
 130 
 131     public String toString() {
 132         return ("KrbException: " + getMessage());
 133     }
 134 
 135     @Override public int hashCode() {
 136         int result = 17;
 137         result = 37 * result + returnCode;
 138         if (error != null) {
 139             result = 37 * result + error.hashCode();
 140         }
 141         return result;
 142     }
 143 
 144     @Override public boolean equals(Object obj) {
 145         if (this == obj) {
 146             return true;
 147         }
 148 
 149         if (!(obj instanceof KrbException)) {
 150             return false;
 151         }
 152 
 153         KrbException other = (KrbException)obj;
 154         if (returnCode != other.returnCode) {
 155             return false;
 156         }
 157         return (error == null)?(other.error == null):
 158             (error.equals(other.error));
 159     }
 160 }