How to access header values of request in Java Restlet?

耗尽温柔 提交于 2019-12-01 04:44:20

问题


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?


回答1:


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.




回答2:


Please also notice that Restlet provides an API for RESTful application. This means that you can access standard headers using this API. In most cases, you donn't need to use the attribute named "org.restlet.http.headers".

For example, if you want to set a Location header in the response, you add this code:

getResponse().setLocationRef("http://...");

Otherwise, since you talk about security, Restlet provides a generic API to support such aspect (see classes ChallengeAuthenticator, Verifier, Enroler).

Hope it helps you. Thierry




回答3:


If you are using Restlet 2.1-RC3 this is the way to get it

 Series<Header> headers = (Series<Header>) getRequestAttributes().get("org.restlet.http.headers");
 String auth = headers.getFirstValue("auth");

This is exactly how it's worked for me. None of the answers above did it. I hope it helps.




回答4:


How would you handle seeking to parse an ID in the GET header from a client request ? Would you approach this in the same manner ?

Since the:

Form headers = (Form) getRequestAttributes().get("org.restlet.http.headers");

returns all the header information and

String hID = headers.getFirstValue("Location");

gets the location(s)

How would you handle parsing out the ID ?




回答5:


Series headers = (Series) getRequestAttributes().get("org.restlet.http.headers");
String origin = headers.getFirstValue("Origin");`

This is just an example of getting the Origin header. If you want to get Location, just change to headers.getFirstValue("Location");

As in the new version of Restlet, getRequestAttributes().get() returns Series instead of Form.



来源:https://stackoverflow.com/questions/17743785/how-to-access-header-values-of-request-in-java-restlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!