KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > crypto > TruststoreView


1 package SnowMailClient.crypto;
2
3 import javax.security.auth.x500.X500Principal JavaDoc;
4 import snow.utils.DateUtils;
5 import java.security.cert.*;
6 import snow.texteditor.SimpleDocument;
7 import java.awt.Insets JavaDoc;
8 import SnowMailClient.Language.Language;
9 import java.awt.event.*;
10 import snow.utils.gui.Icons;
11 import java.awt.FlowLayout JavaDoc;
12 import javax.swing.text.*;
13 import javax.swing.event.*;
14 import javax.swing.event.ListSelectionListener JavaDoc;
15 import java.awt.Component JavaDoc;
16 import java.security.cert.X509Certificate JavaDoc;
17 import java.awt.BorderLayout JavaDoc;
18 import SnowMailClient.MailEngine.VerifiedSSLSocketFactory;
19 import javax.swing.*;
20
21 /** todo: better view. (use inet address as dummy alias, perform stats, last use, ...)
22 */

23 public final class TruststoreView extends JDialog
24 {
25    private final SimpleDocument viewDoc = new SimpleDocument();
26    private final JTextPane viewPane = new JTextPane(viewDoc);
27    private final JList list = new JList();
28
29    public TruststoreView(JFrame parent)
30    {
31       super(parent, "Truststore view", false);
32
33       try
34       {
35         setTitle(Language.translate("Truststore view for")+" "
36             +VerifiedSSLSocketFactory.getInstance().getTruststoreFile());
37       }
38       catch(Exception JavaDoc e)
39       {
40          JOptionPane.showMessageDialog(parent,
41            ""+e.getMessage(), Language.translate("Cannot setup SSL factory"), JOptionPane.ERROR_MESSAGE);
42          return;
43       }
44
45       JPanel cpan= new JPanel(new BorderLayout JavaDoc());
46       cpan.add(new JScrollPane(viewPane), BorderLayout.CENTER);
47
48       JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
49         new JScrollPane(list), cpan);
50       sp.setOneTouchExpandable(true);
51       sp.setDividerLocation(100);
52       add(sp, BorderLayout.CENTER);
53
54       JPanel cp = new JPanel(new FlowLayout JavaDoc(FlowLayout.LEFT,5,2));
55       cpan.add(cp, BorderLayout.SOUTH);
56       JButton remove = new JButton("remove", new Icons.CrossIcon(10,10,true));
57       remove.setMargin(new Insets JavaDoc(0,1,0,1));
58       cp.add(remove);
59       remove.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) {
60            X509Certificate JavaDoc ci = (X509Certificate JavaDoc) list.getSelectedValue();
61            if(ci!=null)
62            {
63               int rep = JOptionPane.showConfirmDialog(TruststoreView.this,
64                  Language.translate("Are you sure you want to remove this certificate from your truststore ?"),
65                 Language.translate("Certificate deletion confirmation"), JOptionPane.YES_NO_CANCEL_OPTION);
66               if(rep!=JOptionPane.YES_OPTION) return;
67
68               try
69               {
70                 VerifiedSSLSocketFactory.getInstance().removeTrusted(ci);
71                 updateCertListModel();
72               }
73               catch(Exception JavaDoc e)
74               {
75                  JOptionPane.showMessageDialog(TruststoreView.this,
76                    Language.translate("can't remove certificate")+":\n "+e.getMessage(),
77                    Language.translate("Error"), JOptionPane.ERROR_MESSAGE);
78                  e.printStackTrace();
79               }
80
81            }
82       } });
83
84       setSize(700,650);
85       setLocationRelativeTo(parent);
86       setVisible(true); // NOT MODAL
87

88       list.setCellRenderer(new DefaultListCellRenderer()
89       {
90          public Component JavaDoc getListCellRendererComponent( JList list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus )
91          {
92             X509Certificate JavaDoc ci = (X509Certificate JavaDoc) value;
93             String JavaDoc vt = ""+ci.getIssuerX500Principal();
94             return super.getListCellRendererComponent(list, vt, index, isSelected, cellHasFocus );
95          }
96       });
97
98       list.addListSelectionListener(new ListSelectionListener JavaDoc()
99       {
100          public void valueChanged( ListSelectionEvent e ) {
101             X509Certificate JavaDoc ci = (X509Certificate JavaDoc) list.getSelectedValue();
102             setCertToView(ci);
103          }
104       });
105
106       updateCertListModel();
107    }
108
109    private void setCertToView(X509Certificate JavaDoc ci)
110    {
111       viewDoc.setText("");
112       if(ci==null) return ;
113
114       viewDoc.appendSection("Resume:\r\n\r\n");
115
116       viewDoc.appendBold( "Validity:\t");
117       viewDoc.append(" from "+DateUtils.formatDateAndTimeFix(ci.getNotBefore().getTime())
118          +" to "+DateUtils.formatDateAndTimeFix(ci.getNotAfter().getTime())+" " );
119       try
120       {
121          ci.checkValidity();
122          viewDoc.append("currently valid");
123       }
124       catch(CertificateExpiredException cfe)
125       {
126          viewDoc.appendError("no more valid!");
127       }
128       catch(CertificateNotYetValidException cnfe)
129       {
130          viewDoc.appendError("not yet valid!");
131       }
132
133       viewDoc.appendBold( "\r\nSerial number:\t");
134       viewDoc.append(""+ci.getSerialNumber());
135
136       viewDoc.appendBold( "\r\nType:\t");
137       viewDoc.append(""+ci.getType());
138
139       X500Principal JavaDoc x500Principal = ci.getIssuerX500Principal();
140       viewDoc.appendBold( "\r\nX.500 name:\t");
141
142       viewDoc.append(""+ x500Principal.getName("RFC1779"));
143
144       viewDoc.append("\r\n\r\n");
145
146       viewDoc.appendSection("Complete Content:\r\n");
147       viewDoc.append(""+ci);
148
149       viewPane.setCaretPosition(0);
150
151    }
152
153    private void updateCertListModel()
154    {
155
156       DefaultListModel lm = new DefaultListModel();
157       try
158       {
159          for(X509Certificate JavaDoc ci :VerifiedSSLSocketFactory.getInstance().getAcceptedIssuers())
160          {
161             lm.addElement(ci);
162          }
163       } catch(Exception JavaDoc e)
164       {
165          JOptionPane.showMessageDialog(TruststoreView.this,
166            Language.translate("can't update certificate list")+":\n "+e.getMessage(),
167            Language.translate("Error"), JOptionPane.ERROR_MESSAGE);
168
169          e.printStackTrace();
170       }
171       list.setModel(lm);
172
173       if(lm.size()>0)
174       {
175          list.setSelectedIndex(0);
176       }
177    }
178
179 /* standalone test */
180    public static void main(String JavaDoc[] args)
181    {
182       new TruststoreView(new JFrame());
183    }
184
185 }
Popular Tags