001package org.eclipse.aether.internal.impl;
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 javax.inject.Named;
023import javax.inject.Singleton;
024
025import java.util.Objects;
026
027import org.eclipse.aether.RepositorySystemSession;
028import org.eclipse.aether.repository.RemoteRepository;
029import org.eclipse.aether.repository.RepositoryPolicy;
030import org.eclipse.aether.spi.connector.checksum.ChecksumPolicy;
031import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider;
032import org.eclipse.aether.transfer.TransferResource;
033
034/**
035 */
036@Singleton
037@Named
038public final class DefaultChecksumPolicyProvider
039    implements ChecksumPolicyProvider
040{
041
042    private static final int ORDINAL_IGNORE = 0;
043
044    private static final int ORDINAL_WARN = 1;
045
046    private static final int ORDINAL_FAIL = 2;
047
048    public DefaultChecksumPolicyProvider()
049    {
050        // enables default constructor
051    }
052
053    public ChecksumPolicy newChecksumPolicy( RepositorySystemSession session, RemoteRepository repository,
054                                             TransferResource resource, String policy )
055    {
056        Objects.requireNonNull( session, "session cannot be null" );
057        Objects.requireNonNull( repository, "repository cannot be null" );
058        Objects.requireNonNull( resource, "resource cannot be null" );
059        validatePolicy( "policy", policy );
060
061        switch ( policy )
062        {
063            case RepositoryPolicy.CHECKSUM_POLICY_IGNORE:
064                return null;
065            case RepositoryPolicy.CHECKSUM_POLICY_FAIL:
066                return new FailChecksumPolicy( resource );
067            case RepositoryPolicy.CHECKSUM_POLICY_WARN:
068                return new WarnChecksumPolicy( resource );
069            default:
070                throw new IllegalArgumentException( "Unsupported policy: " + policy );
071        }
072    }
073
074    public String getEffectiveChecksumPolicy( RepositorySystemSession session, String policy1, String policy2 )
075    {
076        Objects.requireNonNull( session, "session cannot be null" );
077        validatePolicy( "policy1", policy1 );
078        validatePolicy( "policy2", policy2 );
079
080        if ( policy1.equals( policy2 ) )
081        {
082            return policy1;
083        }
084        int ordinal1 = ordinalOfPolicy( policy1 );
085        int ordinal2 = ordinalOfPolicy( policy2 );
086        if ( ordinal2 < ordinal1 )
087        {
088            return ( ordinal2 != ORDINAL_WARN ) ? policy2 : RepositoryPolicy.CHECKSUM_POLICY_WARN;
089        }
090        else
091        {
092            return ( ordinal1 != ORDINAL_WARN ) ? policy1 : RepositoryPolicy.CHECKSUM_POLICY_WARN;
093        }
094    }
095
096    private static int ordinalOfPolicy( String policy )
097    {
098        switch ( policy )
099        {
100            case RepositoryPolicy.CHECKSUM_POLICY_IGNORE:
101                return ORDINAL_IGNORE;
102            case RepositoryPolicy.CHECKSUM_POLICY_FAIL:
103                return ORDINAL_FAIL;
104            case RepositoryPolicy.CHECKSUM_POLICY_WARN:
105                return ORDINAL_WARN;
106            default:
107                throw new IllegalArgumentException( "Unsupported policy: " + policy );
108        }
109    }
110
111    private static void validatePolicy( String paramName, String policy )
112    {
113        Objects.requireNonNull( policy, paramName + "cannot be null" );
114
115        switch ( policy )
116        {
117            case RepositoryPolicy.CHECKSUM_POLICY_IGNORE:
118            case RepositoryPolicy.CHECKSUM_POLICY_FAIL:
119            case RepositoryPolicy.CHECKSUM_POLICY_WARN:
120                break;
121            default:
122                throw new IllegalArgumentException( "Unsupported policy: " + policy );
123        }
124    }
125
126}