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.RepositorySystemSession;
27  import org.eclipse.aether.impl.OfflineController;
28  import org.eclipse.aether.repository.RemoteRepository;
29  import org.eclipse.aether.transfer.RepositoryOfflineException;
30  import org.eclipse.aether.util.ConfigUtils;
31  
32  import static java.util.Objects.requireNonNull;
33  
34  /**
35   *
36   */
37  @Singleton
38  @Named
39  public class DefaultOfflineController implements OfflineController {
40  
41      static final String CONFIG_PROP_OFFLINE_PROTOCOLS = "aether.offline.protocols";
42  
43      static final String CONFIG_PROP_OFFLINE_HOSTS = "aether.offline.hosts";
44  
45      private static final Pattern SEP = Pattern.compile("\\s*,\\s*");
46  
47      @Override
48      public void checkOffline(RepositorySystemSession session, RemoteRepository repository)
49              throws RepositoryOfflineException {
50          requireNonNull(session, "session cannot be null");
51          requireNonNull(repository, "repository cannot be null");
52          if (isOfflineProtocol(session, repository) || isOfflineHost(session, repository)) {
53              return;
54          }
55  
56          throw new RepositoryOfflineException(repository);
57      }
58  
59      private boolean isOfflineProtocol(RepositorySystemSession session, RemoteRepository repository) {
60          String[] protocols = getConfig(session, CONFIG_PROP_OFFLINE_PROTOCOLS);
61          if (protocols != null) {
62              String protocol = repository.getProtocol();
63              if (!protocol.isEmpty()) {
64                  for (String p : protocols) {
65                      if (p.equalsIgnoreCase(protocol)) {
66                          return true;
67                      }
68                  }
69              }
70          }
71          return false;
72      }
73  
74      private boolean isOfflineHost(RepositorySystemSession session, RemoteRepository repository) {
75          String[] hosts = getConfig(session, CONFIG_PROP_OFFLINE_HOSTS);
76          if (hosts != null) {
77              String host = repository.getHost();
78              if (!host.isEmpty()) {
79                  for (String h : hosts) {
80                      if (h.equalsIgnoreCase(host)) {
81                          return true;
82                      }
83                  }
84              }
85          }
86          return false;
87      }
88  
89      private String[] getConfig(RepositorySystemSession session, String key) {
90          String value = ConfigUtils.getString(session, "", key).trim();
91          if (value.isEmpty()) {
92              return null;
93          }
94          return SEP.split(value);
95      }
96  }