Hi Endi,

I am attaching java code file as well with this patch that i have used
for same testing on rhel7.I thought it will be helpful.I did a quick
test similar ones and nothing looks like  breaking with new piece of code.

On 07/28/2016 12:21 AM, Endi Sukma Dewata wrote:
> Geetika,
>
> Yes, more info would be helpful. I have some comments below.
>
> On 7/27/2016 7:37 AM, Geetika Kapoor wrote:
>> Hi,
>>
>> I tried to fix NumberFormatException when i did tps-cert-find with
>> non-integer/invalid range value for size and start.I was doing testing
>> for tps-cert and then i came across this.I thought giving some
>> additional info to users inplace of numberformat.I have done similar
>> fix  on rhel7  compile it and make a jar and test on rhel7 .I can share
>> that patch if needed. Below are the test result.
>>
>> Before fix testing:
>>
>> 1. pki -h pki1.example.com -p 25080 tps-cert-find --start "gy"
>> NumberFormatException: For input string: "gy"
>>
>> 2. pki -h pki1.example.com -p 25080 tps-cert-find --size "gy"
>> NumberFormatException: For input string: "gy"
>>
>> 3. pki -p 25080 tps-cert-find --start
>> 1789999999999999999999999999999999999999999999
>> NumberFormatException: For input string:
>> "1789999999999999999999999999999999999999999999"
>>
>>
>> After fix testing:
>>
>> 1. [root@pki1 ~]# pki -d /opt/rhqa_pki/certdb -c Secret123 -h
>> pki1.example.com -p 25080 tps-cert-find --start "gy"
>> Error: Enter valid integer value for size/start option
>> usage: tps-cert-find [FILTER] [OPTIONS...]
>>     --help            Show help options
>>     --size <size>     Page size
>>     --start <start>   Page start
>>     --token <ID>      Token ID
>
> I think it would be useful to show the user which the parameter has
> the invalid value and also the invalid value itself, so something like
> this:
>
> Error: Invalid value for --start parameter: gy

Fixed : Now it is showing

[root@pki1 ~]# pki -d /opt/rhqa_pki/certdb -c Secret123 -h
pki1.example.com -p 25080 tps-cert-find --size tyu
Error: Invalid value for --size parameter:tyu
[root@pki1 ~]# pki -d /opt/rhqa_pki/certdb -c Secret123 -h
pki1.example.com -p 25080 tps-cert-find --start  tyu
Error: Invalid value for --start parameter:tyu

>
>> 2. [root@pki1 ~]# pki -d /opt/rhqa_pki/certdb -c Secret123 -h
>> pki1.example.com -p 25080 tps-cert-find --size "hy"
>> Error: Enter valid integer value for size/start option
>> usage: tps-cert-find [FILTER] [OPTIONS...]
>>     --help            Show help options
>>     --size <size>     Page size
>>     --start <start>   Page start
>>     --token <ID>      Token ID
>
> Same thing here:
>
> Error: Invalid value for --size parameter: hy
>
> So you may need to create separate try-catch blocks for each parameter.
>
> Another thing, I'm not sure if we should display the command usage
> after the failure. The usage could be very long and it may obscure the
> error message. The error message itself should be sufficient to fix
> the problem, and if needed the user can see the usage using --help
> parameter. We probably can display something like this after the error
> message (replace <command> with the actual command name):

Removed the printhelp() each time because command typed is correct only
values are invalid so that message we have displayed
>
> Try 'pki <command> --help' for more information.
>
> One more thing, please preserve the formatting of the existing code.
> We use 4 spaces instead of tabs for indentation. Thanks.
I have removed tabs.

From a1364f18e7cd8e2d77c4a7860d1acb3aab83de17 Mon Sep 17 00:00:00 2001
From: Geetika Kapoor <[email protected]>
Date: Thu, 28 Jul 2016 02:59:40 -0400
Subject: [PATCH] Fixed NumberFormatException in tps-cert-find

Signed-off-by: Geetika Kapoor <[email protected]>
---
 .../netscape/cmstools/tps/cert/TPSCertFindCLI.java | 34 +++++++++++++++-------
 1 file changed, 23 insertions(+), 11 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertFindCLI.java b/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertFindCLI.java
