1 / 17

ColdBox Interceptors

ColdBox Interceptors. Brad Wood www.codersrevolution.com brad@bradwood.com. What is an interceptor?. An encapsulated piece of logic that “listens” to events that happen during the life-cycle of your application.

sbecky
Download Presentation

ColdBox Interceptors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ColdBoxInterceptors Brad Wood www.codersrevolution.com brad@bradwood.com

  2. What is an interceptor? An encapsulated piece of logic that “listens” to events that happen during the life-cycle of your application. It allows for loose coupling, cross cutting concerns, and implicit chain-able execution points. Interception points can be tapped into without modifying the core code of your application.

  3. What is an interceptor? Interceptors follow the model of event-driven programming. Also known as The  Hollywood principle-  “Don’t call us, we’ll call  you.”

  4. What is an interceptor? There are a number of baked-in interception points that the ColdBox framework broadcasts that can give you AOP-style “before” or “after” filters.Your application can declare and announce custom interception points which model the observer/observable pattern.Multiple listeners for the same event (or interception point) will be chained together in the order of declaration. 

  5. Core Interception Points A sample of Interception points that ColdBox announces out-of-the-box: afterConfigurationLoad preProcess postProcess preEvent postEvent preRender afterCacheElementInsert onException

  6. What are they good for? Security Method Tracing AOP Interceptions like Cache advices on insert and remove Publisher/Consumer operations Content Appending or Pre-Pending View Manipulations Custom SES support

  7. Creating an Interceptor myInterceptor.cfc <cfcomponentdisplayname="myInterceptor" extends="coldbox.system.interceptor"        output="false"> <cffunctionname="preProcess"output="true"> <cfargumentname="event"required="true"> <cfargumentname="interceptData"required="true">         <!--- Do some really awesome stuff here ---> </cffunction> </cfcomponent>

  8. Creating an Interceptor coldbox.cfc interceptors =       [             {                   class="path.to.myInterceptor"             }       ];

  9. Create a custom interception point coldbox.cfc interceptorSettings =       {             throwOnInvalidStates = false,             customInterceptionPoints = "refreshContentHierarchy"       }; ... interceptors =       [             {                   class="interceptors.contentHierarchySynch",                   properties={}             }    ];

  10. Create a custom interception point contentHierarchySynch.cfc <cfcomponentdisplayname="contentHierarchySynch" extends="coldbox.system.interceptor"output="false"singleton="true"> <cfpropertyname="contentService"inject="model:contentService"> <cffunctionname="refreshContentHierarchy"access="public"output="false"> <cfargumentname="event"> <cfargumentname="interceptData"> <cfif not interceptData.justKidding> <cfset contentService.contentXrefNodeBackfill()> </cfif> </cffunction> </cfcomponent>

  11. Create a custom interception point Somewhere in my handlers ... var interceptData = {}; interceptData.justKidding = false; announceInterception('refreshContentHierarchy', interceptData); Somewhere in my model... var interceptData = {}; interceptData.justKidding = true; interceptorService.processState("refreshContentHierarchy",interceptData);

  12. Interceptor Properties You can include properties in the config when you declare an interceptor.   These properties are available during execution to control how the interceptor behaves. {       class="coldbox.system.interceptors.Autowire", properties =                         {                               debugMode="false",                               enableSetterInjection="false"                         } }

  13. Interceptor Properties Interceptors can use these properties with built-in functions including: propertyExists() getProperty(name) setProperty(name, value) if(!properyExists("configFile"))     {             setProperty("configFile","defaultConfig.cfc")     } loadConfig(getProperty("configFile"));

  14. Interceptor chaining If multiple interceptors are listening to the same interception point they will be chained together and executed in the order they are declared in the config.   Any interceptor can break the chain by returning a value of true. preProcess 1 (Returns false or void) preProcess 2 (Returns false or void) preProcess 3 (Returns true) preProcess 4 (Doesn't get executed)

  15. Interceptor chaining ColdBox also supplies each execution chain with a buffer object that can be appended to.  Any content in the buffer at the end of the execution chain will be flushed out to the page. <cffunctionname="preRender"access="public"returntype="boolean"output="false"> <cfargumentname="event"required="true"> <cfargumentname="interceptData"required="true"> <cfscript>             //clear all of it first             clearBuffer();             //Append to buffer             appendToBuffer('I want this text at the bottom of every page.');    </cfscript> </cffunction>

  16. Code Examples • Form Parser • JavaScript Defer • Browser Snob • SES

  17. Questions? Brad Wood www.codersrevolution.com brad@bradwood.com Mailing List: groups.google.com/group/coldbox Docs: http://wiki.coldbox.org/wiki/Interceptors.cfm Website:http://www.coldbox.org/

More Related