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 MapMediaType,Serializer 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 MapMediaType,Serializer 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.
beans
...
util:map id=map map-class=java.util.HashMap
entry
keyutil:constant
static-field=org.restlet.data.MediaType.APPLICATION_FLASH//key
ref local=amfSerializer/
/entry
entry
keyutil:constant
static-field=org.restlet.data.MediaType.APPLICATION_JSON//key
ref local=jsonSerializer/
/entry
entry
keyutil:constant
static-field=org.restlet.data.MediaType.APPLICATION_XML//key
ref local=xmlSerializer/
/entry
/util:map
bean name=/my/resource id=myResource autowire=byName
scope=prototype
class=com.restlets.MyResource
property name=map ref=map/
/bean
...
/beans
On Fri, Jan 22, 2010 at 3:07 PM, Matt Kennedy stinkym...@gmail.com 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 stinkym...@gmail.com 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