loginsettingdialog.java

来自「开源项目openfire的完整源程序」· Java 代码 · 共 707 行 · 第 1/3 页

JAVA
707
字号
            setLayout(new GridBagLayout());
            add(useProxyBox, new GridBagConstraints(0, 0, 2, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));

            add(protocolLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
            add(protocolBox, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));

            add(hostLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
            add(hostField, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));

            add(portLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
            add(portField, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));

            add(usernameLabel, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
            add(usernameField, new GridBagConstraints(1, 4, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));

            add(passwordLabel, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
            add(passwordField, new GridBagConstraints(1, 5, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));


            useProxyBox.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    enableFields(useProxyBox.isSelected());
                }
            });

            // Check localSettings
            if (localPreferences.isProxyEnabled()) {
                useProxyBox.setSelected(true);
            }

            enableFields(useProxyBox.isSelected());

            if (ModelUtil.hasLength(localPreferences.getHost())) {
                hostField.setText(localPreferences.getHost());
            }

            if (ModelUtil.hasLength(localPreferences.getPort())) {
                portField.setText(localPreferences.getPort());
            }

            if (ModelUtil.hasLength(localPreferences.getProxyPassword())) {
                passwordField.setText(localPreferences.getProxyPassword());
            }

            if (ModelUtil.hasLength(localPreferences.getProxyUsername())) {
                usernameField.setText(localPreferences.getProxyUsername());
            }

            if (ModelUtil.hasLength(localPreferences.getProtocol())) {
                protocolBox.setSelectedItem(localPreferences.getProtocol());
            }

        }

        /**
         * Enables the fields of the proxy panel.
         *
         * @param enable true if all fields should be enabled, otherwise false.
         */
        private void enableFields(boolean enable) {
            Component[] comps = getComponents();
            for (int i = 0; i < comps.length; i++) {
                if (comps[i] instanceof JTextField || comps[i] instanceof JComboBox) {
                    JComponent comp = (JComponent)comps[i];
                    comp.setEnabled(enable);
                }
            }
        }

        /**
         * Returns true if a proxy is set.
         *
         * @return true if a proxy is set.
         */
        public boolean useProxy() {
            return useProxyBox.isSelected();
        }

        /**
         * Returns the protocol to use for this proxy.
         *
         * @return the protocol.
         */
        public String getProtocol() {
            return (String)protocolBox.getSelectedItem();
        }

        /**
         * Returns the host to use for this proxy.
         *
         * @return the host.
         */
        public String getHost() {
            return hostField.getText();
        }

        /**
         * Returns the port to use with this proxy.
         *
         * @return the port to use.
         */
        public String getPort() {
            return portField.getText();
        }

        /**
         * Returns the username to use with this proxy.
         *
         * @return the username.
         */
        public String getUsername() {
            return usernameField.getText();
        }

        /**
         * Returns the password to use with this proxy.
         *
         * @return the password.
         */
        public String getPassword() {
            return new String(passwordField.getPassword());
        }

        /**
         * Persist the proxy settings to local preferences.
         */
        public void saveProxySettings() {
            localPreferences.setProxyEnabled(useProxyBox.isSelected());
            if (ModelUtil.hasLength(getProtocol())) {
                localPreferences.setProtocol(getProtocol());
            }

            if (ModelUtil.hasLength(getHost())) {
                localPreferences.setHost(getHost());
            }

            if (ModelUtil.hasLength(getPort())) {
                localPreferences.setPort(getPort());
            }

            if (ModelUtil.hasLength(getUsername())) {
                localPreferences.setProxyUsername(getUsername());
            }

            if (ModelUtil.hasLength(getPassword())) {
                localPreferences.setProxyPassword(getPassword());
            }

            if (!localPreferences.isProxyEnabled()) {
                Properties props = System.getProperties();
                props.remove("socksProxyHost");
                props.remove("socksProxyPort");
                props.remove("http.proxyHost");
                props.remove("http.proxyPort");
                props.remove("http.proxySet");
            }
            else {
                String host = localPreferences.getHost();
                String port = localPreferences.getPort();
                String protocol = localPreferences.getProtocol();

                boolean isValid = ModelUtil.hasLength(host) && ModelUtil.hasLength(port);

                if (isValid) {
                    if (protocol.equals("SOCKS")) {
                        System.setProperty("socksProxyHost", host);
                        System.setProperty("socksProxyPort", port);
                    }
                    else {
                        System.setProperty("http.proxySet", "true");

                        // Set https settings
                        System.setProperty("https.proxyHost", host);
                        System.setProperty("https.proxyPort", port);

                        // Set http settings
                        System.setProperty("http.proxyHost", host);
                        System.setProperty("http.proxyPort", port);
                    }
                }
                else {
                    localPreferences.setProxyEnabled(false);
                }
            }

            SettingsManager.saveSettings();
        }
    }

    /**
     * Updates local preferences with auto discovery settings.
     */
    private void updateAutoDiscovery() {
        boolean isSelected = autoDiscoverBox.isSelected();
        xmppHostField.setEnabled(!isSelected);
        portField.setEnabled(!isSelected);
        localPreferences.setHostAndPortConfigured(!isSelected);
    }

    /**
     * Returns the principal name if one exists.
     *
     * @return the name (ex. derek) of the principal.
     * @throws Exception thrown if a Principal was not found.
     */
    private String getPrincipalName() throws Exception {
        System.setProperty("java.security.krb5.debug", "true");
        System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
        GSSAPIConfiguration config = new GSSAPIConfiguration();
        Configuration.setConfiguration(config);

        LoginContext lc = null;
        try {
            lc = new LoginContext("GetPrincipal");
            lc.login();
        }
        catch (LoginException le) {
            Log.debug(le.getMessage());
            return null;
        }

        Subject mySubject = lc.getSubject();

        for (Principal p : mySubject.getPrincipals()) {
            String name = p.getName();
            int indexOne = name.indexOf("@");
            if (indexOne != -1) {
                return name.substring(0, indexOne);
            }
        }
        return null;
    }
}

⌨️ 快捷键说明

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