| | |
| | | import org.opends.admin.ads.util.ConnectionUtils; |
| | | import org.opends.admin.ads.util.OpendsCertificateException; |
| | | import org.opends.quicksetup.util.Utils; |
| | | import org.opends.server.protocols.ldap.LDAPResultCode; |
| | | import org.opends.server.tools.ClientException; |
| | | import org.opends.server.types.NullOutputStream; |
| | | import org.opends.server.util.PasswordReader; |
| | | import org.opends.server.util.SetupUtils; |
| | | import org.opends.server.util.StaticUtils; |
| | | import com.forgerock.opendj.cli.CLIException; |
| | | |
| | | import com.forgerock.opendj.cli.ClientException; |
| | | import com.forgerock.opendj.cli.ReturnCode; |
| | | |
| | | |
| | | /** |
| | |
| | | * @return Returns <code>true</code> if the user wishes the action to be |
| | | * performed, or <code>false</code> if they refused, or if an |
| | | * exception occurred. |
| | | * @throws CLIException |
| | | * @throws ClientException |
| | | * If the user's response could not be read from the console for |
| | | * some reason. |
| | | */ |
| | | public final boolean confirmAction(LocalizableMessage prompt, final boolean defaultValue) |
| | | throws CLIException |
| | | throws ClientException |
| | | { |
| | | if (!isInteractive()) |
| | | { |
| | |
| | | { |
| | | readLineOfInput(msg); |
| | | } |
| | | catch (CLIException e) |
| | | catch (ClientException e) |
| | | { |
| | | // Ignore the exception - applications don't care. |
| | | } |
| | |
| | | * The prompt. |
| | | * @return Returns the line of input, or <code>null</code> if the end of input |
| | | * has been reached. |
| | | * @throws CLIException |
| | | * @throws ClientException |
| | | * If the line of input could not be retrieved for some reason. |
| | | */ |
| | | public final String readLineOfInput(LocalizableMessage prompt) throws CLIException |
| | | public final String readLineOfInput(LocalizableMessage prompt) throws ClientException |
| | | { |
| | | if (prompt != null) |
| | | { |
| | |
| | | String s = in.readLine(); |
| | | if (s == null) |
| | | { |
| | | throw CLIException |
| | | throw ClientException |
| | | .adaptInputException(new EOFException("End of input")); |
| | | } |
| | | else |
| | |
| | | } |
| | | catch (IOException e) |
| | | { |
| | | throw CLIException.adaptInputException(e); |
| | | throw ClientException.adaptInputException(e); |
| | | } |
| | | } |
| | | |
| | |
| | | * @param msgStyle |
| | | * The formatted style chosen. |
| | | * @return The user's input as a string. |
| | | * @throws CLIException |
| | | * @throws ClientException |
| | | * If an Exception occurs during the process. |
| | | */ |
| | | public final String readInput(final LocalizableMessage prompt, |
| | | final String defaultValue, final Style msgStyle) |
| | | throws CLIException |
| | | throws ClientException |
| | | { |
| | | String answer = null; |
| | | if (msgStyle == Style.TITLE) |
| | |
| | | } |
| | | catch (IOException e) |
| | | { |
| | | throw CLIException.adaptInputException(e); |
| | | throw ClientException.adaptInputException(e); |
| | | } |
| | | |
| | | if (msgStyle == Style.TITLE |
| | |
| | | * The default value to assume if the user presses ENTER without |
| | | * typing anything, or <CODE>null</CODE> if there should not be a |
| | | * default and the user must explicitly provide a value. |
| | | * @throws CLIException |
| | | * @throws ClientException |
| | | * If the line of input could not be retrieved for some reason. |
| | | * @return The string value read from the user. |
| | | */ |
| | | public String readInput(LocalizableMessage prompt, String defaultValue) |
| | | throws CLIException |
| | | throws ClientException |
| | | { |
| | | while (true) |
| | | { |
| | |
| | | { |
| | | s = readInput(prompt, defaultValue); |
| | | } |
| | | catch (CLIException ce) |
| | | catch (ClientException ce) |
| | | { |
| | | logger.warn(LocalizableMessage.raw("Error reading input: " + ce, ce)); |
| | | } |
| | |
| | | * @param prompt |
| | | * The password prompt. |
| | | * @return Returns the password. |
| | | * @throws CLIException |
| | | * @throws ClientException |
| | | * If the password could not be retrieved for some reason. |
| | | */ |
| | | public final String readPassword(LocalizableMessage prompt) throws CLIException |
| | | public final String readPassword(LocalizableMessage prompt) throws ClientException |
| | | { |
| | | err.print(wrapText(prompt + " ", MAX_LINE_WIDTH)); |
| | | char[] pwChars; |
| | |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | throw CLIException.adaptInputException(e); |
| | | throw ClientException.adaptInputException(e); |
| | | } |
| | | return new String(pwChars); |
| | | } |
| | |
| | | { |
| | | pwd = readPassword(prompt); |
| | | } |
| | | catch (CLIException ce) |
| | | catch (ClientException ce) |
| | | { |
| | | logger.warn(LocalizableMessage.raw("Error reading input: " + ce, ce)); |
| | | } |
| | |
| | | * @param defaultValue |
| | | * The port default value. |
| | | * @return Returns the port. |
| | | * @throws CLIException |
| | | * @throws ClientException |
| | | * If the port could not be retrieved for some reason. |
| | | */ |
| | | public final int readPort(LocalizableMessage prompt, final int defaultValue) |
| | | throws CLIException |
| | | throws ClientException |
| | | { |
| | | ValidationCallback<Integer> callback = new ValidationCallback<Integer>() |
| | | { |
| | | @Override |
| | | public Integer validate(ConsoleApplication app, String input) |
| | | throws CLIException |
| | | throws ClientException |
| | | { |
| | | String ninput = input.trim(); |
| | | if (ninput.length() == 0) |
| | |
| | | { |
| | | port = readPort(prompt, defaultValue); |
| | | } |
| | | catch (CLIException ce) |
| | | catch (ClientException ce) |
| | | { |
| | | port = -1; |
| | | logger.warn(LocalizableMessage.raw("Error reading input: " + ce, ce)); |
| | |
| | | * An input validator responsible for validating and decoding the |
| | | * user's response. |
| | | * @return Returns the decoded user's response. |
| | | * @throws CLIException |
| | | * @throws ClientException |
| | | * If an unexpected error occurred which prevented validation. |
| | | */ |
| | | public final <T> T readValidatedInput(LocalizableMessage prompt, |
| | | ValidationCallback<T> validator) throws CLIException |
| | | ValidationCallback<T> validator) throws ClientException |
| | | { |
| | | while (true) |
| | | { |
| | |
| | | * @param maxTries |
| | | * The maximum number of tries that we can make. |
| | | * @return Returns the decoded user's response. |
| | | * @throws CLIException |
| | | * @throws ClientException |
| | | * If an unexpected error occurred which prevented validation or if |
| | | * the maximum number of tries was reached. |
| | | */ |
| | | public final <T> T readValidatedInput(LocalizableMessage prompt, |
| | | ValidationCallback<T> validator, int maxTries) throws CLIException |
| | | ValidationCallback<T> validator, int maxTries) throws ClientException |
| | | { |
| | | int nTries = 0; |
| | | while (nTries < maxTries) |
| | |
| | | } |
| | | nTries++; |
| | | } |
| | | throw new CLIException(ERR_TRIES_LIMIT_REACHED.get(maxTries)); |
| | | throw new ClientException(ReturnCode.TODO, ERR_TRIES_LIMIT_REACHED.get(maxTries)); |
| | | } |
| | | |
| | | /** |
| | |
| | | * the Logger to be used to log the error message. |
| | | * @return Returns <code>true</code> if the user wishes the action to be |
| | | * performed, or <code>false</code> if they refused. |
| | | * @throws CLIException |
| | | * @throws ClientException |
| | | * if the user did not provide valid answer after a certain number |
| | | * of tries (ConsoleApplication.CONFIRMATION_MAX_TRIES) |
| | | */ |
| | | protected final boolean askConfirmation(LocalizableMessage prompt, boolean defaultValue, |
| | | LocalizedLogger logger) throws CLIException |
| | | LocalizedLogger logger) throws ClientException |
| | | { |
| | | boolean v = defaultValue; |
| | | |
| | |
| | | v = confirmAction(prompt, defaultValue); |
| | | done = true; |
| | | } |
| | | catch (CLIException ce) |
| | | catch (ClientException ce) |
| | | { |
| | | if (StaticUtils.hasDescriptor(ce.getMessageObject(), |
| | | ERR_CONFIRMATION_TRIES_LIMIT_REACHED) |
| | |
| | | if (!done) |
| | | { |
| | | // This means we reached the maximum number of tries |
| | | throw new CLIException(ERR_CONFIRMATION_TRIES_LIMIT_REACHED |
| | | throw new ClientException(ReturnCode.TODO, ERR_CONFIRMATION_TRIES_LIMIT_REACHED |
| | | .get(CONFIRMATION_MAX_TRIES)); |
| | | } |
| | | return v; |
| | |
| | | LocalizableMessage message = |
| | | ERR_DSCFG_ERROR_LDAP_FAILED_TO_CONNECT_NOT_TRUSTED.get(hostName, portNumber); |
| | | throw new ClientException( |
| | | LDAPResultCode.CLIENT_SIDE_CONNECT_ERROR, message); |
| | | ReturnCode.CLIENT_SIDE_CONNECT_ERROR, message); |
| | | } |
| | | } |
| | | if (e.getCause() instanceof SSLException) |
| | |
| | | LocalizableMessage message = |
| | | ERR_DSCFG_ERROR_LDAP_FAILED_TO_CONNECT_WRONG_PORT.get(hostName, portNumber); |
| | | throw new ClientException( |
| | | LDAPResultCode.CLIENT_SIDE_CONNECT_ERROR, message); |
| | | ReturnCode.CLIENT_SIDE_CONNECT_ERROR, message); |
| | | } |
| | | } |
| | | String hostPort = |
| | | ServerDescriptor.getServerRepresentation(hostName, portNumber); |
| | | LocalizableMessage message = Utils.getMessageForException(e, hostPort); |
| | | throw new ClientException(LDAPResultCode.CLIENT_SIDE_CONNECT_ERROR, |
| | | throw new ClientException(ReturnCode.CLIENT_SIDE_CONNECT_ERROR, |
| | | message); |
| | | } |
| | | } |
| | |
| | | LocalizableMessage message = |
| | | ERR_DSCFG_ERROR_LDAP_FAILED_TO_CONNECT.get(hostName, portNumber); |
| | | throw new ClientException( |
| | | LDAPResultCode.CLIENT_SIDE_CONNECT_ERROR, message); |
| | | ReturnCode.CLIENT_SIDE_CONNECT_ERROR, message); |
| | | } |
| | | } |
| | | LocalizableMessage message = |
| | | ERR_DSCFG_ERROR_LDAP_FAILED_TO_CONNECT.get(hostName, portNumber); |
| | | throw new ClientException(LDAPResultCode.CLIENT_SIDE_CONNECT_ERROR, |
| | | throw new ClientException(ReturnCode.CLIENT_SIDE_CONNECT_ERROR, |
| | | message); |
| | | } |
| | | } |
| | |
| | | { |
| | | LocalizableMessage message = |
| | | ERR_DSCFG_ERROR_LDAP_FAILED_TO_CONNECT.get(hostName, portNumber); |
| | | throw new ClientException(LDAPResultCode.CLIENT_SIDE_CONNECT_ERROR, |
| | | throw new ClientException(ReturnCode.CLIENT_SIDE_CONNECT_ERROR, |
| | | message); |
| | | } |
| | | } |
| | |
| | | { |
| | | s = readInput(INFO_ADMINISTRATOR_UID_PROMPT.get(), defaultValue); |
| | | } |
| | | catch (CLIException ce) |
| | | catch (ClientException ce) |
| | | { |
| | | logger.warn(LocalizableMessage.raw("Error reading input: " + ce, ce)); |
| | | } |
| | |
| | | { |
| | | newInt = readInteger(prompt, defaultValue); |
| | | } |
| | | catch (CLIException ce) |
| | | catch (ClientException ce) |
| | | { |
| | | newInt = -1; |
| | | logger.warn(LocalizableMessage.raw("Error reading input: " + ce, ce)); |
| | |
| | | * @param defaultValue |
| | | * The default value. |
| | | * @return Returns the value. |
| | | * @throws CLIException |
| | | * @throws ClientException |
| | | * If the value could not be retrieved for some reason. |
| | | */ |
| | | public final int readInteger(LocalizableMessage prompt, final int defaultValue) |
| | | throws CLIException |
| | | throws ClientException |
| | | { |
| | | ValidationCallback<Integer> callback = new ValidationCallback<Integer>() |
| | | { |
| | | @Override |
| | | public Integer validate(ConsoleApplication app, String input) |
| | | throws CLIException |
| | | throws ClientException |
| | | { |
| | | String ninput = input.trim(); |
| | | if (ninput.length() == 0) |