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