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

📄 cbopenconwin.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
         *    Add a listener that checks the security level (and hence
         *    which fields are greyed out) everytime something changes
         *    which might affect stuff.
         */

        securityListener = new SecurityActionListener();
        level.addActionListener(securityListener);
        //TE: get the combo box that has the names of the templates to load and add an action listener to it...
        (myTemplater.getLoadComboBox()).addActionListener(securityListener);
    }


    /**
     * Use this method to insert an extra component.  It is intended for
     * adding the DSML label and field under protocol version.  Hopefully
     * there has been a space provided (via display.newLine()) in the
     * initGUI method.  So use something like
     * display.add(urlLabel = new JLabel("   DSML: "), 0,2,1,1);
     * display.addWide(dsmlService = new JTextField("", 30),4);
     * in the overriding method.
     * NOTE: the reason for not just sticking this method call after adding
     * the protocol stuff is so that we don't stuff up the save/load template
     * coordinates that users may have previously saved.  I.e if we did it this
     * way, the rest of the components that are added after the DSML stuff
     * wont have the saved data loaded.  Using display.newLine() acts as
     * a place holder.
     */

    public void addExtraComponent()
    {
    }


    /**
     * Implements ActionListener to call the checkSecurityLevel method.
     *
     * @author Trudi.
     */

    class SecurityActionListener implements ActionListener
    {

        /**
         * Calls the checkSecurityLevel method.
         */

        public void actionPerformed(ActionEvent event)
        {
            checkSecurityLevel();
        }
    }


    /**
     * this simply checks the state of the security level combo box,
     * and grays out components accordingly.
     */

    protected int checkSecurityLevel()
    {

        int selected = level.getSelectedIndex();
        switch (selected)
        {
            case 0:  // anonymous

                setState(false, false);
                return NONE;

            case 1:  // user + password

                setState(true, true);
                return USER_AUTH;

            case 2:  // ssl + anonymous

                //XXX Big Dirty Hack - use password for non-JKS keystores...
                /*                                
                String caKeystoreType     = JXplorer.getProperty("keystoreType.cacerts", "JKS");  
                if ("JKS".equals(caKeystoreType) == false)
                    setState(false, true);                    // XXX HACK XXX
                else
                */
                
                setState(false, false);
                return SSL_NO_AUTH;

            case 3:  // ssl + user + password

                setState(true, true);
                return SSL_USER_AUTH;

            case 4:  // ssl + sasl + password

                setState(false, true);
                return SASL;

            case 5:  // Vadim: GSSAPI

                setState(false, false);
                return GSSAPI;
        }

        return NONE;
    }

    /**
     * Small utility ftn to handle graying out components at the same time
     * as disabling them.  (real graphics toolkits do this for you...)
     */

    private void setState(boolean user, boolean pwd)
    {
        managerDN.setEnabled(user);
        managerDN.setBackground(user ? Color.white : Color.lightGray);

        if (pwd == false)
            password.setText("");

        password.setEnabled(pwd);
        password.setBackground(pwd ? Color.white : Color.lightGray);
    }


    /**
     * Set's title back to 'open ldap connection'/clears password
     */

    public void resetTitleAndPassword()
    {
        this.setTitle(CBIntText.get("Open LDAP Connection"));
        password.setText("");
    }

    protected String getURL()
            throws NumberFormatException, URISyntaxException
    {
        String host = null;
        String portString = null;
        if (hostName != null)
            host = hostName.getText();
        if (port != null)
            portString = port.getText();

        if (host != null) host = host.trim();
        if (portString != null) portString = portString.trim();

        if (host == null || host.length() < 1)
        {
            throw new URISyntaxException("", CBIntText.get("A host name must be entered for JXplorer to connect to."));
        }

        if (portString == null || portString.length() < 1)
        {
            throw new URISyntaxException("", CBIntText.get("A port number must be entered for JXplorer to connect to."));
        }

        int port = Integer.parseInt(portString); // may throw exception

        if (port < 0) throw new NumberFormatException(CBIntText.get("Negative Port Number is illegal"));

        if (port > 65536) throw new NumberFormatException(CBIntText.get("Port Number {0} is illegal", new String[] {portString}));

        return "ldap://" + host + ":" + port;
    }


    /**
     * Over-ride base class method that is called when the OK button is hit.
     */

    public void doOK()
    {

        try
        {
            log.fine("read values: " + hostName.getText() + ":" + port.getText());

            /*
             *    Read Host and Port
             */

            String url = getURL();  // throws exceptions if URL is bad.

            newCon.setURL(url);

            userMessage.setText(CBIntText.get("Opening Connection To ") + url);

            /*
             *   ldap version number
             */

            if (version.getSelectedItem() == LDAPV2)
                newCon.version = 2;
            else // default for both ldap and dsml
                newCon.version = 3;


            /*
             *    Security Magic
             */

            int securityLevel = checkSecurityLevel();

            newCon.userDN = null;
            newCon.clearPasswords();

            newCon.useGSSAPI = false;

            if (securityLevel == USER_AUTH || securityLevel == SSL_USER_AUTH)
            {
                newCon.userDN = managerDN.getText().trim();
                newCon.pwd = password.getPassword();
                if ((newCon.pwd.length) == 0)
                {					//TE: make sure the user has entered a password.
                    throw new Exception(CBIntText.get("No Password Provided.  Please enter a password."));
                }
            }
            else if (securityLevel == SASL)
            {
                newCon.clientKeystorePwd = password.getPassword();
                if ((newCon.clientKeystorePwd.length) == 0)
                {				//TE: make sure the user has entered a password.
                    throw new Exception(CBIntText.get("No Password Provided.  Please enter a password."));
                }
            }
            //Vadim: GSSAPI
            else if (securityLevel == GSSAPI)
            {
                // username & password are only used if an existing kerberos keystore cannot be found;
                // we'll prompt the user for them elsewhere if neccessary.

                newCon.useGSSAPI = true;
            }

            setVisible(false);

            newCon.useSSL = (securityLevel >= SSL_NO_AUTH && securityLevel != GSSAPI);

            newCon.baseDN = baseDN.getText();


        }
        catch (Exception err)
        {   // a bunch of things may throw exceptions; at this stage we haven't tried
            // to contact the directory, so just reset defaults and carry on...

            new CBErrorWin(this, "Error in data provided: "  + err.getMessage(), err); // automatically visible one-shot.

            //JOptionPane.showMessageDialog(this.getContentPane(),
            //        CBIntText.get("Error in data provided.  (probably unable to parse " +
            //        " the port number, or password.) "),
            //        CBIntText.get("Couldn't Connect"), JOptionPane.ERROR_MESSAGE);
            err.printStackTrace();

            password.setText("");
            setVisible(true);

            this.setTitle(CBIntText.get("Couldn't Connect : Try Again"));
            log.warning("User error in openconwin: " + err);
            userMessage.setText(CBIntText.get("Error Opening Connection."));

            return;
        }

        // DO THE ACTUAL WORK!
        // Now the data has been read, send it off to the connect method to make the connection.

        connect(newCon);

    }

    /**
     * This method is called when the user connection data
     * has been gathered and (roughly) checked.
     *
     * @param connectData the parsed connection data containing
     *                    host and port details, security info, etc.
     */

    public abstract void connect(ConnectionData connectData);
}

⌨️ 快捷键说明

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