001package org.eclipse.aether.internal.impl.filter;
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 java.io.IOException;
023import java.io.UncheckedIOException;
024import java.nio.file.Path;
025
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.ConfigUtils;
030import org.eclipse.aether.util.DirectoryUtils;
031
032import static java.util.Objects.requireNonNull;
033
034/**
035 * Support class for {@link RemoteRepositoryFilterSource} implementations.
036 * <p>
037 * Support class for implementing {@link RemoteRepositoryFilterSource}. It implements basic support
038 * like optional "basedir" calculation, handling of "enabled" flag.
039 * <p>
040 * The configuration keys supported:
041 * <ul>
042 *     <li><pre>aether.remoteRepositoryFilter.${id}.enabled</pre> (boolean) must be explicitly set to "true"
043 *     to become enabled</li>
044 *     <li><pre>aether.remoteRepositoryFilter.${id}.basedir</pre> (string, path) directory from where implementation
045 *     can use files. If unset, default value is ".remoteRepositoryFilters/${id}" and is resolved from local
046 *     repository basedir.</li>
047 * </ul>
048 *
049 * @since 1.9.0
050 */
051public abstract class RemoteRepositoryFilterSourceSupport
052        implements RemoteRepositoryFilterSource
053{
054    private static final String CONFIG_PROP_PREFIX = "aether.remoteRepositoryFilter.";
055
056    private static final String CONF_NAME_BASEDIR = "basedir";
057
058    static final String LOCAL_REPO_PREFIX_DIR = ".remoteRepositoryFilters";
059
060    private final String name;
061
062    protected RemoteRepositoryFilterSourceSupport( String name )
063    {
064        this.name = requireNonNull( name );
065    }
066
067    /**
068     * Utility method to create scoped configuration property key of given name.
069     */
070    protected String configPropKey( String name )
071    {
072        return CONFIG_PROP_PREFIX + this.name + "." + name;
073    }
074
075    /**
076     * Returns {@code true} if session configuration contains this name set to {@code true}.
077     * <p>
078     * Default is {@code false}.
079     */
080    protected boolean isEnabled( RepositorySystemSession session )
081    {
082        return ConfigUtils.getBoolean( session, false, CONFIG_PROP_PREFIX + this.name );
083    }
084
085    /**
086     * Uses common {@link DirectoryUtils#resolveDirectory(RepositorySystemSession, String, String, boolean)} to
087     * calculate (and maybe create) basedir for this implementation, never returns {@code null}. The returned
088     * {@link Path} may not exists, if invoked with {@code mayCreate} being {@code false}.
089     * <p>
090     * Default value is {@code ${LOCAL_REPOSITORY}/.checksums}.
091     *
092     * @return The {@link Path} of basedir, never {@code null}.
093     */
094    protected Path getBasedir( RepositorySystemSession session, boolean mayCreate )
095    {
096        try
097        {
098            return DirectoryUtils.resolveDirectory(
099                    session, LOCAL_REPO_PREFIX_DIR, configPropKey( CONF_NAME_BASEDIR ), mayCreate );
100        }
101        catch ( IOException e )
102        {
103            throw new UncheckedIOException( e );
104        }
105    }
106
107    /**
108     * Simple {@link RemoteRepositoryFilter.Result} immutable implementation.
109     */
110    protected static class SimpleResult implements RemoteRepositoryFilter.Result
111    {
112        private final boolean accepted;
113
114        private final String reasoning;
115
116        public SimpleResult( boolean accepted, String reasoning )
117        {
118            this.accepted = accepted;
119            this.reasoning = requireNonNull( reasoning );
120        }
121
122        @Override
123        public boolean isAccepted()
124        {
125            return accepted;
126        }
127
128        @Override
129        public String reasoning()
130        {
131            return reasoning;
132        }
133    }
134}