001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.internal.impl;
020
021import javax.inject.Named;
022import javax.inject.Singleton;
023
024import org.eclipse.aether.RepositorySystemSession;
025import org.eclipse.aether.repository.RemoteRepository;
026import org.eclipse.aether.repository.RepositoryPolicy;
027import org.eclipse.aether.spi.connector.checksum.ChecksumPolicy;
028import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider;
029import org.eclipse.aether.transfer.TransferResource;
030
031import static java.util.Objects.requireNonNull;
032
033/**
034 */
035@Singleton
036@Named
037public final class DefaultChecksumPolicyProvider implements ChecksumPolicyProvider {
038
039    private static final int ORDINAL_IGNORE = 0;
040
041    private static final int ORDINAL_WARN = 1;
042
043    private static final int ORDINAL_FAIL = 2;
044
045    public DefaultChecksumPolicyProvider() {
046        // enables default constructor
047    }
048
049    public ChecksumPolicy newChecksumPolicy(
050            RepositorySystemSession session, RemoteRepository repository, TransferResource resource, String policy) {
051        requireNonNull(session, "session cannot be null");
052        requireNonNull(repository, "repository cannot be null");
053        requireNonNull(resource, "resource cannot be null");
054        validatePolicy("policy", policy);
055
056        switch (policy) {
057            case RepositoryPolicy.CHECKSUM_POLICY_IGNORE:
058                return null;
059            case RepositoryPolicy.CHECKSUM_POLICY_FAIL:
060                return new FailChecksumPolicy(resource);
061            case RepositoryPolicy.CHECKSUM_POLICY_WARN:
062                return new WarnChecksumPolicy(resource);
063            default:
064                throw new IllegalArgumentException("Unsupported policy: " + policy);
065        }
066    }
067
068    public String getEffectiveChecksumPolicy(RepositorySystemSession session, String policy1, String policy2) {
069        requireNonNull(session, "session cannot be null");
070        validatePolicy("policy1", policy1);
071        validatePolicy("policy2", policy2);
072
073        if (policy1.equals(policy2)) {
074            return policy1;
075        }
076        int ordinal1 = ordinalOfPolicy(policy1);
077        int ordinal2 = ordinalOfPolicy(policy2);
078        if (ordinal2 < ordinal1) {
079            return (ordinal2 != ORDINAL_WARN) ? policy2 : RepositoryPolicy.CHECKSUM_POLICY_WARN;
080        } else {
081            return (ordinal1 != ORDINAL_WARN) ? policy1 : RepositoryPolicy.CHECKSUM_POLICY_WARN;
082        }
083    }
084
085    private static int ordinalOfPolicy(String policy) {
086        switch (policy) {
087            case RepositoryPolicy.CHECKSUM_POLICY_IGNORE:
088                return ORDINAL_IGNORE;
089            case RepositoryPolicy.CHECKSUM_POLICY_FAIL:
090                return ORDINAL_FAIL;
091            case RepositoryPolicy.CHECKSUM_POLICY_WARN:
092                return ORDINAL_WARN;
093            default:
094                throw new IllegalArgumentException("Unsupported policy: " + policy);
095        }
096    }
097
098    private static void validatePolicy(String paramName, String policy) {
099        requireNonNull(policy, paramName + "cannot be null");
100
101        switch (policy) {
102            case RepositoryPolicy.CHECKSUM_POLICY_IGNORE:
103            case RepositoryPolicy.CHECKSUM_POLICY_FAIL:
104            case RepositoryPolicy.CHECKSUM_POLICY_WARN:
105                break;
106            default:
107                throw new IllegalArgumentException("Unsupported policy: " + policy);
108        }
109    }
110}