User loginNavigation |
Interceptors and Exception Mapping for AppceleratorI've been working on some code for both OS Integrators, LLC and for a client as well. I'm using the Appcelerator AJAX framework for both projects. In order to accomplish these goals, I've committed the following to head in the service:java component:
For example let's say you have: @Service(request=CREATE_TICKET_REQUEST, CREATE_TICKET_RESPONSE) You want this to require authentication. If the user has no session or isn't authorized to file tickets, you want to send a security error. So you create a method like this in a class called AuthenticationService: public void checkAuthenticated(Message request, Message response) { throw new SecurityException("Security exception"); Next, you associate this method with your createTicket method by annotating it like so: @BeforeMethods({ However, this puts the exception and error in your create ticket response message. This makes generic error handling (i.e. redirect to the login screen for security exceptions) complicated. What you really want is a different message for security errors. Do this by annotating createTicket like so: @ExceptionResponses({ This returns both the create ticket response message with error=true and the error.security.response message to the client. You can have as many @ExceptionResponse annotations as you like, but the order matters. For instance if at the top of the annotation you have RuntimeException and below that you have SecurityException the runtime exception mapping will not occur as security exception is a more specific instance of it. The same is true for @BeforeMethods, the methods are executed in order, an exception thrown near the top of the stack is returned up the stack (and mapped where appropriate). There is also @AfterMethods for completeness (it also enumerates @InterceptorMethod annotations), but I have never found them particularly useful to be honest. Let me give due credit. I derived inspiration for this from:
And received a lot of help from Martin Robinson @ Appcelerator. To build it:
I'm interested in feedback from anyone who ends up using it. Thanks!
|
Recent blog posts
|