mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noel Rouvignac
21.53.2014 10f162fe7aef97d8a9655b502bfccbe2cfbd7572
Removed duplicated code.


LDAPConnectionArgumentParser.java:
Extracted method getPasswordValue().

LDAPCompare.java, LDAPDelete.java, LDAPModify.java,
LDAPSearch.java:
Used LDAPConnectionArgumentParser.getPasswordValue() to remove duplicated code.

SmallMapTest.java:
Now extends DirectoryServerTestCase.
6 files modified
313 ■■■■■ changed files
opendj3-server-dev/src/server/org/opends/server/tools/LDAPCompare.java 52 ●●●●● patch | view | raw | blame | history
opendj3-server-dev/src/server/org/opends/server/tools/LDAPDelete.java 57 ●●●●● patch | view | raw | blame | history
opendj3-server-dev/src/server/org/opends/server/tools/LDAPModify.java 55 ●●●● patch | view | raw | blame | history
opendj3-server-dev/src/server/org/opends/server/tools/LDAPSearch.java 41 ●●●● patch | view | raw | blame | history
opendj3-server-dev/src/server/org/opends/server/util/args/LDAPConnectionArgumentParser.java 105 ●●●●● patch | view | raw | blame | history
opendj3-server-dev/tests/unit-tests-testng/src/server/org/opends/server/types/SmallMapTest.java 3 ●●●● patch | view | raw | blame | history
opendj3-server-dev/src/server/org/opends/server/tools/LDAPCompare.java
@@ -25,6 +25,7 @@
 *      Portions Copyright 2012-2014 ForgeRock AS
 */
package org.opends.server.tools;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
@@ -37,25 +38,27 @@
import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicInteger;
import org.opends.admin.ads.util.ConnectionUtils;
import org.forgerock.i18n.LocalizableMessage;
import org.opends.server.controls.LDAPAssertionRequestControl;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.DecodeException;
import org.opends.admin.ads.util.ConnectionUtils;
import org.opends.server.controls.LDAPAssertionRequestControl;
import org.opends.server.protocols.ldap.CompareRequestProtocolOp;
import org.opends.server.protocols.ldap.CompareResponseProtocolOp;
import org.opends.server.protocols.ldap.LDAPFilter;
import org.opends.server.protocols.ldap.LDAPMessage;
import org.opends.server.protocols.ldap.ProtocolOp;
import org.opends.server.types.*;
import org.forgerock.opendj.ldap.ByteString;
import org.opends.server.types.Control;
import org.opends.server.types.LDAPException;
import org.opends.server.types.NullOutputStream;
import org.opends.server.util.Base64;
import org.opends.server.util.EmbeddedUtils;
import com.forgerock.opendj.cli.ArgumentException;
import com.forgerock.opendj.cli.ArgumentParser;
import com.forgerock.opendj.cli.BooleanArgument;
import com.forgerock.opendj.cli.ClientException;
import com.forgerock.opendj.cli.CommonArguments;
import com.forgerock.opendj.cli.ConsoleApplication;
import com.forgerock.opendj.cli.FileBasedArgument;
import com.forgerock.opendj.cli.IntegerArgument;
import com.forgerock.opendj.cli.StringArgument;
@@ -65,7 +68,7 @@
import static com.forgerock.opendj.cli.ArgumentConstants.*;
import static org.opends.server.util.ServerConstants.*;
import static org.opends.server.util.StaticUtils.*;
import static org.opends.server.util.args.LDAPConnectionArgumentParser.*;
/**
 * This class provides a tool that can be used to issue compare requests to the
@@ -803,37 +806,16 @@
    String bindDNValue = bindDN.getValue();
    String fileNameValue = filename.getValue();
    String bindPasswordValue = bindPassword.getValue();
    if(bindPasswordValue != null && bindPasswordValue.equals("-")  ||
       (!bindPasswordFile.isPresent()  &&
       (bindDNValue != null && bindPasswordValue == null)))
    String bindPasswordValue;
    try
    {
      // read the password from the stdin.
      try
      {
        out.print(INFO_LDAPAUTH_PASSWORD_PROMPT.get(bindDNValue));
        char[] pwChars = ConsoleApplication.readPassword();
        bindPasswordValue = new String(pwChars);
        //As per rfc 4513(section-5.1.2) a client should avoid sending
        //an empty password to the server.
        while(pwChars.length==0)
        {
          err.println(wrapText(
                  INFO_LDAPAUTH_NON_EMPTY_PASSWORD.get(),
                  MAX_LINE_WIDTH));
          out.print(INFO_LDAPAUTH_PASSWORD_PROMPT.get(bindDNValue));
          pwChars = ConsoleApplication.readPassword();
        }
        bindPasswordValue = new String(pwChars);
      } catch(Exception ex)
      {
        err.println(wrapText(ex.getMessage(), MAX_LINE_WIDTH));
        return CLIENT_SIDE_PARAM_ERROR;
      }
    } else if(bindPasswordValue == null)
      bindPasswordValue = getPasswordValue(
          bindPassword, bindPasswordFile, bindDNValue, out, err);
    }
    catch (ClientException ex)
    {
      // Read from file if it exists.
      bindPasswordValue = bindPasswordFile.getValue();
      err.println(wrapText(ex.getMessage(), MAX_LINE_WIDTH));
      return CLIENT_SIDE_PARAM_ERROR;
    }
    String keyStorePathValue = keyStorePath.getValue();
opendj3-server-dev/src/server/org/opends/server/tools/LDAPDelete.java
@@ -25,8 +25,6 @@
 *      Portions Copyright 2012-2014 ForgeRock AS.
 */
