>From 040047af182ccfa0548b2c7b14a7a324d8406e0a Mon Sep 17 00:00:00 2001
From: Ade Lee <alee@redhat.com>
Date: Mon, 24 Sep 2012 23:23:06 -0400
Subject: [PATCH] fall back to old interface for installtoken if needed

---
 .../com/netscape/certsrv/client/BasicClient.java   | 27 ++++++++
 .../cms/servlet/csadmin/ConfigurationUtils.java    | 74 +++++++++++++++++++++-
 2 files changed, 98 insertions(+), 3 deletions(-)
 create mode 100644 base/common/src/com/netscape/certsrv/client/BasicClient.java

diff --git a/base/common/src/com/netscape/certsrv/client/BasicClient.java b/base/common/src/com/netscape/certsrv/client/BasicClient.java
new file mode 100644
index 0000000000000000000000000000000000000000..e0c5de0fde9619440a71ac7b1a1328f5bbbe61b4
--- /dev/null
+++ b/base/common/src/com/netscape/certsrv/client/BasicClient.java
@@ -0,0 +1,27 @@
+package com.netscape.certsrv.client;
+
+import javax.ws.rs.core.MediaType;
+
+import org.jboss.resteasy.client.ClientRequest;
+import org.jboss.resteasy.client.ClientResponse;
+
+public class BasicClient extends PKIClient {
+
+    public BasicClient(ClientConfig config) {
+        super(config);
+    }
+
+    public ClientResponse<String> post(String content) {
+        ClientResponse<String> response = null;
+        ClientRequest request = executor.createRequest(config.getServerURI().toString());
+        request.body(MediaType.APPLICATION_FORM_URLENCODED, content);
+        try {
+            response = request.post(String.class);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return response;
+    }
+
+
+}
diff --git a/base/common/src/com/netscape/cms/servlet/csadmin/ConfigurationUtils.java b/base/common/src/com/netscape/cms/servlet/csadmin/ConfigurationUtils.java
index bcfe3645942f9a149bf5bc85b2bd7b65a53e3cc8..cf68f2175147ce4d4c16ca8caef07441d3fcbcb9 100644
--- a/base/common/src/com/netscape/cms/servlet/csadmin/ConfigurationUtils.java
+++ b/base/common/src/com/netscape/cms/servlet/csadmin/ConfigurationUtils.java
@@ -52,6 +52,7 @@ import java.util.Vector;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.Response;
 import javax.xml.parsers.ParserConfigurationException;
 
 import netscape.ldap.LDAPAttribute;
@@ -74,6 +75,8 @@ import netscape.security.x509.X509CertImpl;
 import netscape.security.x509.X509Key;
 
 import org.apache.velocity.context.Context;
+import org.jboss.resteasy.client.ClientResponse;
+import org.jboss.resteasy.client.ClientResponseFailure;
 import org.mozilla.jss.CryptoManager;
 import org.mozilla.jss.CryptoManager.NicknameConflictException;
 import org.mozilla.jss.CryptoManager.NotInitializedException;
@@ -135,6 +138,7 @@ import com.netscape.certsrv.base.EPropertyNotFound;
 import com.netscape.certsrv.base.IConfigStore;
 import com.netscape.certsrv.base.ISubsystem;
 import com.netscape.certsrv.ca.ICertificateAuthority;
+import com.netscape.certsrv.client.BasicClient;
 import com.netscape.certsrv.client.ClientConfig;
 import com.netscape.certsrv.dbs.IDBSubsystem;
 import com.netscape.certsrv.dbs.crldb.ICRLIssuingPointRecord;
@@ -199,6 +203,22 @@ public class ConfigurationUtils {
         return getHttpResponse(hostname, port, secure, uri, content, clientnickname, null);
     }
 
+    public static ClientResponse<String> getClientResponse(String hostname, int port, boolean secure,
+            String uri, String content, String clientnickname,
+            SSLCertificateApprovalCallback certApprovalCallback)
+            throws URISyntaxException {
+
+        String protocol = secure ? "https" : "http";
+        ClientConfig config = new ClientConfig();
+        config.setServerURI(protocol + "://" + hostname + ":" + port + "/" + uri);
+        config.setCertNickname(clientnickname);
+
+        BasicClient client = new BasicClient(config);
+        ClientResponse<String> response = client.post(content);
+
+        return response;
+    }
+
     //TODO - replace with Jack's connector code
     // or as we replace calls with restful calls,  remove altogether
     public static String getHttpResponse(String hostname, int port, boolean secure,
@@ -293,7 +313,7 @@ public class ConfigurationUtils {
     }
 
     public static String getInstallToken(String sdhost, int sdport, String user, String passwd)
-            throws EPropertyNotFound, EBaseException, URISyntaxException {
+            throws EPropertyNotFound, EBaseException, URISyntaxException, IOException {
         IConfigStore cs = CMS.getConfigStore();
         String csType = cs.getString("cs.type");
 
@@ -304,9 +324,57 @@ public class ConfigurationUtils {
 
         SystemConfigClient client = new SystemConfigClient(config);
 
-        InstallToken token = client.getInstallToken(data);
+        InstallToken token = null;
+        try {
+            token = client.getInstallToken(data);
+            return token.getToken();
+        } catch (ClientResponseFailure e) {
+            if (e.getResponse().getResponseStatus() == Response.Status.NOT_FOUND) {
+                // try the old servlet
+                String tokenString = getOldCookie(sdhost, sdport, user, passwd);
+                return tokenString;
+            }
 
-        return token.getToken();
+            throw e;
+        }
+    }
+
+    public static String getOldCookie(String sdhost, int sdport, String user, String passwd) throws IOException,
+            EPropertyNotFound, EBaseException, URISyntaxException {
+        IConfigStore cs = CMS.getConfigStore();
+
+        String subca_url = "https://" + CMS.getEEHost() + ":"
+                + CMS.getAdminPort() + "/ca/admin/console/config/wizard" +
+                "?p=5&subsystem=" + cs.getString("cs.type");
+
+        String content = "uid=" + user + "&pwd=" + URLEncoder.encode(passwd, "UTF-8") +
+                "&url=" + URLEncoder.encode(subca_url, "UTF-8");
+
+        ClientResponse<String> response = getClientResponse(sdhost, sdport, true, "/ca/admin/ca/getCookie",
+                content, null, null);
+        String body = response.getEntity();
+        return getContentValue(body, "header.session_id");
+    }
+
+    public static String getContentValue(String body, String header) {
+        StringTokenizer st = new StringTokenizer(body, "\n");
+
+        while (st.hasMoreTokens()) {
+            String line = st.nextToken();
+            // format for line assumed to be name="value";
+
+            int eqPos = line.indexOf('=');
+            if (eqPos != -1) {
+                String name = line.substring(0, eqPos).trim();
+                String tempval = line.substring(eqPos + 1).trim();
+                String value = tempval.replaceAll("(^\")|(\";$)","");
+
+                if (name.equals(header)) {
+                    return value;
+                }
+            }
+        }
+        return null;
     }
 
     public static String getGroupName(String uid, String subsystemname) {
-- 
1.7.11.4