index 9cbdad6da5ea26a618c8fda05a575ced9b4942d0..44f5069e6502aee033a21d65e8182ac46564cc73 100644
--- a/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertFindCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/tps/cert/TPSCertFindCLI.java
@@ -84,26 +84,38 @@ public class TPSCertFindCLI extends CLI {
         String filter = cmdArgs.length > 0 ? cmdArgs[0] : null;
 
         String tokenID = cmd.getOptionValue("token");
-
-        String s = cmd.getOptionValue("start");
-        Integer start = s == null ? null : Integer.valueOf(s);
-
-        s = cmd.getOptionValue("size");
-        Integer size = s == null ? null : Integer.valueOf(s);
-
+        String string3 = cmd.getOptionValue("start");
+        String string4 = cmd.getOptionValue("size");
+        Integer start = null;
+        Integer size = null;
+        try {
+            start = string3 == null ? null : Integer.valueOf(string3);
+        } catch (NumberFormatException e) {
+            System.err.println("Error: Invalid value for --start parameter:" + string3);
+            System.exit(-1);
+        } catch (Exception e) {
+            System.err.println("Error: " + e.getMessage());
+            System.exit(-1);
+        }
+        try {
+            size = string4 == null ? null : Integer.valueOf(string4);
+         } catch (NumberFormatException e) {
+            System.err.println("Error: Invalid value for --size parameter:" + string4);
+            System.exit(-1);
+        } catch (Exception e) {
+            System.err.println("Error: " + e.getMessage());
+            System.exit(-1);
+        }
         TPSCertCollection result = certCLI.certClient.findCerts(filter, tokenID, start, size);
-
         MainCLI.printMessage(result.getTotal() + " entries matched");
         if (result.getTotal() == 0) return;
-
         Collection<TPSCertData> certs = result.getEntries();
         boolean first = true;
-
         for (TPSCertData certData : certs) {
-
             if (first) {
                 first = false;
             } else {
+
                 System.out.println();
             }
 
-- 
1.8.3.1

package com.netscape.cmstools.tps.cert;

import com.netscape.certsrv.tps.cert.TPSCertClient;
import com.netscape.certsrv.tps.cert.TPSCertCollection;
import com.netscape.certsrv.tps.cert.TPSCertData;
import com.netscape.cmstools.cli.CLI;
import com.netscape.cmstools.cli.MainCLI;
import com.netscape.cmstools.tps.cert.TPSCertCLI;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collection;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;

public class TPSCertFindCLI
extends CLI {
    public TPSCertCLI certCLI;

    public TPSCertFindCLI(TPSCertCLI tPSCertCLI) {
        super("find", "Find certificates", (CLI)tPSCertCLI);
        this.certCLI = tPSCertCLI;
        this.createOptions();
    }

    public void printHelp() {
        formatter.printHelp(this.getFullName() + " [FILTER] [OPTIONS...]", this.options);
    }

    public void createOptions() {
        Option option = new Option(null, "token", true, "Token ID");
        option.setArgName("ID");
        this.options.addOption(option);
        option = new Option(null, "start", true, "Page start");
        option.setArgName("start");
        this.options.addOption(option);
        option = new Option(null, "size", true, "Page size");
        option.setArgName("size");
        this.options.addOption(option);
    }

    public void execute(String[] arrstring) throws Exception {
        if (Arrays.asList(arrstring).contains("--help")) {
            this.printHelp();
            System.exit(0);
        }
        CommandLine commandLine = null;
        try {
            commandLine = parser.parse(this.options, arrstring);
        }
        catch (Exception var3_3) {
            System.err.println("Error: " + var3_3.getMessage());
            this.printHelp();
            System.exit(-1);
        }
        String[] arrstring2 = commandLine.getArgs();
        String string = arrstring2.length > 0 ? arrstring2[0] : null;
        String string2 = commandLine.getOptionValue("token");
        String string3 = commandLine.getOptionValue("start");
        String string4 = commandLine.getOptionValue("size");
        Integer n = null;
        Integer n2 = null;
        try {
            n = string3 == null ? null : Integer.valueOf(string3);
         } catch (NumberFormatException e) {
            System.err.println("Error: Invalid value for --start parameter:" + string3);
            System.exit(-1);
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
            System.exit(-1);
        }

        try {
            n2 = string4 == null ? null : Integer.valueOf(string4);
         } catch (NumberFormatException e) {
            System.err.println("Error: Invalid value for --size parameter:" + string4);
            System.exit(-1);
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
            System.exit(-1);
        }

        TPSCertCollection tPSCertCollection = this.certCLI.certClient.findCerts(string, string2, n, n2);
        MainCLI.printMessage((String)("" + tPSCertCollection.getTotal() + " entries matched"));
        if (tPSCertCollection.getTotal() == 0) {
            return;
        }
        Collection<TPSCertData> collection = tPSCertCollection.getEntries();
        boolean bl = true;
        for (TPSCertData tPSCertData : collection) {
            if (bl) {
                bl = false;
            } else {
                System.out.println();
            }
            TPSCertCLI.printCert((TPSCertData)tPSCertData);
        }
        MainCLI.printMessage((String)("Number of entries returned " + collection.size()));
    }
}
_______________________________________________
Pki-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/pki-devel

Reply via email to