>From d0b1053eab8a3745281ae8d6202b4e68cb8f73e7 Mon Sep 17 00:00:00 2001
From: Christina Fu <cfu@redhat.com>
Date: Mon, 24 Mar 2014 15:47:16 -0700
Subject: [PATCH] trac ticket #862 HTTP connection factory multi-uri addendum

---
 base/common/src/com/netscape/certsrv/apps/CMS.java | 28 ------------
 .../src/com/netscape/certsrv/apps/ICMSEngine.java  | 24 ----------
 .../certsrv/connector/IHttpConnection.java         | 12 +++++
 .../src/com/netscape/cmscore/apps/CMSEngine.java   | 10 -----
 .../cmscore/connector/HttpConnFactory.java         | 41 +++--------------
 .../netscape/cmscore/connector/HttpConnection.java | 51 +++++++++++++---------
 .../netscape/cmscore/connector/HttpConnector.java  |  3 +-
 7 files changed, 52 insertions(+), 117 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/apps/CMS.java b/base/common/src/com/netscape/certsrv/apps/CMS.java
index 405e99e..fbcf65a 100644
--- a/base/common/src/com/netscape/certsrv/apps/CMS.java
+++ b/base/common/src/com/netscape/certsrv/apps/CMS.java
@@ -850,19 +850,6 @@ public final class CMS {
      *
      * @param authority remote authority
      * @param factory socket factory
-     * @param op operation to determine receiving servlet (multi-uri support)
-     * @return http connection to the remote authority
-     */
-    public static IHttpConnection getHttpConnection(IRemoteAuthority authority,
-            ISocketFactory factory, String op) {
-        return _engine.getHttpConnection(authority, factory, op);
-    }
-
-    /**
-     * Retrieves the HTTP Connection for use with connector.
-     *
-     * @param authority remote authority
-     * @param factory socket factory
      * @param timeout return error if connection cannot be established within
      *            the timeout period
      * @return http connection to the remote authority
@@ -873,21 +860,6 @@ public final class CMS {
     }
 
     /**
-     * Retrieves the HTTP Connection for use with connector.
-     *
-     * @param authority remote authority
-     * @param factory socket factory
-     * @param timeout return error if connection cannot be established within
-     *            the timeout period
-     * @param op operation to determine receiving servlet (multi-uri support)
-     * @return http connection to the remote authority
-     */
-    public static IHttpConnection getHttpConnection(IRemoteAuthority authority,
-            ISocketFactory factory, int timeout, String op) {
-        return _engine.getHttpConnection(authority, factory, timeout, op);
-    }
-
-    /**
      * Retrieves the request sender for use with connector.
      *
      * @param authority local authority
diff --git a/base/common/src/com/netscape/certsrv/apps/ICMSEngine.java b/base/common/src/com/netscape/certsrv/apps/ICMSEngine.java
index fb3881e..74fa090 100644
--- a/base/common/src/com/netscape/certsrv/apps/ICMSEngine.java
+++ b/base/common/src/com/netscape/certsrv/apps/ICMSEngine.java
@@ -1029,17 +1029,6 @@ public interface ICMSEngine extends ISubsystem {
      *
      * @param authority remote authority
      * @param factory socket factory
-     * @param op operation to determine receiving servlet (multi-uri support)
-     * @return http connection to the remote authority
-     */
-    public IHttpConnection getHttpConnection(IRemoteAuthority authority,
-            ISocketFactory factory, String op);
-
-    /**
-     * Retrieves the HTTP Connection for use with connector.
-     *
-     * @param authority remote authority
-     * @param factory socket factory
      * @param timeout return error if connection cannot be established within
      *            the timeout period
      * @return http connection to the remote authority
@@ -1048,19 +1037,6 @@ public interface ICMSEngine extends ISubsystem {
             ISocketFactory factory, int timeout);
 
     /**
-     * Retrieves the HTTP Connection for use with connector.
-     *
-     * @param authority remote authority
-     * @param factory socket factory
-     * @param timeout return error if connection cannot be established within
-     *            the timeout period
-     * @param op operation to determine receiving servlet (multi-uri support)
-     * @return http connection to the remote authority
-     */
-    public IHttpConnection getHttpConnection(IRemoteAuthority authority,
-            ISocketFactory factory, int timeout, String op);
-
-    /**
      * Retrieves the request sender for use with connector.
      *
      * @param authority local authority
diff --git a/base/common/src/com/netscape/certsrv/connector/IHttpConnection.java b/base/common/src/com/netscape/certsrv/connector/IHttpConnection.java
index 2e87185..995b8e7 100644
--- a/base/common/src/com/netscape/certsrv/connector/IHttpConnection.java
+++ b/base/common/src/com/netscape/certsrv/connector/IHttpConnection.java
@@ -49,4 +49,16 @@ public interface IHttpConnection {
      */
     public HttpResponse send(String msg)
             throws EBaseException;
+
+    /**
+     * Sets the HttpRequest URI before send
+       @param op operation for mapping to uri
+     */
+    public void setRequestURI(String op)
+            throws EBaseException;
+
+    /**
+     * Gets the HttpRequest URI
+     */
+    public String getRequestURI();
 }
