package org.eclipse.aether.internal.impl;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.eclipse.aether.spi.connector.checksum.ChecksumPolicy;
import org.eclipse.aether.transfer.ChecksumFailureException;
import org.eclipse.aether.transfer.TransferResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

abstract class AbstractChecksumPolicy
    implements ChecksumPolicy
{

    protected final Logger logger = LoggerFactory.getLogger( getClass() );

    protected final TransferResource resource;

    protected AbstractChecksumPolicy( TransferResource resource )
    {
        this.resource = resource;
    }

    public boolean onChecksumMatch( String algorithm, int kind )
    {
        return true;
    }

    public void onChecksumMismatch( String algorithm, int kind, ChecksumFailureException exception )
        throws ChecksumFailureException
    {
        if ( ( kind & KIND_UNOFFICIAL ) == 0 )
        {
            throw exception;
        }
    }

    public void onChecksumError( String algorithm, int kind, ChecksumFailureException exception )
        throws ChecksumFailureException
    {
        logger.debug( "Could not validate {} checksum for {}", algorithm, resource.getResourceName(), exception );
    }

    public void onNoMoreChecksums()
        throws ChecksumFailureException
    {
        throw new ChecksumFailureException( "Checksum validation failed, no checksums available" );
    }

    public void onTransferRetry()
    {
    }

}