Hello Sherif,

I've tried with your sample code, and it works for me.
I've just changed this piece of code, in the "represent" method:

MediaType mediaType = variant.getMediaType();
if (MediaType.APPLICATION_JSON.equals(mediaType))

I send you my sample code, that, I've tested it with Restlet 1.1 and 
Restlet 1.2 (current trunk in both cases) .

Best regards,
Thierry Boileau
> Okay..
> I had to add an XLS Representation for a resource and I wan to support a 
> suffix of .json for JSON and .XLS for Excel , but using the code below (and 
> rest of the code in the previous thread).
>
> no matter what extension I use (even other than XLS OR JSON). the 
> Variant.Representation in my represent(Variant variant) method in the 
> resource class always picks up JSON as the type..
>
> Thanks
>
> public HelloWorldResource(Context context, Request request, Response 
> response) {
>
>  super(context, request, response);
>               getVariants().add(new Variant(MediaType.APPLICATION_EXCEL));
>               getVariants().add(new Variant(MediaType.APPLICATION_JSON));
>
> }
>
>
>   
>> Hi Sherif,
>>
>> .js is javascript
>> .json is json :0)
>>
>> There is no need to add js or json to the MetadataService as it comes 
>> with a bunch of common ones already.
>> Remove that line, and try
>>
>> http://localhost:8080/firstStepsServlet/Hello.json (Notice the JSON)
>>
>>
>> Jon
>>
>> Sherif wrote:
>>     
>>> Thanks Jonathan..
>>>
>>> 1. Here’s what I did to support lets say “.js� as an extension to 
>>> indicate JSON as the content type
>>>
>>> *public* *class* FirstStepsApplication *extends* Application {
>>>
>>> /**
>>>
>>> * Creates a root Restlet that will receive all incoming calls.
>>>
>>> */
>>>
>>> @Override
>>>
>>> *public* Restlet createRoot() {
>>>
>>> // Create a router Restlet that routes each call to a
>>>
>>> // new instance of HelloWorldResource.
>>>
>>> Router router = *new* Router(getContext());
>>>
>>> // Defines only one route
>>>
>>> router.attachDefault(HelloWorldResource.*class*);
>>>
>>> *this*.getTunnelService().setExtensionsTunnel(*true*);
>>>
>>> *this*.getTunnelService().setEncodingParameter("output");
>>>
>>> *this*.getMetadataService().addExtension("js", *new* 
>>> Metadata(MediaType./APPLICATION_JSON/.getName(), 
>>> MediaType./APPLICATION_JSON/.getDescription()), *true* );
>>>
>>> *return* router;
>>>
>>> }
>>>
>>> }
>>>
>>> *public* *class* HelloWorldResource *extends* Resource {
>>>
>>> *public* HelloWorldResource(Context context, Request request, Response 
>>> response) {
>>>
>>> *super*(context, request, response);
>>>
>>> // This representation has only one type of representation.
>>>
>>> getVariants().add(*new* Variant(MediaType./TEXT_PLAIN/));
>>>
>>> getVariants().add(*new* Variant(MediaType./APPLICATION_JSON/));
>>>
>>> }
>>>
>>> /**
>>>
>>> * Returns a full representation for a given variant.
>>>
>>> */
>>>
>>> @Override
>>>
>>> *public* Representation represent(Variant variant) *throws* 
>>> ResourceException {
>>>
>>> Representation representation = *null*;
>>>
>>> *if*(variant.getMediaType() == MediaType./APPLICATION_JSON/){
>>>
>>> representation = *new* JsonRepresentation("Hello World");
>>>
>>> }*else* *if* ( variant.getMediaType() == MediaType./TEXT_PLAIN/){
>>>
>>> representation = *new* StringRepresentation(
>>>
>>> "hello, world", MediaType./TEXT_PLAIN/);
>>>
>>> }
>>>
>>> *return* representation;
>>>
>>> }
>>>
>>> }
>>>
>>> However when I call the service using the following URL 
>>> http://localhost:8080/firstStepsServlet/Hello.js the Variant type in 
>>> the HelloWorldResource is of MediaType.TEXT_PLAIN
>>>
>>> Thanks for Your help
>>>
>>> *From:* Jonathan Hall (via Nabble) 
>>> [mailto:ml-user+125526-1692215...@... 
>>> <http://n2.nabble.com/user/SendEmail.jtp?type=node&node=3043073&i=0>]
>>> *Sent:* Friday, June 05, 2009 1:29 PM
>>> *To:* Sherif
>>> *Subject:* Re: Multiple content types
>>>
>>> Have a look at
>>> http://www.restlet.org/documentation/2.0/api/org/restlet/service/TunnelService.html
>>>
>>> getTunnelService().setExtensionsTunnel(true);
>>>
>>> Jon
>>>
>>> Sherif wrote:
>>>       
>>>> I realize RESTLet supports multiple encodings based on the Accept 
>>>>         
>>> Encoding
>>>       
>>>> headers. Does Restlet also have a way to allow encodings based on 
>>>>         
>>> URI patter
>>>       
>>>> e.g. http://mystores/items/1000.json or something like that ?
>>>>
>>>>         
>>> ------------------------------------------------------
>>> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2359775
>>>  
>>> <http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2359775>
>>>
>>> ------------------------------------------------------------------------
>>>
>>> This email is a reply to your post @ 
>>> http://n2.nabble.com/Multiple-content-types-tp3031817p3031841.html
>>> You can reply by email or by visting the link above.
>>>
>>>
>>> ------------------------------------------------------------------------
>>> View this message in context: RE: Multiple content types 
>>> <http://n2.nabble.com/Multiple-content-types-tp3031817p3043073.html>
>>> Sent from the Restlet Discuss mailing list archive 
>>> <http://n2.nabble.com/Restlet-Discuss-f1400322.html> at Nabble.com.
>>>       
>
> ------------------------------------------------------
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2367141
>
>

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2368389
package conneg;

import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;


public class HelloWorldResource extends Resource {

    public HelloWorldResource(Context context, Request request,
            Response response) {
        super(context, request, response);
        getVariants().add(new Variant(MediaType.APPLICATION_EXCEL));
        getVariants().add(new Variant(MediaType.APPLICATION_JSON));
    }

    /**
     * 
     * Returns a full representation for a given variant.
     */

    @Override
    public Representation represent(Variant variant) throws ResourceException {
        Representation representation = null;
        MediaType mediaType = variant.getMediaType();
        if (MediaType.APPLICATION_JSON.equals(mediaType)) {
            representation = new JsonRepresentation("Hello World - json");
        } else if (MediaType.TEXT_PLAIN.equals(mediaType)) {
            representation = new StringRepresentation("hello, world - text",
                    MediaType.TEXT_PLAIN);
        } else if (MediaType.APPLICATION_EXCEL.equals(mediaType)) {
            representation = new StringRepresentation("hello, world - excel",
                    MediaType.TEXT_PLAIN);
        }

        return representation;
    }

}
package conneg;

import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.Router;
import org.restlet.data.Protocol;

public class MyApplication extends Application {
    /**
     * Creates a root Restlet that will receive all incoming calls.
     */

    @Override
    public Restlet createRoot() {
        // Create a router Restlet that routes each call to a
        // new instance of HelloWorldResource.
        Router router = new Router(getContext());
        // Defines only one route
        router.attachDefault(HelloWorldResource.class);

        this.getTunnelService().setExtensionsTunnel(true);
        this.getTunnelService().setEncodingParameter("output");

        return router;
    }
    
    public static void main(String[] args) throws Exception {
        Component component = new Component();
        component.getServers().add(Protocol.HTTP, 8182);
        
        component.getDefaultHost().attach(new MyApplication());
        component.start();
    }

}

Reply via email to