Re: Representation for multiple types?
Sure. With our particular use case we specify a map of media types to data serializers in spring as a hash map. We then inject this map into each resource. When the map is set in the class we then add all the media types specified as map keys as acceptable variants. When the represent() function is called we use the provided media type to retrieve the associated serializer, throwing an unsupported media type error if the variant has no corresponding serializer. We then serialize our data and send it back to the client. It's the sending it back to the client part is where I am stuck. As there is no default Representation class that I can merely pass in any object type to whether it be string, bye array, etc. Here is an example of the Resource class. public class MyResource extends Resource { public Map map; public Representation represent(Variant variant) throws ResourceException { Serializer serializer = map.get(variant.getMediaType()); if (serializer == null) throw new ResourceException(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE); return new Representation(serializer.encode(data)); // ?? } } Here is an example of our spring applicationContext.xml. ... ... On Fri, Jan 22, 2010 at 3:07 PM, Matt Kennedy wrote: > I'm not a Spring user, so maybe that's why it isn't clear to me what you're > asking. Can you try phrasing this question a different way? I think I know > the answer to your problem because I've always had to deal with multiple > media types for the same resource, but I'm not sure I understand the > problem. Can you walk through a use-case? > > > On Jan 22, 2010, at 6:00 PM, Jean-Philippe Steinmetz wrote: > > Thank you for the quick reply. This certainly would resolve the issue but > it doesn't really offer the kind of solution I was hoping for. With this > solution i'd still have to do a mapping of media type to some representation > which is what I am trying to avoid. The thought was that there is a > Representation that just outputs the message body regardless of whatever > media type variant is set for it. That way I don't have to make a mapping of > media type to anything. > > On Fri, Jan 22, 2010 at 1:27 PM, Matt Kennedy wrote: > >> I can't remember what 1.1.6's API looks like, but I do something like this >> with the 2.0 API. Basically, in the Resource's constructor, I use someting >> like: >> >> getVariants().add(new MyXMLRepresentation(this)) >> getVariants().add(new MyJSONRepresentation(this)) >> >> >> Each of those My* classes are a subclass of Representation, and the >> write() method is overridden to produce the custom media type. I pass the >> Resource object to the custom representation so that I have access to the >> resource during the write method. Also important is that in the >> constructor, you call the super constructor with the MediaType and resource >> as arguments, ex: >> >> //Constructor >> public MyJSONRepresentation(Resource res) >> { >> super(MediaType.APPLICATION_JSON, res); >> } >> >> I know something like this can be done with the 1.1.6 API because I was >> using it before I ported to 2.0, but now I can't find the 1.1.6 code. It >> probably isn't too different. >> >> On Jan 22, 2010, at 3:50 PM, Jean-Philippe Steinmetz wrote: >> >> > Hello all, >> > >> > We're using restlet 1.1.6 and for all our resources we want to support a >> dynamic set of media types. We do this in our Spring configuration by >> setting up a list of acceptable media types. My question is if there is a >> Representation class I can use that will essentially accept any type of data >> I push into it? For instance we support HTML, XML, JSON and AMF3 therefore >> most of the time it's a string based representation but in the case of AMF3 >> it is binary (byte array). So far the only way I see it is to make a switch >> statement in our resource class for each media type but this pretty much >> defeats the purpose of specifying supported media types in the application >> configuration. >> > >> > Jean-Philippe >> >> -- >> >> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441312 >> > > > -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441345
Re: Representation for multiple types?
I'm not a Spring user, so maybe that's why it isn't clear to me what you're asking. Can you try phrasing this question a different way? I think I know the answer to your problem because I've always had to deal with multiple media types for the same resource, but I'm not sure I understand the problem. Can you walk through a use-case? On Jan 22, 2010, at 6:00 PM, Jean-Philippe Steinmetz wrote: > Thank you for the quick reply. This certainly would resolve the issue but it > doesn't really offer the kind of solution I was hoping for. With this > solution i'd still have to do a mapping of media type to some representation > which is what I am trying to avoid. The thought was that there is a > Representation that just outputs the message body regardless of whatever > media type variant is set for it. That way I don't have to make a mapping of > media type to anything. > > On Fri, Jan 22, 2010 at 1:27 PM, Matt Kennedy wrote: > I can't remember what 1.1.6's API looks like, but I do something like this > with the 2.0 API. Basically, in the Resource's constructor, I use someting > like: > > getVariants().add(new MyXMLRepresentation(this)) > getVariants().add(new MyJSONRepresentation(this)) > > > Each of those My* classes are a subclass of Representation, and the write() > method is overridden to produce the custom media type. I pass the Resource > object to the custom representation so that I have access to the resource > during the write method. Also important is that in the constructor, you call > the super constructor with the MediaType and resource as arguments, ex: > > //Constructor > public MyJSONRepresentation(Resource res) > { > super(MediaType.APPLICATION_JSON, res); > } > > I know something like this can be done with the 1.1.6 API because I was using > it before I ported to 2.0, but now I can't find the 1.1.6 code. It probably > isn't too different. > > On Jan 22, 2010, at 3:50 PM, Jean-Philippe Steinmetz wrote: > > > Hello all, > > > > We're using restlet 1.1.6 and for all our resources we want to support a > > dynamic set of media types. We do this in our Spring configuration by > > setting up a list of acceptable media types. My question is if there is a > > Representation class I can use that will essentially accept any type of > > data I push into it? For instance we support HTML, XML, JSON and AMF3 > > therefore most of the time it's a string based representation but in the > > case of AMF3 it is binary (byte array). So far the only way I see it is to > > make a switch statement in our resource class for each media type but this > > pretty much defeats the purpose of specifying supported media types in the > > application configuration. > > > > Jean-Philippe > > -- > http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441312 > -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441343
Re: Representation for multiple types?
Thank you for the quick reply. This certainly would resolve the issue but it doesn't really offer the kind of solution I was hoping for. With this solution i'd still have to do a mapping of media type to some representation which is what I am trying to avoid. The thought was that there is a Representation that just outputs the message body regardless of whatever media type variant is set for it. That way I don't have to make a mapping of media type to anything. On Fri, Jan 22, 2010 at 1:27 PM, Matt Kennedy wrote: > I can't remember what 1.1.6's API looks like, but I do something like this > with the 2.0 API. Basically, in the Resource's constructor, I use someting > like: > > getVariants().add(new MyXMLRepresentation(this)) > getVariants().add(new MyJSONRepresentation(this)) > > > Each of those My* classes are a subclass of Representation, and the write() > method is overridden to produce the custom media type. I pass the Resource > object to the custom representation so that I have access to the resource > during the write method. Also important is that in the constructor, you > call the super constructor with the MediaType and resource as arguments, ex: > > //Constructor > public MyJSONRepresentation(Resource res) > { > super(MediaType.APPLICATION_JSON, res); > } > > I know something like this can be done with the 1.1.6 API because I was > using it before I ported to 2.0, but now I can't find the 1.1.6 code. It > probably isn't too different. > > On Jan 22, 2010, at 3:50 PM, Jean-Philippe Steinmetz wrote: > > > Hello all, > > > > We're using restlet 1.1.6 and for all our resources we want to support a > dynamic set of media types. We do this in our Spring configuration by > setting up a list of acceptable media types. My question is if there is a > Representation class I can use that will essentially accept any type of data > I push into it? For instance we support HTML, XML, JSON and AMF3 therefore > most of the time it's a string based representation but in the case of AMF3 > it is binary (byte array). So far the only way I see it is to make a switch > statement in our resource class for each media type but this pretty much > defeats the purpose of specifying supported media types in the application > configuration. > > > > Jean-Philippe > > -- > > http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441312 > -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441339
RE: Re: Restlet atom feed validation
Thanks, Which version is that? I've just updated to 1.1.7 but that's still gving me the same problem. Regards, Bedwyr > Hello, > > thanks for your report, the two bugs are fixed in the current repository. > > Best regards, > Thierry Boileau > > > Hi, > > > > I've tried running an atom feed generated using Resltet 1.1 through the w3c > > validator. > > > > I get errors about the timestamps not being RFC3339 compatible (i'm using > > the Java Date class to populate nodes like). > > > > Also I get an error about the namespace: > > > >line 3, column 0: Avoid Namespace Prefix: atom > > > > where line 3 is > > > > http://www.w3.org/2005/Atom";> > > > > I'm assuming that validation is a problem because some feed aggregators, > > e.g. FeedReader, refuse to display the feed, while others e.g. SharpReader > > do. > > > > Has anyone had any experience/problems consuming and validating feeds > > created by Restlet? > > > > -- > > http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441121 > > > > -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441328
Re: Representation for multiple types?
I can't remember what 1.1.6's API looks like, but I do something like this with the 2.0 API. Basically, in the Resource's constructor, I use someting like: getVariants().add(new MyXMLRepresentation(this)) getVariants().add(new MyJSONRepresentation(this)) Each of those My* classes are a subclass of Representation, and the write() method is overridden to produce the custom media type. I pass the Resource object to the custom representation so that I have access to the resource during the write method. Also important is that in the constructor, you call the super constructor with the MediaType and resource as arguments, ex: //Constructor public MyJSONRepresentation(Resource res) { super(MediaType.APPLICATION_JSON, res); } I know something like this can be done with the 1.1.6 API because I was using it before I ported to 2.0, but now I can't find the 1.1.6 code. It probably isn't too different. On Jan 22, 2010, at 3:50 PM, Jean-Philippe Steinmetz wrote: > Hello all, > > We're using restlet 1.1.6 and for all our resources we want to support a > dynamic set of media types. We do this in our Spring configuration by setting > up a list of acceptable media types. My question is if there is a > Representation class I can use that will essentially accept any type of data > I push into it? For instance we support HTML, XML, JSON and AMF3 therefore > most of the time it's a string based representation but in the case of AMF3 > it is binary (byte array). So far the only way I see it is to make a switch > statement in our resource class for each media type but this pretty much > defeats the purpose of specifying supported media types in the application > configuration. > > Jean-Philippe -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441312
Representation for multiple types?
Hello all, We're using restlet 1.1.6 and for all our resources we want to support a dynamic set of media types. We do this in our Spring configuration by setting up a list of acceptable media types. My question is if there is a Representation class I can use that will essentially accept any type of data I push into it? For instance we support HTML, XML, JSON and AMF3 therefore most of the time it's a string based representation but in the case of AMF3 it is binary (byte array). So far the only way I see it is to make a switch statement in our resource class for each media type but this pretty much defeats the purpose of specifying supported media types in the application configuration. Jean-Philippe -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441306
Re: Restlet atom feed validation
Hello, thanks for your report, the two bugs are fixed in the current repository. Best regards, Thierry Boileau > Hi, > > I've tried running an atom feed generated using Resltet 1.1 through the w3c > validator. > > I get errors about the timestamps not being RFC3339 compatible (i'm using the > Java Date class to populate nodes like). > > Also I get an error about the namespace: > >line 3, column 0: Avoid Namespace Prefix: atom > > where line 3 is > > http://www.w3.org/2005/Atom";> > > I'm assuming that validation is a problem because some feed aggregators, e.g. > FeedReader, refuse to display the feed, while others e.g. SharpReader do. > > Has anyone had any experience/problems consuming and validating feeds created > by Restlet? > > -- > http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441121 > > -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441262
Re: @Get for many Variants
Hi, You could use org.restlet.engine.converter.ConverterHelper too. You might want to register your own Converter if you're not satisfied with the default ones. In this case, the if-MediaType logic will be in the ConverterHelper's toRepresentation method. public class SearchResource extends ServerResource { @Get("xml") public SearchResults doXmlSearch() { return doSearch(getRequest()); } @Get("json") public SearchResults doJSonSearch() { return doSearch(getRequest()); } } Best wishes, Bruno. Thierry Boileau wrote: > Hello, > > you can override the "ServerResource#get(Variant)" method (which is > historically the fundation of the Resource's behaviour, with the post, > put, etc methods): > > @Override > protected void doInit() throws ResourceException { > // Declare the list of supported variants. > getVariants().add(new Variant(MediaType.APPLICATION_XML)); > getVariants().add(new Variant(MediaType.APPLICATION_JSON)); > } > > @Override > protected Representation get(Variant variant) throws ResourceException { > Representation result = null; > > MediaType type = variant.getMediaType(); > if (MediaType.APPLICATION_XML.equals(type)) { > result =new > XmlSearchResponseRepresentation(doSearch(getRequest())); > } else if (MediaType.APPLICATION_JSON.equals(type)) { > result = new > JSonSearchResponseRepresentation(doSearch(getRequest())); } > > return result; > } > > Best regards, > Thierry Boileau > >> Hi everyone, >> >> I'm using restlet to write a Search service.. >> This service will respond in XML or JSon.. >> >> Actually I have a code like that : >> == 8< == >> public class SearchResource extends ServerResource { >> >> @Get("xml") >> public Representation doXmlSearch(Variant v) throw ResourceException { >> return new XmlSearchResponseRepresentation(doSearch(getRequest())); >> } >> >> @Get("json") >> public Representation doJSonSearch(Variant v) throw ResourceException { >> return new JSonSearchResponseRepresentation(doSearch(getRequest())); >> } >> >> } >> == 8< == >> >> As you can see, the unique difference is the Representation name. So I >> wonder if it is a way to write only one method that make a "if" on the >> requested variant to create an Xml or JSon representation.. >> >> Thanks >> >> -- >> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2440940 >> >> -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441238
Re: URI Pattern matching based on regular expressions
Hi Kirk, hum, if the router sets the chosen route as an attribute of the current request, filters or more generally restlets should be able to access this piece of information. Except if the filters are placed before the routing. Best regards, Thierry Boileau > Hi Thierry, > > While this does work if the restlet is the only one in the chain, i have > filters in front of it that require access to my custom route object. > > So that's not working quite as i expected. I'll respond if i find a way > around this. > > Regards, > --KD > > -- > http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441139 > > -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441230
Re: @Get for many Variants
Hello, you can override the "ServerResource#get(Variant)" method (which is historically the fundation of the Resource's behaviour, with the post, put, etc methods): @Override protected void doInit() throws ResourceException { // Declare the list of supported variants. getVariants().add(new Variant(MediaType.APPLICATION_XML)); getVariants().add(new Variant(MediaType.APPLICATION_JSON)); } @Override protected Representation get(Variant variant) throws ResourceException { Representation result = null; MediaType type = variant.getMediaType(); if (MediaType.APPLICATION_XML.equals(type)) { result =new XmlSearchResponseRepresentation(doSearch(getRequest())); } else if (MediaType.APPLICATION_JSON.equals(type)) { result = new JSonSearchResponseRepresentation(doSearch(getRequest())); } return result; } Best regards, Thierry Boileau > Hi everyone, > > I'm using restlet to write a Search service.. > This service will respond in XML or JSon.. > > Actually I have a code like that : > == 8< == > public class SearchResource extends ServerResource { > >@Get("xml") >public Representation doXmlSearch(Variant v) throw ResourceException { > return new XmlSearchResponseRepresentation(doSearch(getRequest())); >} > >@Get("json") >public Representation doJSonSearch(Variant v) throw ResourceException { > return new JSonSearchResponseRepresentation(doSearch(getRequest())); >} > > } > == 8< == > > As you can see, the unique difference is the Representation name. So I wonder > if it is a way to write only one method that make a "if" on the requested > variant to create an Xml or JSon representation.. > > Thanks > > -- > http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2440940 > > -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441224
RE: Re: URI Pattern matching based on regular expressions
Hi Thierry, While this does work if the restlet is the only one in the chain, i have filters in front of it that require access to my custom route object. So that's not working quite as i expected. I'll respond if i find a way around this. Regards, --KD -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441139
Restlet atom feed validation
Hi, I've tried running an atom feed generated using Resltet 1.1 through the w3c validator. I get errors about the timestamps not being RFC3339 compatible (i'm using the Java Date class to populate nodes like ). Also I get an error about the namespace: line 3, column 0: Avoid Namespace Prefix: atom where line 3 is http://www.w3.org/2005/Atom";> I'm assuming that validation is a problem because some feed aggregators, e.g. FeedReader, refuse to display the feed, while others e.g. SharpReader do. Has anyone had any experience/problems consuming and validating feeds created by Restlet? -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441121
Atom validation
Hi, I've tried running an atom feed generated using Resltet 1.1 through the w3c validator. I get errors about the timestamps ( i'm using the Java Date class) not being RFC3339 compatible. Also I get an error about the namespace: line 3, column 0: Avoid Namespace Prefix: atom [help] where line 3 is http://www.w3.org/2005/Atom";> I'm assuming that validation is a problem because some feed aggregators, e.g. FeedReader, refuse to display the feed, while others e.g. SharpReader do. Has anyone had any experience validating feeds created by Restlet? -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441119