RE: Representation for multiple types?
Hi Jean-Philippe, To precise Matts reply, I see two ways to achieve this in Restlet 1.1: 1) You can return and InputStream from your Serializer -> return an InputRepresentation 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 InputRepresentation(serializer.encode(data)); } } 2) Your serializers need an outputstream -> return an OutputRepresentation where the write(OutputStream) method is overridden to call the matched serializer: public class MyResource extends Resource { public Map map; public Representation represent(Variant variant) throws ResourceException { Final Serializer serializer = map.get(variant.getMediaType()); if (serializer == null) throw new ResourceException(Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE); return new OutputRepresentation(){ write(OutputStream os){ serializer.encode(data, os)); } } } } Best regards, Jerome Louvel -- Restlet ~ Founder and Technical Lead ~ <http://www.restlet.org/> http://www.restlet.org Noelios Technologies ~ <http://www.noelios.com/> http://www.noelios.com De : Matt Kennedy [mailto:stinkym...@gmail.com] Envoyé : lundi 25 janvier 2010 06:10 À : discuss@restlet.tigris.org Objet : Re: Representation for multiple types? Can you just use the sample code you provide below, only create a subclass of Representation that takes your serializer as the argument, maybe along with the mime type? Something like: public class MyVariableRepresentation extends Representation { Serializer serializer; public MyVariableRepresentation(MediaType type, Serializer variantSerializer) { super(type); this.serializer = variantSerializer; } //Then override write method or something to call your serializer to do the serializing. } So your sample code becomes: 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 MyVariableRepresentation(variant.getMediaType(), serializer.encode(data)); // ?? } } Does this work? On Jan 22, 2010, at 6:27 PM, Jean-Philippe Steinmetz wrote: 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 output
Re: Representation for multiple types?
Can you just use the sample code you provide below, only create a subclass of Representation that takes your serializer as the argument, maybe along with the mime type? Something like: public class MyVariableRepresentation extends Representation { Serializer serializer; public MyVariableRepresentation(MediaType type, Serializer variantSerializer) { super(type); this.serializer = variantSerializer; } //Then override write method or something to call your serializer to do the serializing. } So your sample code becomes: 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 MyVariableRepresentation(variant.getMediaType(), serializer.encode(data)); // ?? } } Does this work? On Jan 22, 2010, at 6:27 PM, Jean-Philippe Steinmetz wrote: > 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. > > > ... > > > static-field="org.restlet.data.MediaType.APPLICATION_FLASH"/> > > > > static-field="org.restlet.data.MediaType.APPLICATION_JSON"/> > > > > static-field="org.restlet.data.MediaType.APPLICATION_XML"/> > > > > > scope="prototype" > class="com.restlets.MyResource"> > > > ... > > > 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
Re: Representation for multiple types?
Hi Jean-Philipee, there is the converter service, perhaps it helps you. Unfortunately I can't give you details. best regards Stephan Jean-Philippe Steinmetz schrieb: > 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. -- http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2441394
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: 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