How to set response header

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to set response header

Michael Kaye
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to set response header

Tim Peierls
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 <[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Re: How to set response header

Michael Kaye
Excellent, works like a charm. Thanks!!

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=2921256
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to set response header

XfStef
In reply to this post by Tim Peierls
Wow you just saved my life. Everbody and their mother was still posting on the internet that you need to define a Form for the headers. This sort of deprecated information should somehow be reviewed and removed if it's false.

Thanks !
Loading...