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      public DefaultOfflineController() {
48          // enables default constructor
49      }
50  
51      public void checkOffline(RepositorySystemSession session, RemoteRepository repository)
52              throws RepositoryOfflineException {
53          requireNonNull(session, "session cannot be null");
54          requireNonNull(repository, "repository cannot be null");
55          if (isOfflineProtocol(session, repository) || isOfflineHost(session, repository)) {
56              return;
57          }
58  
59          throw new RepositoryOfflineException(repository);
60      }
61  
62      private boolean isOfflineProtocol(RepositorySystemSession session, RemoteRepository repository) {
63          String[] protocols = getConfig(session, CONFIG_PROP_OFFLINE_PROTOCOLS);
64          if (protocols != null) {
65              String protocol = repository.getProtocol();
66              if (protocol.length() > 0) {
67                  for (String p : protocols) {
68                      if (p.equalsIgnoreCase(protocol)) {
69                          return true;
70                      }
71                  }
72              }
73          }
74          return false;
75      }
76  
77      private boolean isOfflineHost(RepositorySystemSession session, RemoteRepository repository) {
78          String[] hosts = getConfig(session, CONFIG_PROP_OFFLINE_HOSTS);
79          if (hosts != null) {
80              String host = repository.getHost();
81              if (host.length() > 0) {
82                  for (String h : hosts) {
83                      if (h.equalsIgnoreCase(host)) {
84                          return true;
85                      }
86                  }
87              }
88          }
89          return false;
90      }
91  
92      private String[] getConfig(RepositorySystemSession session, String key) {
93          String value = ConfigUtils.getString(session, "", key).trim();
94          if (value.isEmpty()) {
95              return null;
96          }
97          return SEP.split(value);
98      }
99  }