Shaun Mccran

My digital playground

19
F
E
B
2009

Consuming 360 Voices XML data feeds - 360 Voice part 2

www.360voice.com hosts a service where you can interrogate your GamerTag through an XML web feed. They host the service themselves, but also provide an API so that you can remotely call it, and use it however you want. So I thought I'd consume their service using ColdFusion, translate it, and display it here. Firstly I want to try and reduce the service overhead. So I will call the 360 voice service on the first instance of page initialisation, and then write the result to a file. The service only updates once a day, so I can safely assume that caching it daily isn't going to be too out of date. Firstly I've setup some global variables to set the file path location.
view plain print about
1<!--- setup the filename --->
2<cfset variables.filepath = GetBaseTemplatePath()>
3<cfset variables.filepath = replace(variables.filepath, 'include.cfm', '', 'all')>
4<cfset variables.todaysfile = variables.filepath & "tmp\#DateFormat(NOW(), 'dd-mm-yyyy')#.xml">
Then check for the existence of a file with todays date as the name. If the file exists, read it and use it, otherwise make a cfhttp call to the url, passing in any of the filtering url variables that the 360 voice API documents, in this case just my gamer tag.
view plain print about
1<cfif fileExists(variables.todaysfile)>
2    <!--- Read local file --->
3    <cffile action="read" file="#variables.todaysfile#" variable="xmlfile">
4    <cfscript>
5        xmlfile = xmlparse(xmlfile);
6    
</cfscript>    
7<cfelse>
8    <cfhttp url="http://www.360voice.com/api/blog-getentries.asp?tag=ect0z" method="GET" charset="utf-8">
9        <cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
10        <cfhttpparam type="Header" name="TE" value="deflate;q=0">
11    </cfhttp>
12    <cfscript>
13        xmlfile = xmlparse(cfhttp.filecontent);
14    
</cfscript>    
15    <cffile action="write" file="#variables.todaysfile#" output="#xmlfile#">
16</cfif>
In the code above I am also using two cfhttpparams to deflate the return response from the service, as I was having issue with this in a compressed format. (Read more here). Now that we have the content and we've parsed it out into an XML object we need to search through and pick out the elements we want. First we write out the header details from the parent node of the xml document. Create an Array, and map the contents to the child node you want. In this case the "api.info" node. Doing this allows you to treat the previous XML object as a standard Array, so we can loop over it, and pick out the elements we want.
view plain print about
1<cfset arrHeader = xmlfile.api.info>
2<cfoutput>
3    <cfloop index="i" from="1" to="#ArrayLen(arrHeader)#">
4    <img src="#arrHeader[i].tile.XmlText#" alt="Gamer Icon"> - 360 Voice.com Blog
5    <!--- #arrHeader[i].link.XmlText# --->
6    </cfloop>
7</cfoutput>
Now we will do much the same thing with the blog contents, but using a different XML child Node for the Array.
view plain print about
1<cfset arrEntries = xmlfile.api.blog.XmlChildren>
2<cfoutput>
3    <cfloop index="i" from="1" to="#ArrayLen(arrEntries)#">
4        <b>#arrEntries[i].title.XmlText# -#arrEntries[i].date.XmlText#</b><br/>
5        #arrEntries[i].body.XmlText#
6        <p><br/></p>
7    </cfloop>
8</cfoutput>
Again just loop through the Array, picking out the elements you want. Next I will add pagination, and possible look at some of the other data that 360 voice stores in its API, such as gamer badges etc.... but that's another article.
18
F
E
B
2009

"The Register" highlights software convergence security issues

The Register news site has just released an important article (Google gears hack attack) that highlights a modern danger with software development. Previously it was quite clear cut as to the platform of your application, IE is it a desktop app, or a web app, or a citrix client app etc. But with the convergence of several modern technologies such as Adobe's AIR and Google's GEARS some of the previous boundaries are becoming blurred.

No longer is an application a single, gateway specific, platform bound instance. As architects we must now encompass new potential security issues that were previously reserved for differing platforms. With the migration of applications across multiple platforms and previous technology boundaries this is something we must all keep in the forefront of software development.

I know I will during the planning phases of my next AIR app.

18
F
E
B
2009

Reading a file using a Java object

Sometimes in Coldfusion development its handy to use coldfusions standard functions such as CFfile or CFdirectory. But you can also invoke the underlying Java objects that share similar functionality. These can often be more efficient too, as there is no coldfusion server compilation layer involved.

For one piece of work I had recently I was reading a series of files, parsing them out and inserting a the collection of records into a database.

So I created used a java file reader object to read the files.

There is the odd extra config line in here, as the server pathing is unix based not windows based, and I'm dynamically reading the files from the directory.

view plain print about
1<cfset variables.dir = cgi.path_translated>
2<cfset variables.replaceVar = listlast(variables.dir, '\')>
3<cfset variables.dir = replaceNoCase(variables.dir, variables.replaceVar,'', 'all')>
4<cfset variables.filename = variables.dir & "datafile#url.fileNo#.txt">

Next I use the file name variable in the java FileReader object call. I wrap the file reader in a BufferedReader object as this greatly reduces the operation overhead of the FileReader object as the data is buffered into memory and not read from the disk.

Next I'll loop over the outptut string from the buffered file reader object, parsing each line and invoking the cfc method of 'processContent', passing in the string (line of data) as the argument 'content'. '

view plain print about
1<cfscript>
2        cnt = 0;
3        srcFile = variables.filename;
4    
5        // create a FileReader object
6
        fr = createObject("java","java.io.FileReader");
7        fr.init(srcFile);
8    
9        // create a BufferedReader object
10
        br = createObject("java","java.io.BufferedReader");
11        br.init(fr);
12        str = br.readLine();
13    
14        // writeOutput(str);
15
        while isDefinedd("str")) {
16            cnt = cnt + 1;
17            str = br.readLine();
18                if(isDefined("str")){
19                    //writeOutput(cnt);
20
                    //writeOutput(str);
21
                    // pass to CF to do the rest
22
                    processContent(str);
23                }
24            }
25        br.close();
26        //writeOutput(cnt);
27
    
</cfscript>

Now you can perform any sort of parsing you want to perform on the content. In the example below I am creating a local structure and populating it with each delimited value. This allows me to use standard notation to pick out individual elements for whatever processing you want.

view plain print about
1<cffunction name="processContent" output="true" hint="function for processing the file content">
2        <cfargument name="content" type="string">
3    
4        <cfset var local = structnew()>
5    
6        <cfset local.today = now()>
7
8        <cfset local.filecontent = replace(arguments.content,'\n',"", "all" )>
9    
10        <!--- list to array based on tab --->
11        <cfset local.data = local.filecontent.split("\t")>
12        <cfset local.variableName1 = local.data[1]>
13        <cfset local.variableName2 = local.data[2]>
14        
15        Etc...
16
17    </cffunction>

17
F
E
B
2009

Creating an MS Dos style text file directory listing - invoked with cfexecute

I have had a need in the past to list directory contents, and filter on the extension type within that directory.

You can do this using cfdirectory, but you can also use cfexecute to run a batch file, which in my experience is a quicker solution than having coldfusion reading large directories.

Create a batch file like this:

view plain print about
1dir \yourdir\*.* /s/b >dir.txt

"\yourdir\*.*" is optional you can filter this by file type by adding a file extension. EG \ or \*.log or \logs\*.*

"dir.txt" is the filename you want the listing to be stored in, choice of name/location is up to you. You can write it out to any directory that the server has permissions on.

eg mylogs.dat or \mylogs\list.txt

_UNKNOWNTRANSLATION_ /