View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.impl.filter;
20  
21  import java.io.IOException;
22  import java.io.UncheckedIOException;
23  import java.nio.file.Path;
24  
25  import org.eclipse.aether.ConfigurationProperties;
26  import org.eclipse.aether.RepositorySystemSession;
27  import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilter;
28  import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilterSource;
29  import org.eclipse.aether.util.DirectoryUtils;
30  
31  import static java.util.Objects.requireNonNull;
32  
33  /**
34   * Support class for {@link RemoteRepositoryFilterSource} implementations.
35   * <p>
36   * Support class for implementing {@link RemoteRepositoryFilterSource}. It implements basic support
37   * like optional "basedir" calculation, handling of "enabled" flag.
38   * <p>
39   * The configuration keys supported:
40   * <ul>
41   *     <li><pre>aether.remoteRepositoryFilter.${id}.enabled</pre> (boolean) must be explicitly set to "true"
42   *     to become enabled</li>
43   *     <li><pre>aether.remoteRepositoryFilter.${id}.basedir</pre> (string, path) directory from where implementation
44   *     can use files. If unset, default value is ".remoteRepositoryFilters/${id}" and is resolved from local
45   *     repository basedir.</li>
46   * </ul>
47   *
48   * @since 1.9.0
49   */
50  public abstract class RemoteRepositoryFilterSourceSupport implements RemoteRepositoryFilterSource {
51      protected static final String CONFIG_PROPS_PREFIX =
52              ConfigurationProperties.PREFIX_AETHER + "remoteRepositoryFilter.";
53  
54      /**
55       * Returns {@code true} if session configuration contains this name set to {@code true}.
56       * <p>
57       * Default is {@code false}.
58       */
59      protected abstract boolean isEnabled(RepositorySystemSession session);
60  
61      /**
62       * Uses common {@link DirectoryUtils#resolveDirectory(RepositorySystemSession, String, String, boolean)} to
63       * calculate (and maybe create) basedir for this implementation, never returns {@code null}. The returned
64       * {@link Path} may not exists, if invoked with {@code mayCreate} being {@code false}.
65       * <p>
66       * Default value is {@code ${LOCAL_REPOSITORY}/.checksums}.
67       *
68       * @return The {@link Path} of basedir, never {@code null}.
69       */
70      protected Path getBasedir(
71              RepositorySystemSession session, String defaultValue, String configPropKey, boolean mayCreate) {
72          try {
73              return DirectoryUtils.resolveDirectory(session, defaultValue, configPropKey, mayCreate);
74          } catch (IOException e) {
75              throw new UncheckedIOException(e);
76          }
77      }
78  
79      /**
80       * Simple {@link RemoteRepositoryFilter.Result} immutable implementation.
81       */
82      protected static class SimpleResult implements RemoteRepositoryFilter.Result {
83          private final boolean accepted;
84  
85          private final String reasoning;
86  
87          public SimpleResult(boolean accepted, String reasoning) {
88              this.accepted = accepted;
89              this.reasoning = requireNonNull(reasoning);
90          }
91  
92          @Override
93          public boolean isAccepted() {
94              return accepted;
95          }
96  
97          @Override
98          public String reasoning() {
99              return reasoning;
100         }
101     }
102 }