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;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.util.regex.Pattern;
25  
26  import org.eclipse.aether.ConfigurationProperties;
27  import org.eclipse.aether.RepositorySystemSession;
28  import org.eclipse.aether.impl.OfflineController;
29  import org.eclipse.aether.repository.RemoteRepository;
30  import org.eclipse.aether.transfer.RepositoryOfflineException;
31  import org.eclipse.aether.util.ConfigUtils;
32  
33  import static java.util.Objects.requireNonNull;
34  
35  /**
36   *
37   */
38  @Singleton
39  @Named
40  public class DefaultOfflineController implements OfflineController {
41      private static final String CONFIG_PROPS_PREFIX = ConfigurationProperties.PREFIX_AETHER + "offline.";
42  
43      /**
44       * Comma-separated list of protocols which are supposed to be resolved offline.
45       *
46       * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
47       * @configurationType {@link java.lang.String}
48       */
49      public static final String CONFIG_PROP_OFFLINE_PROTOCOLS = CONFIG_PROPS_PREFIX + "protocols";
50  
51      /**
52       * Comma-separated list of hosts which are supposed to be resolved offline.
53       *
54       * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
55       * @configurationType {@link java.lang.String}
56       */
57      public static final String CONFIG_PROP_OFFLINE_HOSTS = CONFIG_PROPS_PREFIX + "hosts";
58  
59      private static final Pattern SEP = Pattern.compile("\\s*,\\s*");
60  
61      @Override
62      public void checkOffline(RepositorySystemSession session, RemoteRepository repository)
63              throws RepositoryOfflineException {
64          requireNonNull(session, "session cannot be null");
65          requireNonNull(repository, "repository cannot be null");
66          if (isOfflineProtocol(session, repository) || isOfflineHost(session, repository)) {
67              return;
68          }
69  
70          throw new RepositoryOfflineException(repository);
71      }
72  
73      private boolean isOfflineProtocol(RepositorySystemSession session, RemoteRepository repository) {
74          String[] protocols = getConfig(session, CONFIG_PROP_OFFLINE_PROTOCOLS);
75          if (protocols != null) {
76              String protocol = repository.getProtocol();
77              if (!protocol.isEmpty()) {
78                  for (String p : protocols) {
79                      if (p.equalsIgnoreCase(protocol)) {
80                          return true;
81                      }
82                  }
83              }
84          }
85          return false;
86      }
87  
88      private boolean isOfflineHost(RepositorySystemSession session, RemoteRepository repository) {
89          String[] hosts = getConfig(session, CONFIG_PROP_OFFLINE_HOSTS);
90          if (hosts != null) {
91              String host = repository.getHost();
92              if (!host.isEmpty()) {
93                  for (String h : hosts) {
94                      if (h.equalsIgnoreCase(host)) {
95                          return true;
96                      }
97                  }
98              }
99          }
100         return false;
101     }
102 
103     private String[] getConfig(RepositorySystemSession session, String key) {
104         String value = ConfigUtils.getString(session, "", key).trim();
105         if (value.isEmpty()) {
106             return null;
107         }
108         return SEP.split(value);
109     }
110 }