001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.transfer;
020
021import org.eclipse.aether.RepositoryException;
022
023/**
024 * Thrown in case of a checksum failure during an artifact/metadata download.
025 */
026public class ChecksumFailureException extends RepositoryException {
027
028    private final String expected;
029
030    private final String expectedKind;
031
032    private final String actual;
033
034    private final boolean retryWorthy;
035
036    /**
037     * Creates a new exception with the specified expected and actual checksum. The resulting exception is
038     * {@link #isRetryWorthy() retry-worthy}.
039     *
040     * @param expected The expected checksum as declared by the hosting repository, may be {@code null}.
041     * @param actual The actual checksum as computed from the local bytes, may be {@code null}.
042     * @deprecated Does not reveal expected checksum kind, use other constructor that provide that information as well.
043     */
044    @Deprecated
045    public ChecksumFailureException(String expected, String actual) {
046        this(expected, null, actual);
047    }
048
049    /**
050     * Creates a new exception with the specified expected, expected kind and actual checksum. The resulting exception
051     * is {@link #isRetryWorthy() retry-worthy}.
052     *
053     * @param expected The expected checksum as declared by the hosting repository, may be {@code null}.
054     * @param expectedKind The expected checksum kind, may be {@code null}.
055     * @param actual The actual checksum as computed from the local bytes, may be {@code null}.
056     * @since 1.8.0
057     */
058    public ChecksumFailureException(String expected, String expectedKind, String actual) {
059        super("Checksum validation failed, expected '"
060                + expected + "'" + (expectedKind == null ? "" : " (" + expectedKind + ")")
061                + " but is actually '" + actual + "'");
062        this.expected = expected;
063        this.expectedKind = expectedKind;
064        this.actual = actual;
065        this.retryWorthy = true;
066    }
067
068    /**
069     * Creates a new exception with the specified detail message. The resulting exception is not
070     * {@link #isRetryWorthy() retry-worthy}.
071     *
072     * @param message The detail message, may be {@code null}.
073     */
074    public ChecksumFailureException(String message) {
075        this(false, message, null);
076    }
077
078    /**
079     * Creates a new exception with the specified cause. The resulting exception is not {@link #isRetryWorthy()
080     * retry-worthy}.
081     *
082     * @param cause The exception that caused this one, may be {@code null}.
083     */
084    public ChecksumFailureException(Throwable cause) {
085        this("Checksum validation failed" + getMessage(": ", cause), cause);
086    }
087
088    /**
089     * Creates a new exception with the specified detail message and cause. The resulting exception is not
090     * {@link #isRetryWorthy() retry-worthy}.
091     *
092     * @param message The detail message, may be {@code null}.
093     * @param cause The exception that caused this one, may be {@code null}.
094     */
095    public ChecksumFailureException(String message, Throwable cause) {
096        this(false, message, cause);
097    }
098
099    /**
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}