1 19 20 package org.netbeans.modules.websvc.wsitconf.ui.service.subpanels; 21 22 import java.io.IOException ; 23 import java.util.Enumeration ; 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 ; 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 45 public class KeystorePanel extends JPanel { 46 47 private static final String PKCS12 = "PKCS12"; private static final String JKS = "JKS"; 50 private static final String DEFAULT_PASSWORD="changeit"; 52 private WSDLModel model; 53 private WSDLComponent comp; 54 55 private boolean jsr109 = false; 56 private Project project = null; 57 58 private String keystoreType = JKS; 59 60 private boolean inSync = false; 61 62 public KeystorePanel(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 keyPasswordLabel.setBackground(SectionVisualTheme.getDocumentBackgroundColor()); 74 keyPasswordField.setBackground(SectionVisualTheme.getDocumentBackgroundColor()); 75 keystoreLocationLabel.setBackground(SectionVisualTheme.getDocumentBackgroundColor()); 76 keystoreLocationTextField.setBackground(SectionVisualTheme.getDocumentBackgroundColor()); 77 keystorePasswordLabel.setBackground(SectionVisualTheme.getDocumentBackgroundColor()); 78 keystorePasswordField.setBackground(SectionVisualTheme.getDocumentBackgroundColor()); 79 80 sync(); 81 } 82 83 private String getKeystoreAlias() { 84 return (String ) this.keyAliasCombo.getSelectedItem(); 85 } 86 87 private void setKeystoreAlias(String alias) { 88 this.keyAliasCombo.setSelectedItem(alias); 89 } 90 91 private String getKeyPassword() { 92 return String.valueOf(this.keyPasswordField.getPassword()); 93 } 94 95 private void setKeyPassword(String password) { 96 this.keyPasswordField.setText(password); 97 } 98 99 private char[] getCharKeystorePassword() { 100 return keystorePasswordField.getPassword(); 101 } 102 103 private String getKeystorePassword() { 104 return String.valueOf(this.keystorePasswordField.getPassword()); 105 } 106 107 private void setKeystorePassword(String password) { 108 this.keystorePasswordField.setText(password); 109 } 110 111 private void setKeystorePath(String path) { 112 this.keystoreLocationTextField.setText(path); 113 } 114 115 private String getKeystorePath() { 116 String path = this.keystoreLocationTextField.getText(); 117 if ("".equals(path) || (path == null)) { return null; 119 } 120 return path; 121 } 122 123 private void setKeystoreType(String type) { 124 this.keystoreType = type; 125 } 126 127 private String getKeystoreType() { 128 String type = this.keystoreType; 129 if ("".equals(type) || (type == null)) { return JKS; 131 } 132 return type; 133 } 134 135 public void sync() { 136 inSync = true; 137 138 String keystoreLocation = ProprietarySecurityPolicyModelHelper.getStoreLocation(comp, false); 139 if (keystoreLocation != null) { 140 setKeystorePath(keystoreLocation); 141 } else if (jsr109) { 142 setKeystorePath(getServerStoreLocation()); 143 } 144 145 String keystoreType = ProprietarySecurityPolicyModelHelper.getStoreType(comp, false); 146 if (keystoreType != null) { 147 setKeystoreType(keystoreType); 148 } 149 150 String keyStorePassword = ProprietarySecurityPolicyModelHelper.getStorePassword(comp, false); 151 if (keyStorePassword != null) { 152 setKeystorePassword(keyStorePassword); 153 reloadAliases(); 154 } else if (jsr109) { 155 setKeystorePassword(DEFAULT_PASSWORD); 156 } 157 158 String keyStoreAlias = ProprietarySecurityPolicyModelHelper.getStoreAlias(comp, false); 159 setKeystoreAlias(keyStoreAlias); 160 161 String keyPassword = ProprietarySecurityPolicyModelHelper.getKeyPassword(comp); 162 if (keyPassword != null) { 163 setKeyPassword(keyPassword); 164 } 165 166 enableDisable(); 167 168 inSync = false; 169 } 170 171 private void enableDisable() { 172 keystoreLocationButton.setEnabled(!jsr109); 174 keystoreLocationLabel.setEnabled(!jsr109); 175 keystoreLocationTextField.setEnabled(!jsr109); 176 } 177 178 private String getServerStoreLocation() { 179 String keystoreLocation = null; 180 J2eeModuleProvider mp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class); 181 if (mp != null) { 182 String sID = mp.getServerInstanceID(); 183 J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(sID); 184 File [] keyLocs = null; 185 keyLocs = j2eePlatform.getToolClasspathEntries(J2eePlatform.TOOL_KEYSTORE); 186 if ((keyLocs != null) && (keyLocs.length > 0)) { 187 keystoreLocation = keyLocs[0].getAbsolutePath(); 188 } 189 } 190 return keystoreLocation; 191 } 192 193 public void storeState() { 194 String keystoreAlias = getKeystoreAlias(); 195 if ((keystoreAlias == null) || (keystoreAlias.length() == 0)) { 196 ProprietarySecurityPolicyModelHelper.setKeyStoreAlias(comp, null, false); 197 } else { 198 ProprietarySecurityPolicyModelHelper.setKeyStoreAlias(comp, keystoreAlias, false); 199 } 200 String keyPasswd = getKeyPassword(); 201 if ((keyPasswd == null) || (keyPasswd.length() == 0)) { 202 ProprietarySecurityPolicyModelHelper.setKeyPassword(comp, null, false); 203 } else { 204 ProprietarySecurityPolicyModelHelper.setKeyPassword(comp, keyPasswd, false); 205 } 206 207 String keyStorePasswd = getKeystorePassword(); 208 if ((keyStorePasswd == null) || (keyStorePasswd.length() == 0)) { 209 ProprietarySecurityPolicyModelHelper.setStorePassword(comp, null, false, false); 210 } else { 211 ProprietarySecurityPolicyModelHelper.setStorePassword(comp, keyStorePasswd, false, false); 212 } 213 214 ProprietarySecurityPolicyModelHelper.setStoreType(comp, keystoreType, false, false); 215 216 ProprietarySecurityPolicyModelHelper.setStoreLocation(comp, getKeystorePath(), false, false); 217 } 218 219 224 private void initComponents() { 226 227 keystoreLocationLabel = new javax.swing.JLabel (); 228 keystorePasswordLabel = new javax.swing.JLabel (); 229 keystoreLocationTextField = new javax.swing.JTextField (); 230 keystoreLocationButton = new javax.swing.JButton (); 231 keyAliasLabel = new javax.swing.JLabel (); 232 keyPasswordLabel = new javax.swing.JLabel (); 233 keyAliasCombo = new javax.swing.JComboBox (); 234 keystorePasswordField = new javax.swing.JPasswordField (); 235 keyPasswordField = new javax.swing.JPasswordField (); 236 loadkeysButton = new javax.swing.JButton (); 237 238 keystoreLocationLabel.setText(org.openide.util.NbBundle.getMessage(KeystorePanel.class, "LBL_KeyStorePanel_LocationLabel")); 240 keystorePasswordLabel.setText(org.openide.util.NbBundle.getMessage(KeystorePanel.class, "LBL_StorePanel_StorePassword")); 242 keystoreLocationButton.setText("Browse..."); 243 keystoreLocationButton.addActionListener(new java.awt.event.ActionListener () { 244 public void actionPerformed(java.awt.event.ActionEvent evt) { 245 keystoreLocationButtonActionPerformed(evt); 246 } 247 }); 248 249 keyAliasLabel.setText(org.openide.util.NbBundle.getMessage(KeystorePanel.class, "LBL_KeyStorePanel_KeyAliasLabel")); 251 keyPasswordLabel.setText(org.openide.util.NbBundle.getMessage(KeystorePanel.class, "LBL_Keystore_KeyPasswordLabel")); 253 keyAliasCombo.setEditable(true); 254 255 loadkeysButton.setText(org.openide.util.NbBundle.getMessage(KeystorePanel.class, "LBL_LoadKeys")); loadkeysButton.addActionListener(new java.awt.event.ActionListener () { 257 public void actionPerformed(java.awt.event.ActionEvent evt) { 258 loadkeysButtonActionPerformed(evt); 259 } 260 }); 261 262 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 263 this.setLayout(layout); 264 layout.setHorizontalGroup( 265 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 266 .add(layout.createSequentialGroup() 267 .addContainerGap() 268 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 269 .add(keystoreLocationLabel) 270 .add(keystorePasswordLabel) 271 .add(keyAliasLabel) 272 .add(keyPasswordLabel)) 273 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 274 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) 275 .add(layout.createSequentialGroup() 276 .add(keystoreLocationTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 239, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 277 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 278 .add(keystoreLocationButton)) 279 .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup() 280 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) 281 .add(org.jdesktop.layout.GroupLayout.LEADING, keyAliasCombo, 0, 159, Short.MAX_VALUE) 282 .add(keystorePasswordField) 283 .add(org.jdesktop.layout.GroupLayout.LEADING, keyPasswordField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)) 284 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 285 .add(loadkeysButton) 286 .add(68, 68, 68))) 287 .addContainerGap()) 288 ); 289 layout.setVerticalGroup( 290 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 291 .add(layout.createSequentialGroup() 292 .addContainerGap() 293 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 294 .add(keystoreLocationLabel) 295 .add(keystoreLocationTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 296 .add(keystoreLocationButton)) 297 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 298 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 299 .add(keystorePasswordLabel) 300 .add(keystorePasswordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 301 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 302 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 303 .add(keyAliasLabel) 304 .add(keyAliasCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 305 .add(loadkeysButton)) 306 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 307 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 308 .add(keyPasswordLabel) 309 .add(keyPasswordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 310 .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 311 ); 312 313 layout.linkSize(new java.awt.Component [] {keyAliasCombo, keyPasswordField, keystoreLocationTextField, keystorePasswordField}, org.jdesktop.layout.GroupLayout.VERTICAL); 314 315 } 317 private void loadkeysButtonActionPerformed(java.awt.event.ActionEvent evt) { boolean success = reloadAliases(); 319 if (!success) { 320 DialogDisplayer.getDefault().notify( 321 new NotifyDescriptor.Message(NbBundle.getMessage(KeystorePanel.class, "MSG_WrongPassword" ))); 323 } 324 } 326 private void keystoreLocationButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser(); 328 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 329 chooser.setDialogTitle(NbBundle.getMessage(KeystorePanel.class, "LBL_KeystoreBrowse_Title")); chooser.setFileSelectionMode (JFileChooser.FILES_ONLY); 331 chooser.setMultiSelectionEnabled(false); 332 chooser.setFileFilter(new StoreFileFilter()); 333 File f = new File (keystoreLocationTextField.getText()); 334 File dir = null; 335 if ((f != null) && (f.exists())) { 336 if (f.isDirectory()) { 337 chooser.setCurrentDirectory(f); 338 } else { 339 chooser.setCurrentDirectory(f.getParentFile()); 340 } 341 } 342 if (chooser.showOpenDialog(this)== JFileChooser.APPROVE_OPTION) { 343 File file = chooser.getSelectedFile(); 344 if (file != null) { 345 setKeystorePath(file.getPath()); 346 String extension = FileUtil.getExtension(file.getName()); 347 keystoreType = StoreFileFilter.JKS_EXT.equals(extension) ? JKS : PKCS12; 348 } 349 } 350 } 352 private boolean reloadAliases() { 353 Enumeration <String > aliases; 354 try { 355 aliases = Util.getAliases(getKeystorePath(), getCharKeystorePassword(), keystoreType); 356 } catch (IOException ex) { 357 return false; 358 } 359 keyAliasCombo.removeAllItems(); 360 if (aliases != null) { 361 keyAliasCombo.addItem(""); while (aliases.hasMoreElements()){ 363 String alias = aliases.nextElement(); 364 keyAliasCombo.addItem(alias); 365 } 366 if (keyAliasCombo.getItemCount() > 1) { 367 keyAliasCombo.setSelectedIndex(1); 368 } 369 } 370 return true; 371 } 372 373 private javax.swing.JComboBox keyAliasCombo; 375 private javax.swing.JLabel keyAliasLabel; 376 private javax.swing.JPasswordField keyPasswordField; 377 private javax.swing.JLabel keyPasswordLabel; 378 private javax.swing.JButton keystoreLocationButton; 379 private javax.swing.JLabel keystoreLocationLabel; 380 private javax.swing.JTextField keystoreLocationTextField; 381 private javax.swing.JPasswordField keystorePasswordField; 382 private javax.swing.JLabel keystorePasswordLabel; 383 private javax.swing.JButton loadkeysButton; 384 386 } 387 | Popular Tags |