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 when session is 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 when session is 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      /**
60       * Comma-separated list of repository IDs which are supposed to be resolved when session is offline.
61       *
62       * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
63       * @configurationType {@link java.lang.String}
64       * @since 2.0.8
65       */
66      public static final String CONFIG_PROP_OFFLINE_REPOSITORIES = CONFIG_PROPS_PREFIX + "repositories";
67  
68      private static final Pattern SEP = Pattern.compile("\\s*,\\s*");
69  
70      @Override
71      public void checkOffline(RepositorySystemSession session, RemoteRepository repository)
72              throws RepositoryOfflineException {
73          requireNonNull(session, "session cannot be null");
74          requireNonNull(repository, "repository cannot be null");
75          if (isOfflineProtocol(session, repository)
76                  || isOfflineHost(session, repository)
77                  || isOfflineRepository(session, repository)) {
78              return;
79          }
80  
81          throw new RepositoryOfflineException(repository);
82      }
83  
84      private boolean isOfflineProtocol(RepositorySystemSession session, RemoteRepository repository) {
85          String[] protocols = getConfig(session, CONFIG_PROP_OFFLINE_PROTOCOLS);
86          if (protocols != null) {
87              String protocol = repository.getProtocol();
88              if (!protocol.isEmpty()) {
89                  for (String p : protocols) {
90                      if (p.equalsIgnoreCase(protocol)) {
91                          return true;
92                      }
93                  }
94              }
95          }
96          return false;
97      }
98  
99      private boolean isOfflineHost(RepositorySystemSession session, RemoteRepository repository) {
100         String[] hosts = getConfig(session, CONFIG_PROP_OFFLINE_HOSTS);
101         if (hosts != null) {
102             String host = repository.getHost();
103             if (!host.isEmpty()) {
104                 for (String h : hosts) {
105                     if (h.equalsIgnoreCase(host)) {
106                         return true;
107                     }
108                 }
109             }
110         }
111         return false;
112     }
113 
114     private boolean isOfflineRepository(RepositorySystemSession session, RemoteRepository repository) {
115         String[] repositories = getConfig(session, CONFIG_PROP_OFFLINE_REPOSITORIES);
116         if (repositories != null) {
117             String repositoryId = repository.getId();
118             if (!repositoryId.isEmpty()) {
119                 for (String r : repositories) {
120                     if (r.equals(repositoryId)) {
121                         return true;
122                     }
123                 }
124             }
125         }
126         return false;
127     }
128 
129     private String[] getConfig(RepositorySystemSession session, String key) {
130         String value = ConfigUtils.getString(session, "", key).trim();
131         if (value.isEmpty()) {
132             return null;
133         }
134         return SEP.split(value);
135     }
136 }