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