From 93d923b051fa9632ad6742a4e596fca3ce2c9071 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Mon, 5 May 2014 11:05:43 -0400
Subject: [PATCH] Fixed message format for PKIException.

Previously PKIException was not displayed properly in browser
because it doesn't have a writer for HTML. Now the exception mapper
will compute the message format properly, and will default to XML.
The exception mapper itself has been moved into a server package
due to class dependency. The REST application classes have been
updated accordingly.

Ticket #554
---
 .../dogtagpki/server/ca/rest/CAApplication.java    |  4 ++--
 .../com/netscape/certsrv/base/PKIException.java    | 14 ------------
 .../dogtagpki/server/kra/rest/KRAApplication.java  |  4 ++--
 .../server/ocsp/rest/OCSPApplication.java          |  4 ++--
 .../com/netscape/cms/servlet/base/PKIService.java  | 12 ++++++++--
 .../dogtagpki/server/rest/PKIExceptionMapper.java  | 26 ++++++++++++++++++++++
 .../dogtagpki/server/tks/rest/TKSApplication.java  |  5 ++---
 .../dogtagpki/server/tps/rest/TPSApplication.java  |  5 ++---
 8 files changed, 46 insertions(+), 28 deletions(-)
 create mode 100644 base/server/cms/src/org/dogtagpki/server/rest/PKIExceptionMapper.java

diff --git a/base/ca/src/org/dogtagpki/server/ca/rest/CAApplication.java b/base/ca/src/org/dogtagpki/server/ca/rest/CAApplication.java
index ef8687cc07cb19cc168b919fac2a067ef75940dd..d73b794da2f5f9703d2b3c40f815bcb417e6c7b6 100644
--- a/base/ca/src/org/dogtagpki/server/ca/rest/CAApplication.java
+++ b/base/ca/src/org/dogtagpki/server/ca/rest/CAApplication.java
@@ -10,6 +10,7 @@ import org.dogtagpki.server.rest.AccountService;
 import org.dogtagpki.server.rest.AuditService;
 import org.dogtagpki.server.rest.AuthMethodInterceptor;
 import org.dogtagpki.server.rest.GroupService;
+import org.dogtagpki.server.rest.PKIExceptionMapper;
 import org.dogtagpki.server.rest.MessageFormatInterceptor;
 import org.dogtagpki.server.rest.SecurityDomainService;
 import org.dogtagpki.server.rest.SelfTestService;
@@ -19,7 +20,6 @@ import org.dogtagpki.server.rest.UserService;
 import com.netscape.certsrv.apps.CMS;
 import com.netscape.certsrv.base.EBaseException;
 import com.netscape.certsrv.base.IConfigStore;
