eu.europa.ec.markt.dss.applet.PersonalDataPanel.java Source code

Java tutorial

Introduction

Here is the source code for eu.europa.ec.markt.dss.applet.PersonalDataPanel.java

Source

/*
 * DSS - Digital Signature Services
 *
 * Copyright (C) 2011 European Commission, Directorate-General Internal Market and Services (DG MARKT), B-1049 Bruxelles/Brussel
 *
 * Developed by: 2011 ARHS Developments S.A. (rue Nicolas Bov 2B, L-1253 Luxembourg) http://www.arhs-developments.com
 *
 * This file is part of the "DSS - Digital Signature Services" project.
 *
 * "DSS - Digital Signature Services" is free software: you can redistribute it and/or modify it under the terms of
 * the GNU Lesser General Public License as published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * DSS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * "DSS - Digital Signature Services".  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * PersonalDataPanel.java
 *
 * Created on 28-Apr-2011, 10:04:58
 */
package eu.europa.ec.markt.dss.applet;

import eu.europa.ec.markt.dss.applet.model.SignatureWizardModel;
import eu.europa.ec.markt.dss.applet.wizard.AbstractWizardPanel;
import eu.europa.ec.markt.dss.signature.SignaturePolicy;

import java.util.logging.Logger;

import org.apache.commons.codec.binary.Base64;

/**
 * Additional data for signature meta-data (claimed role, signature policy)
 * 
 * 
 *
 * @version $Revision: 1867 $ - $Date: 2013-04-08 13:44:56 +0200 (Mon, 08 Apr 2013) $
 */

@SuppressWarnings("serial")
public class PersonalDataPanel extends AbstractWizardPanel {

    private static final Logger LOG = Logger.getLogger(PersonalDataPanel.class.getName());

    public final static String ID = "PERSONAL_DATA";
    private SignatureWizardModel model;

