summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEndi Sukma Dewata <edewata@redhat.com>2012-06-07 03:16:37 -0500
committerEndi Sukma Dewata <edewata@redhat.com>2012-06-27 17:25:10 -0500
commit1eba8264adbe42115caac4db655d450a99791ceb (patch)
treeb7c564a54e5235affadf55c233afc52f6e17fa5a
parent4ccfcebe532b97607cfab406a1fa900070573f24 (diff)
downloadpki-1eba8264adbe42115caac4db655d450a99791ceb.tar.gz
pki-1eba8264adbe42115caac4db655d450a99791ceb.tar.xz
pki-1eba8264adbe42115caac4db655d450a99791ceb.zip
Added REST error handler.
A new getEntity() method has been added to obtain the entity from a Response object and also map HTTP errors into exceptions. Ticket #161
-rw-r--r--base/common/src/com/netscape/cms/client/group/GroupRestClient.java6
-rw-r--r--base/common/src/com/netscape/cms/client/user/UserRestClient.java6
-rw-r--r--base/common/src/com/netscape/cms/servlet/csadmin/CMSRestClient.java22
-rw-r--r--base/kra/functional/src/com/netscape/cms/servlet/test/DRMRestClient.java2
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 c5070a447..54b31fef6 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 a214b261e..8bbdefe26 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 ff028660a..7f2c75007 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 772612bf4..82d246e04 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.getB64();
return transportCert;
}