KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsitconf > ui > service > subpanels > TruststorePanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.wsitconf.ui.service.subpanels;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import org.netbeans.modules.websvc.wsitconf.ui.StoreFileFilter;
25 import org.netbeans.modules.websvc.wsitconf.util.Util;
26 import org.netbeans.modules.websvc.wsitconf.wsdlmodelext.ProprietarySecurityPolicyModelHelper;
27 import org.netbeans.modules.xml.multiview.ui.SectionVisualTheme;
28 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
29 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
30 import org.openide.DialogDisplayer;
31 import org.openide.NotifyDescriptor;
32 import org.openide.filesystems.FileUtil;
33 import org.openide.util.NbBundle;
34 import javax.swing.*;
35 import java.io.File JavaDoc;
36 import org.netbeans.api.project.Project;
37 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
38 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
39 import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
40
41 /**
42  *
43  * @author Martin Grebac
44  */

45 public class TruststorePanel extends JPanel {
46
47     private static final String JavaDoc PKCS12 = "PKCS12"; //NOI18N
48
private static final String JavaDoc JKS = "JKS"; //NOI18N
49

50     private static final String JavaDoc DEFAULT_PASSWORD="changeit"; //NOI18N
51

52     private WSDLModel model;
53     private WSDLComponent comp;
54
55     private String JavaDoc storeType = JKS;
56
57     private boolean jsr109 = false;
58     private Project project = null;
59     
60     private boolean inSync = false;
61     
62     public TruststorePanel(WSDLComponent comp, Project p, boolean jsr109) {
63         super();
64         this.model = comp.getModel();
65         this.comp = comp;
66         this.jsr109 = jsr109;
67         this.project = p;
68         
69         initComponents();
70
71         keyAliasCombo.setBackground(SectionVisualTheme.getDocumentBackgroundColor());
72         keyAliasLabel.setBackground(SectionVisualTheme.getDocumentBackgroundColor());
73         storeLocationLabel.setBackground(SectionVisualTheme.getDocumentBackgroundColor());
74         storeLocationTextField.setBackground(SectionVisualTheme.getDocumentBackgroundColor());
75         storePasswordLabel.setBackground(SectionVisualTheme.getDocumentBackgroundColor());
76         storePasswordField.setBackground(SectionVisualTheme.getDocumentBackgroundColor());
77
78         sync();
79     }
80
81     private String JavaDoc getPeerAlias() {
82         return (String JavaDoc) this.keyAliasCombo.getSelectedItem();
83     }
84
85     private void setPeerAlias(String JavaDoc alias) {
86         this.keyAliasCombo.setSelectedItem(alias);
87     }
88
89     private char[] getCharStorePassword() {
90         return storePasswordField.getPassword();
91     }
92     
93     private String JavaDoc getStorePassword() {
94         return String.valueOf(this.storePasswordField.getPassword());
95     }
96
97     private void setStorePassword(String JavaDoc password) {
98         this.storePasswordField.setText(password);
99     }
100
101     private void setStoreLocation(String JavaDoc path) {
102         this.storeLocationTextField.setText(path);
103     }
104     
105     private String JavaDoc getStoreLocation() {
106         String JavaDoc path = this.storeLocationTextField.getText();
107         if ("".equals(path) || (path == null)) { //NOI18N
108
return null;
109         }
110         return path;
111     }
112
113     private void setStoreType(String JavaDoc type) {
114         this.storeType = type;
115     }
116     
117     private String JavaDoc getStoreType() {
118         String JavaDoc type = this.storeType;
119         if ("".equals(type) || (type == null)) { //NOI18N
120
return JKS;
121         }
122         return type;
123     }
124     
125     public void sync() {
126         inSync = true;
127         
128         String JavaDoc storeLocation = ProprietarySecurityPolicyModelHelper.getStoreLocation(comp, true);
129         if (storeLocation != null) {
130             setStoreLocation(storeLocation);
131         } else if (jsr109) {
132             setStoreLocation(getServerStoreLocation());
133         }
134
135         String JavaDoc storeType = ProprietarySecurityPolicyModelHelper.getStoreType(comp, true);
136         if (storeType != null) {
137             setStoreType(storeType);
138         }
139        
140         String JavaDoc storePassword = ProprietarySecurityPolicyModelHelper.getStorePassword(comp, true);
141         if (storePassword != null) {
142             setStorePassword(storePassword);
143             reloadAliases();
144         } else if (jsr109) {
145             setStorePassword(DEFAULT_PASSWORD);
146         }
147
148         String JavaDoc peerAlias = ProprietarySecurityPolicyModelHelper.getTrustPeerAlias(comp);
149         setPeerAlias(peerAlias);
150
151         enableDisable();
152         
153         inSync = false;
154     }
155
156     private String JavaDoc getServerStoreLocation() {
157         String JavaDoc keystoreLocation = null;
158         J2eeModuleProvider mp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class);
159         if (mp != null) {
160             String JavaDoc sID = mp.getServerInstanceID();
161             J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(sID);
162             File JavaDoc[] keyLocs = null;
163             keyLocs = j2eePlatform.getToolClasspathEntries(J2eePlatform.TOOL_TRUSTSTORE);
164             if ((keyLocs != null) && (keyLocs.length > 0)) {
165                 keystoreLocation = keyLocs[0].getAbsolutePath();
166             }
167         }
168         return keystoreLocation;
169     }
170     
171     private void enableDisable() {
172         //these depend on jsr109 state
173
storeLocationButton.setEnabled(!jsr109);
174         storeLocationLabel.setEnabled(!jsr109);
175         storeLocationTextField.setEnabled(!jsr109);
176     }
177     
178     /** This method is called from within the constructor to
179      * initialize the form.
180      * WARNING: Do NOT modify this code. The content of this method is
181      * always regenerated by the Form Editor.
182      */

