com.archivas.clienttools.arcmover.gui.ssl.SSLCertificateAllowDialog.java Source code

Java tutorial

Introduction

Here is the source code for com.archivas.clienttools.arcmover.gui.ssl.SSLCertificateAllowDialog.java

Source

// Copyright 2007 Hitachi Data Systems
// All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

package com.archivas.clienttools.arcmover.gui.ssl;

import java.awt.Dimension;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.*;

import com.archivas.clienttools.arcmover.gui.HCPDataMigrator;
import com.archivas.clienttools.arcutils.profile.AbstractProfileBase;
import com.archivas.clienttools.arcutils.utils.net.SSLCertificateCallback;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class SSLCertificateAllowDialog extends JDialog {

    private static final Logger LOG = Logger.getLogger(SSLCertificateAllowDialog.class.getName());
    private AbstractProfileBase moverProfile;

    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    protected SSLCertificateForm sslCertificateForm;
    protected JLabel issuedToCommonNameLabel;
    protected JLabel issuedByCommonNameLabel;
    protected JLabel validDateRangeLabel;
    protected JTextArea detailsTextArea;
    protected JLabel md5FingerprintLabel;
    protected JLabel issuedToOrganizationLabel;
    protected JLabel issuedByOrganizationLabel;
    protected JLabel issuedToOrganizationalUnitLabel;
    protected JLabel issuedByOrganizationalUnitLabel;
    protected JLabel serialNumberLabel;
    protected JLabel shaFingerprintLabel;
    protected JLabel validityLabel;

    protected SSLCertificateCallback.AllowSSLCert allowCert = SSLCertificateCallback.AllowSSLCert.NO;

    public SSLCertificateAllowDialog(AbstractProfileBase moverProfile) {
        this.moverProfile = moverProfile;
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        setIconImage(SSLCertificateForm.sslFrameIcon.getImage());

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        pack();
    }

    private void onOK() {
        try {
            allowCert = SSLCertificateCallback.AllowSSLCert.NO;
            if (sslCertificateForm.getPermStoreCertCheckBox() != null) {
                if (sslCertificateForm.getPermStoreCertCheckBox().isSelected()) {
                    allowCert = SSLCertificateCallback.AllowSSLCert.PERMANENTLY_SAVE;
                } else {
                    allowCert = SSLCertificateCallback.AllowSSLCert.THIS_SESSION_ONLY;
                }
            }
        } catch (Exception e1) {
            String msg = "Unexpected Error";
            LOG.log(Level.WARNING, msg, e1);
            JOptionPane.showMessageDialog(this, msg, "Error", JOptionPane.ERROR_MESSAGE);
        } finally {
            dispose();
        }
    }

    private void onCancel() {
        try {
            allowCert = SSLCertificateCallback.AllowSSLCert.NO;
        } catch (Exception e1) {
            String msg = "Unexpected Error";
            LOG.log(Level.WARNING, msg, e1);
            JOptionPane.showMessageDialog(this, msg, "Error", JOptionPane.ERROR_MESSAGE);
        } finally {
            dispose();
        }
    }

    public SSLCertificateCallback.AllowSSLCert getAllowCert() {
        return allowCert;
    }

    public void setAllowCert(final SSLCertificateCallback.AllowSSLCert allowCert) {
        this.allowCert = allowCert;
    }

    public AbstractProfileBase getMoverProfile() {
        return moverProfile;
    }

    public void setMoverProfile(final AbstractProfileBase profile) {
        this.moverProfile = profile;
    }

    public void setVisible(boolean visible) {
        if (visible) {
            sslCertificateForm.setSSLCertChain(getMoverProfile().getSSLCertChain());
            sslCertificateForm.setPermStoreCertCheckBoxVisible(true);
            sslCertificateForm.setPermStoreCertCheckBoxEnabled(true);
            pack();
        }
        super.setVisible(visible);
    }

    public void setWarningText(String text) {
        sslCertificateForm.setOtherWarningsText(text);
    }

    {
        // GUI initializer generated by IntelliJ IDEA GUI Designer
        // >>> IMPORTANT!! <<<
        // DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer >>> IMPORTANT!! <<< DO NOT edit this method OR
     * call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPane = new JPanel();
        contentPane.setLayout(new FormLayout("fill:4dlu:noGrow,fill:d:grow,fill:4dlu:noGrow",
                "center:4dlu:noGrow,center:d:grow,top:4dlu:noGrow,center:max(d;15dlu):noGrow,center:4dlu:noGrow"));
        contentPane.setMinimumSize(new Dimension(300, 300));
        contentPane.setPreferredSize(new Dimension(500, 500));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new FormLayout(
                "fill:d:grow,left:4dlu:noGrow,fill:max(d;50dlu):noGrow,left:4dlu:noGrow,fill:max(d;50dlu):noGrow",
                "center:d:grow"));
        panel1.setPreferredSize(new Dimension(300, 24));
        CellConstraints cc = new CellConstraints();
        contentPane.add(panel1, cc.xy(2, 4));
        buttonOK = new JButton();
        buttonOK.setText("Confirm Security Exception");
        buttonOK.setToolTipText("Accept this cerficate");
        panel1.add(buttonOK, cc.xy(3, 1, CellConstraints.DEFAULT, CellConstraints.FILL));
        buttonCancel = new JButton();
        buttonCancel.setText("Cancel");
        panel1.add(buttonCancel, cc.xy(5, 1));
        sslCertificateForm = new SSLCertificateForm();
        contentPane.add(sslCertificateForm.$$$getRootComponent$$$(),
                cc.xy(2, 2, CellConstraints.FILL, CellConstraints.FILL));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return contentPane;
    }
}