users@jersey.java.net

Re: [Jersey] Problem with suffixes mapping to media types

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 21 Aug 2009 09:58:12 +0200

On Aug 20, 2009, at 8:21 PM, Rabick, Mark A (IS) wrote:

> Any ideas
>
Did you miss my previous email:

   http://markmail.org/message/wfhu4yhe4virf7fi?q=list:net.java.dev.jersey.users

I have just committed a fix. Part of the fix was to convert the
functionality into a container request filter:

    com.sun.jersey.api.container.filter.UriConnegFilter

JavaDoc at end of email.

Paul.

/**
  * A URI-based content negotiation filter mapping a dot-declared
suffix in
  * URI to media type that is the value of the <code>Accept</code>
header
  * or a language that is the value of the <code>Accept-Language</
code> header.
  * <p>
  * This filter may be used when the accetable media type and acceptable
  * language need to be declared in the URI.
  * <p>
  * This class may be extended to declare the mappings and the
extending class,
  * <code>foo.MyUriConnegFilter</code> say, can be registered as a
container request
  * filter. When an application is deployed as a Servlet or Filter
such a filter
  * can be registered using the following initialization parameters:
  * <blockquote><pre>
  * &lt;init-param&gt;
  * &lt;param-
name&gt;com.sun.jersey.spi.container.ContainerRequestFilters&lt;/param-
name&gt;
  * &lt;param-value&gt;foo.MyUriConnegFilter&lt;/param-value&gt;
  * &lt;/init-param&gt
  * </pre></blockquote>
  * <p>
  * If a suffix of "atom" is regstered with a media type of
  * "application/atom+xml" then a GET request of:
  * <pre>GET /resource.atom</pre>
  * <p>is transformed to:</p>
  * <pre>GET /resource
  *Accept: application/atom+xml</pre>
  * Any existing "Accept" header value will be replaced.
  * <p>
  * If a suffix of "english: is regstered with a language of
  * "en" then a GET request of:
  * <pre>GET /resource.english</pre>
  * <p>is transformed to:</p>
  * <pre>GET /resource
  *Accept-Language: en</pre>
  * Any existing "Accept-Language"header value will be replaced.
  * <p>
  * The media type mappings are processed before the language type
mappings.
  *
  * @author Paul.Sandoz_at_Sun.Com
  * @see com.sun.jersey.api.container.filter
  */


