From 3c1be21233503587060695dcf824ff39efb39aa7 Mon Sep 17 00:00:00 2001
From: Endi Sukma Dewata <edewata@redhat.com>
Date: Thu, 1 Mar 2012 19:58:34 -0600
Subject: [PATCH] Added CMSException.

The CMSException was added to simplify error handling in REST services.
The exception may include an error message and some other attributes.
When the server throws a CMSException (or its subclass), the exception
will be marshalled into XML and unmarshalled by the client, then thrown
again as a new exception which can be caught by the application.

Ticket #100
---
 pki/.classpath                                     |   11 +-
 pki/base/common/src/CMakeLists.txt                 |    2 +
 .../src/com/netscape/certsrv/dbs/keydb/KeyId.java  |   11 ++
 .../com/netscape/certsrv/request/RequestId.java    |   11 ++
 .../admin/SystemCertificateResourceService.java    |    7 +-
 .../netscape/cms/servlet/base/CMSException.java    |  127 ++++++++++++++++++++
 .../cms/servlet/base/CMSResourceService.java       |   17 +++-
 .../cms/servlet/key/KeyResourceService.java        |    5 -
 .../cms/servlet/key/KeysResourceService.java       |    5 -
 .../servlet/request/KeyRequestResourceService.java |   10 +-
 .../request/KeyRequestsResourceService.java        |    5 -
 .../servlet/request/RequestNotFoundException.java  |   45 +++++++
 .../cms/servlet/test/DRMErrorInterceptor.java      |   43 +++++++
 .../netscape/cms/servlet/test/DRMRestClient.java   |   21 +++-
 .../src/com/netscape/cms/servlet/test/DRMTest.java |   11 ++
 15 files changed, 290 insertions(+), 41 deletions(-)
 create mode 100644 pki/base/common/src/com/netscape/cms/servlet/base/CMSException.java
 create mode 100644 pki/base/common/src/com/netscape/cms/servlet/request/RequestNotFoundException.java
 create mode 100644 pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMErrorInterceptor.java

diff --git a/pki/.classpath b/pki/.classpath
index 48aa45698a4e35710e852123d3e180a36155c7c5..97aa890760ca4ebedab9cb3004c7aa22a7118971 100644
--- a/pki/.classpath
+++ b/pki/.classpath
@@ -24,13 +24,14 @@
 	<classpathentry kind="lib" path="/usr/share/java/servlet.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/xerces-j2.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/junit4.jar"/>
-	<classpathentry kind="lib" path="/usr/share/candlepin/lib/jaxrs-api-2.2.1.GA.jar"/>
-	<classpathentry kind="lib" path="/usr/share/candlepin/lib/resteasy-jaxrs-2.2.1.GA.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/apache-commons-cli.jar"/>
-	<classpathentry kind="lib" path="/usr/share/java/jakarta-commons-httpclient.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/apache-commons-logging.jar"/>
 	<classpathentry kind="lib" path="/usr/share/java/commons-codec.jar"/>
-	<classpathentry kind="lib" path="/usr/share/candlepin/lib/jaxb-impl-2.1.12.jar"/>
-	<classpathentry kind="lib" path="/usr/share/candlepin/lib/resteasy-jaxb-provider-2.2.1.GA.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/resteasy/resteasy-jaxrs-2.3.2.Final.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/resteasy/jaxb-impl-2.2.4.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/resteasy/jaxrs-api-2.3.2.Final.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/resteasy/resteasy-jaxb-provider-2.3.2.Final.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/resteasy/httpcore-4.1.2.jar"/>
+	<classpathentry kind="lib" path="/usr/share/java/resteasy/httpclient-4.1.2.jar"/>
 	<classpathentry kind="output" path="build/classes"/>
 </classpath>
