001package org.eclipse.aether.spi.connector.checksum;
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.transfer.ChecksumFailureException;
023
024/**
025 * A checksum policy gets employed by repository connectors to validate the integrity of a downloaded file. For each
026 * downloaded file, a checksum policy instance is obtained and presented with the available checksums to conclude
027 * whether the download is valid or not. The following pseudo-code illustrates the usage of a checksum policy by a
028 * repository connector in some more detail (the retry logic has been omitted for the sake of brevity):
029 * 
030 * <pre>
031 * void validateChecksums() throws ChecksumFailureException {
032 *   for (checksum : checksums) {
033 *     switch (checksum.state) {
034 *       case MATCH:
035 *         if (policy.onChecksumMatch(...)) {
036 *           return;
037 *         }
038 *         break;
039 *       case MISMATCH:
040 *         policy.onChecksumMismatch(...);
041 *         break;
042 *       case ERROR:
043 *         policy.onChecksumError(...);
044 *         break;
045 *     }
046 *   }
047 *   policy.onNoMoreChecksums();
048 * }
049 * 
050 * void downloadFile() throws Exception {
051 *   ...
052 *   policy = newChecksumPolicy();
053 *   try {
054 *     validateChecksums();
055 *   } catch (ChecksumFailureException e) {
056 *     if (!policy.onTransferChecksumFailure(...)) {
057 *       throw e;
058 *     }
059 *   }
060 * }
061 * </pre>
062 * 
063 * Checksum policies might be stateful and are generally not thread-safe.
064 */
065public interface ChecksumPolicy
066{
067
068    /**
069     * Bit flag indicating a checksum which is not part of the official repository layout/structure.
070     */
071    int KIND_UNOFFICIAL = 0x01;
072
073    /**
074     * Signals a match between the locally computed checksum value and the checksum value declared by the remote
075     * repository.
076     * 
077     * @param algorithm The name of the checksum algorithm being used, must not be {@code null}.
078     * @param kind A bit field providing further details about the checksum. See the {@code KIND_*} constants in this
079     *            interface for possible bit flags.
080     * @return {@code true} to accept the download as valid and stop further validation, {@code false} to continue
081     *         validation with the next checksum.
082     */
083    boolean onChecksumMatch( String algorithm, int kind );
084
085    /**
086     * Signals a mismatch between the locally computed checksum value and the checksum value declared by the remote
087     * repository. A simple policy would just rethrow the provided exception. More sophisticated policies could update
088     * their internal state and defer a conclusion until all available checksums have been processed.
089     * 
090     * @param algorithm The name of the checksum algorithm being used, must not be {@code null}.
091     * @param kind A bit field providing further details about the checksum. See the {@code KIND_*} constants in this
092     *            interface for possible bit flags.
093     * @param exception The exception describing the checksum mismatch, must not be {@code null}.
094     * @throws ChecksumFailureException If the checksum validation is to be failed. If the method returns normally,
095     *             validation continues with the next checksum.
096     */
097    void onChecksumMismatch( String algorithm, int kind, ChecksumFailureException exception )
098        throws ChecksumFailureException;
099
100    /**
101     * Signals an error while computing the local checksum value or retrieving the checksum value from the remote
102     * repository.
103     * 
104     * @param algorithm The name of the checksum algorithm being used, must not be {@code null}.
105     * @param kind A bit field providing further details about the checksum. See the {@code KIND_*} constants in this
106     *            interface for possible bit flags.
107     * @param exception The exception describing the checksum error, must not be {@code null}.
108     * @throws ChecksumFailureException If the checksum validation is to be failed. If the method returns normally,
109     *             validation continues with the next checksum.
110     */
111    void onChecksumError( String algorithm, int kind, ChecksumFailureException exception )
112        throws ChecksumFailureException;
113
114    /**
115     * Signals that all available checksums have been processed.
116     * 
117     * @throws ChecksumFailureException If the checksum validation is to be failed. If the method returns normally, the
118     *             download is assumed to be valid.
119     */
120    void onNoMoreChecksums()
121        throws ChecksumFailureException;
122
123    /**
124     * Signals that the download is being retried after a previously thrown {@link ChecksumFailureException} that is
125     * {@link ChecksumFailureException#isRetryWorthy() retry-worthy}. Policies that maintain internal state will usually
126     * have to reset some of this state at this point to prepare for a new round of validation.
127     */
128    void onTransferRetry();
129
130    /**
131     * Signals that (even after a potential retry) checksum validation has failed. A policy could opt to merely log this
132     * issue or insist on rejecting the downloaded file as unusable.
133     * 
134     * @param exception The exception that was thrown from a prior call to
135     *            {@link #onChecksumMismatch(String, int, ChecksumFailureException)},
136     *            {@link #onChecksumError(String, int, ChecksumFailureException)} or {@link #onNoMoreChecksums()}.
137     * @return {@code true} to accept the download nevertheless and let artifact resolution succeed, {@code false} to
138     *         reject the transferred file as unusable.
139     */
140    boolean onTransferChecksumFailure( ChecksumFailureException exception );
141
142}