Deepu Deepu - 6 months ago 57
Java Question

How to access header values of request in Java Restlet?

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

I solved my problem using

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

I found this link useful.