diff --git a/pki/base/common/src/CMakeLists.txt b/pki/base/common/src/CMakeLists.txt
index ed83bb68e88a550c298fa6c3ab302f1f0cdb71f0..7a34c2cfbc0e19abb540562bd05d5382a777434d 100644
--- a/pki/base/common/src/CMakeLists.txt
+++ b/pki/base/common/src/CMakeLists.txt
@@ -573,6 +573,7 @@ set(pki-cms_java_SRCS
     com/netscape/cms/servlet/base/IndexServlet.java
     com/netscape/cms/servlet/base/UserInfo.java
     com/netscape/cms/servlet/base/PortsServlet.java
+    com/netscape/cms/servlet/base/CMSException.java
     com/netscape/cms/servlet/base/CMSResourceService.java
     com/netscape/cms/servlet/base/CMSServlet.java
     com/netscape/cms/servlet/base/CMSStartServlet.java
@@ -658,6 +659,7 @@ set(pki-cms_java_SRCS
     com/netscape/cms/servlet/request/IReqParser.java
     com/netscape/cms/servlet/request/ReqParser.java
     com/netscape/cms/servlet/request/QueryReq.java
+    com/netscape/cms/servlet/request/RequestNotFoundException.java
     com/netscape/cms/servlet/request/SearchReqs.java
     com/netscape/cms/servlet/request/ProcessCertReq.java
     com/netscape/cms/servlet/request/CertReqParser.java
diff --git a/pki/base/common/src/com/netscape/certsrv/dbs/keydb/KeyId.java b/pki/base/common/src/com/netscape/certsrv/dbs/keydb/KeyId.java
index d319f9e3c5b7bc384ad08b6106c4f13e45526309..c3b463bf3e95af05ab3e0201d7f8e505c26f1ea6 100644
--- a/pki/base/common/src/com/netscape/certsrv/dbs/keydb/KeyId.java
+++ b/pki/base/common/src/com/netscape/certsrv/dbs/keydb/KeyId.java
@@ -94,6 +94,17 @@ public class KeyId {
         return value.toString();
     }
 
+    /**
+     * Converts the KeyId into its hex string representation. The string
+     * form can be stored in a database (such as the LDAP directory)
+     *
+     * @return
+     *         a string containing the hex (hex 16) value for the identifier.
+     */
+    public String toHexString() {
+        return "0x"+value.toString(16);
+    }
+
     @Override
     public int hashCode() {
         final int prime = 31;
diff --git a/pki/base/common/src/com/netscape/certsrv/request/RequestId.java b/pki/base/common/src/com/netscape/certsrv/request/RequestId.java
index e89e6c5ab82516fbb7b35228fdb59e0c5542d39a..0a94e37ac72d8621cbd957f93c96a48417610435 100644
--- a/pki/base/common/src/com/netscape/certsrv/request/RequestId.java
+++ b/pki/base/common/src/com/netscape/certsrv/request/RequestId.java
@@ -93,6 +93,17 @@ public class RequestId {
         return value.toString();
     }
 
+    /**
+     * Converts the RequestId into its hex string representation. The string
+     * form can be stored in a database (such as the LDAP directory)
+     *
+     * @return
+     *         a string containing the hex (base 16) value for the identifier.
+     */
+    public String toHexString() {
+        return "0x"+value.toString(16);
+    }
+
     @Override
     public int hashCode() {
         final int prime = 31;
diff --git a/pki/base/common/src/com/netscape/cms/servlet/admin/SystemCertificateResourceService.java b/pki/base/common/src/com/netscape/cms/servlet/admin/SystemCertificateResourceService.java
index 48f410c73df5740cf3858d46b4da0bedf388a69d..4dccb141fdd6b990206e04ce71f27fe2e205b1de 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/admin/SystemCertificateResourceService.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/admin/SystemCertificateResourceService.java
@@ -21,8 +21,6 @@ package com.netscape.cms.servlet.admin;
 import java.security.cert.CertificateEncodingException;
 
 import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.core.Context;
-import javax.ws.rs.core.Request;
 import javax.ws.rs.core.Response;
 
 import com.netscape.certsrv.apps.CMS;
@@ -39,9 +37,6 @@ import com.netscape.cms.servlet.cert.model.CertificateData;
  */
 public class SystemCertificateResourceService extends CMSResourceService implements SystemCertificateResource {
 
-    @Context
-    Request request;
-
     /**
      * Used to retrieve the transport certificate
      */
@@ -74,7 +69,7 @@ public class SystemCertificateResourceService extends CMSResourceService impleme
             e.printStackTrace();
             throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
         }
-        return sendConditionalGetResponse(DEFAULT_LONG_CACHE_LIFETIME, cert, request);
+        return sendConditionalGetResponse(DEFAULT_LONG_CACHE_LIFETIME, cert);
     }
 
 }
diff --git a/pki/base/common/src/com/netscape/cms/servlet/base/CMSException.java b/pki/base/common/src/com/netscape/cms/servlet/base/CMSException.java
new file mode 100644
index 0000000000000000000000000000000000000000..864dffb809207b48f8bf1de5b80820e5c15fdc1e
--- /dev/null
+++ b/pki/base/common/src/com/netscape/cms/servlet/base/CMSException.java
@@ -0,0 +1,127 @@
+package com.netscape.cms.servlet.base;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlValue;
+import javax.xml.bind.annotation.adapters.XmlAdapter;
+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
+
+public class CMSException extends RuntimeException {
+
+    private static final long serialVersionUID = 6000910362260369923L;
+
+    public CMSException(String message) {
+        super(message);
+    }
+
+    public CMSException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public CMSException(Data data) {
+        super(data.message);
+    }
+
+    public Data getData() {
+        Data data = new Data();
+        data.className = getClass().getName();
+        data.message = getMessage();
+        return data;
+    }
+
+    @XmlRootElement(name="CMSException")
+    public static class Data {
+
+        @XmlElement(name="ClassName")
+        public String className;
+
+        @XmlElement(name="Message")
+        public String message;
+
+        @XmlElement(name="Attributes")
+        @XmlJavaTypeAdapter(MapAdapter.class)
+        public Map<String, String> attributes = new LinkedHashMap<String, String>();
+
+        public String getAttribute(String name) {
+            return attributes.get(name);
+        }
+
+        public void setAttribute(String name, String value) {
+            attributes.put(name, value);
+        }
+    }
+
+    public static class MapAdapter extends XmlAdapter<AttributeList, Map<String, String>> {
+
+        public AttributeList marshal(Map<String, String> map) {
+            AttributeList list = new AttributeList();
+            for (Map.Entry<String, String> entry : map.entrySet()) {
+                Attribute attribute = new Attribute();
+                attribute.name = entry.getKey();
+                attribute.value = entry.getValue();
+                list.attributes.add(attribute);
+            }
+            return list;
+        }
+
+        public Map<String, String> unmarshal(AttributeList list) {
+            Map<String, String> map = new LinkedHashMap<String, String>();
+            for (Attribute attribute : list.attributes) {
+                map.put(attribute.name, attribute.value);
+            }
+            return map;
+        }
+    }
+
+    public static class AttributeList {
+        @XmlElement(name="Attribute")
+        public List<Attribute> attributes = new ArrayList<Attribute>();
+    }
+
+    public static class Attribute {
+
+        @XmlAttribute
+        public String name;
+
+        @XmlValue
+        public String value;
+    }
+
+    @Provider
+    public static class Mapper implements ExceptionMapper<CMSException> {
+
+        public Response toResponse(CMSException exception) {
+            // convert CMSException into HTTP code 500 with XML content
+            return Response
+                    .status(Response.Status.INTERNAL_SERVER_ERROR)
+                    .entity(exception.getData())
+                    .type(MediaType.TEXT_XML)
+                    .build();
+        }
+    }
+
+    public static void main(String args[]) throws Exception {
+        Data data = new Data();
+        data.className = CMSException.class.getName();
+        data.message = "An error has occured";
+        data.setAttribute("attr1", "value1");
+        data.setAttribute("attr2", "value2");
+
+        JAXBContext context = JAXBContext.newInstance(Data.class);
+        Marshaller marshaller = context.createMarshaller();
+        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
+        marshaller.marshal(data, System.out);
+    }
+}
diff --git a/pki/base/common/src/com/netscape/cms/servlet/base/CMSResourceService.java b/pki/base/common/src/com/netscape/cms/servlet/base/CMSResourceService.java
index acddba559c2ac0b9152ca2f43f88cec20fde0358..e23e7d024ac2ca78d47a3d026cd031597ab689d4 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/base/CMSResourceService.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/base/CMSResourceService.java
@@ -20,9 +20,11 @@ package com.netscape.cms.servlet.base;
 import java.security.cert.CertificateEncodingException;
 
 import javax.ws.rs.core.CacheControl;
+import javax.ws.rs.core.Context;
 import javax.ws.rs.core.EntityTag;
 import javax.ws.rs.core.Request;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
 import javax.ws.rs.core.Response.ResponseBuilder;
 
 import com.netscape.certsrv.apps.CMS;
@@ -35,13 +37,24 @@ import com.netscape.cms.servlet.cert.model.CertificateData;
  * 
  */
 public class CMSResourceService {
+
     public static final String HEADER = "-----BEGIN NEW CERTIFICATE REQUEST-----";
     public static final String TRAILER = "-----END NEW CERTIFICATE REQUEST-----";
 
     // caching parameters
-    protected static final int DEFAULT_LONG_CACHE_LIFETIME = 1000;
+    public static final int DEFAULT_LONG_CACHE_LIFETIME = 1000;
 
-    protected Response sendConditionalGetResponse(int ctime, Object object, Request request) {
+    @Context
+    protected UriInfo uriInfo;
+
+    @Context
+    protected Request request;
+
+    public Response createOKResponse(Object object) {
+        return Response.ok(object).build();
+    }
+
+    public Response sendConditionalGetResponse(int ctime, Object object) {
         CacheControl cc = new CacheControl();
         cc.setMaxAge(ctime);
         EntityTag tag = new EntityTag(Integer.toString(object.hashCode()));
diff --git a/pki/base/common/src/com/netscape/cms/servlet/key/KeyResourceService.java b/pki/base/common/src/com/netscape/cms/servlet/key/KeyResourceService.java
index 79e6ccfdb4e45cb1715835b6cac7d3ce4ff77f1c..88f2f8a827ff4825bf3f70bfc1a26824c26c5834 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/key/KeyResourceService.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/key/KeyResourceService.java
@@ -20,10 +20,8 @@ package com.netscape.cms.servlet.key;
 
 
 import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
-import javax.ws.rs.core.UriInfo;
 
 import com.netscape.cms.servlet.base.CMSResourceService;
 import com.netscape.cms.servlet.key.model.KeyDAO;
@@ -43,9 +41,6 @@ import com.netscape.certsrv.dbs.keydb.KeyId;
  */
 public class KeyResourceService extends CMSResourceService implements KeyResource{
     
-    @Context
-    UriInfo uriInfo;
-    
     /**
      * Used to retrieve a key
      * @param data
diff --git a/pki/base/common/src/com/netscape/cms/servlet/key/KeysResourceService.java b/pki/base/common/src/com/netscape/cms/servlet/key/KeysResourceService.java
index b5032fa86c93c2b0fd5053df38f9f7b255748ed6..55b9f503b62c42918630cfc8c5a630caf3e8ac8e 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/key/KeysResourceService.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/key/KeysResourceService.java
@@ -21,9 +21,7 @@
 package com.netscape.cms.servlet.key;
 
 import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.core.Context;
 import javax.ws.rs.core.Response;
-import javax.ws.rs.core.UriInfo;
  
 import com.netscape.certsrv.apps.CMS;
 import com.netscape.certsrv.base.EBaseException;
@@ -37,9 +35,6 @@ import com.netscape.cms.servlet.key.model.KeyDataInfos;
  */
 public class KeysResourceService extends CMSResourceService implements KeysResource {
 
-    @Context
-    UriInfo uriInfo;
-
     /**
      * Used to generate list of key infos based on the search parameters
      */
diff --git a/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestResourceService.java b/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestResourceService.java
index 43e58bbdc604beb6e92eb0042f2e06784afb5e3f..66f099464ead860fddafe83b841e45af5bfc2b97 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestResourceService.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestResourceService.java
@@ -19,12 +19,11 @@
 package com.netscape.cms.servlet.request;
 
 import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
-import javax.ws.rs.core.UriInfo;
 import com.netscape.certsrv.base.EBaseException;
 import com.netscape.certsrv.request.RequestId;
+import com.netscape.cms.servlet.base.CMSException;
 import com.netscape.cms.servlet.base.CMSResourceService;
 import com.netscape.cms.servlet.request.model.ArchivalRequestData;
 import com.netscape.cms.servlet.request.model.KeyRequestDAO;
@@ -37,9 +36,6 @@ import com.netscape.cms.servlet.request.model.RecoveryRequestData;
  */
 public class KeyRequestResourceService extends CMSResourceService implements KeyRequestResource {
 
-    @Context
-    UriInfo uriInfo;
-    
     /**
      * Used to retrieve key request info for a specific request
      */
@@ -52,11 +48,11 @@ public class KeyRequestResourceService extends CMSResourceService implements Key
         } catch (EBaseException e) {
             // log error
             e.printStackTrace();
-            throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
+            throw new CMSException(e.getMessage());
         }
         if (info == null) {
             // request does not exist
-            throw new WebApplicationException(Response.Status.NOT_FOUND);
+            throw new RequestNotFoundException(id);
         }
         return info;
     }
diff --git a/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestsResourceService.java b/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestsResourceService.java
index 9b11a96d606f248de483d4d5b319ec196ef66460..cf914783a65f2d066a555fa484081541e581dbbf 100644
--- a/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestsResourceService.java
+++ b/pki/base/common/src/com/netscape/cms/servlet/request/KeyRequestsResourceService.java
@@ -19,9 +19,7 @@
 package com.netscape.cms.servlet.request;
 
 import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.core.Context;
 import javax.ws.rs.core.Response;
-import javax.ws.rs.core.UriInfo;
 
 import com.netscape.certsrv.apps.CMS;
 import com.netscape.certsrv.base.EBaseException;
@@ -36,9 +34,6 @@ import com.netscape.cms.servlet.request.model.KeyRequestInfos;
  */
 public class KeyRequestsResourceService extends CMSResourceService implements KeyRequestsResource{
 
-    @Context
-    UriInfo uriInfo;
-
     /**
      * Used to generate list of key requests based on the search parameters
      */
diff --git a/pki/base/common/src/com/netscape/cms/servlet/request/RequestNotFoundException.java b/pki/base/common/src/com/netscape/cms/servlet/request/RequestNotFoundException.java
new file mode 100644
index 0000000000000000000000000000000000000000..0eae51d0cd0a416a65674fdc67c753fd228ff8fb
--- /dev/null
+++ b/pki/base/common/src/com/netscape/cms/servlet/request/RequestNotFoundException.java
@@ -0,0 +1,45 @@
+package com.netscape.cms.servlet.request;
+
+import com.netscape.certsrv.request.RequestId;
+import com.netscape.cms.servlet.base.CMSException;
+
+public class RequestNotFoundException extends CMSException {
+
+    private static final long serialVersionUID = -4784839378360933483L;
+
+    public RequestId requestId;
+
+    public RequestNotFoundException(RequestId requestId) {
+        this(requestId, "Request ID "+requestId.toHexString()+" not found");
+    }
+
+    public RequestNotFoundException(RequestId requestId, String message) {
+        super(message);
+        this.requestId = requestId;
+    }
+
+    public RequestNotFoundException(RequestId requestId, String message, Throwable cause) {
+        super(message, cause);
+        this.requestId = requestId;
+    }
+
+    public RequestNotFoundException(Data data) {
+        super(data);
+
+        requestId = new RequestId(data.getAttribute("requestId"));
+    }
+
+    public Data getData() {
+        Data data = super.getData();
+        data.setAttribute("requestId", requestId.toString());
+        return data;
+    }
+
+    public RequestId getRequestId() {
+        return requestId;
+    }
+
+    public void setRequestId(RequestId requestId) {
+        this.requestId = requestId;
+    }
+}
diff --git a/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMErrorInterceptor.java b/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMErrorInterceptor.java
new file mode 100644
index 0000000000000000000000000000000000000000..48d3e7a95c72c6b625bfdac1cec8a60dbac1e734
--- /dev/null
+++ b/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMErrorInterceptor.java
@@ -0,0 +1,43 @@
+package com.netscape.cms.servlet.test;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.Response;
+
+import org.jboss.resteasy.client.ClientResponse;
+import org.jboss.resteasy.client.core.ClientErrorInterceptor;
+
+import com.netscape.cms.servlet.base.CMSException;
+
+public class DRMErrorInterceptor implements ClientErrorInterceptor  {
+
+    public void handle(ClientResponse<?> response) {
+
+        // handle HTTP code 500 only
+        if (!response.getResponseStatus().equals(Response.Status.INTERNAL_SERVER_ERROR)) return;
+
+        MultivaluedMap<String, String> headers = response.getHeaders();
+        String contentType = headers.getFirst("Content-Type");
+
+        // handle XML content only
+        if (!contentType.startsWith(MediaType.TEXT_XML)) return;
+
+        CMSException exception;
+
+        try {
+            // Requires RESTEasy 2.3.2
+            // https://issues.jboss.org/browse/RESTEASY-652
+            CMSException.Data data = response.getEntity(CMSException.Data.class);
+
+            Class<?> clazz = Class.forName(data.className);
+            exception = (CMSException) clazz.getConstructor(CMSException.Data.class).newInstance(data);
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            return;
+        }
+
+        throw exception;
+    }
+
+}
diff --git a/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMRestClient.java b/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMRestClient.java
index 51cead47b029a6ba914dfc1411dbaf6bb5d51435..356f7957b39e3c8a37af2d748a345f88fa081fe3 100644
--- a/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMRestClient.java
+++ b/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMRestClient.java
@@ -1,10 +1,13 @@
 package com.netscape.cms.servlet.test;
 
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.Iterator;
+import java.util.Map;
 
 import org.jboss.resteasy.client.ClientResponse;
 import org.jboss.resteasy.client.ProxyFactory;
+import org.jboss.resteasy.spi.ResteasyProviderFactory;
 
 import com.netscape.certsrv.dbs.keydb.KeyId;
 import com.netscape.certsrv.request.RequestId;
@@ -32,11 +35,15 @@ public class DRMRestClient {
     private SystemCertificateResource systemCertClient;
     
     public DRMRestClient(String baseUri) {
-        systemCertClient = ProxyFactory.create(SystemCertificateResource.class, baseUri);
-        keyRequestsClient = ProxyFactory.create(KeyRequestsResource.class, baseUri);
-        keyRequestClient = ProxyFactory.create(KeyRequestResource.class, baseUri);
-        keysClient = ProxyFactory.create(KeysResource.class, baseUri);
-        keyClient = ProxyFactory.create(KeyResource.class, baseUri);
+        ResteasyProviderFactory providerFactory = ResteasyProviderFactory.getInstance();
+        providerFactory.addClientErrorInterceptor(new DRMErrorInterceptor());
+        Map<String, Object> requestAttributes = new HashMap<String, Object>();
+
+        systemCertClient = ProxyFactory.create(SystemCertificateResource.class, baseUri, providerFactory, requestAttributes);
+        keyRequestsClient = ProxyFactory.create(KeyRequestsResource.class, baseUri, providerFactory, requestAttributes);
+        keyRequestClient = ProxyFactory.create(KeyRequestResource.class, baseUri, providerFactory, requestAttributes);
+        keysClient = ProxyFactory.create(KeysResource.class, baseUri, providerFactory, requestAttributes);
+        keyClient = ProxyFactory.create(KeyResource.class, baseUri, providerFactory, requestAttributes);
     }
     
     public String getTransportCert() {
@@ -125,6 +132,8 @@ public class DRMRestClient {
         return key;
     }
      
-
+    public KeyRequestInfo getRequest(RequestId id) {
+        return keyRequestClient.getRequestInfo(id);
+    }
     
 }
\ No newline at end of file
diff --git a/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMTest.java b/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMTest.java
index 8020ec2cab7542aeab55fbf699c12d713e3cb83a..c58b5b1051f2645e1b1be845231d7e0c468b0adf 100644
--- a/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMTest.java
+++ b/pki/base/kra/functional/src/com/netscape/cms/servlet/test/DRMTest.java
@@ -44,6 +44,7 @@ import com.netscape.cms.servlet.base.CMSResourceService;
 import com.netscape.cms.servlet.key.model.KeyData;
 import com.netscape.cms.servlet.key.model.KeyDataInfo;
 import com.netscape.cms.servlet.request.KeyRequestResource;
+import com.netscape.cms.servlet.request.RequestNotFoundException;
 import com.netscape.cms.servlet.request.model.KeyRequestInfo;
 import com.netscape.cmsutil.crypto.CryptoUtil;
 import com.netscape.cmsutil.util.Utils;
@@ -448,6 +449,16 @@ public class DRMTest {
         } else {
             log("Success: recovered and archived passphrases do match!");
         }
+
+        // Test 23: Get non-existent request
+        RequestId requestId = new RequestId("0xabcdef");
+        log("Getting non-existent request: " + requestId.toHexString());
+        try {
+            client.getRequest(requestId);
+            log("Error: getting non-existent request does not throw an exception");
+        } catch (RequestNotFoundException e) {
+            log("Success: getting non-existent request throws an exception: "+e.getMessage()+" ("+e.getRequestId().toHexString()+")");
+        }
     }
 
     private static void log(String string) {
-- 
1.7.7.4