    /** Creates new form PersonalDataPanel */
    public PersonalDataPanel(final SignatureWizardModel model) {
        this.model = model;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
     * content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        implicitExplicitSP = new javax.swing.ButtonGroup();
        claimedRoleText = new javax.swing.JTextField();
        signaturePolicyOid = new javax.swing.JTextField();
        implicitRadioButton = new javax.swing.JRadioButton();
        explicitRadioButton = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        hashAlgo = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        hashValue = new javax.swing.JTextField();
        includeSigPolicy = new javax.swing.JCheckBox();
        includeSignerClaimedRole = new javax.swing.JCheckBox();

        setBackground(new java.awt.Color(255, 255, 255));

        claimedRoleText.setName("signer_claimed_role"); // NOI18N

        signaturePolicyOid.setName("oid"); // NOI18N

        implicitRadioButton.setBackground(new java.awt.Color(255, 255, 255));
        implicitExplicitSP.add(implicitRadioButton);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("eu/europa/ec/markt/dss/applet/i18n"); // NOI18N
        implicitRadioButton.setText(bundle.getString("IMPLICIT")); // NOI18N
        implicitRadioButton.setName("implicit"); // NOI18N
        implicitRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                implicitRadioButtonActionPerformed(evt);
            }
        });

        explicitRadioButton.setBackground(new java.awt.Color(255, 255, 255));
        implicitExplicitSP.add(explicitRadioButton);
        explicitRadioButton.setText(bundle.getString("EXPLICIT")); // NOI18N
        explicitRadioButton.setName("explicit"); // NOI18N
        explicitRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                explicitRadioButtonActionPerformed(evt);
            }
        });

        jLabel2.setText(bundle.getString("OID")); // NOI18N

        jLabel3.setText(bundle.getString("HASH_ALGORITHM")); // NOI18N

        hashAlgo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "SHA1" }));
        hashAlgo.setName("hash_algo"); // NOI18N

        jLabel4.setText(bundle.getString("HASH_VALUE")); // NOI18N

        hashValue.setName("hash_value"); // NOI18N

        includeSigPolicy.setBackground(new java.awt.Color(255, 255, 255));
        includeSigPolicy.setFont(new java.awt.Font("Tahoma", 1, 11));
        includeSigPolicy.setText(bundle.getString("SIGNATURE_POLICY")); // NOI18N
        includeSigPolicy.setName("signature_policy_check"); // NOI18N
        includeSigPolicy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                includeSigPolicyActionPerformed(evt);
            }
        });

        includeSignerClaimedRole.setBackground(new java.awt.Color(255, 255, 255));
        includeSignerClaimedRole.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        includeSignerClaimedRole.setText(bundle.getString("CLAIMED_ROLE")); // NOI18N
        includeSignerClaimedRole.setName("signer_claimed_role_checkbox"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup().addComponent(includeSignerClaimedRole)
                                        .addPreferredGap(
                                                javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(claimedRoleText, javax.swing.GroupLayout.DEFAULT_SIZE, 237,
                                                Short.MAX_VALUE))
                                .addGroup(layout.createSequentialGroup().addComponent(includeSigPolicy)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(explicitRadioButton).addComponent(implicitRadioButton)
                                                .addGroup(layout.createSequentialGroup().addGap(21, 21, 21)
                                                        .addGroup(layout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addGroup(layout.createSequentialGroup()
                                                                        .addComponent(jLabel3)
                                                                        .addPreferredGap(
                                                                                javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                        .addComponent(hashAlgo, 0, 158,
                                                                                Short.MAX_VALUE))
                                                                .addGroup(layout.createSequentialGroup()
                                                                        .addComponent(jLabel2)
                                                                        .addPreferredGap(
                                                                                javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                        .addComponent(signaturePolicyOid,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                211, Short.MAX_VALUE))
                                                                .addGroup(layout.createSequentialGroup()
                                                                        .addComponent(jLabel4)
                                                                        .addPreferredGap(
                                                                                javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                        .addComponent(hashValue,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                131, Short.MAX_VALUE)))))))
                        .addGap(14, 14, 14)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(claimedRoleText, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(includeSignerClaimedRole))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(implicitRadioButton).addComponent(includeSigPolicy))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(explicitRadioButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(signaturePolicyOid, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3).addComponent(hashAlgo,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel4).addComponent(hashValue,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(142, Short.MAX_VALUE)));
    }// </editor-fold>//GEN-END:initComponents

    private void explicitRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_explicitRadioButtonActionPerformed
        showHideExplicitPolicy(true);
    }// GEN-LAST:event_explicitRadioButtonActionPerformed

    private void implicitRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_implicitRadioButtonActionPerformed
        showHideExplicitPolicy(false);
    }// GEN-LAST:event_implicitRadioButtonActionPerformed

    private void includeSigPolicyActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_includeSigPolicyActionPerformed
        if (includeSigPolicy.isSelected()) {
            explicitRadioButton.setVisible(true);
            implicitRadioButton.setVisible(true);
            implicitExplicitSP.clearSelection();
            showHideExplicitPolicy(false);
        } else {
            explicitRadioButton.setVisible(false);
            implicitRadioButton.setVisible(false);
            showHideExplicitPolicy(false);
        }
    }// GEN-LAST:event_includeSigPolicyActionPerformed

    private void showHideExplicitPolicy(boolean show) {
        jLabel2.setVisible(show);
        signaturePolicyOid.setVisible(show);
        jLabel3.setVisible(show);
        hashAlgo.setVisible(show);
        jLabel4.setVisible(show);
        hashValue.setVisible(show);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField claimedRoleText;
    private javax.swing.JRadioButton explicitRadioButton;
    private javax.swing.JComboBox hashAlgo;
    private javax.swing.JTextField hashValue;
    private javax.swing.ButtonGroup implicitExplicitSP;
    private javax.swing.JRadioButton implicitRadioButton;
    private javax.swing.JCheckBox includeSigPolicy;
    private javax.swing.JCheckBox includeSignerClaimedRole;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField signaturePolicyOid;

    // End of variables declaration//GEN-END:variables

    @Override
    public Object getNextPanelDescriptor() {
        return SaveDocumentPanel.ID;
    }

    @Override
    public Object getBackPanelDescriptor() {
        return ChooseCertificatePanel.ID;
    }

    @Override
    public Object getPanelDescriptorIdentifier() {
        return ID;
    }

    @Override
    public void aboutToHidePanel() {
        if (includeSignerClaimedRole.isSelected()) {
            /*
             * If the claimed role is include but the value is empty, then the value is set a empty string.
             */
            if (claimedRoleText.getText() != null && claimedRoleText.getText().trim().length() > 0) {
                model.setClaimedRole(claimedRoleText.getText());
            } else {
                model.setClaimedRole("");
            }
        } else {
            model.setClaimedRole(null);
        }

        model.setSignaturePolicyType(SignaturePolicy.NO_POLICY);
        if (includeSigPolicy.isSelected()) {
            if (implicitRadioButton.isSelected()) {
                model.setSignaturePolicyType(SignaturePolicy.IMPLICIT);
            } else if (explicitRadioButton.isSelected()) {
                if (signaturePolicyOid.getText() != null && signaturePolicyOid.getText().trim().length() > 0
                        && hashAlgo.getSelectedItem() != null) {
                    model.setSignaturePolicy(signaturePolicyOid.getText());
                    model.setSignaturePolicyAlgo(hashAlgo.getSelectedItem().toString());
                    model.setSignaturePolicyValue(Base64.decodeBase64(hashValue.getText()));
                    model.setSignaturePolicyType(SignaturePolicy.EXPLICIT);
                } else {
                    LOG.warning("Explicit SignaturePolicy but parameters incomplete. Select NO_SIG_POLICY");
                }
            }
        }
    }

    private boolean mustDisplaySignaturePolicy() {
        return !model.isPreconfiguredSignaturePolicy()
                && (model.getSignaturePolicy() == null && !model.getSignatureFormat().endsWith("-BES"));
    }

    @Override
    public void aboutToDisplayPanel() {
        getWizard().setStepsProgression(6);
        if (mustDisplaySignaturePolicy()) {
            signaturePolicyOid.setVisible(true);
            includeSigPolicy.setVisible(true);
        } else {
            signaturePolicyOid.setVisible(false);
            includeSigPolicy.setVisible(false);
            includeSigPolicy.setSelected(false);
        }
        if (!includeSigPolicy.isSelected()) {
            explicitRadioButton.setVisible(false);
            implicitRadioButton.setVisible(false);
            showHideExplicitPolicy(false);
        } else {
            explicitRadioButton.setVisible(true);
            implicitRadioButton.setVisible(true);
            if (explicitRadioButton.isSelected()) {
                showHideExplicitPolicy(true);
            } else {
                showHideExplicitPolicy(false);
            }
        }
    }
}