Blog   About   Contact

ColdBoxProxy and JSON

If you work a lot with JSON and remote calls like me, the following coldboxproxy.cfc code could be handy for you.

<cfcomponent name="coldboxproxy" output="false" extends="coldbox.system.extras.ColdboxProxy">

    <cffunction name="process" output="true" access="remote" returntype="any" hint="Process a remote call and return data/objects back.">
        <cfset var results = "">

            <cftry>
                <!--- Call the actual proxy --->
                <cfset results = super.process(argumentCollection=arguments)>
        
                <!--- JSON ? --->
                <cfif isDefined('arguments.json')>
                    <cfheader name="expires" value="Mon, 03 Sep 1973 00:00:01 GMT">
                    <cfheader name="pragma" value="no-cache">
                    <cfheader name="cache-control" value="no-cache">
                    <cfcontent type="text/html">
                    #getPlugin("JSON").encode(data:results,queryKeyCase:"upper")#     
                <cfelse>
                    <cfreturn results>
                </cfif>

                <cfcatch type="any">#processException(cfcatch)#</cfcatch>
            </cftry>

    </cffunction>
    
    <cffunction name="processException" output="false" access="private" returntype="string" hint="Process exception and returns bugReport">
        <cfargument name="Exception" type="any" required="true" hint="The exception structure (cfcatch)">
        
        <cfset var exceptionService = "">
        <cfset var ExceptionBean = "">        
        <cfset var interceptData = StructNew()>        
        
        <!--- Get Exception Service --->
        <cfset ExceptionService = getController().getExceptionService()>
        
        <!--- Intercept The Exception --->
        <cfset interceptData.exception = arguments.exception>
        <cfset announceInterception('onException',interceptData)>
        
        <!--- Handle The Exception --->
        <cfset ExceptionBean = ExceptionService.ExceptionHandler(arguments.exception,"coldboxproxy","ColdBox Proxy Exception")>                    
        
        <!--- Return rendered bugreport --->
        <cfreturn exceptionService.renderBugReport(ExceptionBean)>
    </cffunction>
    
</cfcomponent>

The URL of your remote call will be something like this:

coldboxproxy.cfc?method=process&json&event=ehCompany.getCompanies

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.9.004.