1 package com.ca.commons.naming; 2 3 import java.awt.Color ; 4 import java.awt.Frame ; 5 import java.awt.event.ActionEvent ; 6 import java.awt.event.ActionListener ; 7 import java.net.URISyntaxException ; 8 import java.util.logging.Logger ; 9 10 11 import javax.swing.JLabel ; 12 import javax.swing.JPasswordField ; 13 import javax.swing.JTextField ; 14 import javax.swing.border.TitledBorder ; 15 16 import com.ca.commons.jndi.ConnectionData; 17 import com.ca.commons.cbutil.*; 18 19 23 24 25 28 public abstract class CBOpenConWin extends CBDialog 29 { 30 33 34 protected ConnectionData newCon; 36 protected JTextField hostName, port, baseDN, managerDN; 37 protected CBJComboBox version, level; 38 protected JPasswordField password; 40 protected JLabel hostLabel, portLabel, userMessage; 41 protected CBSaveLoadTemplate myTemplater; 42 43 static int threadID = 1; static final boolean DEBUGTHREADS = false; 45 46 static final int NONE = 0; 48 static final int USER_AUTH = 1; 49 static final int SSL_NO_AUTH = 2; 50 static final int SSL_USER_AUTH = 3; 51 static final int SASL = 4; 52 static final int GSSAPI = 5; 54 protected static final String LDAPV2 = CBIntText.get("LDAP v2"); 56 protected static final String LDAPV3 = CBIntText.get("LDAP v3"); 57 58 59 protected SecurityActionListener securityListener; 60 61 private final static Logger log = Logger.getLogger(CBOpenConWin.class.getName()); 62 63 64 82 83 public CBOpenConWin(Frame owner, JLabel statusDisplay, String clientcerts, String cacerts, 84 String referral, String aliasType) 85 { 86 this(owner, statusDisplay, clientcerts, cacerts, referral, aliasType, null); 87 } 88 89 90 108 public CBOpenConWin(Frame owner, JLabel statusDisplay, String clientcerts, String cacerts, 109 String referral, String aliasType, String helpID) 110 { 111 this(owner, statusDisplay, clientcerts, cacerts, helpID); 112 113 newCon.referralType = referral; 114 newCon.aliasType = aliasType; 115 } 116 117 118 136 137 public CBOpenConWin(Frame owner, JLabel statusDisplay, String clientcerts, String cacerts) 138 { 139 this(owner, statusDisplay, clientcerts, cacerts, null); 140 } 141 142 160 public CBOpenConWin(Frame owner, JLabel statusDisplay, String clientcerts, String cacerts, String helpID) 161 { 162 super(owner, CBIntText.get("Open LDAP connection"), helpID); 164 newCon = new ConnectionData(); 166 newCon.clientcerts = clientcerts; 167 newCon.cacerts = cacerts; 168 169 initGUI(statusDisplay); 170 } 171 172 protected void initGUI(JLabel statusDisplay) 173 { 174 String oldConnection = ""; 175 int oldPortNo = 19389; 176 String oldBaseDN = ""; 177 179 userMessage = statusDisplay; 180 181 182 185 186 display.makeHeavy(); 187 JLabel temp; 188 display.add(hostLabel = new JLabel (" " + CBIntText.get("Host") + ": ")); 189 display.addWide(hostName = new JTextField (oldConnection, 30), 2); 190 hostLabel.setToolTipText(CBIntText.get("The url of the server; e.g. ") + "'www.cai.com'"); 191 192 display.add(portLabel = new JLabel (" " + CBIntText.get("Port") + ": ")); 193 display.addLine(port = new JTextField (String.valueOf(oldPortNo), 5)); 194 portLabel.setToolTipText(CBIntText.get("The port number of the LDAP server; often 389.")); 195 196 display.add(temp = new JLabel (" " + CBIntText.get("Protocol") + (": "))); 197 display.add(version = new CBJComboBox(new String []{LDAPV2, LDAPV3})); 198 version.setSelectedIndex(1); 199 temp.setToolTipText(CBIntText.get("For all but the oldest servers, this should be 'Ldap v3'.")); 200 version.setToolTipText(CBIntText.get("For all but the oldest servers, this should be 'Ldap v3'.")); 201 202 display.addLine(new JLabel ("")); 204 display.newLine(); 206 209 210 CBPanel inset = new CBPanel(); 211 inset.setBorder(new TitledBorder (CBIntText.get("Optional Values"))); 212 213 inset.add(temp = new JLabel (CBIntText.get("Base DN") + ": ")); 214 temp.setToolTipText(CBIntText.get("The base to start browsing from; e.g. ") + "'o=Democorp,c=au'"); 215 inset.makeWide(); 216 inset.addln(baseDN = new JTextField (String.valueOf(oldBaseDN), 30)); 217 218 219 display.addLines(inset, 1); 220 221 222 225 226 CBPanel ssl = new CBPanel(); 227 228 String [] securityOptions = { 229 CBIntText.get("Anonymous"), 230 CBIntText.get("User + Password"), 231 CBIntText.get("SSL + Anonymous"), 232 CBIntText.get("SSL + User + Password"), 233 CBIntText.get("SSL + SASL + Keystore Password"), 234 CBIntText.get("GSSAPI") }; 236 237 ssl.setBorder(new TitledBorder (CBIntText.get("Security"))); 238 239 ssl.makeLight(); 240 ssl.add(temp = new JLabel (CBIntText.get("Level") + ":")); 241 temp.setToolTipText(CBIntText.get("The level of authentication.")); 242 243 level = new CBJComboBox(securityOptions); 244 level.setToolTipText(CBIntText.get("Before using SSL, make sure you've set up your keystores in the 'Security' menu.")); 245 ssl.addln(level); 246 247 ssl.add(temp = new JLabel (CBIntText.get("User DN") + ": ")); 248 ssl.addln(managerDN = new JTextField (30)); 249 temp.setToolTipText(CBIntText.get("To log on as an authenticated user, enter your user dn here.")); 250 251 ssl.add(temp = new JLabel (CBIntText.get("Password") + ": ")); 252 ssl.addLine(password = new JPasswordField (30)); 253 temp.setToolTipText(CBIntText.get("Set your user password (or SASL keystore password) here.")); 254 255 display.addLines(ssl, 3); 256 257 OK.setToolTipText(CBIntText.get("Click here to connect using current settings.")); 258 259 263 264 display.addWide(myTemplater = new CBSaveLoadTemplate("connections.txt"), 5); 265 266 addExtraComponent(); 268 display.newLine(); 269 270 display.add(new JLabel ("")); 272 display.doLayout(); 273 274 getContentPane().add(display); 275 276 doLayout(); 277 278 myTemplater.loadDefault(); 279 280 checkSecurityLevel(); 281 282 287 288 securityListener = new SecurityActionListener(); 289 level.addActionListener(securityListener); 290 (myTemplater.getLoadComboBox()).addActionListener(securityListener); 292 } 293 294 295 310 311 public void addExtraComponent() 312 { 313 } 314 315 316 321 322 class SecurityActionListener implements ActionListener 323 { 324 325 328 329 public void actionPerformed(ActionEvent event) 330 { 331 checkSecurityLevel(); 332 } 333 } 334 335 336 340 341 protected int checkSecurityLevel() 342 { 343 344 int selected = level.getSelectedIndex(); 345 switch (selected) 346 { 347 case 0: 349 setState(false, false); 350 return NONE; 351 352 case 1: 354 setState(true, true); 355 return USER_AUTH; 356 357 case 2: 359 366 367 setState(false, false); 368 return SSL_NO_AUTH; 369 370 case 3: 372 setState(true, true); 373 return SSL_USER_AUTH; 374 375 case 4: 377 setState(false, true); 378 return SASL; 379 380 case 5: 382 setState(false, false); 383 return GSSAPI; 384 } 385 386 return NONE; 387 } 388 389 393 394 private void setState(boolean user, boolean pwd) 395 { 396 managerDN.setEnabled(user); 397 managerDN.setBackground(user ? Color.white : Color.lightGray); 398 399 if (pwd == false) 400 password.setText(""); 401 402 password.setEnabled(pwd); 403 password.setBackground(pwd ? Color.white : Color.lightGray); 404 } 405 406 407 410 411 public void resetTitleAndPassword() 412 { 413 this.setTitle(CBIntText.get("Open LDAP Connection")); 414 password.setText(""); 415 } 416 417 protected String getURL() 418 throws NumberFormatException , URISyntaxException 419 { 420 String host = null; 421 String portString = null; 422 if (hostName != null) 423 host = hostName.getText(); 424 if (port != null) 425 portString = port.getText(); 426 427 if (host != null) host = host.trim(); 428 if (portString != null) portString = portString.trim(); 429 430 if (host == null || host.length() < 1) 431 { 432 throw new URISyntaxException ("", CBIntText.get("A host name must be entered for JXplorer to connect to.")); 433 } 434 435 if (portString == null || portString.length() < 1) 436 { 437 throw new URISyntaxException ("", CBIntText.get("A port number must be entered for JXplorer to connect to.")); 438 } 439 440 int port = Integer.parseInt(portString); 442 if (port < 0) throw new NumberFormatException (CBIntText.get("Negative Port Number is illegal")); 443 444 if (port > 65536) throw new NumberFormatException (CBIntText.get("Port Number {0} is illegal", new String [] {portString})); 445 446 return "ldap://" + host + ":" + port; 447 } 448 449 450 453 454 public void doOK() 455 { 456 457 try 458 { 459 log.fine("read values: " + hostName.getText() + ":" + port.getText()); 460 461 464 465 String url = getURL(); 467 newCon.setURL(url); 468 469 userMessage.setText(CBIntText.get("Opening Connection To ") + url); 470 471 474 475 if (version.getSelectedItem() == LDAPV2) 476 newCon.version = 2; 477 else newCon.version = 3; 479 480 481 484 485 int securityLevel = checkSecurityLevel(); 486 487 newCon.userDN = null; 488 newCon.clearPasswords(); 489 490 newCon.useGSSAPI = false; 491 492 if (securityLevel == USER_AUTH || securityLevel == SSL_USER_AUTH) 493 { 494 newCon.userDN = managerDN.getText().trim(); 495 newCon.pwd = password.getPassword(); 496 if ((newCon.pwd.length) == 0) 497 { throw new Exception (CBIntText.get("No Password Provided. Please enter a password.")); 499 } 500 } 501 else if (securityLevel == SASL) 502 { 503 newCon.clientKeystorePwd = password.getPassword(); 504 if ((newCon.clientKeystorePwd.length) == 0) 505 { throw new Exception (CBIntText.get("No Password Provided. Please enter a password.")); 507 } 508 } 509 else if (securityLevel == GSSAPI) 511 { 512 515 newCon.useGSSAPI = true; 516 } 517 518 setVisible(false); 519 520 newCon.useSSL = (securityLevel >= SSL_NO_AUTH && securityLevel != GSSAPI); 521 522 newCon.baseDN = baseDN.getText(); 523 524 525 } 526 catch (Exception err) 527 { 530 new CBErrorWin(this, "Error in data provided: " + err.getMessage(), err); 532 err.printStackTrace(); 537 538 password.setText(""); 539 setVisible(true); 540 541 this.setTitle(CBIntText.get("Couldn't Connect : Try Again")); 542 log.warning("User error in openconwin: " + err); 543 userMessage.setText(CBIntText.get("Error Opening Connection.")); 544 545 return; 546 } 547 548 551 connect(newCon); 552 553 } 554 555 562 563 public abstract void connect(ConnectionData connectData); 564 } | Popular Tags |