From 995c245e96fa2b7de586be4515f8017adeb04222 Mon Sep 17 00:00:00 2001
From: neil_a_wilson <neil_a_wilson@localhost>
Date: Sun, 08 Oct 2006 23:35:31 +0000
Subject: [PATCH] Update the LDAPCompare tool to fail with an appropriate error message if the assertion value is to be read from a file but that file does not exist or is not readable.

---
 opendj-sdk/opends/src/server/org/opends/server/tools/LDAPToolUtils.java   |   27 ++++++++++++++++-----------
 opendj-sdk/opends/src/server/org/opends/server/tools/LDAPCompare.java     |   13 +++++++++++--
 opendj-sdk/opends/src/server/org/opends/server/messages/ToolMessages.java |   13 +++++++++++++
 3 files changed, 40 insertions(+), 13 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/messages/ToolMessages.java b/opendj-sdk/opends/src/server/org/opends/server/messages/ToolMessages.java
index 97fe9d2..545d0c9 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/messages/ToolMessages.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/messages/ToolMessages.java
@@ -6622,6 +6622,16 @@
 
 
   /**
+   * The message ID for the message that will be used if an error occurs while
+   * trying to read the assertion value from a file.  This takes a single
+   * argument, which is a message explaining the problem that occurred.
+   */
+  public static final int MSGID_COMPARE_CANNOT_READ_ASSERTION_VALUE_FROM_FILE =
+       CATEGORY_MASK_TOOLS | SEVERITY_MASK_INFORMATIONAL | 674;
+
+
+
+  /**
    * Associates a set of generic messages with the message IDs defined in this
    * class.
    */
@@ -7203,6 +7213,9 @@
     registerMessage(MSGID_COMPARE_CANNOT_BASE64_DECODE_ASSERTION_VALUE,
                     "The assertion value was indicated to be base64-encoded, " +
                     "but an error occurred while trying to decode the value.");
+    registerMessage(MSGID_COMPARE_CANNOT_READ_ASSERTION_VALUE_FROM_FILE,
+                    "Unable to read the assertion value from the specified " +
+                    "file:  %s.");
     registerMessage(MSGID_SEARCH_DESCRIPTION_BASEDN,
                     "The base DN for the search.");
     registerMessage(MSGID_SEARCH_DESCRIPTION_SIZE_LIMIT,
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPCompare.java b/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPCompare.java
index 709ac39..4973675 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPCompare.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPCompare.java
@@ -545,8 +545,17 @@
         }
       } else if(nextChar == '<')
       {
-        String filePath = remainder.substring(1, remainder.length());
-        attributeVal = LDAPToolUtils.readBytesFromFile(filePath);
+        try
+        {
+          String filePath = remainder.substring(1, remainder.length());
+          attributeVal = LDAPToolUtils.readBytesFromFile(filePath);
+        }
+        catch (Exception e)
+        {
+          int msgID = MSGID_COMPARE_CANNOT_READ_ASSERTION_VALUE_FROM_FILE;
+          err.println(getMessage(msgID, String.valueOf(e)));
+          return 1;
+        }
       } else
       {
         attributeVal = remainder.getBytes();
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPToolUtils.java b/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPToolUtils.java
index d630f3d..418d268 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPToolUtils.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/LDAPToolUtils.java
@@ -57,7 +57,8 @@
    * @param  argString  The argument string containing the encoded control
    *                    information.
    *
-   * @return  The control decoded from the provided string.
+   * @return  The control decoded from the provided string, or <CODE>null</CODE>
+   *          if an error occurs while parsing the argument value.
    */
   public static LDAPControl getControl(String argString)
   {
@@ -117,8 +118,15 @@
     {
       // Read data from the file.
       String fileURL = valString.substring(1, valString.length());
-      byte[] val = readBytesFromFile(fileURL);
-      controlValue = new ASN1OctetString(val);
+      try
+      {
+        byte[] val = readBytesFromFile(fileURL);
+        controlValue = new ASN1OctetString(val);
+      }
+      catch (Exception e)
+      {
+        return null;
+      }
     } else
     {
       controlValue = new ASN1OctetString(valString);
@@ -135,8 +143,12 @@
    * @param  filePath  The path to the file that should be read.
    *
    * @return  A byte array containing the contents of the requested file.
+   *
+   * @throws  IOException  If a problem occurs while trying to read the
+   *                       specified file.
    */
   public static byte[] readBytesFromFile(String filePath)
+         throws IOException
   {
       byte[] val = null;
       FileInputStream fis = null;
@@ -162,18 +174,11 @@
         }
 
         return val;
-      } catch(IOException ie)
-      {
-        System.err.println("Could not completely read file "+filePath);
-        System.err.println(ie.getMessage());
-        return null;
       } finally
       {
-        try
+        if (fis != null)
         {
           fis.close();
-        } catch(IOException ioe)
-        {
         }
       }
   }

--
Gitblit v1.10.0