183     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
184
private void initComponents() {
185
186         storeLocationLabel = new javax.swing.JLabel JavaDoc();
187         storePasswordLabel = new javax.swing.JLabel JavaDoc();
188         storeLocationTextField = new javax.swing.JTextField JavaDoc();
189         storeLocationButton = new javax.swing.JButton JavaDoc();
190         keyAliasLabel = new javax.swing.JLabel JavaDoc();
191         keyAliasCombo = new javax.swing.JComboBox JavaDoc();
192         storePasswordField = new javax.swing.JPasswordField JavaDoc();
193         loadkeysButton = new javax.swing.JButton JavaDoc();
194
195         storeLocationLabel.setText(org.openide.util.NbBundle.getMessage(TruststorePanel.class, "LBL_KeyStorePanel_LocationLabel")); // NOI18N
196

197         storePasswordLabel.setText(org.openide.util.NbBundle.getMessage(TruststorePanel.class, "LBL_TruststorePanel_TruststorePassword")); // NOI18N
198

199         storeLocationButton.setText("Browse...");
200         storeLocationButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
201             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
202                 storeLocationButtonActionPerformed(evt);
203             }
204         });
205
206         keyAliasLabel.setText(org.openide.util.NbBundle.getMessage(TruststorePanel.class, "LBL_KeyStorePanel_KeyAliasLabel")); // NOI18N
207

208         keyAliasCombo.setEditable(true);
209
210         loadkeysButton.setText(org.openide.util.NbBundle.getMessage(TruststorePanel.class, "LBL_LoadKeys")); // NOI18N
211
loadkeysButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
212             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
213                 loadkeysButtonActionPerformed(evt);
214             }
215         });
216
217         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
218         this.setLayout(layout);
219         layout.setHorizontalGroup(
220             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
221             .add(layout.createSequentialGroup()
222                 .addContainerGap()
223                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
224                     .add(storeLocationLabel)
225                     .add(storePasswordLabel)
226                     .add(keyAliasLabel))
227                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
228                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
229                     .add(layout.createSequentialGroup()
230                         .add(storeLocationTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 239, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
231                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
232                         .add(storeLocationButton))
233                     .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
234                         .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
235                             .add(org.jdesktop.layout.GroupLayout.LEADING, keyAliasCombo, 0, 159, Short.MAX_VALUE)
236                             .add(storePasswordField))
237                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
238                         .add(loadkeysButton)
239                         .add(68, 68, 68)))
240                 .addContainerGap(18, Short.MAX_VALUE))
241         );
242         layout.setVerticalGroup(
243             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
244             .add(layout.createSequentialGroup()
245                 .addContainerGap()
246                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
247                     .add(storeLocationLabel)
248                     .add(storeLocationTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
249                     .add(storeLocationButton))
250                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
251                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
252                     .add(storePasswordLabel)
253                     .add(storePasswordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
254                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
255                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
256                     .add(keyAliasLabel)
257                     .add(keyAliasCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
258                     .add(loadkeysButton))
259                 .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
260         );
261
262         layout.linkSize(new java.awt.Component JavaDoc[] {keyAliasCombo, storeLocationTextField, storePasswordField}, org.jdesktop.layout.GroupLayout.VERTICAL);
263
264     }// </editor-fold>//GEN-END:initComponents
265

266     private void loadkeysButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_loadkeysButtonActionPerformed
267
boolean success = reloadAliases();
268         if (!success) {
269             DialogDisplayer.getDefault().notify(
270                     new NotifyDescriptor.Message(NbBundle.getMessage(TruststorePanel.class, "MSG_WrongPassword" //NOI18N
271
)));
272         }
273     }//GEN-LAST:event_loadkeysButtonActionPerformed
274

