1

I'm trying to create a HEAD response with restlet. Unfortunatly there is ony a @Get annotation, but the restlet author states, that you have to use a @Get, and then compare the Method. As the documentation/specification says, there can be no body, but only a message header.

Now how to create a message header that will be send to the server, because the following code does not work, it sends this headers: HTTP/1.1 204 No Content, Content-Length: 0

protected void addResponseHeader(String name, String value) {
    Form responseHeaders = (Form)getResponse().getAttributes().get(HeaderConstants.ATTRIBUTE_HEADERS);
    if (responseHeaders == null) {
        responseHeaders = new Form();
        getResponse().getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, responseHeaders);
    }
    responseHeaders.add(new Parameter(name, value));
}

The concrete code on server-side:

@Get
public void execute() {
    if (Method.HEAD.equals(getMethod())) {
        //optional: getResponse().getEntity().setMediaType(MediaType.TEXT_PLAIN);
        getResponse().setStatus(Status.SUCCESS_OK, "hello head");
        addResponseHeader("X-my-header", "value");
    }
}

The client code:

@Test
public void head() {
    Request request = new Request(Method.HEAD, url);
    Response response = query(request);
    assertEquals(Status.SUCCESS_OK, response.getStatus());
    Form form = (Form)response.getAttributes().get(HeaderConstants.ATTRIBUTE_HEADERS);
    assertEquals("value", form.getFirstValue("X-my-value")); // does fail because it is null
}
Dag
  • 10,079
  • 8
  • 51
  • 74

1 Answers1

2

You just need to implement @Get for real : should work with a HTTP GET fine first. Then if you issue a HTTP HEAD, it will be handled automatically by the framework, nothing else to do on your side. Just focus on getting GET implemented correctly.

Jerome Louvel
  • 2,882
  • 18
  • 19
  • Hi Jerome, GET works, but HEAD not. So what did I wrong, just saying implement it correctly is not so helpful. I searched the web a lot and did not find an answer. Why is HEAD response 204 No Content, Content-Length: 0? And where is my custom header gone? – Dag Jun 18 '11 at 20:04
  • Hi Dag, looks like a bug. Could you enter an issue? http://www.restlet.org/community/issues – Jerome Louvel Jul 02 '11 at 15:47