001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2008-2010 Sun Microsystems, Inc. 015 * Portions Copyright 2014-2015 ForgeRock AS. 016 */ 017 018package org.opends.guitools.uninstaller.ui; 019 020import java.awt.Dimension; 021import java.awt.GridBagConstraints; 022import java.awt.GridBagLayout; 023import java.awt.event.ActionEvent; 024import java.awt.event.ActionListener; 025import java.net.URI; 026import java.security.cert.X509Certificate; 027import java.util.ArrayList; 028 029import javax.naming.NamingException; 030import javax.naming.ldap.InitialLdapContext; 031import javax.swing.Box; 032import javax.swing.JButton; 033import javax.swing.JDialog; 034import javax.swing.JFrame; 035import javax.swing.JLabel; 036import javax.swing.JPanel; 037import javax.swing.JTextField; 038import javax.swing.SwingUtilities; 039import javax.swing.text.JTextComponent; 040 041import org.forgerock.i18n.LocalizableMessage; 042import org.forgerock.i18n.slf4j.LocalizedLogger; 043import org.opends.admin.ads.ADSContext; 044import org.opends.admin.ads.util.ApplicationTrustManager; 045import org.opends.guitools.controlpanel.datamodel.ConnectionProtocolPolicy; 046import org.opends.guitools.controlpanel.datamodel.ControlPanelInfo; 047import org.opends.guitools.controlpanel.util.ConfigFromFile; 048import org.opends.quicksetup.ApplicationException; 049import org.opends.quicksetup.Constants; 050import org.opends.quicksetup.Installation; 051import org.opends.quicksetup.ReturnCode; 052import org.opends.quicksetup.Step; 053import org.opends.quicksetup.UserData; 054import org.opends.quicksetup.UserDataCertificateException; 055import org.opends.quicksetup.event.MinimumSizeComponentListener; 056import org.opends.quicksetup.ui.CertificateDialog; 057import org.opends.quicksetup.ui.UIFactory; 058import org.opends.quicksetup.ui.Utilities; 059import org.opends.quicksetup.util.BackgroundTask; 060import org.opends.quicksetup.util.UIKeyStore; 061import org.opends.quicksetup.util.Utils; 062 063import static com.forgerock.opendj.cli.Utils.*; 064 065import static org.opends.messages.AdminToolMessages.*; 066import static org.opends.messages.QuickSetupMessages.*; 067 068/** 069 * This class is a dialog that appears when the user must provide authentication 070 * to connect to the Directory Server in order to be able to display 071 * information. 072 */ 073public class LoginDialog extends JDialog 074{ 075 private static final long serialVersionUID = 9049409381101152000L; 076 077 private JFrame parent; 078 079 private JLabel lHostName; 080 private JLabel lUid; 081 private JLabel lPwd; 082 083 private JTextField tfHostName; 084 private JTextField tfUid; 085 private JTextField tfPwd; 086 087 private JButton cancelButton; 088 private JButton okButton; 089 090 private boolean isCanceled = true; 091 092 private ApplicationTrustManager trustManager; 093 private int timeout; 094 095 private InitialLdapContext ctx; 096 097 private String usedUrl; 098 099 private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); 100 101 /** 102 * Constructor of the LoginDialog. 103 * @param parent the parent frame for this dialog. 104 * @param trustManager the trust manager to be used for the secure 105 * connections. 106 * @param timeout the timeout to establish the connection in milliseconds. 107 * Use {@code 0} to express no timeout. 108 */ 109 public LoginDialog(JFrame parent, ApplicationTrustManager trustManager, 110 int timeout) 111 { 112 super(parent); 113 setTitle(INFO_LOGIN_DIALOG_TITLE.get().toString()); 114 this.parent = parent; 115 getContentPane().add(createPanel()); 116 if (trustManager == null) 117 { 118 throw new IllegalArgumentException("The trustmanager cannot be null."); 119 } 120 this.trustManager = trustManager; 121 this.timeout = timeout; 122 /* 123 * TODO: find a way to calculate this dynamically. This is done to avoid 124 * all the text in a single line. 125 */ 126 setPreferredSize(new Dimension(500, 250)); 127 addComponentListener(new MinimumSizeComponentListener(this, 500, 250)); 128 getRootPane().setDefaultButton(okButton); 129 } 130 131 /** 132 * Returns <CODE>true</CODE> if the user clicked on cancel and 133 * <CODE>false</CODE> otherwise. 134 * @return <CODE>true</CODE> if the user clicked on cancel and 135 * <CODE>false</CODE> otherwise. 136 */ 137 public boolean isCanceled() 138 { 139 return isCanceled; 140 } 141 142 /** {@inheritDoc} */ 143 @Override 144 public void setVisible(boolean visible) 145 { 146 cancelButton.setEnabled(true); 147 okButton.setEnabled(true); 148 if (visible) 149 { 150 tfPwd.setText(""); 151 tfPwd.requestFocusInWindow(); 152 UIFactory.setTextStyle(lHostName, 153 UIFactory.TextStyle.PRIMARY_FIELD_VALID); 154 UIFactory.setTextStyle(lUid, 155 UIFactory.TextStyle.PRIMARY_FIELD_VALID); 156 UIFactory.setTextStyle(lPwd, 157 UIFactory.TextStyle.PRIMARY_FIELD_VALID); 158 getRootPane().setDefaultButton(okButton); 159 } 160 super.setVisible(visible); 161 } 162 163 /** 164 * Returns the Host Name as is referenced in other servers. 165 * @return the Host Name as is referenced in other servers. 166 */ 167 public String getHostName() 168 { 169 return tfHostName.getText(); 170 } 171 172 /** 173 * Returns the Administrator UID provided by the user. 174 * @return the Administrator UID provided by the user. 175 */ 176 public String getAdministratorUid() 177 { 178 return tfUid.getText(); 179 } 180 181 /** 182 * Returns the Administrator password provided by the user. 183 * @return the Administrator password provided by the user. 184 */ 185 public String getAdministratorPwd() 186 { 187 return tfPwd.getText(); 188 } 189 190 /** 191 * Returns the connection we got with the provided authentication. 192 * @return the connection we got with the provided authentication. 193 */ 194 public InitialLdapContext getContext() 195 { 196 return ctx; 197 } 198 199 /** 200 * Creates and returns the panel of the dialog. 201 * @return the panel of the dialog. 202 */ 203 private JPanel createPanel() 204 { 205 JPanel p1 = new JPanel(new GridBagLayout()); 206 p1.setBackground(UIFactory.CURRENT_STEP_PANEL_BACKGROUND); 207 p1.setBorder(UIFactory.DIALOG_PANEL_BORDER); 208 GridBagConstraints gbc = new GridBagConstraints(); 209 gbc.gridwidth = GridBagConstraints.RELATIVE; 210 gbc.anchor = GridBagConstraints.NORTHWEST; 211 gbc.insets = UIFactory.getCurrentStepPanelInsets(); 212 p1.add(UIFactory.makeJLabel(UIFactory.IconType.INFORMATION_LARGE, null, 213 UIFactory.TextStyle.NO_STYLE), gbc); 214 gbc.weightx = 1.0; 215 gbc.fill = GridBagConstraints.BOTH; 216 gbc.gridwidth = GridBagConstraints.REMAINDER; 217 gbc.insets.left = 0; 218 LocalizableMessage msg = INFO_UNINSTALL_LOGIN_DIALOG_MSG.get(); 219 220 JTextComponent textPane = 221 UIFactory.makeHtmlPane(msg, UIFactory.INSTRUCTIONS_FONT); 222 textPane.setOpaque(false); 223 textPane.setEditable(false); 224 p1.add(textPane, gbc); 225 226 JPanel p2 = new JPanel(new GridBagLayout()); 227 p2.setOpaque(false); 228 229 gbc.gridwidth = GridBagConstraints.RELATIVE; 230 gbc.weightx = 0.0; 231 gbc.insets = UIFactory.getEmptyInsets(); 232 gbc.anchor = GridBagConstraints.WEST; 233 gbc.fill = GridBagConstraints.HORIZONTAL; 234 lHostName = UIFactory.makeJLabel(UIFactory.IconType.NO_ICON, 235 INFO_UNINSTALL_LOGIN_HOST_NAME_LABEL.get(), 236 UIFactory.TextStyle.PRIMARY_FIELD_VALID); 237 p2.add(lHostName, gbc); 238 gbc.weightx = 1.0; 239 gbc.insets.left = UIFactory.LEFT_INSET_PRIMARY_FIELD; 240 gbc.gridwidth = GridBagConstraints.REMAINDER; 241 tfHostName = UIFactory.makeJTextField( 242 LocalizableMessage.raw(UserData.getDefaultHostName()), 243 INFO_UNINSTALL_LOGIN_HOST_NAME_TOOLTIP.get(), 244 UIFactory.HOST_FIELD_SIZE, UIFactory.TextStyle.TEXTFIELD); 245 p2.add(tfHostName, gbc); 246 247 gbc.gridwidth = GridBagConstraints.RELATIVE; 248 gbc.weightx = 0.0; 249 gbc.insets.left = 0; 250 gbc.insets.top = UIFactory.TOP_INSET_PRIMARY_FIELD; 251 gbc.anchor = GridBagConstraints.WEST; 252 gbc.fill = GridBagConstraints.HORIZONTAL; 253 lUid = UIFactory.makeJLabel(UIFactory.IconType.NO_ICON, 254 INFO_GLOBAL_ADMINISTRATOR_UID_LABEL.get(), 255 UIFactory.TextStyle.PRIMARY_FIELD_VALID); 256 p2.add(lUid, gbc); 257 gbc.weightx = 1.0; 258 gbc.insets.left = UIFactory.LEFT_INSET_PRIMARY_FIELD; 259 gbc.gridwidth = GridBagConstraints.REMAINDER; 260 tfUid = UIFactory.makeJTextField(LocalizableMessage.raw(Constants.GLOBAL_ADMIN_UID), 261 INFO_UNINSTALL_LOGIN_UID_TOOLTIP.get(), 262 UIFactory.DN_FIELD_SIZE, UIFactory.TextStyle.TEXTFIELD); 263 p2.add(tfUid, gbc); 264 265 gbc.gridwidth = GridBagConstraints.RELATIVE; 266 gbc.weightx = 0.0; 267 gbc.insets.left = 0; 268 lPwd = UIFactory.makeJLabel(UIFactory.IconType.NO_ICON, 269 INFO_GLOBAL_ADMINISTRATOR_PWD_LABEL.get(), 270 UIFactory.TextStyle.PRIMARY_FIELD_VALID); 271 p2.add(lPwd, gbc); 272 gbc.insets.left = UIFactory.LEFT_INSET_PRIMARY_FIELD; 273 gbc.fill = GridBagConstraints.HORIZONTAL; 274 gbc.gridwidth = GridBagConstraints.REMAINDER; 275 JPanel p3 = new JPanel(new GridBagLayout()); 276 p3.setOpaque(false); 277 tfPwd = UIFactory.makeJPasswordField(null, 278 INFO_UNINSTALL_LOGIN_PWD_TOOLTIP.get(), 279 UIFactory.PASSWORD_FIELD_SIZE, UIFactory.TextStyle.PASSWORD_FIELD); 280 p2.add(p3, gbc); 281 gbc.insets = UIFactory.getEmptyInsets(); 282 gbc.gridwidth = GridBagConstraints.RELATIVE; 283 gbc.weightx = 0.0; 284 p3.add(tfPwd, gbc); 285 gbc.gridwidth = GridBagConstraints.REMAINDER; 286 gbc.weightx = 1.0; 287 p3.add(Box.createHorizontalGlue(), gbc); 288 289 290 gbc.fill = GridBagConstraints.HORIZONTAL; 291 gbc.insets = UIFactory.getEmptyInsets(); 292 gbc.gridwidth = GridBagConstraints.RELATIVE; 293 gbc.weightx = 0.0; 294 gbc.insets.top = 0; 295 p1.add(Box.createHorizontalGlue(), gbc); 296 gbc.weightx = 1.0; 297 gbc.gridwidth = GridBagConstraints.REMAINDER; 298 gbc.insets.right = UIFactory.getCurrentStepPanelInsets().right; 299 p1.add(p2, gbc); 300 gbc.weighty = 1.0; 301 gbc.fill = GridBagConstraints.VERTICAL; 302 gbc.insets.bottom = UIFactory.getCurrentStepPanelInsets().bottom; 303 p1.add(Box.createVerticalGlue(), gbc); 304 305 306 JPanel buttonPanel = new JPanel(new GridBagLayout()); 307 buttonPanel.setOpaque(false); 308 gbc.fill = GridBagConstraints.HORIZONTAL; 309 gbc.weightx = 1.0; 310 gbc.insets = UIFactory.getEmptyInsets(); 311 gbc.gridwidth = 3; 312 buttonPanel.add(Box.createHorizontalGlue(), gbc); 313 gbc.gridwidth = GridBagConstraints.RELATIVE; 314 gbc.fill = GridBagConstraints.NONE; 315 gbc.weightx = 0.0; 316 okButton = 317 UIFactory.makeJButton(INFO_OK_BUTTON_LABEL.get(), 318 INFO_UNINSTALL_LOGIN_OK_BUTTON_TOOLTIP.get()); 319 buttonPanel.add(okButton, gbc); 320 okButton.addActionListener(new ActionListener() 321 { 322 @Override 323 public void actionPerformed(ActionEvent ev) 324 { 325 okClicked(); 326 } 327 }); 328 329 gbc.gridwidth = GridBagConstraints.REMAINDER; 330 gbc.insets.left = UIFactory.HORIZONTAL_INSET_BETWEEN_BUTTONS; 331 cancelButton = 332 UIFactory.makeJButton(INFO_CANCEL_BUTTON_LABEL.get(), 333 INFO_UNINSTALL_LOGIN_CANCEL_BUTTON_TOOLTIP.get()); 334 buttonPanel.add(cancelButton, gbc); 335 cancelButton.addActionListener(new ActionListener() 336 { 337 @Override 338 public void actionPerformed(ActionEvent ev) 339 { 340 cancelClicked(); 341 } 342 }); 343 344 JPanel p = new JPanel(new GridBagLayout()); 345 p.setBackground(UIFactory.DEFAULT_BACKGROUND); 346 gbc.insets = UIFactory.getEmptyInsets(); 347 gbc.fill = GridBagConstraints.BOTH; 348 gbc.gridwidth = GridBagConstraints.REMAINDER; 349 gbc.weightx = 1.0; 350 gbc.weighty = 1.0; 351 p.add(p1, gbc); 352 gbc.weighty = 0.0; 353 gbc.insets = UIFactory.getButtonsPanelInsets(); 354 p.add(buttonPanel, gbc); 355 356 return p; 357 } 358 359 /** 360 * Method called when user clicks on cancel. 361 * 362 */ 363 private void cancelClicked() 364 { 365 isCanceled = true; 366 dispose(); 367 } 368 369 /** 370 * Method called when user clicks on OK. 371 * 372 */ 373 private void okClicked() 374 { 375 BackgroundTask<Boolean> worker = new BackgroundTask<Boolean>() 376 { 377 @Override 378 public Boolean processBackgroundTask() throws NamingException, 379 ApplicationException 380 { 381 Boolean isServerRunning = Boolean.TRUE; 382 ctx = null; 383 try 384 { 385 ControlPanelInfo info = ControlPanelInfo.getInstance(); 386 info.setTrustManager(getTrustManager()); 387 info.setConnectTimeout(timeout); 388 info.regenerateDescriptor(); 389 ConfigFromFile conf = new ConfigFromFile(); 390 conf.readConfiguration(); 391 String dn = ADSContext.getAdministratorDN(tfUid.getText()); 392 String pwd = tfPwd.getText(); 393 info.setConnectionPolicy(ConnectionProtocolPolicy.USE_ADMIN); 394 usedUrl = info.getAdminConnectorURL(); 395 if (usedUrl == null) 396 { 397 throw new ApplicationException(ReturnCode.APPLICATION_ERROR, 398 ERR_COULD_NOT_FIND_VALID_LDAPURL.get(), null); 399 } 400 ctx = 401 org.opends.guitools.controlpanel.util.Utilities.getAdminDirContext( 402 info, dn, pwd); 403 404 405 } catch (NamingException ne) 406 { 407 if (isServerRunning()) 408 { 409 throw ne; 410 } 411 isServerRunning = Boolean.FALSE; 412 } catch (ApplicationException | IllegalStateException e) 413 { 414 throw e; 415 } catch (Throwable t) 416 { 417 throw new IllegalStateException("Unexpected throwable.", t); 418 } 419 return isServerRunning; 420 } 421 422 @Override 423 public void backgroundTaskCompleted(Boolean returnValue, 424 Throwable throwable) 425 { 426 if (throwable != null) 427 { 428 logger.info(LocalizableMessage.raw("Error connecting: " + throwable, throwable)); 429 if (isCertificateException(throwable)) 430 { 431 ApplicationTrustManager.Cause cause = 432 trustManager.getLastRefusedCause(); 433 434 logger.info(LocalizableMessage.raw("Certificate exception cause: "+cause)); 435 UserDataCertificateException.Type excType = null; 436 if (cause == ApplicationTrustManager.Cause.NOT_TRUSTED) 437 { 438 excType = UserDataCertificateException.Type.NOT_TRUSTED; 439 } 440 else if (cause == 441 ApplicationTrustManager.Cause.HOST_NAME_MISMATCH) 442 { 443 excType = UserDataCertificateException.Type.HOST_NAME_MISMATCH; 444 } 445 else 446 { 447 LocalizableMessage msg = getThrowableMsg( 448 INFO_ERROR_CONNECTING_TO_LOCAL.get(), throwable); 449 displayError(msg, INFO_ERROR_TITLE.get()); 450 } 451 452 if (excType != null) 453 { 454 String h; 455 int p; 456 try 457 { 458 URI uri = new URI(usedUrl); 459 h = uri.getHost(); 460 p = uri.getPort(); 461 } 462 catch (Throwable t) 463 { 464 logger.warn(LocalizableMessage.raw( 465 "Error parsing ldap url of ldap url.", t)); 466 h = INFO_NOT_AVAILABLE_LABEL.get().toString(); 467 p = -1; 468 } 469 UserDataCertificateException udce = 470 new UserDataCertificateException(Step.REPLICATION_OPTIONS, 471 INFO_CERTIFICATE_EXCEPTION.get(h, p), 472 throwable, h, p, 473 getTrustManager().getLastRefusedChain(), 474 getTrustManager().getLastRefusedAuthType(), excType); 475 476 handleCertificateException(udce); 477 } 478 } 479 else if (throwable instanceof NamingException) 480 { 481 boolean uidInvalid = false; 482 boolean pwdInvalid = false; 483 484 String uid = tfUid.getText(); 485 ArrayList<LocalizableMessage> possibleCauses = new ArrayList<>(); 486 if ("".equals(uid.trim())) 487 { 488 uidInvalid = true; 489 possibleCauses.add(INFO_EMPTY_ADMINISTRATOR_UID.get()); 490 } 491 492 if ("".equals(tfPwd.getText())) 493 { 494 pwdInvalid = true; 495 possibleCauses.add(INFO_EMPTY_PWD.get()); 496 } 497 if (uidInvalid) 498 { 499 UIFactory.setTextStyle(lUid, 500 UIFactory.TextStyle.PRIMARY_FIELD_INVALID); 501 } 502 else 503 { 504 UIFactory.setTextStyle(lUid, 505 UIFactory.TextStyle.PRIMARY_FIELD_VALID); 506 pwdInvalid = true; 507 } 508 if (pwdInvalid) 509 { 510 UIFactory.setTextStyle(lPwd, 511 UIFactory.TextStyle.PRIMARY_FIELD_INVALID); 512 } 513 else 514 { 515 UIFactory.setTextStyle(lPwd, 516 UIFactory.TextStyle.PRIMARY_FIELD_VALID); 517 } 518 if (possibleCauses.size() > 0) 519 { 520 // LocalizableMessage with causes 521 displayError( 522 ERR_CANNOT_CONNECT_TO_LOGIN_WITH_CAUSE.get( 523 Utils.getMessageFromCollection(possibleCauses, "\n")), 524 INFO_ERROR_TITLE.get()); 525 } 526 else 527 { 528 // Generic message 529 displayError( 530 Utils.getMessageForException((NamingException)throwable), 531 INFO_ERROR_TITLE.get()); 532 } 533 } 534 else if (throwable instanceof ApplicationException) 535 { 536 displayError(((ApplicationException)throwable).getMessageObject(), 537 INFO_ERROR_TITLE.get()); 538 } 539 else 540 { 541 // This is a bug 542 logger.error(LocalizableMessage.raw("Unexpected throwable: "+throwable, 543 throwable)); 544 displayError( 545 getThrowableMsg(INFO_BUG_MSG.get(), throwable), 546 INFO_ERROR_TITLE.get()); 547 } 548 cancelButton.setEnabled(true); 549 okButton.setEnabled(true); 550 } else 551 { 552 if (Boolean.FALSE.equals(returnValue)) 553 { 554 displayInformationMessage( 555 INFO_LOGIN_DIALOG_SERVER_NOT_RUNNING_MSG.get(), 556 INFO_LOGIN_DIALOG_SERVER_NOT_RUNNING_TITLE.get()); 557 } 558 else 559 { 560 String hostName = tfHostName.getText(); 561 if (hostName == null || hostName.trim().length() == 0) 562 { 563 displayError(INFO_EMPTY_REMOTE_HOST.get(), 564 INFO_ERROR_TITLE.get()); 565 UIFactory.setTextStyle(lHostName, 566 UIFactory.TextStyle.PRIMARY_FIELD_INVALID); 567 } 568 else 569 { 570 UIFactory.setTextStyle(lHostName, 571 UIFactory.TextStyle.PRIMARY_FIELD_VALID); 572 } 573 } 574 UIFactory.setTextStyle(lUid, 575 UIFactory.TextStyle.PRIMARY_FIELD_VALID); 576 UIFactory.setTextStyle(lPwd, 577 UIFactory.TextStyle.PRIMARY_FIELD_VALID); 578 579 isCanceled = false; 580 cancelButton.setEnabled(true); 581 okButton.setEnabled(true); 582 dispose(); 583 } 584 } 585 }; 586 cancelButton.setEnabled(false); 587 okButton.setEnabled(false); 588 worker.startBackgroundTask(); 589 } 590 591 /** 592 * Displays an error message dialog. 593 * 594 * @param msg 595 * the error message. 596 * @param title 597 * the title for the dialog. 598 */ 599 private void displayError(LocalizableMessage msg, LocalizableMessage title) 600 { 601 Utilities.displayError(parent, msg, title); 602 toFront(); 603 604 } 605 606 /** 607 * Displays an information message dialog. 608 * 609 * @param msg 610 * the information message. 611 * @param title 612 * the title for the dialog. 613 */ 614 private void displayInformationMessage(LocalizableMessage msg, LocalizableMessage title) 615 { 616 Utilities.displayInformationMessage(parent, msg, title); 617 toFront(); 618 } 619 620 /** 621 * Returns whether the server is running or not. 622 * @return <CODE>true</CODE> if the server is running and <CODE>false</CODE> 623 * otherwise. 624 */ 625 private boolean isServerRunning() 626 { 627 return Installation.getLocal().getStatus().isServerRunning(); 628 } 629 630 /** 631 * Returns the trust manager that can be used to establish secure connections. 632 * @return the trust manager that can be used to establish secure connections. 633 */ 634 private ApplicationTrustManager getTrustManager() 635 { 636 return trustManager; 637 } 638 639 /** 640 * Displays a dialog asking the user to accept a certificate if the user 641 * accepts it, we update the trust manager and simulate a click on "OK" to 642 * re-check the authentication. 643 * This method assumes that we are being called from the event thread. 644 */ 645 private void handleCertificateException(UserDataCertificateException ce) 646 { 647 CertificateDialog dlg = new CertificateDialog(parent, ce); 648 dlg.pack(); 649 dlg.setVisible(true); 650 if (dlg.getUserAnswer() != CertificateDialog.ReturnType.NOT_ACCEPTED) 651 { 652 X509Certificate[] chain = ce.getChain(); 653 String authType = ce.getAuthType(); 654 String host = ce.getHost(); 655 656 if (chain != null && authType != null && host != null) 657 { 658 logger.info(LocalizableMessage.raw("Accepting certificate presented by host "+host)); 659 getTrustManager().acceptCertificate(chain, authType, host); 660 /* Simulate a click on the OK by calling in the okClicked method. */ 661 SwingUtilities.invokeLater(new Runnable() 662 { 663 @Override 664 public void run() 665 { 666 okClicked(); 667 } 668 }); 669 } 670 else 671 { 672 if (chain == null) 673 { 674 logger.warn(LocalizableMessage.raw( 675 "The chain is null for the UserDataCertificateException")); 676 } 677 if (authType == null) 678 { 679 logger.warn(LocalizableMessage.raw( 680 "The auth type is null for the UserDataCertificateException")); 681 } 682 if (host == null) 683 { 684 logger.warn(LocalizableMessage.raw( 685 "The host is null for the UserDataCertificateException")); 686 } 687 } 688 } 689 if (dlg.getUserAnswer() == 690 CertificateDialog.ReturnType.ACCEPTED_PERMANENTLY) 691 { 692 X509Certificate[] chain = ce.getChain(); 693 if (chain != null) 694 { 695 try 696 { 697 UIKeyStore.acceptCertificate(chain); 698 } 699 catch (Throwable t) 700 { 701 logger.warn(LocalizableMessage.raw("Error accepting certificate: "+t, t)); 702 } 703 } 704 } 705 } 706 707 /** 708 * Method written for testing purposes. 709 * @param args the arguments to be passed to the test program. 710 */ 711 public static void main(String[] args) 712 { 713 try 714 { 715 LoginDialog dlg = new LoginDialog( 716 org.opends.guitools.controlpanel.util.Utilities.createFrame(), 717 new ApplicationTrustManager(null), 718 5000); 719 dlg.pack(); 720 dlg.setVisible(true); 721 } catch (Exception ex) 722 { 723 ex.printStackTrace(); 724 } 725 } 726}