View Javadoc
1   package org.eclipse.aether.transfer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.eclipse.aether.RepositoryException;
23  
24  /**
25   * Thrown in case of a checksum failure during an artifact/metadata download.
26   */
27  public class ChecksumFailureException
28      extends RepositoryException
29  {
30  
31      private final String expected;
32  
33      private final String expectedKind;
34  
35      private final String actual;
36  
37      private final boolean retryWorthy;
38  
39      /**
40       * Creates a new exception with the specified expected and actual checksum. The resulting exception is
41       * {@link #isRetryWorthy() retry-worthy}.
42       * 
43       * @param expected The expected checksum as declared by the hosting repository, may be {@code null}.
44       * @param actual The actual checksum as computed from the local bytes, may be {@code null}.
45       * @deprecated Does not reveal expected checksum kind, use other constructor that provide that information as well.
46       */
47      @Deprecated
48      public ChecksumFailureException( String expected, String actual )
49      {
50          this( expected, null, actual );
51      }
52  
53      /**
54       * Creates a new exception with the specified expected, expected kind and actual checksum. The resulting exception
55       * is {@link #isRetryWorthy() retry-worthy}.
56       *
57       * @param expected The expected checksum as declared by the hosting repository, may be {@code null}.
58       * @param expectedKind The expected checksum kind, may be {@code null}.
59       * @param actual The actual checksum as computed from the local bytes, may be {@code null}.
60       * @since 1.8.0
61       */
62      public ChecksumFailureException( String expected, String expectedKind, String actual )
63      {
64          super( "Checksum validation failed, expected '"
65              + expected + "'" + ( expectedKind == null ? "" : " (" + expectedKind + ")" )
66              + " but is actually '" + actual + "'" );
67          this.expected = expected;
68          this.expectedKind = expectedKind;
69          this.actual = actual;
70          this.retryWorthy = true;
71      }
72  
73      /**
74       * Creates a new exception with the specified detail message. The resulting exception is not
75       * {@link #isRetryWorthy() retry-worthy}.
76       * 
77       * @param message The detail message, may be {@code null}.
78       */
79      public ChecksumFailureException( String message )
80      {
81          this( false, message, null );
82      }
83  
84      /**
85       * Creates a new exception with the specified cause. The resulting exception is not {@link #isRetryWorthy()
86       * retry-worthy}.
87       * 
88       * @param cause The exception that caused this one, may be {@code null}.
89       */
90      public ChecksumFailureException( Throwable cause )
91      {
92          this( "Checksum validation failed" + getMessage( ": ", cause ), cause );
93      }
94  
95      /**
96       * Creates a new exception with the specified detail message and cause. The resulting exception is not
97       * {@link #isRetryWorthy() retry-worthy}.
98       * 
99       * @param message The detail message, may be {@code null}.
100      * @param cause The exception that caused this one, may be {@code null}.
101      */
102     public ChecksumFailureException( String message, Throwable cause )
103     {
104         this( false, message, cause );
105     }
106 
107     /**
108      * Creates a new exception with the specified retry flag, detail message and cause.
109      * 
110      * @param retryWorthy {@code true} if the exception is retry-worthy, {@code false} otherwise.
111      * @param message The detail message, may be {@code null}.
112      * @param cause The exception that caused this one, may be {@code null}.
113      */
114     public ChecksumFailureException( boolean retryWorthy, String message, Throwable cause )
115     {
116         super( message, cause );
117         this.expected = "";
118         this.expectedKind = "";
119         this.actual = "";
120         this.retryWorthy = retryWorthy;
121     }
122 
123     /**
124      * Gets the expected checksum for the downloaded artifact/metadata.
125      * 
126      * @return The expected checksum as declared by the hosting repository or {@code null} if unknown.
127      */
128     public String getExpected()
129     {
130         return expected;
131     }
132 
133     /**
134      * Gets the expected checksum kind for the downloaded artifact/metadata.
135      *
136      * @return The expected checksum kind or {@code null} if unknown.
137      * @since 1.8.0
138      */
139     public String getExpectedKind()
140     {
141         return expectedKind;
142     }
143 
144     /**
145      * Gets the actual checksum for the downloaded artifact/metadata.
146      * 
147      * @return The actual checksum as computed from the local bytes or {@code null} if unknown.
148      */
149     public String getActual()
150     {
151         return actual;
152     }
153 
154     /**
155      * Indicates whether the corresponding download is retry-worthy.
156      * 
157      * @return {@code true} if retrying the download might solve the checksum failure, {@code false} if the checksum
158      *         failure is non-recoverable.
159      */
160     public boolean isRetryWorthy()
161     {
162         return retryWorthy;
163     }
164 
165 }