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 and actual checksum. The resulting exception is
38       * {@link #isRetryWorthy() retry-worthy}.
39       *
40       * @param expected The expected checksum as declared by the hosting repository, may be {@code null}.
41       * @param actual The actual checksum as computed from the local bytes, may be {@code null}.
42       * @deprecated Does not reveal expected checksum kind, use other constructor that provide that information as well.
43       */
44      @Deprecated
45      public ChecksumFailureException(String expected, String actual) {
46          this(expected, null, actual);
47      }
48  
49      /**
50       * Creates a new exception with the specified expected, expected kind and actual checksum. The resulting exception
51       * is {@link #isRetryWorthy() retry-worthy}.
52       *
53       * @param expected The expected checksum as declared by the hosting repository, may be {@code null}.
54       * @param expectedKind The expected checksum kind, may be {@code null}.
55       * @param actual The actual checksum as computed from the local bytes, may be {@code null}.
56       * @since 1.8.0
57       */
58      public ChecksumFailureException(String expected, String expectedKind, String actual) {
59          super("Checksum validation failed, expected '"
60                  + expected + "'" + (expectedKind == null ? "" : " (" + expectedKind + ")")
61                  + " but is actually '" + actual + "'");
62          this.expected = expected;
63          this.expectedKind = expectedKind;
64          this.actual = actual;
65          this.retryWorthy = true;
66      }
67  
68      /**
69       * Creates a new exception with the specified detail message. The resulting exception is not
70       * {@link #isRetryWorthy() retry-worthy}.
71       *
72       * @param message The detail message, may be {@code null}.
73       */
74      public ChecksumFailureException(String message) {
75          this(false, message, null);
76      }
77  
78      /**
79       * Creates a new exception with the specified cause. The resulting exception is not {@link #isRetryWorthy()
80       * retry-worthy}.
81       *
82       * @param cause The exception that caused this one, may be {@code null}.
83       */
84      public ChecksumFailureException(Throwable cause) {
85          this("Checksum validation failed" + getMessage(": ", cause), cause);
86      }
87  
88      /**
89       * Creates a new exception with the specified detail message and cause. The resulting exception is not
90       * {@link #isRetryWorthy() retry-worthy}.
91       *
92       * @param message The detail message, may be {@code null}.
93       * @param cause The exception that caused this one, may be {@code null}.
94       */
95      public ChecksumFailureException(String message, Throwable cause) {
96          this(false, message, cause);
97      }
98  
99      /**
100      * Creates a new exception with the specified retry flag, detail message and cause.
101      *
102      * @param retryWorthy {@code true} if the exception is retry-worthy, {@code false} otherwise.
103      * @param message The detail message, may be {@code null}.
104      * @param cause The exception that caused this one, may be {@code null}.
105      */
106     public ChecksumFailureException(boolean retryWorthy, String message, Throwable cause) {
107         super(message, cause);
108         this.expected = "";
109         this.expectedKind = "";
110         this.actual = "";
111         this.retryWorthy = retryWorthy;
112     }
113 
114     /**
115      * Gets the expected checksum for the downloaded artifact/metadata.
116      *
117      * @return The expected checksum as declared by the hosting repository or {@code null} if unknown.
118      */
119     public String getExpected() {
120         return expected;
121     }
122 
123     /**
124      * Gets the expected checksum kind for the downloaded artifact/metadata.
125      *
126      * @return The expected checksum kind or {@code null} if unknown.
127      * @since 1.8.0
128      */
129     public String getExpectedKind() {
130         return expectedKind;
131     }
132 
133     /**
134      * Gets the actual checksum for the downloaded artifact/metadata.
135      *
136      * @return The actual checksum as computed from the local bytes or {@code null} if unknown.
137      */
138     public String getActual() {
139         return actual;
140     }
141 
142     /**
143      * Indicates whether the corresponding download is retry-worthy.
144      *
145      * @return {@code true} if retrying the download might solve the checksum failure, {@code false} if the checksum
146      *         failure is non-recoverable.
147      */
148     public boolean isRetryWorthy() {
149         return retryWorthy;
150     }
151 }