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.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
45
46
47
48
49 public static final String CONFIG_PROP_OFFLINE_PROTOCOLS = CONFIG_PROPS_PREFIX + "protocols";
50
51
52
53
54
55
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 }