LoginDialog.java revision 2f6d798e90520dd1b83ac30e53838ae6fd41a150
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at legal-notices/CDDLv1_0.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2008-2010 Sun Microsystems, Inc.
* Portions Copyright 2014-2015 ForgeRock AS
*/
/**
* This class is a dialog that appears when the user must provide authentication
* to connect to the Directory Server in order to be able to display
* information.
*/
public class LoginDialog extends JDialog
{
private static final long serialVersionUID = 9049409381101152000L;
private JTextField tfHostName;
private JTextField tfUid;
private JTextField tfPwd;
private JButton cancelButton;
private boolean isCanceled = true;
private ApplicationTrustManager trustManager;
private int timeout;
private InitialLdapContext ctx;
/**
* Constructor of the LoginDialog.
* @param parent the parent frame for this dialog.
* @param trustManager the trust manager to be used for the secure
* connections.
* @param timeout the timeout to establish the connection in milliseconds.
* Use {@code 0} to express no timeout.
*/
int timeout)
{
super(parent);
if (trustManager == null)
{
throw new IllegalArgumentException("The trustmanager cannot be null.");
}
this.trustManager = trustManager;
/*
* TODO: find a way to calculate this dynamically. This is done to avoid
* all the text in a single line.
*/
}
/**
* Returns <CODE>true</CODE> if the user clicked on cancel and
* <CODE>false</CODE> otherwise.
* @return <CODE>true</CODE> if the user clicked on cancel and
* <CODE>false</CODE> otherwise.
*/
public boolean isCanceled()
{
return isCanceled;
}
/** {@inheritDoc} */
public void setVisible(boolean visible)
{
cancelButton.setEnabled(true);
okButton.setEnabled(true);
if (visible)
{
}
super.setVisible(visible);
}
/**
* Returns the Host Name as is referenced in other servers.
* @return the Host Name as is referenced in other servers.
*/
public String getHostName()
{
return tfHostName.getText();
}
/**
* Returns the Administrator UID provided by the user.
* @return the Administrator UID provided by the user.
*/
public String getAdministratorUid()
{
}
/**
* Returns the Administrator password provided by the user.
* @return the Administrator password provided by the user.
*/
public String getAdministratorPwd()
{
}
/**
* Returns the connection we got with the provided authentication.
* @return the connection we got with the provided authentication.
*/
public InitialLdapContext getContext()
{
return ctx;
}
/**
* Creates and returns the panel of the dialog.
* @return the panel of the dialog.
*/
private JPanel createPanel()
{
textPane.setEditable(false);
buttonPanel.setOpaque(false);
okButton =
{
{
okClicked();
}
});
{
{
}
});
return p;
}
/**
* Method called when user clicks on cancel.
*
*/
private void cancelClicked()
{
isCanceled = true;
dispose();
}
/**
* Method called when user clicks on OK.
*
*/
private void okClicked()
{
{
{
try
{
{
}
ctx =
} catch (NamingException ne)
{
if (isServerRunning())
{
throw ne;
}
} catch (ApplicationException | IllegalStateException e)
{
throw e;
} catch (Throwable t)
{
throw new IllegalStateException("Unexpected throwable.", t);
}
return isServerRunning;
}
{
{
{
{
}
else if (cause ==
{
}
else
{
}
{
String h;
int p;
try
{
}
catch (Throwable t)
{
"Error parsing ldap url of ldap url.", t));
p = -1;
}
INFO_CERTIFICATE_EXCEPTION.get(h, p),
throwable, h, p,
}
}
else if (throwable instanceof NamingException)
{
boolean uidInvalid = false;
boolean pwdInvalid = false;
{
uidInvalid = true;
}
{
pwdInvalid = true;
}
if (uidInvalid)
{
}
else
{
pwdInvalid = true;
}
if (pwdInvalid)
{
}
else
{
}
{
// LocalizableMessage with causes
INFO_ERROR_TITLE.get());
}
else
{
// Generic message
INFO_ERROR_TITLE.get());
}
}
else if (throwable instanceof ApplicationException)
{
INFO_ERROR_TITLE.get());
}
else
{
// This is a bug
throwable));
INFO_ERROR_TITLE.get());
}
cancelButton.setEnabled(true);
okButton.setEnabled(true);
} else
{
{
}
else
{
{
INFO_ERROR_TITLE.get());
}
else
{
}
}
isCanceled = false;
cancelButton.setEnabled(true);
okButton.setEnabled(true);
dispose();
}
}
};
cancelButton.setEnabled(false);
okButton.setEnabled(false);
}
/**
* Displays an error message dialog.
*
* @param msg
* the error message.
* @param title
* the title for the dialog.
*/
{
toFront();
}
/**
* Displays an information message dialog.
*
* @param msg
* the information message.
* @param title
* the title for the dialog.
*/
{
toFront();
}
/**
* Returns whether the server is running or not.
* @return <CODE>true</CODE> if the server is running and <CODE>false</CODE>
* otherwise.
*/
private boolean isServerRunning()
{
}
/**
* Returns the trust manager that can be used to establish secure connections.
* @return the trust manager that can be used to establish secure connections.
*/
private ApplicationTrustManager getTrustManager()
{
return trustManager;
}
/**
* Displays a dialog asking the user to accept a certificate if the user
* accepts it, we update the trust manager and simulate a click on "OK" to
* re-check the authentication.
* This method assumes that we are being called from the event thread.
*/
{
dlg.setVisible(true);
{
{
/* Simulate a click on the OK by calling in the okClicked method. */
{
public void run()
{
okClicked();
}
});
}
else
{
{
"The chain is null for the UserDataCertificateException"));
}
{
"The auth type is null for the UserDataCertificateException"));
}
{
"The host is null for the UserDataCertificateException"));
}
}
}
if (dlg.getUserAnswer() ==
{
{
try
{
}
catch (Throwable t)
{
}
}
}
}
/**
* Method written for testing purposes.
* @param args the arguments to be passed to the test program.
*/
{
try
{
new ApplicationTrustManager(null),
5000);
dlg.setVisible(true);
{
}
}
}