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 actual;
034
035    private final boolean retryWorthy;
036
037    /**
038     * Creates a new exception with the specified expected and actual checksum. The resulting exception is
039     * {@link #isRetryWorthy() retry-worthy}.
040     * 
041     * @param expected The expected checksum as declared by the hosting repository, may be {@code null}.
042     * @param actual The actual checksum as computed from the local bytes, may be {@code null}.
043     */
044    public ChecksumFailureException( String expected, String actual )
045    {
046        super( "Checksum validation failed, expected " + expected + " but is " + actual );
047        this.expected = expected;
048        this.actual = actual;
049        retryWorthy = true;
050    }
051
052    /**
053     * Creates a new exception with the specified detail message. The resulting exception is not
054     * {@link #isRetryWorthy() retry-worthy}.
055     * 
056     * @param message The detail message, may be {@code null}.
057     */
058    public ChecksumFailureException( String message )
059    {
060        this( false, message, null );
061    }
062
063    /**
064     * Creates a new exception with the specified cause. The resulting exception is not {@link #isRetryWorthy()
065     * retry-worthy}.
066     * 
067     * @param cause The exception that caused this one, may be {@code null}.
068     */
069    public ChecksumFailureException( Throwable cause )
070    {
071        this( "Checksum validation failed" + getMessage( ": ", cause ), cause );
072    }
073
074    /**
075     * Creates a new exception with the specified detail message and cause. The resulting exception is not
076     * {@link #isRetryWorthy() retry-worthy}.
077     * 
078     * @param message The detail message, may be {@code null}.
079     * @param cause The exception that caused this one, may be {@code null}.
080     */
081    public ChecksumFailureException( String message, Throwable cause )
082    {
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    {
095        super( message, cause );
096        expected = actual = "";
097        this.retryWorthy = retryWorthy;
098    }
099
100    /**
101     * Gets the expected checksum for the downloaded artifact/metadata.
102     * 
103     * @return The expected checksum as declared by the hosting repository or {@code null} if unknown.
104     */
105    public String getExpected()
106    {
107        return expected;
108    }
109
110    /**
111     * Gets the actual checksum for the downloaded artifact/metadata.
112     * 
113     * @return The actual checksum as computed from the local bytes or {@code null} if unknown.
114     */
115    public String getActual()
116    {
117        return actual;
118    }
119
120    /**
121     * Indicates whether the corresponding download is retry-worthy.
122     * 
123     * @return {@code true} if retrying the download might solve the checksum failure, {@code false} if the checksum
124     *         failure is non-recoverable.
125     */
126    public boolean isRetryWorthy()
127    {
128        return retryWorthy;
129    }
130
131}