RIAP protocol and local Representions

2010-01-24 Thread Valdis Rigdon
I can't seem to phrase my question the way I want, so I'm giving some 
background along with the question(s) and hopefully it make sense to someone.

I am designing an API around REST resources, using Restlet as the core.  I'm 
using the new annotation based client side interfaces, using 
ClientResource#wrap and extending ServerResource on the server side, again, 
using annotations.  The ServerResource implementations do not return 
Representation instances; I'm relying on the ConverterService to perform all 
conversions.  All of this works great (minus a few bugs/enhancements I've 
already entered).  There is also a more traditional API sitting on top of the 
REST resource interfaces.  The traditional API is just a thin wrapper around 
the REST resources, providing some state management and consistency across 
different resources.   We also want to use this traditional API from the server 
side, from within ServerResource instances, and have it transparently switch to 
RIAP by injecting the appropriate URL prefix.

In general, this works, but I've run into an issue with converting objects for 
local invocations when using the RIAP protocol.  It seems that 
DefaultConverter only handles certain Representations or Serializable objects.  
Since I know in my circumstance I'm using RIAP, and in the same JVM, I do not 
want to serialize anything -- I really want to return the reference to the 
original caller and avoid any serialization overhead.  While my EJB2 days are 
(thankfully) in the past, this is essentially the Remote/Local interfaces of 
EJBs if I do recall.

1) Is this a reasonable thing to do or is there a better way?  As I've been 
building our APIs out, I'm really liking the way things look.  It means I can 
use the same APIs from both a remote client and locally within the same JVM 
simply by switching the URL prefix.

2) Is there a ConverterHelper that will simply hold the local reference and 
return it?  Is there a media type for this?  Using 
application/x-java-serialized-object doesn't seem right.


Thanks,
Valdis

--
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2441538


Re: Representation for multiple types?

2010-01-24 Thread Matt Kennedy
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 

Bug in MediaType Parameter handling

2010-01-24 Thread Carsten Lohmann
Hi,

when I do the following:
---
MediaType mediaType = MediaType.valueOf(application/atom+xml; charset=UTF-8);
System.out.println(mediaType.toString());
---
I get this output (with 2.0M6):
application/atom+xml; charset=UTF-8; charset=UTF-8

Seems to be a bug in the handling of parameters.

Cheers,
Carsten

--
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2441619


RE: Re: URI Pattern matching based on regular expressions

2010-01-24 Thread Kirk Daries
apologies, meant to say 'a reference to the ROUTER in my 'application 
context'...

--
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2441234


RE: Re: URI Pattern matching based on regular expressions

2010-01-24 Thread Kirk Daries
Hi Thierry,

That's exactly the problem.
I have a security filter in front of the router.

The idea was that i would expose some permission based attributes associated 
with the route for use by the security filter but since the filter fires 
before i get there...

well... chicken before the egg ^_^.

I've hacked it in the mean time by storing a reference to the route in my 
'application context' and manually call 'getNext' from the security filter to 
figure out which route is being invoked  and hence get hold of the extra 
permission bits i added on.

Really ugly... but it works right now.
I'll mull it over the weekend.

Regards,
--KD

--
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447dsMessageId=2441232