package org.opends.server.tools;
import org.opends.admin.ads.util.ConnectionUtils;
import org.forgerock.i18n.LocalizableMessage;
import java.io.BufferedReader;
import java.io.FileReader;
@@ -39,33 +37,35 @@
import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicInteger;
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.DecodeException;
import org.opends.admin.ads.util.ConnectionUtils;
import org.opends.server.controls.SubtreeDeleteControl;
import org.opends.server.protocols.ldap.DeleteRequestProtocolOp;
import org.opends.server.protocols.ldap.DeleteResponseProtocolOp;
import org.opends.server.protocols.ldap.LDAPMessage;
import org.opends.server.protocols.ldap.ProtocolOp;
import org.opends.server.types.*;
import org.forgerock.opendj.ldap.ByteString;
import org.opends.server.types.Control;
import org.opends.server.types.LDAPException;
import org.opends.server.types.NullOutputStream;
import org.opends.server.util.EmbeddedUtils;
import com.forgerock.opendj.cli.ArgumentException;
import com.forgerock.opendj.cli.ArgumentParser;
import com.forgerock.opendj.cli.BooleanArgument;
import com.forgerock.opendj.cli.CommonArguments;
import com.forgerock.opendj.cli.ConsoleApplication;
import com.forgerock.opendj.cli.FileBasedArgument;
import com.forgerock.opendj.cli.IntegerArgument;
import com.forgerock.opendj.cli.StringArgument;
import org.opends.server.controls.SubtreeDeleteControl;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import static org.opends.messages.ToolMessages.*;
import static org.opends.server.protocols.ldap.LDAPResultCode.*;
import static org.opends.server.util.ServerConstants.*;
import static org.opends.server.util.StaticUtils.*;
import static com.forgerock.opendj.cli.ArgumentConstants.*;
import static org.opends.server.util.args.LDAPConnectionArgumentParser.*;
/**
 * This class provides a tool that can be used to issue delete requests to the
@@ -616,38 +616,17 @@
    String bindDNValue = bindDN.getValue();
    String fileNameValue = filename.getValue();
    String bindPasswordValue = bindPassword.getValue();
    if(bindPasswordValue != null && bindPasswordValue.equals("-")  ||
      (!bindPasswordFile.isPresent()  &&
       (bindDNValue != null && bindPasswordValue == null)))
    String bindPasswordValue;
    try
    {
      // read the password from the stdin.
      try
      {
        out.print(INFO_LDAPAUTH_PASSWORD_PROMPT.get(bindDNValue));
        char[] pwChars = ConsoleApplication.readPassword();
        bindPasswordValue = new String(pwChars);
        //As per rfc 4513(section-5.1.2) a client should avoid sending
        //an empty password to the server.
        while(pwChars.length==0)
        {
          err.println(wrapText(
                  INFO_LDAPAUTH_NON_EMPTY_PASSWORD.get(),
                  MAX_LINE_WIDTH));
          out.print(INFO_LDAPAUTH_PASSWORD_PROMPT.get(bindDNValue));
          pwChars = ConsoleApplication.readPassword();
        }
        bindPasswordValue = new String(pwChars);
      } catch(Exception ex)
      {
        logger.traceException(ex);
        err.println(wrapText(ex.getMessage(), MAX_LINE_WIDTH));
        return CLIENT_SIDE_PARAM_ERROR;
      }
    } else if(bindPasswordValue == null)
      bindPasswordValue = getPasswordValue(
          bindPassword, bindPasswordFile, bindDNValue, out, err);
    }
    catch (Exception ex)
    {
      // Read from file if it exists.
      bindPasswordValue = bindPasswordFile.getValue();
      logger.traceException(ex);
      err.println(wrapText(ex.getMessage(), MAX_LINE_WIDTH));
      return CLIENT_SIDE_PARAM_ERROR;
    }
    String keyStorePathValue = keyStorePath.getValue();
opendj3-server-dev/src/server/org/opends/server/tools/LDAPModify.java
@@ -26,8 +26,6 @@
 *      Portions Copyright 2012-2014 ForgeRock AS.
 */
