Sean Landis wrote:
I want to set my application-specific attributes on the client so that the
server can access them. This does not seem to be possible.
If I:
request.getAttributes().put(MY_ATTRIBUTE, value);
on the client, then on the server there are 5 attributes in the map, but they
all belong to org.restlet. My attribute isn't there. One of the attributes
is the org.restlet.http.headers series.
If I do:
Series headers = (Series) request.getAttributes().get(HEADERS_ATTRIBUTE);
headers.add(MY_ATTRIBUTE, value);
request.getAttributes().put(HEADERS_ATTRIBUTE, headers);
Then on the server, all the attributes are gone.
How do I accomplish this feat?
Sean,
it looks like you are mixing up headers with attributes.
* Headers is what is passing over the wire in the 'header' of HTTP messages.
* Attributes is what can be attached to the in memory request objects
these stay inside the VM. (not passed over the wire)
The confusion is easily made by the fact that the restlet API is holding
all the header-lines in a Series<Paramater> object that is hooked up as
a single attribute with name "org.restlet.http.headers".
I'm assuming the design opts to do things like this since the
request-response model is not limited to merely the HTTP protocol.
Now, assuming you want to pass data in a custom header from the client
to the server, you can use this on the client:
Request request = new Request();
request.setResourceRef("http://127.0.0.1:8182/header/");
Series<Parameter> headers = new Form();
headers.add("X-mpo", "my own header added");
request.getAttributes().put("org.restlet.http.headers", headers);
on the server you can just dump and return the headers with:
StringBuffer sb = new StringBuffer();
Series<Parameter> headers = (Series)
request.getAttributes().get("org.restlet.http.headers");
for (Parameter h : headers)
{
sb.append("header name = ");
sb.append(h.getName());
sb.append("\n value = ");
sb.append(h.getValue());
sb.append("\n");
System.out.println(sb.toString());
}
response.setEntity(sb.toString(), MediaType.TEXT_PLAIN);
Note that passing those values as request-parameters is a more common
way of doing things. (headers are a more hidden way of passing stuff,
and will typically require custom clients to be able to set them and
pass them over)
Further samples for this are to be found in the examples:
see in the package org.restlet.example.misc for the classes HeaderTest,
SimpleClient and SimpleServer.
HTH,
-marc=