There have been some changes in the types involved. The object returned by
Response/Request.getAttributes().get("org.restlet.http.headers") is a
Series<Header>, not a Form, which is a Series<Parameter>. Because of type
erasure, there is no runtime problem until the Restlet Engine tries to make
use of the Parameter you stored and treat it as a Header.

You'll have to re-write your code to do something like this:

...
import org.restlet.engine.header.Header;
...
    private static final String HEADERS_KEY = "org.restlet.http.headers";
...
    @SuppressWarnings("unchecked")
    static Series<Header> getMessageHeaders(Message message) {
        ConcurrentMap<String, Object> attrs = message.getAttributes();
        Series<Header> headers = (Series<Header>) attrs.get(HEADERS_KEY);
        if (headers == null) {
            headers = new Series<Header>(Header.class);
            Series<Header> prev = (Series<Header>)
                attrs.putIfAbsent(HEADERS_KEY, headers);
            if (prev != null) { headers = prev; }
        }
        return headers;
    }
...
    // In some method, what you originally wanted to do:
    getMessageHeaders(getResponse()).add("Access-Control-Allow-Origin",
"*");

The use of putIfAbsent is probably overkill, since the only chance that
another thread is modifying the attribute map is with asynchronous
handling, but it can't hurt.

--tim

On Thu, Feb 9, 2012 at 2:54 PM, Michael Kaye <mik...@gmail.com> wrote:

> Previous to the 2.0.11, I was able to set the responseHeaders as follows.
>
>
> Form responseHeaders = (Form)
> getResponse().getAttributes().get("org.restlet.http.headers");
>
> if ( responseHeaders == null ) {
>                responseHeaders = new Form();
>
>  getResponse().getAttributes().put("org.restlet.http.headers",
> responseHeaders);
>        }
> responseHeaders.add("Access-Control-Allow-Origin", "*");
>
> Now, I get the following runtime error,
>
> java.lang.ClassCastException: org.restlet.data.Parameter cannot be cast to
> org.restlet.engine.header.Header
>
> How should I set response headers now?
>
> ------------------------------------------------------
>
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2920188
>

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2920205

Reply via email to