I am developing a RESTlet API (JAVA), and I’ve created a custom authorization filter that I run all requests through before passing it to my router. In my requests I always pass the session ID as a request attribute, e.g.
http://localhost:8080/myAPI/{sid}/someResource/
Now, in my functions that extends ServerResource, I can do something like this to easily extract that {sid}:
String sid = (getRequestAttributes().containsKey("sid")) ? getRequestAttributes().get("sid").toString() : "";
My problem is, in my authorization function, which extends Filter (the authorization function is not called via a router, but is called in my main createInboundRoot() function), I cannot use the same method to extract the {sid}. I’ve created a workaround using string manipulation of request.getResourceRef().getSegments(), but there must be a better way?
Any help will be appreciated!
Thanks
You can create a common parent class for any child of
ServerResource. like this:And then override the
doInit()method of the ServerResource class in it.Now any new child class of
ServerResourcethat you want to perform this authorization check, must extend thisCommonParentResourceclass. Like this:Two points are important here:
doInit()of any child class ofServerResourceis called before calling any method annotated with@Get/@Post/ …(Caution) If you do not use this statement:
i.e. if you do not set status of response to an error, then methods annotated with
@Get/@Post/@Put/ … will get called ! But if your program sets the status of the response to an error-status, then the@Get/@Post/@Put/ … will not get executed, and the end user will see the error message represented by: