use of aQute.libg.glob.Glob in project bnd by bndtools.
the class ConnectionSettings method createProxyHandler.
/**
* Create Proxy Handler from ProxyDTO
*/
public static ProxyHandler createProxyHandler(final ProxyDTO proxyDTO) {
return new ProxyHandler() {
Glob[] globs;
private ProxySetup proxySetup;
@Override
public ProxySetup forURL(URL url) throws Exception {
switch(proxyDTO.protocol) {
case DIRECT:
break;
case HTTP:
String scheme = url.getProtocol();
if (scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https")) {
// ok
} else
return null;
break;
case SOCKS:
break;
default:
break;
}
String host = url.getHost();
if (host != null) {
if (isNonProxyHost(host))
return null;
}
if (proxySetup == null) {
proxySetup = new ProxySetup();
if (proxyDTO.username != null && proxyDTO.password != null)
proxySetup.authentication = new PasswordAuthentication(proxyDTO.username, proxyDTO.password.toCharArray());
SocketAddress socketAddress;
if (proxyDTO.host != null)
socketAddress = new InetSocketAddress(proxyDTO.host, proxyDTO.port);
else
socketAddress = new InetSocketAddress(proxyDTO.port);
proxySetup.proxy = new Proxy(proxyDTO.protocol, socketAddress);
}
return proxySetup;
}
public boolean isNonProxyHost(String host) {
Glob[] globs = getNonProxyHosts(proxyDTO);
for (Glob glob : globs) {
if (glob.matcher(host).matches())
return true;
}
return false;
}
public Glob[] getNonProxyHosts(final ProxyDTO proxyDTO) {
// not synchronized because conflicts only do some double work
if (globs == null) {
if (proxyDTO.nonProxyHosts == null)
globs = new Glob[0];
else {
String[] parts = proxyDTO.nonProxyHosts.split("\\s*\\|\\s*");
globs = new Glob[parts.length];
for (int i = 0; i < parts.length; i++) globs[i] = new Glob(parts[i]);
}
}
return globs;
}
};
}
use of aQute.libg.glob.Glob in project bnd by bndtools.
the class ReplacerAdapter method ls.
String ls(String[] args, boolean relative) {
if (args.length < 2)
throw new IllegalArgumentException("the ${ls} macro must at least have a directory as parameter");
File dir = IO.getFile(base, args[1]);
if (!dir.isAbsolute())
throw new IllegalArgumentException("the ${ls} macro directory parameter is not absolute: " + dir);
if (!dir.exists())
throw new IllegalArgumentException("the ${ls} macro directory parameter does not exist: " + dir);
if (!dir.isDirectory())
throw new IllegalArgumentException("the ${ls} macro directory parameter points to a file instead of a directory: " + dir);
List<File> files = new ArrayList<File>(new SortedList<File>(dir.listFiles()));
for (int i = 2; i < args.length; i++) {
Glob filters = new Glob(args[i]);
filters.select(files);
}
ExtList<String> result = new ExtList<String>();
for (File file : files) result.add(relative ? file.getName() : file.getAbsolutePath().replace(File.separatorChar, '/'));
return result.join(",");
}
use of aQute.libg.glob.Glob in project bnd by bndtools.
the class bnd method _action.
@Description("Execute an action on a repo, or if no name is give, list the actions")
public void _action(ActionOptions opts) throws Exception {
Project project = getProject(opts.project());
if (project == null) {
error("Not in a project directory");
return;
}
Glob filter = opts.filter();
if (filter == null)
filter = new Glob("*");
List<Actionable> actionables = project.getPlugins(Actionable.class);
if (actionables.isEmpty()) {
error("No actionables in [%s]", project.getPlugins());
return;
}
for (Actionable o : actionables) {
if (filter.matcher(o.title()).matches()) {
logger.debug("actionable {} - {}", o, o.title());
Map<String, Runnable> map = o.actions();
if (map != null) {
if (opts._arguments().isEmpty()) {
out.printf("# %s%n", o.title());
if (opts.tooltip() && o.tooltip() != null) {
out.printf("%s%n", o.tooltip());
}
out.printf("## actions%n");
for (String entry : map.keySet()) {
out.printf(" %s%n", entry);
}
} else {
for (String entry : opts._arguments()) {
Runnable r = map.get(entry);
if (r != null) {
r.run();
}
}
}
}
}
}
getInfo(project);
}
use of aQute.libg.glob.Glob in project bnd by bndtools.
the class ConnectionSettings method createUrlConnectionHandler.
public URLConnectionHandler createUrlConnectionHandler(ServerDTO serverDTO) {
final Glob match = new Glob(serverDTO.match == null ? serverDTO.id : serverDTO.match);
final BasicAuthentication basic = getBasicAuthentication(serverDTO.username, serverDTO.password);
final HttpsVerification https = new HttpsVerification(serverDTO.trust, serverDTO.verify, getParent());
return new URLConnectionHandler() {
@Override
public boolean matches(URL url) {
String scheme = url.getProtocol().toLowerCase();
StringBuilder address = new StringBuilder();
address.append(scheme).append("://").append(url.getHost());
if (url.getPort() > 0 && url.getPort() != url.getDefaultPort())
address.append(":").append(url.getPort());
return match.matcher(address).matches();
}
@Override
public void handle(URLConnection connection) throws Exception {
if (basic != null)
basic.handle(connection);
if (isHttps(connection) && https != null) {
https.handle(connection);
}
}
boolean isHttps(URLConnection connection) {
return "https".equalsIgnoreCase(connection.getURL().getProtocol());
}
public String toString() {
return "Server [ match=" + match + ", basic=" + basic + ", https=" + https + "]";
}
};
}
use of aQute.libg.glob.Glob in project bnd by bndtools.
the class ConnectionSettings method isActive.
private boolean isActive(ProxyDTO proxy) throws SocketException {
if (!proxy.active)
return false;
String mask = proxy.mask;
if (mask == null)
return true;
String[] clauses = mask.split("\\s*,\\s*");
for (String clause : clauses) try {
String[] parts = clause.split("\\s*:\\s*");
Glob g = new Glob(parts[0]);
byte[] address = null;
int maskLength = 0;
if (parts.length > 1) {
String[] pp = parts[1].split("/");
address = InetAddress.getByName(pp[0]).getAddress();
maskLength = pp.length > 1 ? Integer.parseInt(pp[1]) : address.length * 8;
}
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
NetworkInterface ni = e.nextElement();
if (ni == null)
continue;
if (!ni.isUp())
continue;
if (g.matcher(ni.getName()).matches()) {
if (address == null)
return true;
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
byte[] iaa = ia.getAddress().getAddress();
if (address.length != iaa.length)
continue;
if (maskLength != 0 && ia.getNetworkPrefixLength() != maskLength)
continue;
if (Arrays.equals(address, iaa))
return true;
}
}
}
} catch (Exception e) {
exception(e, "Failed to parse proxy 'mask' clause in settings: %s", clause);
}
return false;
}
Aggregations