[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Pki-devel] [PATCH] 609 CRMFPopClient improvements.



The CRMFPopClient has been modified to use the HttpClient library
to connect to the server, to show the HTTP status code if an error
occurs, and to show the NSS database directory in verbose mode.

--
Endi S. Dewata
From 7503014f0a5bc4558e9c1264c9251307dd45d8d0 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata redhat com>
Date: Sat, 13 Jun 2015 11:43:40 -0400
Subject: [PATCH] CRMFPopClient improvements.

The CRMFPopClient has been modified to use the HttpClient library
to connect to the server, to show the HTTP status code if an error
occurs, and to show the NSS database directory in verbose mode.
---
 .../src/com/netscape/cmstools/CRMFPopClient.java   | 45 ++++++++++++++++------
 1 file changed, 33 insertions(+), 12 deletions(-)

diff --git a/base/java-tools/src/com/netscape/cmstools/CRMFPopClient.java b/base/java-tools/src/com/netscape/cmstools/CRMFPopClient.java
index 81b9c4370f244f234153b14980460bf866b42294..76d8f51d1a3c240181ef0717be8756501a38c554 100644
--- a/base/java-tools/src/com/netscape/cmstools/CRMFPopClient.java
+++ b/base/java-tools/src/com/netscape/cmstools/CRMFPopClient.java
@@ -21,12 +21,9 @@ import java.io.BufferedReader;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileWriter;
-import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
 import java.io.StringWriter;
-import java.net.URL;
-import java.net.URLConnection;
 import java.net.URLEncoder;
 import java.security.KeyPair;
 import java.security.MessageDigest;
@@ -40,6 +37,11 @@ import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.PosixParser;
 import org.apache.commons.io.FileUtils;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.util.EntityUtils;
 import org.mozilla.jss.CryptoManager;
 import org.mozilla.jss.asn1.ASN1Util;
 import org.mozilla.jss.asn1.BIT_STRING;
@@ -382,7 +384,7 @@ public class CRMFPopClient {
         }
 
         try {
-            if (verbose) System.out.println("Initializing security database");
+            if (verbose) System.out.println("Initializing security database: " + databaseDir);
             CryptoManager.initialize(databaseDir);
 
             CryptoManager manager = CryptoManager.getInstance();
@@ -717,7 +719,7 @@ public class CRMFPopClient {
             String profileID,
             String requestor) throws Exception {
 
-        String s = "http://"; + hostPort + "/ca/ee/ca/profileSubmit"
+        String url = "http://"; + hostPort + "/ca/ee/ca/profileSubmit"
                 + "?cert_request_type=crmf"
                 + "&cert_request=" + URLEncoder.encode(request, "UTF-8")
                 + "&renewal=false"
@@ -726,20 +728,37 @@ public class CRMFPopClient {
                 + "&SubId=profile";
 
         if (username != null) {
-            s += "&uid=" + URLEncoder.encode(username, "UTF-8");
-            s += "&sn_uid=" + URLEncoder.encode(username, "UTF-8");
+            url += "&uid=" + URLEncoder.encode(username, "UTF-8");
+            url += "&sn_uid=" + URLEncoder.encode(username, "UTF-8");
         }
 
         if (requestor != null) {
-            s += "&requestor_name=" + URLEncoder.encode(requestor, "UTF-8");
+            url += "&requestor_name=" + URLEncoder.encode(requestor, "UTF-8");
         }
 
-        URL url = new URL(s);
         if (verbose) System.out.println("Opening " + url);
 
-        URLConnection conn = url.openConnection();
-        InputStream is = conn.getInputStream();
-        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
+        DefaultHttpClient client = new DefaultHttpClient();
+        HttpGet method = new HttpGet(url);
+        try {
+            HttpResponse response = client.execute(method);
+
+            if (response.getStatusLine().getStatusCode() != 200) {
+                throw new Exception("Error: " + response.getStatusLine());
+            }
+
+            processResponse(response);
+
+        } finally {
+            method.releaseConnection();
+        }
+    }
+
+    public void processResponse(HttpResponse response) throws Exception {
+
+        HttpEntity entity = response.getEntity();
+
+        BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
 
         if (verbose) System.out.println("--------------------");
         String line = null;
@@ -794,6 +813,8 @@ public class CRMFPopClient {
         if (reason != null) {
             System.out.println("Reason: " + reason);
         }
+
+        EntityUtils.consume(entity);
     }
 
     public boolean isEncoded(String elementValue) {
-- 
1.9.3


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]