Search in sources :

Example 11 with Glob

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;
        }
    };
}
Also used : ProxyHandler(aQute.bnd.service.url.ProxyHandler) InetSocketAddress(java.net.InetSocketAddress) URL(java.net.URL) Proxy(java.net.Proxy) Glob(aQute.libg.glob.Glob) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) PasswordAuthentication(java.net.PasswordAuthentication)

Example 12 with Glob

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(",");
}
Also used : ExtList(aQute.lib.collections.ExtList) ArrayList(java.util.ArrayList) Glob(aQute.libg.glob.Glob) File(java.io.File)

Example 13 with Glob

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);
}
Also used : Project(aQute.bnd.build.Project) Actionable(aQute.bnd.service.Actionable) Glob(aQute.libg.glob.Glob) Description(aQute.lib.getopt.Description)

Example 14 with Glob

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 + "]";
        }
    };
}
Also used : URLConnectionHandler(aQute.bnd.service.url.URLConnectionHandler) HttpsVerification(aQute.bnd.url.HttpsVerification) Glob(aQute.libg.glob.Glob) BasicAuthentication(aQute.bnd.url.BasicAuthentication) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 15 with Glob

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;
}
Also used : Enumeration(java.util.Enumeration) InterfaceAddress(java.net.InterfaceAddress) Glob(aQute.libg.glob.Glob) NetworkInterface(java.net.NetworkInterface) SocketException(java.net.SocketException)

Aggregations

Glob (aQute.libg.glob.Glob)22 ArrayList (java.util.ArrayList)11 File (java.io.File)7 Parameters (aQute.bnd.header.Parameters)3 Description (aQute.lib.getopt.Description)3 IOException (java.io.IOException)3 Project (aQute.bnd.build.Project)2 Attrs (aQute.bnd.header.Attrs)2 Domain (aQute.bnd.osgi.Domain)2 Jar (aQute.bnd.osgi.Jar)2 CommandData (aQute.jpm.lib.CommandData)2 Service (aQute.jpm.lib.Service)2 ServiceData (aQute.jpm.lib.ServiceData)2 ExtList (aQute.lib.collections.ExtList)2 InputStream (java.io.InputStream)2 SocketException (java.net.SocketException)2 URL (java.net.URL)2 Matcher (java.util.regex.Matcher)2 Container (aQute.bnd.build.Container)1 PomFromManifest (aQute.bnd.maven.PomFromManifest)1