diff options
| author | Endi S. Dewata <edewata@redhat.com> | 2014-02-10 13:56:15 -0500 |
|---|---|---|
| committer | Endi S. Dewata <edewata@redhat.com> | 2014-03-04 22:41:46 -0500 |
| commit | 56b9ead088c59ea76d796002e74d42d7e31eac44 (patch) | |
| tree | 38473afc8d126936ef806b44af2aae01f2ab8fcd /base/server/cms/src/org | |
| parent | 9e14d6f3e5e69093067f0a76e5b7090c102e11d3 (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/org')
| -rw-r--r-- | base/server/cms/src/org/dogtagpki/server/rest/MessageFormatInterceptor.java | 77 |
1 files changed, 77 insertions, 0 deletions
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); + } + } +} |
