CertPathValidatorException.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* CertPathValidatorException.java -- wraps an exception during validation
  2. of a CertPath
  3. Copyright (C) 2002 Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.security.cert;
  33. import java.io.PrintStream;
  34. import java.io.PrintWriter;
  35. import java.security.GeneralSecurityException;
  36. /**
  37. * Indicates a problem while validating a certification path. In addition,
  38. * it can store the path an index in that path that caused the problem. This
  39. * class is not thread-safe.
  40. *
  41. * @author Eric Blake <ebb9@email.byu.edu>
  42. * @see CertPathValidator
  43. * @since 1.4
  44. * @status updated to 1.4
  45. */
  46. public class CertPathValidatorException extends GeneralSecurityException
  47. {
  48. /**
  49. * Compatible with JDK 1.4+.
  50. */
  51. private static final long serialVersionUID = -3083180014971893139L;
  52. /**
  53. * The index of the certificate path that failed, or -1.
  54. *
  55. * @serial the failed index
  56. */
  57. private final int index;
  58. /**
  59. * The <code>CertPath</code> that failed.
  60. *
  61. * @serial the object being validated at time of failure
  62. */
  63. private final CertPath certPath;
  64. /**
  65. * Create an exception without a message. The cause may be initialized. The
  66. * index is set to -1 and the failed CertPath object to null.
  67. */
  68. public CertPathValidatorException()
  69. {
  70. this((String) null);
  71. }
  72. /**
  73. * Create an exception with a message. The cause may be initialized. The
  74. * index is set to -1 and the failed CertPath object to null.
  75. *
  76. * @param msg a message to display with exception
  77. */
  78. public CertPathValidatorException(String msg)
  79. {
  80. super(msg);
  81. index = -1;
  82. certPath = null;
  83. }
  84. /**
  85. * Create an exception with a cause. The message will be
  86. * <code>cause == null ? null : cause.toString()</code>. The index is set
  87. * to -1 and the failed CertPath object to null.
  88. *
  89. * @param cause the cause
  90. */
  91. public CertPathValidatorException(Throwable cause)
  92. {
  93. this(cause == null ? null : cause.toString(), cause, null, -1);
  94. }
  95. /**
  96. * Create an exception with a cause and a message. The index is set to -1
  97. * and the failed CertPath object to null.
  98. *
  99. * @param msg the message
  100. * @param cause the cause
  101. */
  102. public CertPathValidatorException(String msg, Throwable cause)
  103. {
  104. this(msg, cause, null, -1);
  105. }
  106. /**
  107. * Create an exception with a cause, message, failed object, and index of
  108. * failure in that CertPath.
  109. *
  110. * @param msg the message
  111. * @param cause the cause
  112. * @param certPath the path that was being validated, or null
  113. * @param index the index of the path, or -1
  114. * @throws IndexOutOfBoundsException if index is &lt; -1 or
  115. * &gt; certPath.getCertificates().size()
  116. * @throws IllegalArgumentException if certPath is null but index != -1
  117. */
  118. public CertPathValidatorException(String msg, Throwable cause,
  119. CertPath certPath, int index)
  120. {
  121. super(msg);
  122. initCause(cause);
  123. if (index < -1 || (certPath != null
  124. && index >= certPath.getCertificates().size()))
  125. throw new IndexOutOfBoundsException();
  126. if ((certPath == null) != (index == -1))
  127. throw new IllegalArgumentException();
  128. this.certPath = certPath;
  129. this.index = index;
  130. }
  131. /**
  132. * Get the detail message.
  133. *
  134. * @return the detail message
  135. */
  136. public String getMessage()
  137. {
  138. return super.getMessage();
  139. }
  140. /**
  141. * Get the certificate path that had the failure, or null.
  142. *
  143. * @return the culprit path
  144. */
  145. public CertPath getCertPath()
  146. {
  147. return certPath;
  148. }
  149. /**
  150. * Get the index that failed, or -1.
  151. *
  152. * @return the colprit index
  153. */
  154. public int getIndex()
  155. {
  156. return index;
  157. }
  158. /**
  159. * Get the cause, null if unknown.
  160. *
  161. * @return the cause
  162. */
  163. public Throwable getCause()
  164. {
  165. return super.getCause();
  166. }
  167. /**
  168. * Convert this to a string, including its cause.
  169. *
  170. * @return the string conversion
  171. */
  172. public String toString()
  173. {
  174. return super.toString();
  175. }
  176. /**
  177. * Print the stack trace to <code>System.err</code>.
  178. */
  179. public void printStackTrace()
  180. {
  181. super.printStackTrace();
  182. }
  183. /**
  184. * Print the stack trace to a stream.
  185. *
  186. * @param stream the stream
  187. */
  188. public void printStackTrace(PrintStream stream)
  189. {
  190. super.printStackTrace(stream);
  191. }
  192. /**
  193. * Print the stack trace to a stream.
  194. *
  195. * @param stream the stream
  196. */
  197. public void printStackTrace(PrintWriter stream)
  198. {
  199. super.printStackTrace(stream);
  200. }
  201. }