From 34f5363f6bb00912e897eb1dac238bdbb8392ca4 Mon Sep 17 00:00:00 2001
From: Ade Lee <alee@redhat.com>
Date: Sat, 16 Apr 2016 10:56:18 -0400
Subject: [PATCH 286/293] Add realm to the request record

Part of Trac Ticket #2041
---
 .../netscape/certsrv/request/CMSRequestInfo.java   | 17 ++++++++++++++
 .../src/com/netscape/certsrv/request/IRequest.java | 20 +++++++++++++---
 .../netscape/certsrv/request/IRequestRecord.java   |  2 ++
 .../netscape/cmscore/request/ARequestQueue.java    | 27 +++++++++++++++-------
 .../netscape/cmscore/request/ARequestRecord.java   |  1 +
 .../netscape/cmscore/request/RequestRecord.java    | 21 ++++++++++++++---
 .../src/com/netscape/cmscore/request/Schema.java   |  1 +
 .../cmscore/request/RequestDefaultStub.java        | 21 ++++++++++++-----
 8 files changed, 90 insertions(+), 20 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/request/CMSRequestInfo.java b/base/common/src/com/netscape/certsrv/request/CMSRequestInfo.java
index f7b34f0a2f25ff2665aa86bc569f71f9ce46b90b..bfd1b2f0a15cad4c42e2c2eebe6bdfcb04462565 100644
--- a/base/common/src/com/netscape/certsrv/request/CMSRequestInfo.java
+++ b/base/common/src/com/netscape/certsrv/request/CMSRequestInfo.java
@@ -35,6 +35,9 @@ public  class CMSRequestInfo {
     @XmlElement
     protected String requestURL;
 
+    @XmlElement
+    protected String realm;
+
     /**
      * @return the requestType
      */
@@ -85,10 +88,19 @@ public  class CMSRequestInfo {
         this.requestURL = requestURL;
     }
 
+    public String getRealm() {
+        return realm;
+    }
+
+    public void setRealm(String realm) {
+        this.realm = realm;
+    }
+
     @Override
     public int hashCode() {
         final int prime = 31;
         int result = 1;
+        result = prime * result + ((realm == null) ? 0 : realm.hashCode());
         result = prime * result + ((requestStatus == null) ? 0 : requestStatus.hashCode());
         result = prime * result + ((requestType == null) ? 0 : requestType.hashCode());
         result = prime * result + ((requestURL == null) ? 0 : requestURL.hashCode());
@@ -104,6 +116,11 @@ public  class CMSRequestInfo {
         if (getClass() != obj.getClass())
             return false;
         CMSRequestInfo other = (CMSRequestInfo) obj;
+        if (realm == null) {
+            if (other.realm != null)
+                return false;
+        } else if (!realm.equals(other.realm))
+            return false;
         if (requestStatus == null) {
             if (other.requestStatus != null)
                 return false;
diff --git a/base/common/src/com/netscape/certsrv/request/IRequest.java b/base/common/src/com/netscape/certsrv/request/IRequest.java
index 8d4ec98fbd8f8703b4792668530d530bd00f072c..8a002ab134f18cbff7e933f6d66652a2b79f0815 100644
--- a/base/common/src/com/netscape/certsrv/request/IRequest.java
+++ b/base/common/src/com/netscape/certsrv/request/IRequest.java
@@ -27,15 +27,15 @@ import java.util.Hashtable;
 import java.util.Locale;
 import java.util.Vector;
 
+import com.netscape.certsrv.authentication.IAuthToken;
+import com.netscape.certsrv.base.IAttrSet;
+
 import netscape.security.x509.CertificateExtensions;
 import netscape.security.x509.CertificateSubjectName;
 import netscape.security.x509.RevokedCertImpl;
 import netscape.security.x509.X509CertImpl;
 import netscape.security.x509.X509CertInfo;
 
-import com.netscape.certsrv.authentication.IAuthToken;
-import com.netscape.certsrv.base.IAttrSet;
-
 /**
  * An interface that defines abilities of request objects,
  *
@@ -305,6 +305,9 @@ public interface IRequest extends Serializable {
     // String error messages
     public static final String ERROR = "Error";
 
+    // authentication realm
+    public static final String REALM = "realm";
+
     /**
      * Copies meta attributes (excluding request Id, etc.) of another request
      * to this request.
@@ -771,4 +774,15 @@ public interface IRequest extends Serializable {
      */
     public IAttrSet asIAttrSet();
 
+    /**
+     * Get realm
+     * @return String
+     */
+    public String getRealm();
+
+    /**
+     * Set the realm
+     * @param realm
+     */
+    public void setRealm(String realm);
 }
diff --git a/base/common/src/com/netscape/certsrv/request/IRequestRecord.java b/base/common/src/com/netscape/certsrv/request/IRequestRecord.java
index eba6cabb830459486dead6eb616240f5789ab3b1..7b041fbf83ee93afe737fb5cfab22ce50708411a 100644
--- a/base/common/src/com/netscape/certsrv/request/IRequestRecord.java
+++ b/base/common/src/com/netscape/certsrv/request/IRequestRecord.java
@@ -61,6 +61,8 @@ public interface IRequestRecord
     // and the RequestRecord.
     public final static String ATTR_EXT_DATA = "requestExtData";
 
+    public final static String ATTR_REALM = "realm";
+
     /**
      * Gets the request id.
      *
diff --git a/base/server/cmscore/src/com/netscape/cmscore/request/ARequestQueue.java b/base/server/cmscore/src/com/netscape/cmscore/request/ARequestQueue.java
index 798da3fa54c413802acfafb6dae4bf60c93a0cf2..fc0a4149b39c5fa28c1761d9988f491623f5bfd9 100644
--- a/base/server/cmscore/src/com/netscape/cmscore/request/ARequestQueue.java
+++ b/base/server/cmscore/src/com/netscape/cmscore/request/ARequestQueue.java
@@ -33,14 +33,6 @@ import java.util.Locale;
 import java.util.Set;
 import java.util.Vector;
 
-import netscape.security.util.DerInputStream;
-import netscape.security.x509.CertificateExtensions;
-import netscape.security.x509.CertificateSubjectName;
-import netscape.security.x509.RevokedCertImpl;
-import netscape.security.x509.X509CertImpl;
-import netscape.security.x509.X509CertInfo;
-import netscape.security.x509.X509ExtensionException;
-
 import com.netscape.certsrv.apps.CMS;
 import com.netscape.certsrv.authentication.AuthToken;
 import com.netscape.certsrv.authentication.IAuthToken;
@@ -61,6 +53,14 @@ import com.netscape.certsrv.request.PolicyResult;
 import com.netscape.certsrv.request.RequestId;
 import com.netscape.certsrv.request.RequestStatus;
 
+import netscape.security.util.DerInputStream;
+import netscape.security.x509.CertificateExtensions;
+import netscape.security.x509.CertificateSubjectName;
+import netscape.security.x509.RevokedCertImpl;
+import netscape.security.x509.X509CertImpl;
+import netscape.security.x509.X509CertInfo;
+import netscape.security.x509.X509ExtensionException;
+
 /**
  * The ARequestQueue class is an abstract class that implements
  * most portions of the IRequestQueue interface. This includes
@@ -1432,10 +1432,21 @@ class Request implements IRequest {
     protected String mOwner;
     protected String mRequestType;
     protected String mContext; // string for now.
+    protected String realm;
     protected ExtDataHashtable<Object> mExtData = new ExtDataHashtable<Object>();
 
     Date mCreationTime = CMS.getCurrentDate();
     Date mModificationTime = CMS.getCurrentDate();
+
+    @Override
+    public String getRealm() {
+        return realm;
+    }
+
+    @Override
+    public void setRealm(String realm) {
+        this.realm = realm;
+    }
 }
 
 class RequestIAttrSetWrapper implements IAttrSet {
diff --git a/base/server/cmscore/src/com/netscape/cmscore/request/ARequestRecord.java b/base/server/cmscore/src/com/netscape/cmscore/request/ARequestRecord.java
index 5f2aa50b2c3108831c2d3824241615d502d9342f..418422a9b0b233c959a189a80a390d54709edb9c 100644
--- a/base/server/cmscore/src/com/netscape/cmscore/request/ARequestRecord.java
+++ b/base/server/cmscore/src/com/netscape/cmscore/request/ARequestRecord.java
@@ -39,4 +39,5 @@ class ARequestRecord {
     String mOwner;
     String mRequestType;
     Hashtable<String, Object> mExtData;
+    String mRealm;
 };
diff --git a/base/server/cmscore/src/com/netscape/cmscore/request/RequestRecord.java b/base/server/cmscore/src/com/netscape/cmscore/request/RequestRecord.java
index 023dd9478a0fdbc3f150b1a590fa9357b598afaa..38060c2f2e9e70c9ed6bc977c58c710fecfb34f6 100644
--- a/base/server/cmscore/src/com/netscape/cmscore/request/RequestRecord.java
+++ b/base/server/cmscore/src/com/netscape/cmscore/request/RequestRecord.java
@@ -30,9 +30,6 @@ import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.Vector;
 
-import netscape.ldap.LDAPAttribute;
-import netscape.ldap.LDAPAttributeSet;
-
 import com.netscape.certsrv.apps.CMS;
 import com.netscape.certsrv.base.EBaseException;
 import com.netscape.certsrv.dbs.EDBException;
@@ -53,6 +50,9 @@ import com.netscape.cmscore.dbs.DateMapper;
 import com.netscape.cmscore.dbs.StringMapper;
 import com.netscape.cmscore.util.Debug;
 
+import netscape.ldap.LDAPAttribute;
+import netscape.ldap.LDAPAttributeSet;
+
 //
 // A request record is the stored version of a request.
 // It has a set of attributes that are mapped into LDAP
@@ -92,6 +92,8 @@ public class RequestRecord
             return mOwner;
         else if (name.equals(IRequestRecord.ATTR_EXT_DATA))
             return mExtData;
+        else if (name.equals(IRequestRecord.ATTR_REALM))
+            return mRealm;
         else {
             RequestAttr ra = mAttrTable.get(name);
 
@@ -119,6 +121,8 @@ public class RequestRecord
             mSourceId = (String) o;
         else if (name.equals(IRequestRecord.ATTR_REQUEST_OWNER))
             mOwner = (String) o;
+        else if (name.equals(IRequestRecord.ATTR_REALM))
+            mRealm = (String) o;
         else if (name.equals(IRequestRecord.ATTR_EXT_DATA))
             mExtData = (Hashtable<String, Object>) o;
         else {
@@ -155,6 +159,7 @@ public class RequestRecord
         mOwner = r.getRequestOwner();
         mCreateTime = r.getCreationTime();
         mModifyTime = r.getModificationTime();
+        mRealm = r.getRealm();
         mExtData = loadExtDataFromRequest(r);
 
         for (int i = 0; i < mRequestA.length; i++) {
@@ -168,6 +173,7 @@ public class RequestRecord
         r.setRequestOwner(mOwner);
         a.modModificationTime(r, mModifyTime);
         a.modCreationTime(r, mCreateTime);
+        r.setRealm(mRealm);
         storeExtDataIntoRequest(r);
 
         for (int i = 0; i < mRequestA.length; i++) {
@@ -192,6 +198,12 @@ public class RequestRecord
         mods.add(IRequestRecord.ATTR_EXT_DATA,
                 Modification.MOD_REPLACE, loadExtDataFromRequest(r));
 
+        if (r.getRealm() != null) {
+            mods.add(IRequestRecord.ATTR_REALM, Modification.MOD_REPLACE, r.getRealm());
+        } else {
+            mods.add(IRequestRecord.ATTR_REALM, Modification.MOD_DELETE, null);
+        }
+
         for (int i = 0; i < mRequestA.length; i++) {
             mRequestA[i].mod(mods, r);
         }
@@ -213,6 +225,8 @@ public class RequestRecord
                 new StringMapper(Schema.LDAP_ATTR_SOURCE_ID));
         reg.registerAttribute(IRequestRecord.ATTR_REQUEST_OWNER,
                 new StringMapper(Schema.LDAP_ATTR_REQUEST_OWNER));
+        reg.registerAttribute(IRequestRecord.ATTR_REALM,
+                new StringMapper(Schema.LDAP_ATTR_REALM));
         ExtAttrDynMapper extAttrMapper = new ExtAttrDynMapper();
         reg.registerAttribute(IRequestRecord.ATTR_EXT_DATA, extAttrMapper);
         reg.registerDynamicMapper(extAttrMapper);
@@ -306,6 +320,7 @@ public class RequestRecord
         mAttrs.add(IRequestRecord.ATTR_MODIFY_TIME);
         mAttrs.add(IRequestRecord.ATTR_SOURCE_ID);
         mAttrs.add(IRequestRecord.ATTR_REQUEST_OWNER);
+        mAttrs.add(IRequestRecord.ATTR_REALM);
         mAttrs.add(IRequestRecord.ATTR_EXT_DATA);
 
         for (int i = 0; i < mRequestA.length; i++) {
diff --git a/base/server/cmscore/src/com/netscape/cmscore/request/Schema.java b/base/server/cmscore/src/com/netscape/cmscore/request/Schema.java
index 89a7b74b9509c0b1cf73d4ad124efe7cf51f0961..75605d7a30a7e7ee695a04871196153e3fa68dae 100644
--- a/base/server/cmscore/src/com/netscape/cmscore/request/Schema.java
+++ b/base/server/cmscore/src/com/netscape/cmscore/request/Schema.java
@@ -38,6 +38,7 @@ class Schema {
     public static final String LDAP_ATTR_AGENT_GROUP = "requestAgentGroup";
     public static final String LDAP_ATTR_REQUEST_TYPE = "requestType";
     public static final String LDAP_ATTR_REQUEST_ERROR = "requestError";
+    public static final String LDAP_ATTR_REALM = "realm";
 
     // This attribute is a placeholder used by ExtAttrDynMapper
     public static final String LDAP_ATTR_EXT_ATTR = "extAttr";
diff --git a/base/server/test/com/netscape/cmscore/request/RequestDefaultStub.java b/base/server/test/com/netscape/cmscore/request/RequestDefaultStub.java
index 7a6bde40886733bf64e97ac6d63cccef96b98de8..b55238de5501f53ac82d53721043bfafe7513b16 100644
--- a/base/server/test/com/netscape/cmscore/request/RequestDefaultStub.java
+++ b/base/server/test/com/netscape/cmscore/request/RequestDefaultStub.java
@@ -7,18 +7,18 @@ import java.util.Hashtable;
 import java.util.Locale;
 import java.util.Vector;
 
-import netscape.security.x509.CertificateExtensions;
-import netscape.security.x509.CertificateSubjectName;
-import netscape.security.x509.RevokedCertImpl;
-import netscape.security.x509.X509CertImpl;
-import netscape.security.x509.X509CertInfo;
-
 import com.netscape.certsrv.authentication.IAuthToken;
 import com.netscape.certsrv.base.IAttrSet;
 import com.netscape.certsrv.request.IRequest;
 import com.netscape.certsrv.request.RequestId;
 import com.netscape.certsrv.request.RequestStatus;
 
+import netscape.security.x509.CertificateExtensions;
+import netscape.security.x509.CertificateSubjectName;
+import netscape.security.x509.RevokedCertImpl;
+import netscape.security.x509.X509CertImpl;
+import netscape.security.x509.X509CertInfo;
+
 /**
  * Default testing stub for the IRequest interface.
  */
@@ -268,4 +268,13 @@ public class RequestDefaultStub implements IRequest {
     public IAttrSet asIAttrSet() {
         return null;
     }
+
+    @Override
+    public String getRealm() {
+        return null;
+    }
+
+    @Override
+    public void setRealm(String realm) {
+    }
 }
-- 
2.4.3

