From 9db9db78e750e8f93c36080642e8d5fee2ec7b09 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Mon, 10 Feb 2014 13:56:15 -0500
Subject: [PATCH] Added CLI parameter to select message format.

A new CLI parameter has been added to specify the format of the HTTP
messages exchanged between the client and server. This is done by
setting the default consumes and produces when creating the proxy.
For this to work the @Consumes and @Produces annotations will be
removed in the subsequent patch.

Ticket #554
---
 .../com/netscape/certsrv/client/ClientConfig.java  | 16 +++++++++++++
 .../com/netscape/certsrv/client/PKIConnection.java | 18 ++++++++++++++-
 .../src/com/netscape/cmstools/cli/MainCLI.java     |  8 +++++++
 .../com/netscape/cms/servlet/base/PKIService.java  | 27 ++++++++++++++++++++++
 4 files changed, 68 insertions(+), 1 deletion(-)

diff --git a/base/common/src/com/netscape/certsrv/client/ClientConfig.java b/base/common/src/com/netscape/certsrv/client/ClientConfig.java
index c85db52d3b0ea8dd13d451e56b7c45d38b855126..da4e3d780f1aa01a88061ed9af868cb4097b63a5 100644
--- a/base/common/src/com/netscape/certsrv/client/ClientConfig.java
+++ b/base/common/src/com/netscape/certsrv/client/ClientConfig.java
@@ -55,6 +55,7 @@ public class ClientConfig {
     String certPassword;
     String username;
     String password;
+    String messageFormat;
 
     @XmlElement(name="ServerURI")
     public URI getServerURI() {
@@ -123,6 +124,15 @@ public class ClientConfig {
         this.password = password;
     }
 
+    @XmlElement(name="MessageFormat")
+    public String getMessageFormat() {
+        return messageFormat;
+    }
+
+    public void setMessageFormat(String messageFormat) {
+        this.messageFormat = messageFormat;
+    }
+
     @Override
     public int hashCode() {
         final int prime = 31;
@@ -130,6 +140,7 @@ public class ClientConfig {
         result = prime * result + ((certDatabase == null) ? 0 : certDatabase.hashCode());
         result = prime * result + ((certNickname == null) ? 0 : certNickname.hashCode());
         result = prime * result + ((certPassword == null) ? 0 : certPassword.hashCode());
+        result = prime * result + ((messageFormat == null) ? 0 : messageFormat.hashCode());
         result = prime * result + ((password == null) ? 0 : password.hashCode());
         result = prime * result + ((serverURI == null) ? 0 : serverURI.hashCode());
         result = prime * result + ((username == null) ? 0 : username.hashCode());
@@ -160,6 +171,11 @@ public class ClientConfig {
                 return false;
         } else if (!certPassword.equals(other.certPassword))
             return false;
+        if (messageFormat == null) {
+            if (other.messageFormat != null)
+                return false;
+        } else if (!messageFormat.equals(other.messageFormat))
+            return false;
         if (password == null) {
             if (other.password != null)
                 return false;
diff --git a/base/common/src/com/netscape/certsrv/client/PKIConnection.java b/base/common/src/com/netscape/certsrv/client/PKIConnection.java
index 1c3e58a1be5d4c6a679c39e7e01547324f86724a..30df41f1a15ca263df56e503101e3dbca3ef8413 100644
--- a/base/common/src/com/netscape/certsrv/client/PKIConnection.java
+++ b/base/common/src/com/netscape/certsrv/client/PKIConnection.java
@@ -23,6 +23,7 @@ import java.util.HashSet;
 import java.util.List;
 
 import javax.ws.rs.client.Entity;
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
 
@@ -511,7 +512,22 @@ public class PKIConnection {
 
     public <T> T createProxy(URI uri, Class<T> clazz) throws URISyntaxException {
         ResteasyWebTarget target = resteasyClient.target(uri);
-        return ProxyBuilder.builder(clazz, target).build();
+        ProxyBuilder<T> builder = ProxyBuilder.builder(clazz, target);
+
+        String messageFormat = config.getMessageFormat();
+        MediaType contentType;
+
+        if (messageFormat == null) {
+            contentType = MediaType.APPLICATION_XML_TYPE;
+
+        } else {
+            contentType = MediaType.valueOf("application/" + messageFormat);
+        }
+
+        builder.defaultConsumes(contentType);
+        builder.defaultProduces(contentType);
+
+        return builder.build();
     }
 
     public <T> T getEntity(Response response, Class<T> clazz) {
diff --git a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java
index 082c8140b9f482a579b95a0afce747de2eb97272..9c4f2ec6ff0b2704b6def947612f04d01871c9df 100644
--- a/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java
+++ b/base/java-tools/src/com/netscape/cmstools/cli/MainCLI.java
@@ -185,6 +185,10 @@ public class MainCLI extends CLI {
         option.setArgName("list");
         options.addOption(option);
 
+        option = new Option(null, "message-format", true, "Mesage format: xml (default), json");
+        option.setArgName("format");
+        options.addOption(option);
+
         options.addOption("v", false, "Verbose");
         options.addOption(null, "help", false, "Help");
         options.addOption(null, "version", false, "Version");
@@ -252,6 +256,10 @@ public class MainCLI extends CLI {
         }
 
         if (verbose) System.out.println("Security database: "+this.certDatabase.getAbsolutePath());
+
+        String messageFormat = cmd.getOptionValue("message-format");
+        config.setMessageFormat(messageFormat);
+        if (verbose) System.out.println("Message format: " + messageFormat);
     }
 
     public void convertCertStatusList(String list, Collection<Integer> statuses) throws Exception {
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 6550715f581666551ec1b9807b816a1b576b12d4..a2ffcfca31d2087474a5d841ce3c9df67b236fb1 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
@@ -20,6 +20,7 @@ package com.netscape.cms.servlet.base;
 import java.lang.reflect.Method;
 import java.net.URI;
 import java.security.cert.CertificateEncodingException;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
@@ -27,8 +28,10 @@ import java.util.Map;
 
 import javax.ws.rs.FormParam;
 import javax.ws.rs.core.CacheControl;
+import javax.ws.rs.core.Context;
 import javax.ws.rs.core.EntityTag;
 import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Request;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.ResponseBuilder;
@@ -52,18 +55,40 @@ public class PKIService {
     // caching parameters
     public static final int DEFAULT_LONG_CACHE_LIFETIME = 1000;
 
+    @Context
+    private HttpHeaders headers;
+
     public ILogger logger = CMS.getLogger();
     public IAuditor auditor = CMS.getAuditor();
 
+    public List<MediaType> messageFormats = Arrays.asList(
+            MediaType.APPLICATION_XML_TYPE,
+            MediaType.APPLICATION_JSON_TYPE
+    );
+
+    public MediaType getMessageFormat() {
+
+        for (MediaType acceptableFormat : headers.getAcceptableMediaTypes()) {
+            if (messageFormats.contains(acceptableFormat)) return acceptableFormat;
+        }
+
+        MediaType requestFormat = headers.getMediaType();
+        if (messageFormats.contains(requestFormat)) return requestFormat;
+
+        return messageFormats.get(0);
+    }
+
     public Response createOKResponse() {
         return Response
                 .ok()
+                .type(getMessageFormat())
                 .build();
     }
 
     public Response createOKResponse(Object entity) {
         return Response
                 .ok(entity)
+                .type(getMessageFormat())
                 .build();
     }
 
@@ -71,12 +96,14 @@ public class PKIService {
         return Response
                 .created(link)
                 .entity(entity)
+                .type(getMessageFormat())
                 .build();
     }
 
     public Response createNoContentResponse() {
         return Response
                 .noContent()
+                .type(getMessageFormat())
                 .build();
     }
 
-- 
1.8.4.2

