⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectiondata.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

        pwd = null;
        caKeystorePwd = null;
        clientKeystorePwd = null;
    }


    /**
     * Sets the url from the host & port, e.g. "ldap://" + host + ":" + port".
     * (NB: If the protocol is <i>NOT</i> LDAP, (e.g. DSML) this must be set first.
     *
     * @param host the host name to connect to, e.g. echidna or 168.10.5.122.
     * @param port the host port to connect to, e.g. 19389.
     */

    public void setURL(String host, int port)
    {
        if (protocol == LDAP)
            url = "ldap://" + host + ":" + port;
        else if (protocol == DSML)
            url = "http://" + host + ":" + port;

    }

    /**
     * Sets the url from the host & port, e.g. "ldap://" + host + ":" + port".
     * (NB: If the protocol is <i>NOT</i> LDAP, (e.g. DSML) this must be set first.
     *
     * @param URL The full URL to connect to
     */

    public void setURL(String URL)
    {
        if (protocol == LDAP)
        {
            if (URL.toLowerCase().startsWith("ldap://"))
                url = URL;
            else
                url = "ldap://" + URL;
        }
        else if (protocol == DSML)
        {
            if (URL.toLowerCase().startsWith("http://"))
                url = URL;
            else if (URL.toLowerCase().startsWith("dsml://"))
                url = "http://" + URL.substring(7);
            else
                url = "http://" + URL;
        }
        else    // not sure if this is necessary...
        {
            if (URL.toLowerCase().startsWith("ldap:"))
            {
                protocol = LDAP;
                url = URL;
            }
            else if (URL.toLowerCase().startsWith("http:"))
            {
                protocol = DSML;
                url = URL;
            }
            else if (URL.toLowerCase().startsWith("dsml:"))
            {
                protocol = DSML;
                url = "http:" + URL.substring(5);
            }
        }
    }

    public String getURL()
    {
        return url;
    }

    /**
     * Gets the host name from the url string.
     *
     * @return the host name for example: DEMOCORP.
     */

    // parse rules; the url is always of the form <protocol>://<hostname>:<port>[/server stuff (for dsml only)]

    public String getHost()
    {
        if (url == null)
            return null;

        int protocolSeparator = url.indexOf("://") + 3;
        int portSeparator = url.indexOf(":", protocolSeparator);
        return url.substring(protocolSeparator, portSeparator);
    }


    /**
     * Gets the port number from the url string.
     *
     * @return the port number for example: 19389.
     */

    public int getPort()
    {
        if (url == null)
            return -1;

        try
        {
            int protocolSeparator = url.indexOf("://") + 3;
            int portSeparator = url.indexOf(":", protocolSeparator) + 1;
            int serverDetails = url.indexOf("/", portSeparator);

            String port = (serverDetails == -1) ? url.substring(portSeparator) : url.substring(portSeparator, serverDetails);
            int portNumber = Integer.parseInt(port);
            if (portNumber > 65536 || portNumber <= 0)
                return -1;

            return portNumber;
        }
        catch (NumberFormatException nfe)
        {
            return -1;
        }
    }


    /**
     * Returns this data object as a string (doesn't include passwords)..
     *
     * @return the data object as a string.
     */

    public String toString()
    {
        return new String("baseDN: " + baseDN +
                "\nversion: " + Integer.toString(version) +
                "\nurl: " + url +
                "\nuserDN: " + userDN +
                "\nreferralType: " + referralType +
                "\naliasType: " + aliasType +
                "\nuseSSL: " + String.valueOf(useSSL) +
                "\ncacerts: " + cacerts +
                "\nclientcerts: " + clientcerts +
                "\ncaKeystoreType: " + caKeystoreType +
                "\nclientKeystoreType: " + clientKeystoreType +
                "\ncaKeystorePwd; " + new String(caKeystorePwd) +
                "\nclientKeystorePwd: " + new String(clientKeystorePwd) +
                "\ntracing: " + String.valueOf(tracing) +
                "\nprotocol: " + protocol +
                "\nsslSocketFactory: " + sslSocketFactory +
                "\nuseGSSAPI: " + String.valueOf(useGSSAPI));
    }

    /**
     * This returns the connection data as a jndi env object suitable for
     * use in opening a directory context.
     *
     * @return
     * @throws NamingException a large range of exceptions, ranging from invalid data through
     *                         to problems setting up the SSL connection.
     */
    public Hashtable getJNDIEnvironment()
            throws NamingException
    {
/*
   These are the ConnectionData variables to be placed into the hash table...

        version
        url
        userDN
        pwd
        referralType
        aliasType
        useSSL
        cacerts
        clientcerts
        caKeystorePwd
        clientKeystorePwd
        caKeystoreType
        clientKeystoreType
        tracing
        sslTracing
        useGSSAPI

        ... while 'extraProperties' is simply appended to the environment data
*/
        checkData();  // throws NamingException if data invalid.

        Hashtable env = new Hashtable();  // an environment for jndi context parameters

        if (protocol == DSML)       //TE: set the protocol to DSML or LDAP.
            env.put(Context.INITIAL_CONTEXT_FACTORY, DEFAULT_DSML_CTX);
        else if (protocol == LDAP)
            env.put(Context.INITIAL_CONTEXT_FACTORY, DEFAULT_CTX);

        JNDIOps.setupBasicProperties(env, url, tracing, referralType, aliasType);       // set up the bare minimum parameters


        // XXX should we pass the security type through to here to avoid this implicit check??
        // XXX evil warning - this sets Context.SECURITY_AUTHENTICATION to simple, it is over-ridden
        // XXX by other types below.
        if (pwd != null && userDN != null)
        {
            JNDIOps.setupSimpleSecurityProperties(env, userDN, pwd);
        }
        // add the SSL ('ca...') and possible SASL ('client...') parameters
        if (useSSL)
        {

            if (tracing)
                sslTracing = true;  // for the time being, bind tracing and sslTracing.

            JNDIOps.setupSSLProperties(env, cacerts, clientcerts,
                    caKeystorePwd, clientKeystorePwd,
                    caKeystoreType, clientKeystoreType,
                    sslTracing, sslSocketFactory);
        }

        // Vadim: GSSAPI

        if (useGSSAPI)
        {
            env.put(Context.SECURITY_AUTHENTICATION, "GSSAPI");
            //Maybe include something like JNDIOps.setupKerberosProperties here??
        }

        // Add any 'extra' properties to the list.
        if (extraProperties!=null && extraProperties.size()>0)
        {
            Enumeration extraKeys = extraProperties.keys();
            while (extraKeys.hasMoreElements())
            {
                try
                {
                    String key = (String)extraKeys.nextElement();
                    String value = (String)extraProperties.getProperty(key);
                    if (value != null)
                        env.put(key, value);
                }
                catch (ClassCastException e) {} // do nothing, but skip non string properties (should never happen)
            }
        }

        return env;
    }

    /**
     * This method confirms that the data entered in the ConnectionData object is
     * consistent, complete and valid.
     *
     * @throws NamingException thrown if the data is inconsistent or incomplete.
     */
    public void checkData() throws NamingException
    {
        // sanity check
        if (url == null)
            throw new NamingException("URL not specified in openContext()!");

        if (version < 2 || version > 3)
            throw new NamingException("Incorrect ldap Version! (was " + version + ")");

        if (useSSL && (cacerts == null))
            throw new NamingException("Cannot use SSL without a trusted CA certificates JKS file.");

        if (referralType == null) referralType = "follow";  // not an error not to specify this.

        if (aliasType == null) aliasType = "finding"; // not an error not to specify this

        if ("followthrowignore".indexOf(referralType) == -1)
            throw new NamingException("unknown referral type: " + referralType + " (setting to 'follow')");
    }

    public void putExtraProperty(String key, String property)
    {
        if (extraProperties==null)
            extraProperties = new Properties();

        extraProperties.put(key, property);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -