This is another one of those cases crying out for my proposed loopback patch 
(or whatever eventually comes of the "optimize internal calls" RFE) ... 
otherwise the elegant code Thierry suggests here is fairly slow and expensive 
to execute. 

----- Original Message ----- 
From: "Thierry Boileau" <[EMAIL PROTECTED]> 
To: [email protected] 
Sent: Tuesday, August 28, 2007 3:19:17 AM (GMT-0500) America/New_York 
Subject: Re: Sending a resource URI as a form parameter 

Hi Vincent, 

you can retrieve a color representation in the post method by requesting the 
Color resource with a color uri : 

Representation color1 = null; 
Response response = getContext().getDispatcher().get(color1Uri); 
if (response.getStatus().isSuccess){ 
color1 = response.getEntity(); 
} 

You can also define your own ColorRepresentation (sent by the Color resource 
according to the client accepted media types). 
best regards, 
Thierry Boileau 



On 8/28/07, Vincent < [EMAIL PROTECTED] > wrote: 

Hello, 

Here is what I'm trying to do: 

POST /colorMixer 
form: 
color1= http://www.colors.com/color/blue 
color2= http://www.colors.com/color/red 

A successful call will return 
201 CREATED 
http://www.colors.com/clor/purple 

My question is how do I implement the ColorMixerResource.post (Representation) 
method? 
More exactly, how to I get the Blue and Red objects from the uris? 

I have a ColorResource class that has a constructor that knows how to 
retrieve the right color from the DB: 

public ColorResource(contex,request,response){ 
String colorCode = (String) request.getAttributes().get("color"); 
// lookup that color code in the DB 
Color color = .... 
} 


Solution 1 
========== 
I am tempted to try to reuse that code, because when I get to this constructor, 
the request is already parsed, the attributes are extracted according to the 
template I specified for this resource. 

ColorMixer.post(Representation entity){ 
Form f = getRequest().getEntityAsForm(); 
String color1Uri = f.getFiestValue("color1"); 
String color2Uri = f.getFiestValue("color2"); 

Request request1 = new Request(color1Uri...) 
color1Resource = new Color1Resource(getContext(),request1, getResponse()); 
Color color1 = color1Resource.getColor(); 

// same thing for color2 
... 

Color result = Mixer.mix(color1,color2); 

// return the representation of the resulting color in the response 
} 




Solution 2 
=========== 
A second approach would be to -somehow- reuse the template for the 
ColorResource resource and use it to parse the uris. 

ColorMixer.post(Representation entity){ 
Form f = getRequest().getEntityAsForm(); 
String color1Uri = f.getFiestValue("color1"); 
String color2Uri = f.getFiestValue("color2"); 

Map<String, Object> variables = new HashMap<String,Object>(); 
if (ColorTemplate.parse(color1Uri,variables) != -1){ 
String Color1Code = (String)variables.get("color"); 
// get the color from the DB 
} 


// etc. 

} 

To make this work the createRoute method would have to stick the templates 
in the context so that they are available to resources. 


Neither approaches satisfies me. Yet, I'm sure there is a simple and elegant 
solution. 

Any suggestions? 

Thanks, 

-Vincent. 








Reply via email to