> I have mapped 3 suffixes to media types for content type negotiation
> and only 2 of the 3 are working. The resource is:
>
> @ImplicitProduces
> ({CnodbMediaType.APPLICATION_RDF_XML,MediaType.APPLICATION_XML})
>
> @Singleton
>
> @Path("v1/ontology")
>
> public class OntologyResource extends BaseResource {
>
> private byte[] rdfRepresentation;
>
> private byte[] ntripleRepresentation;
>
> private byte[] n3Representation;
>
>
>
> private @javax.ws.rs.core.Context HttpHeaders httpHeaders;
>
> /**
>
> * No-argument Constructor.
>
> * @throws Exception
>
> * @throws NamingException
>
> */
>
> public OntologyResource() throws NamingException, Exception {
>
> rdfRepresentation = null;
>
> ntripleRepresentation = null;
>
> javax.naming.Context context = new InitialContext();
>
> new CnodbModelDb(context);
>
> }
>
> /**
>
> * Retrieve an RDF representation of the current CNODB
> ontology with optional
>
> * instance data..
>
> * @param getInstanceData If true, return the instance
> data as well.
>
> * @param ignoreCache If true, query ontology from
> database, not the stored version.
>
> *
>
> * @response.representation.200.mediaType application/rdf+xml
>
> * @response.representation.200.doc This is the
> representation returned by default.
>
> *
>
> * @return A response containing the ontology and
> instance data if requested.
>
> */
>
> @GET //_at_Path("rdf")
>
> @Produces(CnodbMediaType.APPLICATION_RDF_XML)
>
> public synchronized Response getRDF(
>
> @QueryParam("ind") @DefaultValue("false")
> Boolean getInstanceData,
>
> @QueryParam("ignoreCache")
> @DefaultValue("false") Boolean ignoreCache)
>
> {
>
> System.out.println("getRDF - getInstanceData=" +
> getInstanceData + " ignoreCache=" + ignoreCache);
>
> // commented out for brevity
>
> }
>
> @GET
>
> @Produces(CnodbMediaType.TEXT_NTRIPLE)
>
> public synchronized Response getNTRIPLE(
>
> @QueryParam("ind") @DefaultValue("false")
> Boolean getInstanceData,
>
> @QueryParam("ignoreCache")
> @DefaultValue("false") Boolean ignoreCache)
>
> {
>
> System.out.println("getNTRIPLE - getInstanceData=" +
> getInstanceData + " ignoreCache=" + ignoreCache);
>
> System.out.println("httpHeaders acceptable media
> types: " + httpHeaders.getAcceptableMediaTypes());
>
> // commented out for brevity
>
> }
>
>
>
> @GET
>
> @Produces(CnodbMediaType.TEXT_N3)
>
> public synchronized Response getN3(
>
> @QueryParam("ind") @DefaultValue("false")
> Boolean getInstanceData,
>
> @QueryParam("ignoreCache")
> @DefaultValue("false") Boolean ignoreCache)
>
> {
>
> System.out.println("getN3 - getInstanceData=" +
> getInstanceData + " ignoreCache=" + ignoreCache);
>
> // commented out for brevity
>
> }
>
> }
>
> The CnodbMediaType-s are from this file:
>
> public class CnodbMediaType {
>
>
>
> public static final String TEXT_N3 = "text/n3";
>
> public static final MediaType TEXT_N3_TYPE = new
> MediaType("text","n3");
>
> public final static String APPLICATION_RDF_XML =
> "application/rdf+xml";
>
> public final static MediaType APPLICATION_RDF_XML_TYPE = new
> MediaType("application","rdf+xml");
>
> public static final String TEXT_NTRIPLE = "text/ntriple";
>
> public static final MediaType TEXT_NTRIPLE_TYPE = new
> MediaType("text","ntriple");
>
> }
>
> My Package resource configuration file is mapping the extension
> “rdf” to application/rdf+xml, “nt” to text/ntriple and“n3” to text/
> n3 as follows:
>
> public class CnodbRestResourceConfig extends PackagesResourceConfig {
>
> /**
>
> * This constructor defines the root package for the Jersey
> runtime
>
> * to use to find resource end-points.
>
> */
>
> public CnodbRestResourceConfig() {
>
> super("mil.cnodb.rs");
>
> }
>
>
>
> /**
>
> * This method returns the current media type mappings that
> translate a
>
> * file extension on a URI into a desired {_at_link MediaType}.
>
> * @return The mappings from extension to media
> type.
>
> *
>
> * @see
> com.sun.jersey.api.core.DefaultResourceConfig#getMediaTypeMappings()
>
> */
>
> @Override
>
> public Map<String, MediaType> getMediaTypeMappings() {
>
> Map<String, MediaType> m = new HashMap<String, MediaType>();
>
> m.put("xml", MediaType.APPLICATION_XML_TYPE);
>
> m.put("json", MediaType.APPLICATION_JSON_TYPE);
>
> m.put("html", MediaType.TEXT_HTML_TYPE);
>
> m.put("n3", CnodbMediaType.TEXT_N3_TYPE);
>
> m.put("rdf", CnodbMediaType.APPLICATION_RDF_XML_TYPE);
>
> m.put("nt", CnodbMediaType.TEXT_NTRIPLE_TYPE);
>
> return m;
>
> }
>
> }
>
> If I specify a URL: http://localhost:7001/cnodb/rest/v1/
> ontology.rdf or http://localhost:7001/cnodb/rest/v1/ontology.n3the
> appropriate resource methods are called but if I specify http://localhost:7001/cnodb/rest/v1/ontology.nt
> , I don’t get a response and the resource method getNTRIPLE(…) doea
> not appear to get called. Is there a way I can intercept the HTTP
> request to view the ‘accept’ header and see if in fact the .nt
> suffix is being mapped properly?
>
> --mark
>
> _______________________________________________
> Mark A. Rabick
> Software Engineer
> Northrop Grumman - Integrated Mission Systems (IS/DSD/IMS)
> 3200 Samson Way
> Bellevue, NE 68123
> Em: mark.rabick_at_ngc.com
> Remember PFC Ross A. McGinnis...
> http://www.army.mil/medalofhonor/McGinnis/index.html
> ... MA2 Michael A. Monsoor, Lt. Michael P. Murphy, Cpl. Jason
> Dunham, SFC Paul Ray Smith and the rest...
> http://www.cmohs.org/recipients/most_recent.htm
>