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