-import com.netscape.certsrv.base.PKIException;
 
 public class CAApplication extends Application {
 
@@ -86,7 +86,7 @@ public class CAApplication extends Application {
         }
 
         // exception mapper
-        classes.add(PKIException.Mapper.class);
+        classes.add(PKIExceptionMapper.class);
 
         // interceptors
         singletons.add(new AuthMethodInterceptor());
diff --git a/base/common/src/com/netscape/certsrv/base/PKIException.java b/base/common/src/com/netscape/certsrv/base/PKIException.java
index ae557d46516767c11552fe7b8adcd67bc0523730..e9f0066580e46282003d88e23d70248027109399 100644
--- a/base/common/src/com/netscape/certsrv/base/PKIException.java
+++ b/base/common/src/com/netscape/certsrv/base/PKIException.java
@@ -18,8 +18,6 @@
 package com.netscape.certsrv.base;
 
 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.XmlElement;
@@ -94,18 +92,6 @@ public class PKIException extends RuntimeException {
         public String message;
     }
 
-    @Provider
-    public static class Mapper implements ExceptionMapper<PKIException> {
-
-        public Response toResponse(PKIException exception) {
-            // convert PKIException into HTTP response
-            return Response
-                    .status(exception.getCode())
-                    .entity(exception.getData())
-                    .build();
-        }
-    }
-
     public static void main(String args[]) throws Exception {
         Data data = new Data();
         data.className = PKIException.class.getName();
diff --git a/base/kra/src/org/dogtagpki/server/kra/rest/KRAApplication.java b/base/kra/src/org/dogtagpki/server/kra/rest/KRAApplication.java
index 4f598cb992d1ef5ac6e35b4500ea794988984c8a..815763cdd82ed2b427c585e52d344ecc02602d24 100644
--- a/base/kra/src/org/dogtagpki/server/kra/rest/KRAApplication.java
+++ b/base/kra/src/org/dogtagpki/server/kra/rest/KRAApplication.java
@@ -10,6 +10,7 @@ import org.dogtagpki.server.rest.AccountService;
 import org.dogtagpki.server.rest.AuditService;
 import org.dogtagpki.server.rest.AuthMethodInterceptor;
 import org.dogtagpki.server.rest.GroupService;
+import org.dogtagpki.server.rest.PKIExceptionMapper;
 import org.dogtagpki.server.rest.MessageFormatInterceptor;
 import org.dogtagpki.server.rest.SecurityDomainService;
 import org.dogtagpki.server.rest.SelfTestService;
@@ -19,7 +20,6 @@ import org.dogtagpki.server.rest.UserService;
 import com.netscape.certsrv.apps.CMS;
 import com.netscape.certsrv.base.EBaseException;
 import com.netscape.certsrv.base.IConfigStore;
-import com.netscape.certsrv.base.PKIException;
 
 public class KRAApplication extends Application {
 
@@ -64,7 +64,7 @@ public class KRAApplication extends Application {
         classes.add(SystemCertService.class);
 
         // exception mapper
-        classes.add(PKIException.Mapper.class);
+        classes.add(PKIExceptionMapper.class);
 
         // interceptors
         singletons.add(new AuthMethodInterceptor());
diff --git a/base/ocsp/src/org/dogtagpki/server/ocsp/rest/OCSPApplication.java b/base/ocsp/src/org/dogtagpki/server/ocsp/rest/OCSPApplication.java
index 392d923f98dfacf832afaf386ed3fb43164b7b4b..1950edf29d06e6b881d901fbcc9141d76592940d 100644
--- a/base/ocsp/src/org/dogtagpki/server/ocsp/rest/OCSPApplication.java
+++ b/base/ocsp/src/org/dogtagpki/server/ocsp/rest/OCSPApplication.java
@@ -10,6 +10,7 @@ import org.dogtagpki.server.rest.AccountService;
 import org.dogtagpki.server.rest.AuditService;
 import org.dogtagpki.server.rest.AuthMethodInterceptor;
 import org.dogtagpki.server.rest.GroupService;
+import org.dogtagpki.server.rest.PKIExceptionMapper;
 import org.dogtagpki.server.rest.MessageFormatInterceptor;
 import org.dogtagpki.server.rest.SecurityDomainService;
 import org.dogtagpki.server.rest.SelfTestService;
@@ -19,7 +20,6 @@ import org.dogtagpki.server.rest.UserService;
 import com.netscape.certsrv.apps.CMS;
 import com.netscape.certsrv.base.EBaseException;
 import com.netscape.certsrv.base.IConfigStore;
-import com.netscape.certsrv.base.PKIException;
 
 public class OCSPApplication extends Application {
 
@@ -60,7 +60,7 @@ public class OCSPApplication extends Application {
         classes.add(SystemCertService.class);
 
         // exception mapper
-        classes.add(PKIException.Mapper.class);
+        classes.add(PKIExceptionMapper.class);
 
         // interceptors
         singletons.add(new AuthMethodInterceptor());
diff --git a/base/server/cms/src/com/netscape/cms/servlet/base/PKIService.java b/base/server/cms/src/com/netscape/cms/servlet/base/PKIService.java
index 9338b9645f9b9d0113439f758c8999c4c9d564d4..866db63fd9273b5d88036cdb631abc3c04f5b22a 100644
--- a/base/server/cms/src/com/netscape/cms/servlet/base/PKIService.java
+++ b/base/server/cms/src/com/netscape/cms/servlet/base/PKIService.java
@@ -87,7 +87,7 @@ public class PKIService {
         return null;
     }
 
-    public MediaType getResponseFormat() {
+    public static MediaType getResponseFormat(HttpHeaders headers) {
         MediaType contentType = headers.getMediaType();
         List<MediaType> acceptableFormats = headers.getAcceptableMediaTypes();
 
@@ -104,10 +104,18 @@ public class PKIService {
         } else {
             responseFormat = resolveFormat(acceptableFormats);
         }
-        if (responseFormat == null) throw new PKIException(Response.Status.NOT_ACCEPTABLE);
+
+        if (responseFormat == null) {
+            throw new PKIException(Response.Status.NOT_ACCEPTABLE);
+        }
+
         return responseFormat;
     }
 
+    public MediaType getResponseFormat() {
+        return getResponseFormat(headers);
+    }
+
     public Response createOKResponse() {
         return Response
                 .ok()
diff --git a/base/server/cms/src/org/dogtagpki/server/rest/PKIExceptionMapper.java b/base/server/cms/src/org/dogtagpki/server/rest/PKIExceptionMapper.java
new file mode 100644
index 0000000000000000000000000000000000000000..072ac05e65679baf455ef5fa5ecb0aac4cde50dd
--- /dev/null
+++ b/base/server/cms/src/org/dogtagpki/server/rest/PKIExceptionMapper.java
@@ -0,0 +1,26 @@
+package org.dogtagpki.server.rest;
+
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+
+import com.netscape.certsrv.base.PKIException;
+import com.netscape.cms.servlet.base.PKIService;
+
+@Provider
+public class PKIExceptionMapper implements ExceptionMapper<PKIException> {
+
+    @Context
+    private HttpHeaders headers;
+
+    public Response toResponse(PKIException exception) {
+        // convert PKIException into HTTP response
+        return Response
+                .status(exception.getCode())
+                .entity(exception.getData())
+                .type(PKIService.getResponseFormat(headers))
+                .build();
+    }
+}
diff --git a/base/tks/src/org/dogtagpki/server/tks/rest/TKSApplication.java b/base/tks/src/org/dogtagpki/server/tks/rest/TKSApplication.java
index 20b10ca40d9b3d6393109c4d6ac30bc82736c03d..c0fdc6734fe2d27b5122d8c81f8d0ae9cf4aa3c8 100644
--- a/base/tks/src/org/dogtagpki/server/tks/rest/TKSApplication.java
+++ b/base/tks/src/org/dogtagpki/server/tks/rest/TKSApplication.java
@@ -10,13 +10,12 @@ import org.dogtagpki.server.rest.AccountService;
 import org.dogtagpki.server.rest.AuditService;
 import org.dogtagpki.server.rest.AuthMethodInterceptor;
 import org.dogtagpki.server.rest.GroupService;
+import org.dogtagpki.server.rest.PKIExceptionMapper;
 import org.dogtagpki.server.rest.MessageFormatInterceptor;
 import org.dogtagpki.server.rest.SelfTestService;
 import org.dogtagpki.server.rest.SystemCertService;
 import org.dogtagpki.server.rest.UserService;
 
-import com.netscape.certsrv.base.PKIException;
-
 public class TKSApplication extends Application {
 
     private Set<Object> singletons = new LinkedHashSet<Object>();
@@ -47,7 +46,7 @@ public class TKSApplication extends Application {
         classes.add(TPSConnectorService.class);
 
         // exception mapper
-        classes.add(PKIException.Mapper.class);
+        classes.add(PKIExceptionMapper.class);
 
         // interceptors
         singletons.add(new AuthMethodInterceptor());
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/TPSApplication.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/TPSApplication.java
index a8161d203f671e2844d229a7102fcbc7a3b1414c..fc06fd8bb6f851af9b394f3eabf3b2f589633734 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/TPSApplication.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/TPSApplication.java
@@ -27,14 +27,13 @@ import org.dogtagpki.server.rest.AccountService;
 import org.dogtagpki.server.rest.AuditService;
 import org.dogtagpki.server.rest.AuthMethodInterceptor;
 import org.dogtagpki.server.rest.GroupService;
+import org.dogtagpki.server.rest.PKIExceptionMapper;
 import org.dogtagpki.server.rest.MessageFormatInterceptor;
 import org.dogtagpki.server.rest.SelfTestService;
 import org.dogtagpki.server.rest.SystemCertService;
 import org.dogtagpki.server.rest.UserService;
 import org.dogtagpki.server.tps.config.ConfigService;
 
-import com.netscape.certsrv.base.PKIException;
-
 /**
  * @author Endi S. Dewata <edewata@redhat.com>
  */
@@ -87,7 +86,7 @@ public class TPSApplication extends Application {
         classes.add(TokenService.class);
 
         // exception mapper
-        classes.add(PKIException.Mapper.class);
+        classes.add(PKIExceptionMapper.class);
 
         // interceptors
         singletons.add(new AuthMethodInterceptor());
-- 
1.8.4.2

