Getting to know ColdBox interceptors, why would I want to use them
Tags: Coldfusion, ColdFusion 9, Coldbox
Today I wanted to give a very quick post on interceptors, and how you might leverage them in a real world example.
Out of the box ColdBox has a number of interception points that can be leveraged from, from Session Starting up to Exception handling, to creating your own interceptor point.
But how would I know when I would be best likely to use these, well there is no hard and fast rule on that. However I thought I would give a real world example that is best suited for interceptors, and using your own in your Application.
But before we go into that one thing to remember, that with any of your own interceptors you need to register these points. In Which you would declare in your ColdBox.cfc config file.
</Interceptors>
And if you were using script based code, you would use
throwOnInvalidStates = false,
customInterceptors = "onLog,onRecordInserted"
};
So now we can say that anywhere within the application we can set up code, that will be notified of these announcements.
Now one way to think about interceptors when using them in ColdBox, is that you can extend functionality that can do a variety of different things. Which leads us to how you might use this in your own application, and before I go into that I would like to say that you don't need to use interceptors at the start of your project.
Ok now to the Application, in this scenario we have created a very simple forum application. And the logic is only a guide to get you thinking, and to help illustrate what I am talking about. Lets take a look at the current posting of a topic to a forum.
ORMService.populate(target = post, memento=rc);
ORMService.save(post);
I have kept this to the basics for now and won't do the code to do validation, we will just assume that we are going to validate the code in here as well.
And now we have a new requirement that has come in for this project, which is going to have an option to allow for BBCode and restrict other forms of HTML and JS etc.
So lets look at how we will do this with interception points. The first thing we will need to do is add our interception point to the custom points, that is defined in the configuration of ColdBox, and we just add the following.
throwOnInvalidStates = false,
customInterceptors = "cleanPost"
};
And when the application is re-initialised this point will now be available to be used, and all we then need to do is modify the code snippet making the post happen to this.
announceInterception('cleanPost', data)>
ORMService.populate(target = post, memento=rc);
ORMService.save(post);
Now although you can see that I have said we well send an event of for anyone who wishes to listen to cleanPost, with the data that this interceptor will need. In this case I will just pass the actual post in question, you are more than welcome to set this up to pass what ever you require. In this example I am just going to require the actual post information, so it is going to look something like this.
post = event.getValue('postText', '');
user = userService.getUser();
}
This data structure is very basic, I am getting the post from the submitted form. And then getting the userService to get me the current user, now this is just an example. You could very easily do this a number of ways, and the way that I would prefer is to inject the userService into the interceptor, but I won't confuse you any more and we will just go with this for now.
So now lets have a look at what our interceptor might look like.
public void function cleanPost(event, interceptData) {
}
I am going to stop here before placing the code into the method here, and just explain what I have just done. You can see that I have included a dependency for the plugin AntiSammy, this is because I am going to use that to do the cleaning of the post. I also want to say that as part of the requirement for this new feature, the admin has the option to setup whether the BBCode is available or not.
So in the interceptor we can do this.
AntiSamy.HtmlSanitizer(rc[key], "BBCode");
}
And that is pretty much it. I would like to say that I am using a custom AntiSamy config file here, and its not within my intentions to go through and explain how set this all up just now. What is important to note here is that we can continue to add more functionality, without really changing the core code.
This means that you can just remove the interceptor, and the code will not fire. So lets say you created this as a module called forums, for instance and the main application could then be given control and how to handle anything extra you might want to give it.
The possibilities are endless and defined by how you want to release your code, you could provide basic code here to do something and as indicated just let the main application then listen for this interception point and then do what they need too.
I have tried to give this as a very basic example on how you could and may use interceptors in your code, and in the next few days I will give another example on how I use interceptors in my main Application, to provide easier development with ColdFusion 9 and ORM.
Do not forget to register your interceptor
There are no comments for this entry.



TweetBacks