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.RepositorySystemSession;
26  import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilter;
27  import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilterSource;
28  import org.eclipse.aether.util.ConfigUtils;
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      private static final String CONFIG_PROP_PREFIX = "aether.remoteRepositoryFilter.";
52  
53      private static final String CONF_NAME_BASEDIR = "basedir";
54  
55      static final String LOCAL_REPO_PREFIX_DIR = ".remoteRepositoryFilters";
56  
57      private final String name;
58  
59      protected RemoteRepositoryFilterSourceSupport(String name) {
60          this.name = requireNonNull(name);
61      }
62  
63      /**
64       * Utility method to create scoped configuration property key of given name.
65       */
66      protected String configPropKey(String name) {
67          return CONFIG_PROP_PREFIX + this.name + "." + name;
68      }
69  
70      /**
71       * Returns {@code true} if session configuration contains this name set to {@code true}.
72       * <p>
73       * Default is {@code false}.
74       */
75      protected boolean isEnabled(RepositorySystemSession session) {
76          return ConfigUtils.getBoolean(session, false, CONFIG_PROP_PREFIX + this.name);
77      }
78  
79      /**
80       * Uses common {@link DirectoryUtils#resolveDirectory(RepositorySystemSession, String, String, boolean)} to
81       * calculate (and maybe create) basedir for this implementation, never returns {@code null}. The returned
82       * {@link Path} may not exists, if invoked with {@code mayCreate} being {@code false}.
83       * <p>
84       * Default value is {@code ${LOCAL_REPOSITORY}/.checksums}.
85       *
86       * @return The {@link Path} of basedir, never {@code null}.
87       */
88      protected Path getBasedir(RepositorySystemSession session, boolean mayCreate) {
89          try {
90              return DirectoryUtils.resolveDirectory(
91                      session, LOCAL_REPO_PREFIX_DIR, configPropKey(CONF_NAME_BASEDIR), mayCreate);
92          } catch (IOException e) {
93              throw new UncheckedIOException(e);
94          }
95      }
96  
97      /**
98       * Simple {@link RemoteRepositoryFilter.Result} immutable implementation.
99       */
100     protected static class SimpleResult implements RemoteRepositoryFilter.Result {
101         private final boolean accepted;
102 
103         private final String reasoning;
104 
105         public SimpleResult(boolean accepted, String reasoning) {
106             this.accepted = accepted;
107             this.reasoning = requireNonNull(reasoning);
108         }
109 
110         @Override
111         public boolean isAccepted() {
112             return accepted;
113         }
114 
115         @Override
116         public String reasoning() {
117             return reasoning;
118         }
119     }
120 }