summaryrefslogtreecommitdiffstats
path: root/base/server/cms/src
diff options
context:
space:
mode:
authorEndi S. Dewata <edewata@redhat.com>2014-02-10 13:56:15 -0500
committerEndi S. Dewata <edewata@redhat.com>2014-03-04 22:41:46 -0500
commit56b9ead088c59ea76d796002e74d42d7e31eac44 (patch)
tree38473afc8d126936ef806b44af2aae01f2ab8fcd /base/server/cms/src
parent9e14d6f3e5e69093067f0a76e5b7090c102e11d3 (diff)
Added CLI parameter to select message format.
A new CLI parameter has been added to allow the user select the REST message format. This is done by setting the default consumes and produces when creating the client proxy. For this to work the hard-coded @Consumes and @Produces annotations need to be removed from the interface definition. A new interceptor has been added to validate the message format before executing the operation. Ticket #554
Diffstat (limited to 'base/server/cms/src')
-rw-r--r--base/server/cms/src/com/netscape/cms/servlet/base/PKIService.java41
-rw-r--r--base/server/cms/src/org/dogtagpki/server/rest/MessageFormatInterceptor.java77
2 files changed, 118 insertions, 0 deletions
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 6550715f5..27dea60db 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,13 +28,16 @@ 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;
import com.netscape.certsrv.apps.CMS;
+import com.netscape.certsrv.base.PKIException;
import com.netscape.certsrv.cert.CertData;
import com.netscape.certsrv.logging.IAuditor;
import com.netscape.certsrv.logging.ILogger;
@@ -52,18 +56,53 @@ public class PKIService {
// caching parameters
public static final int DEFAULT_LONG_CACHE_LIFETIME = 1000;
+ public static List<MediaType> MESSAGE_FORMATS = Arrays.asList(
+ MediaType.APPLICATION_XML_TYPE,
+ MediaType.APPLICATION_JSON_TYPE
+ );
+
+ @Context
+ private HttpHeaders headers;
+
public ILogger logger = CMS.getLogger();
public IAuditor auditor = CMS.getAuditor();
+ public static MediaType resolveFormat(MediaType format) {
+
+ for (MediaType supportedFormat : MESSAGE_FORMATS) {
+ if (format.isCompatible(supportedFormat)) return supportedFormat;
+ }
+
+ return null;
+ }
+
+ public static MediaType resolveFormat(List<MediaType> formats) {
+
+ for (MediaType acceptableFormat : formats) {
+ MediaType supportedFormat = resolveFormat(acceptableFormat);
+ if (supportedFormat != null) return supportedFormat;
+ }
+
+ return null;
+ }
+
+ public MediaType getResponseFormat() {
+ MediaType responseFormat = resolveFormat(headers.getAcceptableMediaTypes());
+ if (responseFormat == null) throw new PKIException(Response.Status.NOT_ACCEPTABLE);
+ return responseFormat;
+ }
+
public Response createOKResponse() {
return Response
.ok()
+ .type(getResponseFormat())
.build();
}
public Response createOKResponse(Object entity) {
return Response
.ok(entity)
+ .type(getResponseFormat())
.build();
}
@@ -71,12 +110,14 @@ public class PKIService {
return Response
.created(link)
.entity(entity)
+ .type(getResponseFormat())
.build();
}
public Response createNoContentResponse() {
return Response
.noContent()
+ .type(getResponseFormat())
.build();
}
diff --git a/base/server/cms/src/org/dogtagpki/server/rest/MessageFormatInterceptor.java b/base/server/cms/src/org/dogtagpki/server/rest/MessageFormatInterceptor.java
new file mode 100644
index 000000000..6734efe1d
--- /dev/null
+++ b/base/server/cms/src/org/dogtagpki/server/rest/MessageFormatInterceptor.java
@@ -0,0 +1,77 @@
+//--- BEGIN COPYRIGHT BLOCK ---
+//This program is free software; you can redistribute it and/or modify
+//it under the terms of the GNU General Public License as published by
+//the Free Software Foundation; version 2 of the License.
+//
+//This program is distributed in the hope that it will be useful,
+//but WITHOUT ANY WARRANTY; without even the implied warranty of
+//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+//GNU General Public License for more details.
+//
+//You should have received a copy of the GNU General Public License along
+//with this program; if not, write to the Free Software Foundation, Inc.,
+//51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+//
+//(C) 2014 Red Hat, Inc.
+//All rights reserved.
+//--- END COPYRIGHT BLOCK ---
+package org.dogtagpki.server.rest;
+
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.util.List;
+
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.container.ContainerRequestContext;
+import javax.ws.rs.container.ContainerRequestFilter;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.Provider;
+
+import org.jboss.resteasy.core.ResourceMethodInvoker;
+
+import com.netscape.certsrv.apps.CMS;
+import com.netscape.cms.servlet.base.PKIService;
+
+/**
+ * @author Endi S. Dewata
+ */
+@Provider
+public class MessageFormatInterceptor implements ContainerRequestFilter {
+
+ @Override
+ public void filter(ContainerRequestContext requestContext) throws IOException {
+ ResourceMethodInvoker methodInvoker = (ResourceMethodInvoker) requestContext
+ .getProperty("org.jboss.resteasy.core.ResourceMethodInvoker");
+ Method method = methodInvoker.getMethod();
+ Class<?> clazz = methodInvoker.getResourceClass();
+
+ CMS.debug("MessageFormatInterceptor: " + clazz.getSimpleName() + "." + method.getName() + "()");
+
+ MediaType contentType = requestContext.getMediaType();
+ CMS.debug("MessageFormatInterceptor: content-type: " + contentType);
+
+ if (contentType != null) {
+ MediaType requestFormat = PKIService.resolveFormat(contentType);
+
+ if (requestFormat == null) {
+ throw new WebApplicationException(Response.Status.UNSUPPORTED_MEDIA_TYPE);
+ }
+
+ CMS.debug("MessageFormatInterceptor: request format: " + requestFormat);
+ }
+
+ List<MediaType> acceptableFormats = requestContext.getAcceptableMediaTypes();
+ CMS.debug("MessageFormatInterceptor: acceptable formats: " + acceptableFormats);
+
+ if (acceptableFormats != null) {
+ MediaType responseFormat = PKIService.resolveFormat(acceptableFormats);
+
+ if (responseFormat == null) {
+ throw new WebApplicationException(Response.Status.NOT_ACCEPTABLE);
+ }
+
+ CMS.debug("MessageFormatInterceptor: response format: " + responseFormat);
+ }
+ }
+}