Thanks Vincent
This was actually helpful for me

--------------------------------------------------
From: "Vincent Ricard" <[EMAIL PROTECTED]>
Sent: Thursday, August 28, 2008 9:49 AM
To: <discuss@restlet.tigris.org>
Subject:  [RFC] WadlResource, get a param value according to the description

Hi,

Here is what i added to my WadlResource subclass (and the super class of
all my resources): [see below].

I think it would be nice (in a future version) to have a similar method
directly available in WadlResource.

For now, this code just fulfills my needs:
- it does not handle multi-valued param (but can be easily added: split
into a getFirstParam and a getParam methods)
- it does not handle the "plain" type (which requires a more complex code
for the xpath stuff)
- it does not handle the conversion to the correct type (as declared by
the "type" attribute) (add a "Converter" object in the method signature?)
- it does not handle the "required" attribute (we could add a
WadlRequiredException in the extension or something like that)
- it does not handle the "header" type because i dont need ;-) and because
i dont know if there is an abstract layer to reach the HTTP headers
without creating a strong dependency with another extension (like
ServerServlet, or an other).

Despite of all that, now i can just call:
getParam(getRequestInfo(), "service");

Any comments are welcome (and anyone can reuse this code :-).

--- snip ---

protected String getParam(RequestInfo requestInfo, String parameterName) {
List<ParameterInfo> parameters = requestInfo.getParameters();
for (ParameterInfo parameter : parameters) {
if (parameterName.equals(parameter.getName())) {
return getParam(parameter);
}
}
return null;
}

protected String getParam(ParameterInfo parameterInfo) {
if (parameterInfo.getFixed() != null) {
return parameterInfo.getFixed();
}

String value = null;
if (ParameterStyle.HEADER.equals(parameterInfo.getStyle())) {
// not yet implemented
} else if (ParameterStyle.TEMPLATE.equals(parameterInfo.getStyle())) {
Object parameter =
getRequest().getAttributes().get(parameterInfo.getName());
value = (parameter == null) ? null : Reference.decode((String) parameter);
} else if (ParameterStyle.MATRIX.equals(parameterInfo.getStyle())) {
Parameter parameter = getMatrix().getFirst(parameterInfo.getName());
value = (parameter == null) ? null : parameter.getValue();
} else if (ParameterStyle.QUERY.equals(parameterInfo.getStyle())) {
Parameter parameter = getQuery().getFirst(parameterInfo.getName());
value = (parameter == null) ? null : parameter.getValue();
} else if (ParameterStyle.PLAIN.equals(parameterInfo.getStyle())) {
// not yet implemented
}

if (value == null) {
value = parameterInfo.getDefaultValue();
}

return value;
}

--- snip ---
--
Vincent Ricard


Reply via email to