package org.opends.server.tools;
import org.opends.admin.ads.util.ConnectionUtils;
import org.forgerock.i18n.LocalizableMessage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -38,10 +36,14 @@
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import org.opends.server.controls.*;
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.opends.server.plugins.ChangeNumberControlPlugin;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.DecodeException;
import org.forgerock.opendj.ldap.ResultCode;
import org.opends.admin.ads.util.ConnectionUtils;
import org.opends.server.controls.*;
import org.opends.server.plugins.ChangeNumberControlPlugin;
import org.opends.server.protocols.ldap.AddRequestProtocolOp;
import org.opends.server.protocols.ldap.AddResponseProtocolOp;
import org.opends.server.protocols.ldap.DeleteRequestProtocolOp;
@@ -58,8 +60,6 @@
import org.opends.server.protocols.ldap.ModifyDNResponseProtocolOp;
import org.opends.server.protocols.ldap.ProtocolOp;
import org.opends.server.types.*;
import org.forgerock.opendj.ldap.ResultCode;
import org.forgerock.opendj.ldap.ByteString;
import org.opends.server.util.AddChangeRecordEntry;
import org.opends.server.util.ChangeRecordEntry;
import org.opends.server.util.EmbeddedUtils;
@@ -72,7 +72,6 @@
import com.forgerock.opendj.cli.ArgumentParser;
import com.forgerock.opendj.cli.BooleanArgument;
import com.forgerock.opendj.cli.CommonArguments;
import com.forgerock.opendj.cli.ConsoleApplication;
import com.forgerock.opendj.cli.FileBasedArgument;
import com.forgerock.opendj.cli.IntegerArgument;
import com.forgerock.opendj.cli.StringArgument;
@@ -82,8 +81,7 @@
import static com.forgerock.opendj.cli.ArgumentConstants.*;
import static org.opends.server.util.ServerConstants.*;
import static org.opends.server.util.StaticUtils.*;
import static org.opends.server.util.args.LDAPConnectionArgumentParser.*;
/**
 * This class provides a tool that can be used to issue modify requests to the
@@ -944,38 +942,17 @@
    String bindDNValue = bindDN.getValue();
    String fileNameValue = filename.getValue();
    String bindPasswordValue = bindPassword.getValue();
    if(bindPasswordValue != null && bindPasswordValue.equals("-")  ||
        (!bindPasswordFile.isPresent()  &&
        (bindDNValue != null && bindPasswordValue == null)))
    String bindPasswordValue;
    try
    {
      // read the password from the stdin.
      try
      {
        out.print(INFO_LDAPAUTH_PASSWORD_PROMPT.get(bindDNValue));
        char[] pwChars = ConsoleApplication.readPassword();
        bindPasswordValue = new String(pwChars);
        //As per rfc 4513(section-5.1.2) a client should avoid sending
        //an empty password to the server.
        while(pwChars.length==0)
        {
          err.println(wrapText(
                  INFO_LDAPAUTH_NON_EMPTY_PASSWORD.get(),
                  MAX_LINE_WIDTH));
          out.print(INFO_LDAPAUTH_PASSWORD_PROMPT.get(bindDNValue));
          pwChars = ConsoleApplication.readPassword();
        }
        bindPasswordValue = new String(pwChars);
      } catch(Exception ex)
      {
        logger.traceException(ex);
        err.println(wrapText(ex.getMessage(), MAX_LINE_WIDTH));
        return CLIENT_SIDE_PARAM_ERROR;
      }
    } else if(bindPasswordValue == null)
      bindPasswordValue = getPasswordValue(
          bindPassword, bindPasswordFile, bindDNValue, out, err);
    }
    catch (Exception ex)
    {
      // Read from file if it exists.
      bindPasswordValue = bindPasswordFile.getValue();
      logger.traceException(ex);
      err.println(wrapText(ex.getMessage(), MAX_LINE_WIDTH));
      return CLIENT_SIDE_PARAM_ERROR;
    }
    String keyStorePathValue = keyStorePath.getValue();
opendj3-server-dev/src/server/org/opends/server/tools/LDAPSearch.java
@@ -49,7 +49,6 @@
import com.forgerock.opendj.cli.ArgumentParser;
import com.forgerock.opendj.cli.BooleanArgument;
import com.forgerock.opendj.cli.CommonArguments;
import com.forgerock.opendj.cli.ConsoleApplication;
import com.forgerock.opendj.cli.FileBasedArgument;
import com.forgerock.opendj.cli.IntegerArgument;
import com.forgerock.opendj.cli.MultiChoiceArgument;
@@ -61,6 +60,7 @@
import static com.forgerock.opendj.cli.ArgumentConstants.*;
import static org.opends.server.util.ServerConstants.*;
import static org.opends.server.util.StaticUtils.*;
import static org.opends.server.util.args.LDAPConnectionArgumentParser.*;
/**
 * This class provides a tool that can be used to issue search requests to the
@@ -1199,39 +1199,17 @@
    String baseDNValue = baseDN.getValue();
    String bindDNValue = bindDN.getValue();
    String fileNameValue = filename.getValue();
    String bindPasswordValue = bindPassword.getValue();
    if(bindPasswordValue != null && bindPasswordValue.equals("-")  ||
      (!bindPasswordFile.isPresent()  &&
      (bindDNValue != null && bindPasswordValue == null)))
    String bindPasswordValue;
    try
    {
      // read the password from the stdin.
      try
      {
        out.print(INFO_LDAPAUTH_PASSWORD_PROMPT.get(bindDNValue));
        char[] pwChars = ConsoleApplication.readPassword();
        bindPasswordValue = new String(pwChars);
        //As per rfc 4513(section-5.1.2) a client should avoid sending
        //an empty password to the server.
        while(pwChars.length ==0)
        {
          err.println(wrapText(
                  INFO_LDAPAUTH_NON_EMPTY_PASSWORD.get(),
                  MAX_LINE_WIDTH));
          out.print(INFO_LDAPAUTH_PASSWORD_PROMPT.get(bindDNValue));
          pwChars = ConsoleApplication.readPassword();
        }
        bindPasswordValue = new String(pwChars);
      } catch(Exception ex)
      {
        logger.traceException(ex);
        err.println(wrapText(ex.getMessage(), MAX_LINE_WIDTH));
        return CLIENT_SIDE_PARAM_ERROR;
      }
      bindPasswordValue = getPasswordValue(
          bindPassword, bindPasswordFile, bindDNValue, out, err);
    }
    else if(bindPasswordValue == null)
    catch (Exception ex)
    {
      // Read from file if it exists.
      bindPasswordValue = bindPasswordFile.getValue();
      logger.traceException(ex);
      err.println(wrapText(ex.getMessage(), MAX_LINE_WIDTH));
      return CLIENT_SIDE_PARAM_ERROR;
    }
    String keyStorePathValue = keyStorePath.getValue();
@@ -1832,5 +1810,6 @@
      }
    }
  }
}
opendj3-server-dev/src/server/org/opends/server/util/args/LDAPConnectionArgumentParser.java
@@ -27,37 +27,36 @@
package org.opends.server.util.args;
import org.forgerock.i18n.LocalizableMessage;
import java.io.PrintStream;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicInteger;
import static org.opends.messages.ToolMessages.*;
import javax.net.ssl.SSLException;
import org.forgerock.i18n.LocalizableMessage;
import org.opends.server.admin.client.cli.SecureConnectionCliArgs;
import org.opends.server.tools.LDAPConnection;
import org.opends.server.tools.LDAPConnectionOptions;
import org.opends.server.tools.SSLConnectionFactory;
import org.opends.server.tools.SSLConnectionException;
import org.opends.server.tools.LDAPConnectionException;
import static org.opends.server.util.ServerConstants.MAX_LINE_WIDTH;
import static org.opends.server.util.StaticUtils.wrapText;
import org.opends.server.util.cli.LDAPConnectionConsoleInteraction;
import org.opends.server.admin.client.cli.SecureConnectionCliArgs;
import org.opends.server.types.OpenDsException;
import org.opends.server.util.cli.LDAPConnectionConsoleInteraction;
import com.forgerock.opendj.cli.Argument;
import com.forgerock.opendj.cli.ArgumentException;
import com.forgerock.opendj.cli.ArgumentParser;
import com.forgerock.opendj.cli.ArgumentGroup;
import com.forgerock.opendj.cli.ClientException;
import com.forgerock.opendj.cli.ConsoleApplication;
import com.forgerock.opendj.cli.FileBasedArgument;
import com.forgerock.opendj.cli.StringArgument;
import java.util.LinkedList;
import java.util.LinkedHashSet;
import java.util.concurrent.atomic.AtomicInteger;
import java.io.PrintStream;
import javax.net.ssl.SSLException;
import static org.opends.messages.ToolMessages.*;
import static org.opends.server.util.ServerConstants.*;
import static org.opends.server.util.StaticUtils.*;
/**
 * Creates an argument parser pre-populated with arguments for specifying
@@ -493,39 +492,63 @@
                                        PrintStream out,
                                        PrintStream err)
  {
    String pwd = bindPwdArg.getValue();
    String bindDN = bindDnArg.getValue();
    if(pwd != null && pwd.equals("-")  ||
      (!bindPwdFileArg.isPresent()  &&
      (bindDN != null && pwd == null)))
    try
    {
      return getPasswordValue(bindPwdArg, bindPwdFileArg, bindDnArg.getValue(), out,
          err);
    }
    catch (Exception ex)
    {
      err.println(wrapText(ex.getMessage(), MAX_LINE_WIDTH));
      return null;
    }
  }
  /**
   * Commodity method that retrieves the password value analyzing the contents
   * of a string argument and of a file based argument.  It assumes that the
   * arguments have already been parsed and validated.
   * If the string is a dash, or no password is available, it will prompt for
   * it on the command line.
   *
   * @param bindPassword the string argument for the password.
   * @param bindPasswordFile the file based argument for the password.
   * @param bindDNValue the string value for the bindDN.
   * @param out stream to write message.
   * @param err stream to write error message.
   * @return the password value.
   * @throws ClientException if the password cannot be read
   */
  public static String getPasswordValue(StringArgument bindPassword,
      FileBasedArgument bindPasswordFile, String bindDNValue, PrintStream out,
      PrintStream err) throws ClientException
  {
    String bindPasswordValue = bindPassword.getValue();
    if ("-".equals(bindPasswordValue)
        || (!bindPasswordFile.isPresent()
            && bindDNValue != null
            && bindPasswordValue == null))
    {
      // read the password from the stdin.
      try
      out.print(INFO_LDAPAUTH_PASSWORD_PROMPT.get(bindDNValue));
      char[] pwChars = ConsoleApplication.readPassword();
      // As per rfc 4513(section-5.1.2) a client should avoid sending
      // an empty password to the server.
      while (pwChars.length == 0)
      {
        out.print(INFO_LDAPAUTH_PASSWORD_PROMPT.get(bindDN));
        char[] pwChars = ConsoleApplication.readPassword();
        pwd = new String(pwChars);
        //As per rfc 4513(section-5.1.2) a client should avoid sending
        //an empty password to the server.
        while(pwChars.length ==0)
        {
          err.println(wrapText(
                  INFO_LDAPAUTH_NON_EMPTY_PASSWORD.get(),
                  MAX_LINE_WIDTH));
          out.print(INFO_LDAPAUTH_PASSWORD_PROMPT.get(bindDN));
          pwChars = ConsoleApplication.readPassword();
        }
        pwd = new String(pwChars);
      } catch(Exception ex)
      {
        err.println(wrapText(ex.getMessage(), MAX_LINE_WIDTH));
        return null;
        err.println(wrapText(INFO_LDAPAUTH_NON_EMPTY_PASSWORD.get(),
            MAX_LINE_WIDTH));
        out.print(INFO_LDAPAUTH_PASSWORD_PROMPT.get(bindDNValue));
        pwChars = ConsoleApplication.readPassword();
      }
      return new String(pwChars);
    }
    else if (pwd == null)    {
      pwd = bindPwdFileArg.getValue();
    else if (bindPasswordValue == null)
    {
      // Read from file if it exists.
      return bindPasswordFile.getValue();
    }
    return pwd;
    return bindPasswordValue;
  }
  private void addLdapConnectionArguments(ArgumentGroup argGroup,
opendj3-server-dev/tests/unit-tests-testng/src/server/org/opends/server/types/SmallMapTest.java
@@ -27,13 +27,14 @@
import java.util.*;
import java.util.Map.Entry;
import org.opends.server.DirectoryServerTestCase;
import org.testng.annotations.Test;
import static org.assertj.core.api.Assertions.*;
import static org.testng.Assert.*;
@SuppressWarnings("javadoc")
public class SmallMapTest
public class SmallMapTest extends DirectoryServerTestCase
{
  @Test