963458
From 749c8966aff781c0a8e5b114df64aeb76e1e9bdb Mon Sep 17 00:00:00 2001
963458
From: Fraser Tweedale <ftweedal@redhat.com>
963458
Date: Thu, 30 May 2019 19:44:00 +1000
963458
Subject: [PATCH] PKIExceptionMapper: coerce media type to XML or JSON
963458
963458
Some resources do not return (upon success) application/json or
963458
application/xml.  For example, some resources in AuthorityService
963458
can return application/pkix-cert, application/x-pem-file or
963458
application/pkcs7-mime.  But if a PKIException exception (e.g.
963458
ResourceNotFoundException) occurs in such a method, RESTEasy can't
963458
turn the PKIException.Data entity into the declared media type, and
963458
it throws a NoMessageBodyWriterFoundFailure, causing a 500 Internal
963458
Server Error response.
963458
963458
Update PKIExceptionMapper to always coerce the response Content-Type
963458
to either application/xml or application/json.  If the Accept header
963458
preferences one of these, the preferred media type is used.
963458
Otherwise we default to application/xml.
963458
963458
Fixes: https://pagure.io/dogtagpki/issue/3102
963458
(cherry picked from commit bef30cde3a452a459c5d29010861314dd8b86642)
963458
---
963458
 .../org/dogtagpki/server/rest/PKIExceptionMapper.java | 19 ++++++++++++++++++-
963458
 1 file changed, 18 insertions(+), 1 deletion(-)
963458
963458
diff --git a/base/server/cms/src/org/dogtagpki/server/rest/PKIExceptionMapper.java b/base/server/cms/src/org/dogtagpki/server/rest/PKIExceptionMapper.java
963458
index 072ac05..0cfab44 100644
963458
--- a/base/server/cms/src/org/dogtagpki/server/rest/PKIExceptionMapper.java
963458
+++ b/base/server/cms/src/org/dogtagpki/server/rest/PKIExceptionMapper.java
963458
@@ -2,6 +2,7 @@ package org.dogtagpki.server.rest;
963458
 
963458
 import javax.ws.rs.core.Context;
963458
 import javax.ws.rs.core.HttpHeaders;
963458
+import javax.ws.rs.core.MediaType;
963458
 import javax.ws.rs.core.Response;
963458
 import javax.ws.rs.ext.ExceptionMapper;
963458
 import javax.ws.rs.ext.Provider;
963458
@@ -17,10 +18,26 @@ public class PKIExceptionMapper implements ExceptionMapper<PKIException> {
963458
 
963458
     public Response toResponse(PKIException exception) {
963458
         // convert PKIException into HTTP response
963458
+
963458
+        // The exception Data can only be serialised as XML or JSON,
963458
+        // so coerce the response content type to one of these.
963458
+        // Default to XML, but consider the Accept header.
963458
+        MediaType contentType = MediaType.APPLICATION_XML_TYPE;
963458
+        for (MediaType acceptType : headers.getAcceptableMediaTypes()) {
963458
+            if (acceptType.isCompatible(MediaType.APPLICATION_XML_TYPE)) {
963458
+                contentType = MediaType.APPLICATION_XML_TYPE;
963458
+                break;
963458
+            }
963458
+            if (acceptType.isCompatible(MediaType.APPLICATION_JSON_TYPE)) {
963458
+                contentType = MediaType.APPLICATION_JSON_TYPE;
963458
+                break;
963458
+            }
963458
+        }
963458
+
963458
         return Response
963458
                 .status(exception.getCode())
963458
                 .entity(exception.getData())
963458
-                .type(PKIService.getResponseFormat(headers))
963458
+                .type(contentType)
963458
                 .build();
963458
     }
963458
 }
963458
-- 
963458
1.8.3.1
963458