1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }