Remote Synthesis
Search my blog:
Viewing By Entry / Main
Aug 14, 2007

ColdFusion 8 - Passing Common Attributes Via AttributesCollection

We have all heard how the new AttributeCollection attribute that exists on most ColdFusion tags in ColdFusion 8 can prevent having to write multiple CFMail tags to handle situations where the attributes may vary (say, one with CC and one without). However, I was actually doing some sample code for cffeed and cfpresentation when I came up with another potential usage.

Where I work everything needs to be accessed via the proxy. Thus, every tag like cfhttp or cffeed needs to pass my proxy URL with port, username and password. Well, since the attributeCollection is simply a structure, we can actually create a common proxy structure and append this as needed, with one annoying caveat. Let's take a look.

(On a side note, I had an issue with cffeed and proxies - see the end of this post if you are curious)Let's say that in one part of my application, I am using cfhttp, in another cffeed and in yet another perhaps cfpresentation (I can't remember what other tags may use proxy info, but these will do for now). Rather than have to repeat this proxy info for each tag, I will create a proxy structure, perhaps in application scope, containing all my proxy details in the following format:

<cfset myProxy = {    proxyServer="my.proxy.com",    proxyport="8080",    proxyUser="username",    proxyPassword="password"    }>

Now that I have done this, I can simply create the attributes for cfhttp or cffeed, for example, and append this to the structure like so:

<!--- create the feed arguments ---> <cfset myFeed = {    source="http://www.remotesynthesis.com/blog/rss.cfm?mode=short",    query="rsFeed"    }/> <cfset structAppend(myFeed,myProxy) /> <cffeed attributeCollection="#myFeed#" /> <P>
<!--- create the cfhttp arguments ---> <cfset myHttp = {    url="http://www.remotesynthesis.com/blog/rss.cfm?mode=short",    result="rsHttp"    }/> <cfset structAppend(myHttp,myProxy) /> <cfhttp attributeCollection="#myHttp#" />

In both cases, rather than repeat the same proxy info, I just append it. Now, this would be fabulous if every tag that took proxy info used the same attribute names (which seems obvious right?). Sadly, this is not the case. For instance, cfpresentation uses proxyHost rather than proxyServer - why? I don't know. Also, it is important to note that you cannot pass extraneous values in the attributeCollection and have them ignored - meaning if I pass both proxyHost and proxyServer I would get an error.

To solve this, I created a simple UDF that would allow you to simply replace a key name. I actually duplicate the structure in this case so as not to mess up the reference to the original structure. Here is the code:

<cffunction name="structRenameKey" access="public" output="false" returntype="struct">    <cfargument name="structure" type="struct" required="true" />    <cfargument name="keyName" type="string" required="true" />    <cfargument name="newName" type="string" required="true" />        <cfset newStruct = duplicate(arguments.structure) />    <cfset newStruct[arguments.newName] = newStruct[arguments.keyName] />    <cfset structDelete(newStruct,arguments.keyName) />        <cfreturn newStruct /> </cffunction>

Now we can use this function to pass the proxy information to cfpresentation like so:

<cfset myPresentation = {    title="Illudium PU-36 Code Generator"    }/> <cfset structAppend(myPresentation,structRenameKey(myProxy,"proxyServer","proxyHost")) /> <cfpresentation attributeCollection="#myPresentation#"> </cfpresentation>

Offhand I can't seem to think of other common attributes that this method could be used for, but I am fairly certain other uses might exist. If not, it is still useful if you do have to use a proxy and only want to keep that information in a single location. I think it also highlights what a useful and helpful addition the attributeCollection is.

On a side note, I could not seem to get cffeed to work with my proxy information. The same url and proxy info would return fine in both cfpresentation and cfhttp but not in cffeed. I am wondering if this is a bug or if something else is going on. Has anyone else encountered this?

Comments
Rupesh Kumar
Hi Brian, cffeed does support proxy and it should be able to fetch the content if you provide the proxy information. Could you please check once more and confirm that it does not work whereas cfhttp work? Thanks & Regards, Rupesh


Brian Rinaldi
Rupesh, I believe this post was about a pre-release version of 8. I can confirm that proxy does work on cffeed in the final release. Thanks.


Write your comment



(it will not be displayed)