Re: Multiple content types
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>]
RE: Re: Multiple content types
Hi Sherif, Could you send us the trace of your HTTP request/response exchange including HTTP headers? I suscept that you "Accept" header might not be correct. Best regards, Jerome Louvel -- Restlet ~ Founder and Lead developer ~ http://www.restlet.org Noelios Technologies ~ Co-founder ~ http://www.noelios.com -Message d'origine- De : Sherif Ahmed [mailto:sherifah...@hotmail.com] Envoyé : mercredi 1 juillet 2009 20:00 À : discuss@restlet.tigris.org Objet : RE: Re: Multiple content types 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 >
RE: Re: Multiple content types
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
Re: Multiple content types
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=2360443
RE: Multiple content types
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...@n2.nabble.com] 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/TunnelServi ce.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 <http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2359 775> &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: http://n2.nabble.com/Multiple-content-types-tp3031817p3043073.html Sent from the Restlet Discuss mailing list archive at Nabble.com. -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2360290
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