1. /*
  2. * @(#)CertPathValidatorException.java 1.7 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security.cert;
  8. import java.io.PrintStream;
  9. import java.io.PrintWriter;
  10. /**
  11. * An exception indicating one of a variety of problems encountered when
  12. * validating a certification path.
  13. * <p>
  14. * A <code>CertPathValidatorException</code> provides support for wrapping
  15. * exceptions. The {@link #getCause getCause} method returns the throwable,
  16. * if any, that caused this exception to be thrown.
  17. * <p>
  18. * A <code>CertPathValidatorException</code> may also include the
  19. * certification path that was being validated when the exception was thrown
  20. * and the index of the certificate in the certification path that caused the
  21. * exception to be thrown. Use the {@link #getCertPath getCertPath} and
  22. * {@link #getIndex getIndex} methods to retrieve this information.
  23. *
  24. * <p>
  25. * <b>Concurrent Access</b>
  26. * <p>
  27. * Unless otherwise specified, the methods defined in this class are not
  28. * thread-safe. Multiple threads that need to access a single
  29. * object concurrently should synchronize amongst themselves and
  30. * provide the necessary locking. Multiple threads each manipulating
  31. * separate objects need not synchronize.
  32. *
  33. * @see CertPathValidator
  34. *
  35. * @version 1.7 01/23/03
  36. * @since 1.4
  37. * @author Yassir Elley
  38. */
  39. public class CertPathValidatorException extends
  40. java.security.GeneralSecurityException {
  41. /**
  42. * @serial the index of the certificate in the certification path
  43. * that caused the exception to be thrown
  44. */
  45. private int index = -1;
  46. /**
  47. * @serial the <code>CertPath</code> that was being validated when
  48. * the exception was thrown
  49. */
  50. private CertPath certPath;
  51. /**
  52. * Creates a <code>CertPathValidatorException</code> with
  53. * no detail message.
  54. */
  55. public CertPathValidatorException() {
  56. super();
  57. }
  58. /**
  59. * Creates a <code>CertPathValidatorException</code> with the given
  60. * detail message. A detail message is a <code>String</code> that
  61. * describes this particular exception.
  62. *
  63. * @param msg the detail message
  64. */
  65. public CertPathValidatorException(String msg) {
  66. super(msg);
  67. }
  68. /**
  69. * Creates a <code>CertPathValidatorException</code> that wraps the
  70. * specified throwable. This allows any exception to be converted into a
  71. * <code>CertPathValidatorException</code>, while retaining information
  72. * about the wrapped exception, which may be useful for debugging. The
  73. * detail message is set to (<code>cause==null ? null : cause.toString()
  74. * </code>) (which typically contains the class and detail message of
  75. * cause).
  76. *
  77. * @param cause the cause (which is saved for later retrieval by the
  78. * {@link #getCause getCause()} method). (A <code>null</code> value is
  79. * permitted, and indicates that the cause is nonexistent or unknown.)
  80. */
  81. public CertPathValidatorException(Throwable cause) {
  82. super();
  83. initCause(cause);
  84. }
  85. /**
  86. * Creates a <code>CertPathValidatorException</code> with the specified
  87. * detail message and cause.
  88. *
  89. * @param msg the detail message
  90. * @param cause the cause (which is saved for later retrieval by the
  91. * {@link #getCause getCause()} method). (A <code>null</code> value is
  92. * permitted, and indicates that the cause is nonexistent or unknown.)
  93. */
  94. public CertPathValidatorException(String msg, Throwable cause) {
  95. super(msg);
  96. initCause(cause);
  97. }
  98. /**
  99. * Creates a <code>CertPathValidatorException</code> with the specified
  100. * detail message, cause, certification path, and index.
  101. *
  102. * @param msg the detail message (or <code>null</code> if none)
  103. * @param cause the cause (or <code>null</code> if none)
  104. * @param certPath the certification path that was in the process of
  105. * being validated when the error was encountered
  106. * @param index the index of the certificate in the certification path
  107. * that caused the error (or -1 if not applicable). Note that
  108. * the list of certificates in a <code>CertPath</code> is zero based.
  109. * @throws IndexOutofBoundsException if the index is out of range
  110. * <code>(index < -1 || (certPath != null && index >=
  111. * certPath.getCertificates().size())</code>
  112. * @throws IllegalArgumentException if <code>certPath</code> is
  113. * <code>null</code> and <code>index</code> is not -1
  114. */
  115. public CertPathValidatorException(String msg, Throwable cause,
  116. CertPath certPath, int index)
  117. {
  118. this(msg, cause);
  119. if (certPath == null && index != -1)
  120. throw new IllegalArgumentException();
  121. if (index < -1 ||
  122. (certPath != null && index >= certPath.getCertificates().size()))
  123. throw new IndexOutOfBoundsException();
  124. this.certPath = certPath;
  125. this.index = index;
  126. }
  127. /**
  128. * Returns the detail message for this
  129. * <code>CertPathValidatorException</code>.
  130. *
  131. * @return the detail message, or <code>null</code> if neither the message
  132. * nor cause were specified
  133. */
  134. public String getMessage() {
  135. return super.getMessage();
  136. }
  137. /**
  138. * Returns the certification path that was being validated when
  139. * the exception was thrown.
  140. *
  141. * @return the <code>CertPath</code> that was being validated when
  142. * the exception was thrown (or <code>null</code> if not specified)
  143. */
  144. public CertPath getCertPath() {
  145. return this.certPath;
  146. }
  147. /**
  148. * Returns the index of the certificate in the certification path
  149. * that caused the exception to be thrown. Note that the list of
  150. * certificates in a <code>CertPath</code> is zero based. If no
  151. * index has been set, -1 is returned.
  152. *
  153. * @return the index that has been set, or -1 if none has been set
  154. */
  155. public int getIndex() {
  156. return this.index;
  157. }
  158. /**
  159. * Returns the cause of this <code>CertPathValidatorException</code> or
  160. * <code>null</code> if the cause is nonexistent or unknown.
  161. *
  162. * @return the cause of this throwable or <code>null</code> if the cause
  163. * is nonexistent or unknown.
  164. */
  165. public Throwable getCause() {
  166. return super.getCause();
  167. }
  168. /**
  169. * Returns a string describing this exception, including a description
  170. * of the internal (wrapped) cause if there is one.
  171. *
  172. * @return a string representation of this
  173. * <code>CertPathValidatorException</code>
  174. */
  175. public String toString() {
  176. if (getCause() == null)
  177. return super.toString();
  178. else
  179. return super.toString() +
  180. "; internal cause is: \n\t" +
  181. getCause().toString();
  182. }
  183. /**
  184. * Prints a stack trace to <code>System.err</code>, including the backtrace
  185. * of the cause, if any.
  186. */
  187. public void printStackTrace() {
  188. printStackTrace(System.err);
  189. }
  190. /**
  191. * Prints a stack trace to a <code>PrintStream</code>, including the
  192. * backtrace of the cause, if any.
  193. *
  194. * @param ps the <code>PrintStream</code> to use for output
  195. */
  196. public void printStackTrace(PrintStream ps) {
  197. super.printStackTrace(ps);
  198. }
  199. /**
  200. * Prints a stack trace to a <code>PrintWriter</code>, including the
  201. * backtrace of the cause, if any.
  202. *
  203. * @param pw the <code>PrintWriter</code> to use for output
  204. */
  205. public void printStackTrace(PrintWriter pw) {
  206. super.printStackTrace(pw);
  207. }
  208. }