From 7e2fc0737e0af404299929ea21442cb9f2c87aa5 Mon Sep 17 00:00:00 2001
From: Endi Sukma Dewata <edewata@redhat.com>
Date: Thu, 7 Jun 2012 03:16:37 -0500
Subject: [PATCH] Added REST error handler.

If a REST method returns a Response object it has to be handled
manually. A new getEntity() method has been added to obtain the
entity from the Response object and also map HTTP errors into
exceptions.

Ticket #161
---
 .../netscape/cms/client/group/GroupRestClient.java |    6 ++--
 .../netscape/cms/client/user/UserRestClient.java   |    6 ++--
 .../cms/servlet/csadmin/CMSRestClient.java         |   22 ++++++++++++++++++++
 .../netscape/cms/servlet/test/DRMRestClient.java   |    2 +-
 4 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/base/common/src/com/netscape/cms/client/group/GroupRestClient.java b/base/common/src/com/netscape/cms/client/group/GroupRestClient.java
index c5070a447dec5d381d51555c3860f9f832015c36..54b31fef671f15c40574dc8ea42c9b330284f7d4 100644
--- a/base/common/src/com/netscape/cms/client/group/GroupRestClient.java
+++ b/base/common/src/com/netscape/cms/client/group/GroupRestClient.java
@@ -59,13 +59,13 @@ public class GroupRestClient extends CMSRestClient {
     public GroupData addGroup(GroupData groupData) {
         @SuppressWarnings("unchecked")
         ClientResponse<GroupData> response = (ClientResponse<GroupData>)groupClient.addGroup(groupData);
-        return response.getEntity();
+        return getEntity(response);
     }
 
     public GroupData modifyGroup(String groupID, GroupData groupData) {
         @SuppressWarnings("unchecked")
         ClientResponse<GroupData> response = (ClientResponse<GroupData>)groupClient.modifyGroup(groupID, groupData);
-        return response.getEntity();
+        return getEntity(response);
     }
 
     public void removeGroup(String groupID) {
@@ -83,7 +83,7 @@ public class GroupRestClient extends CMSRestClient {
     public GroupMemberData addGroupMember(String groupID, String memberID) {
         @SuppressWarnings("unchecked")
         ClientResponse<GroupMemberData> response = (ClientResponse<GroupMemberData>)groupMemberClient.addGroupMember(groupID, memberID);
-        return response.getEntity();
+        return getEntity(response);
     }
 
     public void removeGroupMember(String groupID, String memberID) {
diff --git a/base/common/src/com/netscape/cms/client/user/UserRestClient.java b/base/common/src/com/netscape/cms/client/user/UserRestClient.java
index a214b261eb294080bc10d10cf44ddf4c068c642b..8bbdefe26f27d8d1a8617ae6876d3c0dcfdf966d 100644
--- a/base/common/src/com/netscape/cms/client/user/UserRestClient.java
+++ b/base/common/src/com/netscape/cms/client/user/UserRestClient.java
@@ -59,13 +59,13 @@ public class UserRestClient extends CMSRestClient {
     public UserData addUser(UserData userData) {
         @SuppressWarnings("unchecked")
         ClientResponse<UserData> response = (ClientResponse<UserData>)userClient.addUser(userData);
-        return response.getEntity();
+        return getEntity(response);
     }
 
     public UserData modifyUser(String userID, UserData userData) {
         @SuppressWarnings("unchecked")
         ClientResponse<UserData> response = (ClientResponse<UserData>)userClient.modifyUser(userID, userData);
-        return response.getEntity();
+        return getEntity(response);
     }
 
     public void removeUser(String userID) {
@@ -83,7 +83,7 @@ public class UserRestClient extends CMSRestClient {
     public UserCertData addUserCert(String userID, UserCertData userCertData) {
         @SuppressWarnings("unchecked")
         ClientResponse<UserCertData> response = (ClientResponse<UserCertData>)userCertClient.addUserCert(userID, userCertData);
-        return response.getEntity();
+        return getEntity(response);
     }
 
     public void removeUserCert(String userID, String certID) {
diff --git a/base/common/src/com/netscape/cms/servlet/csadmin/CMSRestClient.java b/base/common/src/com/netscape/cms/servlet/csadmin/CMSRestClient.java
index ff028660a382e410fc11915b9584bf8a9d7b8f30..7f2c75007b85ab29d764a04f5051b13dc8cace60 100644
--- a/base/common/src/com/netscape/cms/servlet/csadmin/CMSRestClient.java
+++ b/base/common/src/com/netscape/cms/servlet/csadmin/CMSRestClient.java
@@ -17,8 +17,12 @@ import org.apache.http.conn.scheme.SchemeSocketFactory;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.params.HttpParams;
 import org.jboss.resteasy.client.ClientExecutor;
+import org.jboss.resteasy.client.ClientResponse;
+import org.jboss.resteasy.client.ClientResponseFailure;
 import org.jboss.resteasy.client.ProxyFactory;
+import org.jboss.resteasy.client.core.BaseClientResponse;
 import org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor;
+import org.jboss.resteasy.client.core.extractors.ClientErrorHandler;
 import org.jboss.resteasy.spi.ResteasyProviderFactory;
 import org.mozilla.jss.ssl.SSLCertificateApprovalCallback;
 import org.mozilla.jss.ssl.SSLSocket;
@@ -27,6 +31,7 @@ public abstract class CMSRestClient {
 
     protected String clientCertNickname;
     protected ResteasyProviderFactory providerFactory;
+    protected ClientErrorHandler errorHandler;
     protected ClientExecutor executor;
     protected URI uri;
 
@@ -59,6 +64,7 @@ public abstract class CMSRestClient {
         executor = new ApacheHttpClient4Executor(httpclient);
         providerFactory = ResteasyProviderFactory.getInstance();
         providerFactory.addClientErrorInterceptor(new CMSErrorInterceptor());
+        errorHandler = new ClientErrorHandler(providerFactory.getClientErrorInterceptors());
     }
 
     private class ServerCertApprovalCB implements SSLCertificateApprovalCallback {
@@ -168,4 +174,20 @@ public abstract class CMSRestClient {
     public <T> T createProxy(Class<T> clazz) {
         return ProxyFactory.create(clazz, uri, executor, providerFactory);
     }
+
+    @SuppressWarnings("unchecked")
+    public <T> T getEntity(ClientResponse<T> response) {
+        BaseClientResponse<T> clientResponse = (BaseClientResponse<T>)response;
+        try {
+            clientResponse.checkFailureStatus();
+
+       } catch (ClientResponseFailure e) {
+            errorHandler.clientErrorHandling((BaseClientResponse<T>) e.getResponse(), e);
+
+       } catch (RuntimeException e) {
+            errorHandler.clientErrorHandling(clientResponse, e);
+       }
+
+       return response.getEntity();
+    }
 }
diff --git a/base/kra/functional/src/com/netscape/cms/servlet/test/DRMRestClient.java b/base/kra/functional/src/com/netscape/cms/servlet/test/DRMRestClient.java
index cd25d7b78338a803a16bbc0d8e64a8e0fcc1c8ce..5dbcac1a0a2a09a6d36904668ce37a8d2f023521 100644
--- a/base/kra/functional/src/com/netscape/cms/servlet/test/DRMRestClient.java
+++ b/base/kra/functional/src/com/netscape/cms/servlet/test/DRMRestClient.java
@@ -48,7 +48,7 @@ public class DRMRestClient  extends CMSRestClient {
         @SuppressWarnings("unchecked")
         ClientResponse<CertificateData> response = (ClientResponse<CertificateData>) systemCertClient
                 .getTransportCert();
-        CertificateData certData = response.getEntity();
+        CertificateData certData = getEntity(response);
         String transportCert = certData.getEncoded();
         return transportCert;
     }
-- 
1.7.7.6

