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.filter;
020
021import java.io.IOException;
022import java.io.UncheckedIOException;
023import java.nio.file.Path;
024
025import org.eclipse.aether.ConfigurationProperties;
026import org.eclipse.aether.RepositorySystemSession;
027import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilter;
028import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilterSource;
029import org.eclipse.aether.util.DirectoryUtils;
030
031import static java.util.Objects.requireNonNull;
032
033/**
034 * Support class for {@link RemoteRepositoryFilterSource} implementations.
035 * <p>
036 * Support class for implementing {@link RemoteRepositoryFilterSource}. It implements basic support
037 * like optional "basedir" calculation, handling of "enabled" flag.
038 * <p>
039 * The configuration keys supported:
040 * <ul>
041 *     <li><pre>aether.remoteRepositoryFilter.${id}.enabled</pre> (boolean) must be explicitly set to "true"
042 *     to become enabled</li>
043 *     <li><pre>aether.remoteRepositoryFilter.${id}.basedir</pre> (string, path) directory from where implementation
044 *     can use files. If unset, default value is ".remoteRepositoryFilters/${id}" and is resolved from local
045 *     repository basedir.</li>
046 * </ul>
047 *
048 * @since 1.9.0
049 */
050public abstract class RemoteRepositoryFilterSourceSupport implements RemoteRepositoryFilterSource {
051    protected static final String CONFIG_PROPS_PREFIX =
052            ConfigurationProperties.PREFIX_AETHER + "remoteRepositoryFilter.";
053
054    /**
055     * Returns {@code true} if session configuration contains this name set to {@code true}.
056     * <p>
057     * Default is {@code false}.
058     */
059    protected abstract boolean isEnabled(RepositorySystemSession session);
060
061    /**
062     * Uses common {@link DirectoryUtils#resolveDirectory(RepositorySystemSession, String, String, boolean)} to
063     * calculate (and maybe create) basedir for this implementation, never returns {@code null}. The returned
064     * {@link Path} may not exists, if invoked with {@code mayCreate} being {@code false}.
065     * <p>
066     * Default value is {@code ${LOCAL_REPOSITORY}/.checksums}.
067     *
068     * @return The {@link Path} of basedir, never {@code null}.
069     */
070    protected Path getBasedir(
071            RepositorySystemSession session, String defaultValue, String configPropKey, boolean mayCreate) {
072        try {
073            return DirectoryUtils.resolveDirectory(session, defaultValue, configPropKey, mayCreate);
074        } catch (IOException e) {
075            throw new UncheckedIOException(e);
076        }
077    }
078
079    /**
080     * Simple {@link RemoteRepositoryFilter.Result} immutable implementation.
081     */
082    protected static class SimpleResult implements RemoteRepositoryFilter.Result {
083        private final boolean accepted;
084
085        private final String reasoning;
086
087        public SimpleResult(boolean accepted, String reasoning) {
088            this.accepted = accepted;
089            this.reasoning = requireNonNull(reasoning);
090        }
091
092        @Override
093        public boolean isAccepted() {
094            return accepted;
095        }
096
097        @Override
098        public String reasoning() {
099            return reasoning;
100        }
101    }
102}