How to access header values of request in Java Restlet?

Deepu picture Deepu · Jul 19, 2013 · Viewed 9.6k times · Source

I am developing web services using Restlet Java.

For this I want to protect some webservices from unauthorized clients. So I have written Filter class. In that Filter class I want to get the headers of the Request. But I am getting the following error -

java.lang.ClassCastException: org.restlet.engine.http.HttpRequest cannot be cast to javax.servlet.http.HttpServletRequest

The coding is -

public class MyFilter extends Filter {

    @Override
    protected int beforeHandle(Request request, Response response) {

        int result = STOP;

        HttpServletRequest httpReq = (HttpServletRequest) request;
        String user_token = httpReq.getHeader("auth");

        if(user_token.equals("xyz")) {
            result = CONTINUE;
        }

        return result;
    }
}

Please suggest me a way to access the header values of Request in Java Restlet?

Answer

Deepu picture Deepu · Jul 19, 2013

I solved my problem using

Form headers = (Form) request.getAttributes().get("org.restlet.http.headers");
String user_token = headers.getFirstValue("Location");

I found this http://blog.yudongli.com/2009/12/get-request-header-in-restlet_13.html link useful.