diff --git a/base/server/cmscore/src/com/netscape/cmscore/apps/CMSEngine.java b/base/server/cmscore/src/com/netscape/cmscore/apps/CMSEngine.java
index 456cc77..68c6482 100644
--- a/base/server/cmscore/src/com/netscape/cmscore/apps/CMSEngine.java
+++ b/base/server/cmscore/src/com/netscape/cmscore/apps/CMSEngine.java
@@ -801,16 +801,6 @@ public class CMSEngine implements ICMSEngine {
         return new HttpConnection(authority, factory, timeout);
     }
 
-    public IHttpConnection getHttpConnection(IRemoteAuthority authority,
-            ISocketFactory factory, String op) {
-        return new HttpConnection(authority, factory, op);
-    }
-
-    public IHttpConnection getHttpConnection(IRemoteAuthority authority,
-            ISocketFactory factory, int timeout, String op) {
-        return new HttpConnection(authority, factory, timeout, op);
-    }
-
     public IResender getResender(IAuthority authority, String nickname,
             IRemoteAuthority remote, int interval) {
         return new Resender(authority, nickname, remote, interval);
diff --git a/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnFactory.java b/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnFactory.java
index be727a5..db2a51a 100644
--- a/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnFactory.java
+++ b/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnFactory.java
@@ -113,7 +113,7 @@ public class HttpConnFactory {
         CMS.debug("leaving HttpConnFactory init.");
     }
 
-    private IHttpConnection createConnection(String op) throws EBaseException {
+    private IHttpConnection createConnection() throws EBaseException {
 
         IHttpConnection retConn = null;
 
@@ -122,20 +122,11 @@ public class HttpConnFactory {
         try {
             ISocketFactory tFactory = new JssSSLSocketFactory(mNickname);
 
-            if (op == null) {
-                if (mTimeout == 0) {
-                    retConn = CMS.getHttpConnection(mDest, tFactory);
-                } else {
-                    retConn = CMS.getHttpConnection(mDest, tFactory, mTimeout);
-                }
+            if (mTimeout == 0) {
+                retConn = CMS.getHttpConnection(mDest, tFactory);
             } else {
-                if (mTimeout == 0) {
-                    retConn = CMS.getHttpConnection(mDest, tFactory, op);
-                } else {
-                    retConn = CMS.getHttpConnection(mDest, tFactory, mTimeout, op);
-                }
+                retConn = CMS.getHttpConnection(mDest, tFactory, mTimeout);
             }
-
         } catch (Exception e) {
 
             CMS.debug("can't make new Htpp Connection");
@@ -150,7 +141,7 @@ public class HttpConnFactory {
     /**
      * makes the minumum number of connections
      */
-    private void makeMinimum(String op) throws EBaseException {
+    private void makeMinimum() throws EBaseException {
 
         CMS.debug("In HttpConnFactory.makeMinimum.");
         int increment;
@@ -165,7 +156,7 @@ public class HttpConnFactory {
             CMS.debug(
                     "increasing minimum connections by " + increment);
             for (int i = increment - 1; i >= 0; i--) {
-                mConns[i] = createConnection(op);
+                mConns[i] = createConnection();
             }
             mTotal += increment;
             mNumConns += increment;
@@ -198,15 +189,6 @@ public class HttpConnFactory {
         return getConn(true);
     }
 
-    /*
-     * See getConn() above
-     * @param op operation to determine the receiving servlet (multi-uri support)
-     */
-    public IHttpConnection getConn(String op)
-            throws EBaseException {
-        return getConn(true, op);
-    }
-
     /**
      * Returns a Http connection - a clone of the master connection.
      * All connections should be returned to the factory using returnConn()
@@ -231,20 +213,11 @@ public class HttpConnFactory {
      */
     public synchronized IHttpConnection getConn(boolean waitForConn)
             throws EBaseException {
-        return getConn(waitForConn, null);
-    }
-
-    /*
-     * See getConn() above
-     * @param op operation to determine the receiving servlet (multi-uri support)
-     */
-    public synchronized IHttpConnection getConn(boolean waitForConn, String op)
-            throws EBaseException {
         boolean waited = false;
 
         CMS.debug("In HttpConnFactory.getConn");
         if (mNumConns == 0)
-            makeMinimum(op);
+            makeMinimum();
         if (mNumConns == 0) {
             if (!waitForConn)
                 return null;
diff --git a/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnection.java b/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnection.java
index 30d70be..2bacccd 100644
--- a/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnection.java
+++ b/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnection.java
@@ -60,14 +60,27 @@ public class HttpConnection implements IHttpConnection {
         return false;
     }
 
-    public HttpConnection(IRemoteAuthority dest, ISocketFactory factory) {
-        this(dest, factory, null);
+    public void setRequestURI(String op)
+            throws EBaseException {
+        String uri;
+        if (op != null) {
+            uri = mDest.getURI(op);
+        } else {
+            throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_ATTRIBUTE", "setRequestURI cannot take null op"));
+        }
+
+        if (uri != null) {
+            mHttpreq.setURI(uri);
+        } else {
+            throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_ATTRIBUTE", "setRequestURI cannot find uri for op"));
+        }
     }
 
-    /*
-     * @param op operation to determine the receiving servlet (multi-uri support)
-     */
-    public HttpConnection(IRemoteAuthority dest, ISocketFactory factory, String op) {
+    public String getRequestURI() {
+        return mHttpreq.getURI();
+    }
+
+    public HttpConnection(IRemoteAuthority dest, ISocketFactory factory) {
         mDest = dest;
         mReqEncoder = new HttpRequestEncoder();
         mHttpClient = new HttpClient(factory);
@@ -75,11 +88,10 @@ public class HttpConnection implements IHttpConnection {
             Debug.trace("Created HttpClient");
         try {
             mHttpreq.setMethod("POST");
-            if (op == null)
+            // in case of multi-uri, uri will be set right before send
+            //   by calling setRequestURI(op)
+            if (mDest.getURI() != null)
                 mHttpreq.setURI(mDest.getURI());
-            else {
-                mHttpreq.setURI(mDest.getURI(op));
-            }
 
             String contentType = dest.getContentType();
             if (contentType != null) {
@@ -110,26 +122,20 @@ public class HttpConnection implements IHttpConnection {
         }
     }
 
-    // Inserted by beomsuk
-    public HttpConnection(IRemoteAuthority dest, ISocketFactory factory, int timeout) {
-        this(dest, factory, timeout, null);
-    }
-
     /*
      * @param op operation to determine the receiving servlet (multi-uri support)
      */
-    public HttpConnection(IRemoteAuthority dest, ISocketFactory factory, int timeout, String op) {
+    public HttpConnection(IRemoteAuthority dest, ISocketFactory factory, int timeout) {
         mDest = dest;
         mReqEncoder = new HttpRequestEncoder();
         mHttpClient = new HttpClient(factory);
         CMS.debug("HttpConn:Created HttpConnection: factory " + factory + "client " + mHttpClient);
         try {
             mHttpreq.setMethod("POST");
-            if (op == null)
+            // in case of multi-uri, uri will be set right before send
+            //   by calling setRequestURI(op)
+            if (mDest.getURI() != null)
                 mHttpreq.setURI(mDest.getURI());
-            else {
-                mHttpreq.setURI(mDest.getURI(op));
-            }
 
             String contentType = dest.getContentType();
             if (contentType != null) {
@@ -163,6 +169,7 @@ public class HttpConnection implements IHttpConnection {
         CMS.debug("in HttpConnection.send " + this);
         if (Debug.ON)
             Debug.trace("encoding request ");
+
         String content = null;
 
         try {
@@ -222,6 +229,10 @@ public class HttpConnection implements IHttpConnection {
         HttpResponse resp = null;
         boolean reconnect = false;
 
+        if (getRequestURI() == null) {
+            throw new EBaseException(CMS.getUserMessage("CMS_BASE_INVALID_ATTRIBUTE", "URI not set in HttpRequest"));
+        }
+
         mHttpreq.setHeader("Content-Length",
                 Integer.toString(content.length()));
         CMS.debug("HttpConnection.doSend: with String content length: " + Integer.toString(content.length()));
diff --git a/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnector.java b/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnector.java
index ff0ece1..f9e7637 100644
--- a/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnector.java
+++ b/base/server/cmscore/src/com/netscape/cmscore/connector/HttpConnector.java
@@ -112,7 +112,8 @@ public class HttpConnector implements IConnector {
         HttpResponse resp = null;
         IHttpConnection curConn = null;
         try {
-            curConn = mConnFactory.getConn(op);
+            curConn = mConnFactory.getConn();
+            curConn.setRequestURI(op);
             resp = curConn.send(msg);
         } catch (EBaseException e) {
             CMS.debug("HttpConnector: send():"+ e.toString());
-- 
1.8.4.2

