Paul Sandoz wrote:
> Hi Craig,
>
> It looks like from the code we could do with some Jersey-specific 
> functionality so the application can obtain the most acceptable media 
> type.
>
> I know JAX-RS does not specify this but Jersey does know what is the 
> most acceptable media type is (if any, which may be a wildcard) before 
> the matching resource method is invoked. Thus this could be exposed.
>
I think it would indeed be useful to have an injectable object that 
includes more details, including what media type / language / encoding 
we are producing (i.e. what Variant was selected).
> Paul.
>
Craig
>
> Begin forwarded message:
>
>> *From: *craigmcc_at_dev.java.net <mailto:craigmcc_at_dev.java.net>
>> *Date: *January 9, 2009 4:52:09 AM CEST
>> *To: *commits_at_jersey.dev.java.net <mailto:commits_at_jersey.dev.java.net>
>> *Subject: **svn commit: r1832 - 
>> trunk/jersey/contribs/jersey-atom-abdera/src/test/java/com/sun/jersey/atom/abdera/impl/provider/entity/TestingResource.java*
>> *Reply-To: *commits_at_jersey.dev.java.net 
>> <mailto:commits_at_jersey.dev.java.net>
>>
>> Author: craigmcc
>> Date: 2009-01-09 03:52:05+0000
>> New Revision: 1832
>>
>> Modified:
>>   trunk/jersey/contribs/jersey-atom-abdera/src/test/java/com/sun/jersey/atom/abdera/impl/provider/entity/TestingResource.java
>>
>> Log:
>> Add some logic that determines which of the media types listed in 
>> @Produces
>> we are actually going to be sending.
>>
>>
>> Modified: 
>> trunk/jersey/contribs/jersey-atom-abdera/src/test/java/com/sun/jersey/atom/abdera/impl/provider/entity/TestingResource.java
>> Url: 
>> https://jersey.dev.java.net/source/browse/jersey/trunk/jersey/contribs/jersey-atom-abdera/src/test/java/com/sun/jersey/atom/abdera/impl/provider/entity/TestingResource.java?view=diff&rev=1832&p1=trunk/jersey/contribs/jersey-atom-abdera/src/test/java/com/sun/jersey/atom/abdera/impl/provider/entity/TestingResource.java&p2=trunk/jersey/contribs/jersey-atom-abdera/src/test/java/com/sun/jersey/atom/abdera/impl/provider/entity/TestingResource.java&r1=1831&r2=1832 
>> <https://jersey.dev.java.net/source/browse/jersey/trunk/jersey/contribs/jersey-atom-abdera/src/test/java/com/sun/jersey/atom/abdera/impl/provider/entity/TestingResource.java?view=diff&rev=1832&p1=trunk/jersey/contribs/jersey-atom-abdera/src/test/java/com/sun/jersey/atom/abdera/impl/provider/entity/TestingResource.java&p2=trunk/jersey/contribs/jersey-atom-abdera/src/test/java/com/sun/jersey/atom/abdera/impl/provider/entity/TestingResource.java&r1=1831&r2=1832>
>> ==============================================================================
>> --- 
>> trunk/jersey/contribs/jersey-atom-abdera/src/test/java/com/sun/jersey/atom/abdera/impl/provider/entity/TestingResource.java 
>> (original)
>> +++ 
>> trunk/jersey/contribs/jersey-atom-abdera/src/test/java/com/sun/jersey/atom/abdera/impl/provider/entity/TestingResource.java 
>> 2009-01-09 03:52:05+0000
>> @@ -38,12 +38,18 @@
>> package com.sun.jersey.atom.abdera.impl.provider.entity;
>>
>> import com.sun.jersey.atom.abdera.ContentHelper;
>> +import java.lang.annotation.Annotation;
>> +import java.lang.reflect.Method;
>> +import java.util.ArrayList;
>> +import java.util.List;
>> import javax.ws.rs.GET;
>> import javax.ws.rs.Path;
>> import javax.ws.rs.Produces;
>> import javax.ws.rs.core.Context;
>> import javax.ws.rs.core.MediaType;
>> +import javax.ws.rs.core.Request;
>> import javax.ws.rs.core.Response;
>> +import javax.ws.rs.core.Variant;
>> import org.apache.abdera.model.Entry;
>>
>> /**
>> @@ -55,10 +61,14 @@
>>     @Context
>>     ContentHelper contentHelper;
>>
>> +    @Context
>> +    Request request;
>> +
>>     @GET
>>     @Path("categories")
>>     @Produces({"application/atomcat+xml", "application/xml", 
>> "text/xml", "application/atomcat+json", "application/json"})
>>     public Response getCategories() {
>> +        dumpSelectedMediaType("getCategories");
>>         return Response.ok(TestingFactory.createCategories()).build();
>>     }
>>
>> @@ -66,6 +76,7 @@
>>     @Path("content")
>>     @Produces("application/xml")
>>     public Response getContent() {
>> +        dumpSelectedMediaType("getContent");
>>         Entry entry = TestingFactory.createEntry();
>>         ContentBean bean = new ContentBean("foo value", "bar value");
>>         contentHelper.setContentEntity(entry, 
>> MediaType.APPLICATION_XML_TYPE, bean);
>> @@ -76,6 +87,7 @@
>>     @Path("entry")
>>     @Produces({"application/atom", "application/atom+xml", 
>> "application/xml", "text/xml", "application/atom+json", 
>> "application/json"})
>>     public Response getEntry() {
>> +        dumpSelectedMediaType("getEntry");
>>         return Response.ok(TestingFactory.createEntry()).build();
>>     }
>>
>> @@ -83,6 +95,7 @@
>>     @Path("feed")
>>     @Produces({"application/atom", "application/atom+xml", 
>> "application/xml", "text/xml", "application/atom+json", 
>> "application/json"})
>>     public Response getFeed() {
>> +        dumpSelectedMediaType("getFeed");
>>         return Response.ok(TestingFactory.createFeed()).build();
>>     }
>>
>> @@ -90,7 +103,55 @@
>>     @Path("service")
>>     @Produces({"application/atomsvc+xml", "application/xml", 
>> "text/xml", "application/atomsvc+json", "application/json"})
>>     public Response getService() {
>> +        dumpSelectedMediaType("getService");
>>         return Response.ok(TestingFactory.createService()).build();
>>     }
>>
>> +    private void dumpSelectedMediaType(String methodName) {
>> +        System.out.println("Method " + methodName + " selected media 
>> type " + calculateSelectedMediaType(methodName));
>> +    }
>> +
>> +    /**
>> +     * <p>Calculate and return the media type that we should be 
>> producing
>> +     * on this call, following the same algorithm that JAX-RS does.</p>
>> +     *
>> +     * @param methodName Name of the method (in this class) that was 
>> called
>> +     *
>> +     * @exception IllegalArgumentException if we cannot identify the 
>> appropriate
>> +     *  @Produces annotation for this method name
>> +     */
>> +    private MediaType calculateSelectedMediaType(String methodName) {
>> +        // Reflect to get the Method definition for this methodName
>> +        // The following logic assumes none of the method names are 
>> overloaded,
>> +        // which is true for this class but might not be in general
>> +        Method[] methods = this.getClass().getDeclaredMethods();
>> +        Method method = null;
>> +        for (int i = 0; i < methods.length; i++) {
>> +            if (methodName.equals(methods[i].getName())) {
>> +                method = methods[i];
>> +                break;
>> +            }
>> +        }
>> +        if (method == null) {
>> +            throw new IllegalArgumentException("Cannot find Method 
>> instance for method " + methodName);
>> +        }
>> +        // Look for the @Produces annotation on this method
>> +        Annotation annotation = method.getAnnotation(Produces.class);
>> +        if (annotation == null) {
>> +            throw new IllegalArgumentException("Method " + 
>> methodName + " does not have an @Produces annotation");
>> +        }
>> +        // Build a list of Variants based on the @Produces annotation
>> +        List<Variant> variants = new ArrayList<Variant>();
>> +        String[] items = ((Produces) annotation).value();
>> +        for (String item : items) {
>> +            String[] parts = item.split("/");
>> +            if (parts.length != 2) {
>> +                throw new IllegalArgumentException("Media type " + 
>> item + " on @Produces list for method " + methodName + " is malformed");
>> +            }
>> +            variants.add(new Variant(new MediaType(parts[0], 
>> parts[1]), null, null));
>> +        }
>> +        // Return the media type from the variant that was selected
>> +        return request.selectVariant(variants).getMediaType();
>> +    }
>> +
>> }
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: commits-unsubscribe_at_jersey.dev.java.net 
>> <mailto:commits-unsubscribe_at_jersey.dev.java.net>
>> For additional commands, e-mail: commits-help_at_jersey.dev.java.net 
>> <mailto:commits-help_at_jersey.dev.java.net>
>>
>