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    @Override
046    public ChecksumPolicy newChecksumPolicy(
047            RepositorySystemSession session, RemoteRepository repository, TransferResource resource, String policy) {
048        requireNonNull(session, "session cannot be null");
049        requireNonNull(repository, "repository cannot be null");
050        requireNonNull(resource, "resource cannot be null");
051        validatePolicy("policy", policy);
052
053        switch (policy) {
054            case RepositoryPolicy.CHECKSUM_POLICY_IGNORE:
055                return null;
056            case RepositoryPolicy.CHECKSUM_POLICY_FAIL:
057                return new FailChecksumPolicy(resource);
058            case RepositoryPolicy.CHECKSUM_POLICY_WARN:
059                return new WarnChecksumPolicy(resource);
060            default:
061                throw new IllegalArgumentException("Unsupported policy: " + policy);
062        }
063    }
064
065    @Override
066    public String getEffectiveChecksumPolicy(RepositorySystemSession session, String policy1, String policy2) {
067        requireNonNull(session, "session cannot be null");
068        validatePolicy("policy1", policy1);
069        validatePolicy("policy2", policy2);
070
071        if (policy1.equals(policy2)) {
072            return policy1;
073        }
074        int ordinal1 = ordinalOfPolicy(policy1);
075        int ordinal2 = ordinalOfPolicy(policy2);
076        if (ordinal2 < ordinal1) {
077            return (ordinal2 != ORDINAL_WARN) ? policy2 : RepositoryPolicy.CHECKSUM_POLICY_WARN;
078        } else {
079            return (ordinal1 != ORDINAL_WARN) ? policy1 : RepositoryPolicy.CHECKSUM_POLICY_WARN;
080        }
081    }
082
083    private static int ordinalOfPolicy(String policy) {
084        switch (policy) {
085            case RepositoryPolicy.CHECKSUM_POLICY_IGNORE:
086                return ORDINAL_IGNORE;
087            case RepositoryPolicy.CHECKSUM_POLICY_FAIL:
088                return ORDINAL_FAIL;
089            case RepositoryPolicy.CHECKSUM_POLICY_WARN:
090                return ORDINAL_WARN;
091            default:
092                throw new IllegalArgumentException("Unsupported policy: " + policy);
093        }
094    }
095
096    private static void validatePolicy(String paramName, String policy) {
097        requireNonNull(policy, paramName + "cannot be null");
098
099        switch (policy) {
100            case RepositoryPolicy.CHECKSUM_POLICY_IGNORE:
101            case RepositoryPolicy.CHECKSUM_POLICY_FAIL:
102            case RepositoryPolicy.CHECKSUM_POLICY_WARN:
103                break;
104            default:
105                throw new IllegalArgumentException("Unsupported policy: " + policy);
106        }
107    }
108}