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, expected kind and actual checksum. The resulting exception
038     * is {@link #isRetryWorthy() retry-worthy}.
039     *
040     * @param expected The expected checksum as declared by the hosting repository, may be {@code null}.
041     * @param expectedKind The expected checksum kind, may be {@code null}.
042     * @param actual The actual checksum as computed from the local bytes, may be {@code null}.
043     * @since 1.8.0
044     */
045    public ChecksumFailureException(String expected, String expectedKind, String actual) {
046        super("Checksum validation failed, expected '"
047                + expected + "'" + (expectedKind == null ? "" : " (" + expectedKind + ")")
048                + " but is actually '" + actual + "'");
049        this.expected = expected;
050        this.expectedKind = expectedKind;
051        this.actual = actual;
052        this.retryWorthy = true;
053    }
054
055    /**
056     * Creates a new exception with the specified detail message. The resulting exception is not
057     * {@link #isRetryWorthy() retry-worthy}.
058     *
059     * @param message The detail message, may be {@code null}.
060     */
061    public ChecksumFailureException(String message) {
062        this(false, message, null);
063    }
064
065    /**
066     * Creates a new exception with the specified cause. The resulting exception is not {@link #isRetryWorthy()
067     * retry-worthy}.
068     *
069     * @param cause The exception that caused this one, may be {@code null}.
070     */
071    public ChecksumFailureException(Throwable cause) {
072        this("Checksum validation failed" + getMessage(": ", cause), cause);
073    }
074
075    /**
076     * Creates a new exception with the specified detail message and cause. The resulting exception is not
077     * {@link #isRetryWorthy() retry-worthy}.
078     *
079     * @param message The detail message, may be {@code null}.
080     * @param cause The exception that caused this one, may be {@code null}.
081     */
082    public ChecksumFailureException(String message, Throwable cause) {
083        this(false, message, cause);
084    }
085
086    /**
087     * Creates a new exception with the specified retry flag, detail message and cause.
088     *
089     * @param retryWorthy {@code true} if the exception is retry-worthy, {@code false} otherwise.
090     * @param message The detail message, may be {@code null}.
091     * @param cause The exception that caused this one, may be {@code null}.
092     */
093    public ChecksumFailureException(boolean retryWorthy, String message, Throwable cause) {
094        super(message, cause);
095        this.expected = "";
096        this.expectedKind = "";
097        this.actual = "";
098        this.retryWorthy = retryWorthy;
099    }
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}