275     private void storeLocationButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_storeLocationButtonActionPerformed
276
JFileChooser chooser = new JFileChooser();
277         FileUtil.preventFileChooserSymlinkTraversal(chooser, null);
278         chooser.setDialogTitle(NbBundle.getMessage(TruststorePanel.class, "LBL_TruststoreBrowse_Title")); //NOI18N
279
chooser.setFileSelectionMode (JFileChooser.FILES_ONLY);
280         chooser.setMultiSelectionEnabled(false);
281         chooser.setFileFilter(new StoreFileFilter());
282         File JavaDoc f = new File JavaDoc(storeLocationTextField.getText());
283         File JavaDoc dir = null;
284         if ((f != null) && (f.exists())) {
285             if (f.isDirectory()) {
286                 chooser.setCurrentDirectory(f);
287             } else {
288                 chooser.setCurrentDirectory(f.getParentFile());
289             }
290         }
291         if (chooser.showOpenDialog(this)== JFileChooser.APPROVE_OPTION) {
292             File JavaDoc file = chooser.getSelectedFile();
293             if (file != null) {
294                 setStoreLocation(file.getPath());
295                 String JavaDoc extension = FileUtil.getExtension(file.getName());
296                 storeType = StoreFileFilter.JKS_EXT.equals(extension) ? JKS : PKCS12;
297             }
298         }
299     }//GEN-LAST:event_storeLocationButtonActionPerformed
300

301     public void storeState() {
302         String JavaDoc peerAlias = getPeerAlias();
303         if ((peerAlias != null) && (peerAlias.length() == 0)) {
304             ProprietarySecurityPolicyModelHelper.setTrustPeerAlias(comp, null, false);
305         } else {
306             ProprietarySecurityPolicyModelHelper.setTrustPeerAlias(comp, peerAlias, false);
307         }
308
309         String JavaDoc storePasswd = getStorePassword();
310         if ((storePasswd != null) && (storePasswd.length() == 0)) {
311             ProprietarySecurityPolicyModelHelper.setStorePassword(comp, null, true, false);
312         } else {
313             ProprietarySecurityPolicyModelHelper.setStorePassword(comp, storePasswd, true, false);
314         }
315         
316         ProprietarySecurityPolicyModelHelper.setStoreType(comp, storeType, true, false);
317         
318         ProprietarySecurityPolicyModelHelper.setStoreLocation(comp, getStoreLocation(), true, false);
319     }
320     
321     private boolean reloadAliases() {
322         Enumeration JavaDoc<String JavaDoc> aliases;
323         try {
324             aliases = Util.getAliases(getStoreLocation(), getCharStorePassword(), storeType);
325         } catch (IOException JavaDoc ex) {
326             ex.printStackTrace();
327             return false;
328         }
329         keyAliasCombo.removeAllItems();
330         if (aliases != null) {
331             keyAliasCombo.addItem(""); //NOI18N
332
while (aliases.hasMoreElements()){
333                 String JavaDoc alias = aliases.nextElement();
334                 keyAliasCombo.addItem(alias);
335             }
336             if (keyAliasCombo.getItemCount() > 1) {
337                 keyAliasCombo.setSelectedIndex(1);
338             }
339         }
340         return true;
341     }
342         
343     // Variables declaration - do not modify//GEN-BEGIN:variables
344
private javax.swing.JComboBox JavaDoc keyAliasCombo;
345     private javax.swing.JLabel JavaDoc keyAliasLabel;
346     private javax.swing.JButton JavaDoc loadkeysButton;
347     private javax.swing.JButton JavaDoc storeLocationButton;
348     private javax.swing.JLabel JavaDoc storeLocationLabel;
349     private javax.swing.JTextField JavaDoc storeLocationTextField;
350     private javax.swing.JPasswordField JavaDoc storePasswordField;
351     private javax.swing.JLabel JavaDoc storePasswordLabel;
352     // End of variables declaration//GEN-END:variables
353

354 }
355
Popular Tags