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 TruststorePanel 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 String 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 getPeerAlias() { 82 return (String ) this.keyAliasCombo.getSelectedItem(); 83 } 84 85 private void setPeerAlias(String alias) { 86 this.keyAliasCombo.setSelectedItem(alias); 87 } 88 89 private char[] getCharStorePassword() { 90 return storePasswordField.getPassword(); 91 } 92 93 private String getStorePassword() { 94 return String.valueOf(this.storePasswordField.getPassword()); 95 } 96 97 private void setStorePassword(String password) { 98 this.storePasswordField.setText(password); 99 } 100 101 private void setStoreLocation(String path) { 102 this.storeLocationTextField.setText(path); 103 } 104 105 private String getStoreLocation() { 106 String path = this.storeLocationTextField.getText(); 107 if ("".equals(path) || (path == null)) { return null; 109 } 110 return path; 111 } 112 113 private void setStoreType(String type) { 114 this.storeType = type; 115 } 116 117 private String getStoreType() { 118 String type = this.storeType; 119 if ("".equals(type) || (type == null)) { return JKS; 121 } 122 return type; 123 } 124 125 public void sync() { 126 inSync = true; 127 128 String storeLocation = ProprietarySecurityPolicyModelHelper.getStoreLocation(comp, true); 129 if (storeLocation != null) { 130 setStoreLocation(storeLocation); 131 } else if (jsr109) { 132 setStoreLocation(getServerStoreLocation()); 133 } 134 135 String storeType = ProprietarySecurityPolicyModelHelper.getStoreType(comp, true); 136 if (storeType != null) { 137 setStoreType(storeType); 138 } 139 140 String 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 peerAlias = ProprietarySecurityPolicyModelHelper.getTrustPeerAlias(comp); 149 setPeerAlias(peerAlias); 150 151 enableDisable(); 152 153 inSync = false; 154 } 155 156 private String getServerStoreLocation() { 157 String keystoreLocation = null; 158 J2eeModuleProvider mp = (J2eeModuleProvider)project.getLookup().lookup(J2eeModuleProvider.class); 159 if (mp != null) { 160 String sID = mp.getServerInstanceID(); 161 J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(sID); 162 File [] 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 storeLocationButton.setEnabled(!jsr109); 174 storeLocationLabel.setEnabled(!jsr109); 175 storeLocationTextField.setEnabled(!jsr109); 176 } 177 178 183 private void initComponents() { 185 186 storeLocationLabel = new javax.swing.JLabel (); 187 storePasswordLabel = new javax.swing.JLabel (); 188 storeLocationTextField = new javax.swing.JTextField (); 189 storeLocationButton = new javax.swing.JButton (); 190 keyAliasLabel = new javax.swing.JLabel (); 191 keyAliasCombo = new javax.swing.JComboBox (); 192 storePasswordField = new javax.swing.JPasswordField (); 193 loadkeysButton = new javax.swing.JButton (); 194 195 storeLocationLabel.setText(org.openide.util.NbBundle.getMessage(TruststorePanel.class, "LBL_KeyStorePanel_LocationLabel")); 197 storePasswordLabel.setText(org.openide.util.NbBundle.getMessage(TruststorePanel.class, "LBL_TruststorePanel_TruststorePassword")); 199 storeLocationButton.setText("Browse..."); 200 storeLocationButton.addActionListener(new java.awt.event.ActionListener () { 201 public void actionPerformed(java.awt.event.ActionEvent evt) { 202 storeLocationButtonActionPerformed(evt); 203 } 204 }); 205 206 keyAliasLabel.setText(org.openide.util.NbBundle.getMessage(TruststorePanel.class, "LBL_KeyStorePanel_KeyAliasLabel")); 208 keyAliasCombo.setEditable(true); 209 210 loadkeysButton.setText(org.openide.util.NbBundle.getMessage(TruststorePanel.class, "LBL_LoadKeys")); loadkeysButton.addActionListener(new java.awt.event.ActionListener () { 212 public void actionPerformed(java.awt.event.ActionEvent 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 [] {keyAliasCombo, storeLocationTextField, storePasswordField}, org.jdesktop.layout.GroupLayout.VERTICAL); 263 264 } 266 private void loadkeysButtonActionPerformed(java.awt.event.ActionEvent evt) { boolean success = reloadAliases(); 268 if (!success) { 269 DialogDisplayer.getDefault().notify( 270 new NotifyDescriptor.Message(NbBundle.getMessage(TruststorePanel.class, "MSG_WrongPassword" ))); 272 } 273 } 275 private void storeLocationButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser(); 277 FileUtil.preventFileChooserSymlinkTraversal(chooser, null); 278 chooser.setDialogTitle(NbBundle.getMessage(TruststorePanel.class, "LBL_TruststoreBrowse_Title")); chooser.setFileSelectionMode (JFileChooser.FILES_ONLY); 280 chooser.setMultiSelectionEnabled(false); 281 chooser.setFileFilter(new StoreFileFilter()); 282 File f = new File (storeLocationTextField.getText()); 283 File 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 file = chooser.getSelectedFile(); 293 if (file != null) { 294 setStoreLocation(file.getPath()); 295 String extension = FileUtil.getExtension(file.getName()); 296 storeType = StoreFileFilter.JKS_EXT.equals(extension) ? JKS : PKCS12; 297 } 298 } 299 } 301 public void storeState() { 302 String 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 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 <String > aliases; 323 try { 324 aliases = Util.getAliases(getStoreLocation(), getCharStorePassword(), storeType); 325 } catch (IOException ex) { 326 ex.printStackTrace(); 327 return false; 328 } 329 keyAliasCombo.removeAllItems(); 330 if (aliases != null) { 331 keyAliasCombo.addItem(""); while (aliases.hasMoreElements()){ 333 String 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 private javax.swing.JComboBox keyAliasCombo; 345 private javax.swing.JLabel keyAliasLabel; 346 private javax.swing.JButton loadkeysButton; 347 private javax.swing.JButton storeLocationButton; 348 private javax.swing.JLabel storeLocationLabel; 349 private javax.swing.JTextField storeLocationTextField; 350 private javax.swing.JPasswordField storePasswordField; 351 private javax.swing.JLabel storePasswordLabel; 352 354 } 355 | Popular Tags |