Embedded Cfdocument

I meant to blog this a while back. One of the things I think is really cool about Cold Fusion 7 is the fact that it can dynamically create Flashpaper by way of <cfdocument>. I think there are two big advantages to using Flashpaper over PDF’s in web applications: 1. Since they’re just .swf files, they load much faster; 2. Since they load faster you can embed them in pages. However when you use <cfdocument> the existing website interface is completely replaced by Flashpaper’s interface. I believe there are some cases when you wouldn’t want that. According, I have written a cfc that will allow you to pass content in and embed <cfdocument> created Flashpaper in line.

Check it out: Flashpaper Embedder.

Wikipedia Showdown

I’ve writen before about how sometimes the amount of content in wikipedia on certain subjects disturbs me (here and here.) I have off for the next few days and was looking to work my brain a little bit and wrote this little application that will compare how many characters are written about any two subjects in wikipedia. So for example you can discover that more is written about “blankbabied” then “zombo.com“. So check it out, and come up with and comment on your own crazy showdowns.

Wikipedia Showdown!

In order to do this, I had to rely on CFHTTP to get the job done. I’ve included the cfc that handles grabbing the input from wikipedia in the extended entry.

<cfcomponent>
<cffunction access=”public” name=”stripHTML” output=”false” returntype=”string” hint=”Removes HTML from input string.”>
<cfargument name=”str” type=”string” hint=”String to clean.” required=”yes”>
<cfreturn REReplaceNoCase(str,”<[^>]*>”,””,”ALL”)>
</cffunction>

<cffunction access=”public” name=”weigh” output=”true” returntype=”struct”>
<cfargument name=”search_term” type=”string” required=”yes” hint=”The search term. “>

<cfset search_url=”http://en.wikipedia.org/wiki/Special:Search”&gt;
<cfset return_struct.searchTerm=arguments.search_term>

<cfhttp url=”#search_url#” method=”post” delimiter=”,” resolveurl=”no”>
<cfhttpparam type=”formfield” name=”search” value=”#arguments.search_term#” />
</cfhttp>

<cfif findNoCase(“Search – Wikipedia, the free encyclopedia”, cfhttp.FileContent)>
<cfset return_struct.contents=”There are no records for that search term.”>
<cfset return_struct.length=0>
<cfset return_struct.url=””>
<cfreturn return_struct>
</cfif>

<cfset contents=cfhttp.FileContent>

<cfset contents_start=FindNoCase(“bodyContent”, contents)>
<cfset contents_end=FindNoCase(“catlinks”, contents)>
<cfset contents_len=(contents_end-contents_start)>

<cfset contents=Mid(contents,contents_start, contents_len)>
<cfset contents=stripHTML(contents)>
<cfset contents_crap=14+9>

<cfset contents=Mid(contents, 14, Len(contents) -contents_crap)>

<cfset return_struct.contents=contents>
<cfset return_struct.length=Len(contents)>

<cfset retrieved_location=FindNoCase(“Retrieved from”,contents)>
<cfset article_url=Mid(contents, retrieved_location, Len(contents) – retrieved_location)>

<cfset article_url=replace(article_url,”Retrieved from”, “”, “ALL”)>
<cfset article_url=replace(article_url,””””, “”, “ALL”)>
<cfset article_url=trim(article_url)>
<cfset return_struct.url=article_url>

<cfreturn return_struct>

</cffunction>

</cfcomponent>

Image Manipulation CFC

I was tired of putting so much work to get pictures posted, and my options for third party tools are somewhat limited cause I’m just a wee customer on this host. (I still like them, I just like to do stuff for myself.)

So I went looking for a cfc that would allow for image manipulation. I figured someone must have figured out how to get Java objects to do it. I found Alagad Image Component, which seemed like both functional and cost overkill. But after more digging I found tmt_img.cfc.

Tmt_img does basically everything you need to crop and resize images, so I built a little picture display interface around it so I never have to crop or shrink an image for preview ever again. It reads in an image from one location, checks to see if a preview image has been created for it, and if not create it. It handles the linking, and the sizing, and the writing display html for it. I’ve included my image display cfc below.

<cfcomponent hint=”Handles displaying images.”>

<cfinvoke method=”init”>

<cffunction access=”private” name=”init” output=”false” returntype=”void” hint=”Sets the initial variables. “>
<cfset image_folder=”d:path_to_your_fileswwwrootimages”>
<cfset tn_folder= image_folder & “tn”>
<cfset image_url=”http://www.your_domain.com/images”&gt;
<cfset tn_url= image_url & “/tn”>
<cfobject component=”tmt_img” name=”imageObj”>
</cffunction>

<cffunction access=”public” name=”linkImage” output=”true” returntype=”void” hint=”Show a preview image and link of an image.”>
<cfargument name=”image” required=”yes” type=”string” hint=”The name of the image to preview and link to.”>

<cfset file_type=Right(arguments.image, 3)>
<cfset file_name=Left(arguments.image, Len(arguments.image)-4)>
<cfset tn=Replace(arguments.image, “.#file_type#”, “_tn.#file_type#”, “ALL”)>

<cfdirectory directory=”#tn_folder#” action=”list” name=”directoryList” recurse=”no”>

<cfquery name=”tnFile” dbtype=”query”>
select count(*) as tnExists
from directoryList
where name=’#tn#’
</cfquery>

<cfif Len(tnFile.tnExists) lt 1 or tnFile.tnExists eq 0>
<cfinvoke component=”#imageObj#” method=”resize” returnvariable=”imageProperties”>
<cfinvokeargument name=”source” value=”#image_folder##arguments.image#”>
<cfinvokeargument name=”destination” value=”#tn_folder##tn#”>
<cfinvokeargument name=”newWidth” value=”200″>
</cfinvoke>
</cfif>

<cfinvoke component=”#imageObj#” method=”getDimensions” returnvariable=”imageProperties”>
<cfinvokeargument name=”imgPath” value=”#tn_folder##tn#”>
</cfinvoke>

<cfoutput>
<a href=”#image_url#/#arguments.image#”>
<img src=”#tn_url#/#tn#” alt=”#file_name#” width=”#imageProperties.width#” height=”#imageProperties.height#”>
</a>
</cfoutput>

</cffunction>

</cfcomponent>

More Site Nonsense

I have weird little preferences about my site, and I really wanted my links section to to come from my list of RSS feeds. But I use Sharpreader which is is a desktop client so I can’t just pull my links in from another site., or publish, or work any other magic. However, Sharpreader can export to an OPML file, which is just a variant of XML. So I should be able to download a CFML OPML parser. Turns out that by “download,” I mean “construct.” Since I couldn’t find one, I’ve included the source to make it happen. Enjoy, don’t enjoy, use, don’t use, whatever.

<cffunction access=”public” name=”display_opml” output=”true” returntype=”void” hint=”Takes an opml file and outputs a list of links. “>
<cfargument name=”opml_file” type=”string” required=”yes” default=”” hint=”The file location of the opml file to display.”>

<cftry>
<cffile action=”read” file=”#arguments.opml_file#” variable=”feeds”>
<cfset feedsXML=XMLParse(feeds)>

<cfoutput>
<cfloop index=”i” from=”1″ to=”#ArrayLen(feedsXML.opml.body.outline)#”>

#feedsXML.opml.body.outline[i].XmlAttributes.title#

Https Sockets in CFC

A while back I needed to interact with raw https sockets from Cold Fusion, and the https setting for CFX_RawSocket stopped working. (I think I configuration change on the target machine made this process fail.) Unable to deal with trying to get support for a free product, I felt compelled to write my own way of doing it. Some one in my office had writted a CFX tag very similar to CFX_RawSocket, so I had a starting point.

Turns out, it can be done completely without resorting to CFX writing. It can be done completely using Cold Fusions built in ability to handle Java Objects. I figured I would share the source if anyone else needed it.

<!— ############# —>
<!— https_socket —>
<!— ############# —>
<cffunction access=”package” name=”https_socket” output=”false” returntype=”string” hint=”Allows socket communication over https to occur. “>
<cfargument name=”host” type=”string” required=”yes” hint=”The host to send message to. “>
<cfargument name=”message” type=”string” required=”yes” hint=”The message to send to the host.”>
<cfargument name=”port” type=”numeric” required=”no” default=”443″ hint=”The port to connect to. ” >

<!— Use the sslfactory to create an object based on the Socket class —>
<cfset factory = CreateObject (“java”,”javax.net.ssl.SSLSocketFactory”).getDefault()>
<cfset sock = factory.createSocket(arguments.host,arguments.port)>
<cfset sock.startHandshake()>
<!— Connect to output stream —>
<cfset sout = sock.getOutputStream()>

<!— Connect to input stream —>
<cfset out = createObject( “java”, “java.io.PrintWriter” ).init(sout)>
<cfset sinput = sock.getInputStream()>

<!— I have no clue what is going here. I just know it has to be done this way. —>
<cfset inputStreamReader= createObject( “java”, “java.io.InputStreamReader”).init(sinput)>
<cfset input = createObject( “java”, “java.io.BufferedReader”).init(InputStreamReader)>

<!— Send message to server. —>
<cfset out.println(“#arguments.message#”)>
<cfset out.println()>
<cfset out.flush()>
<!— Get back response. —>
<!— I only need the response header, so I only get the first line —>
<cfset results=input.readLine()>

<cfset sock.close()>

<cfreturn results>
</cffunction>

ColdFusion Blackstone Coolness

So I’ve been testing Macromedia’s next version of Cold Fusion, named: Blackstone.
I was writing a component, and used the self-doccumenting feature of cfc’s to create a little documentation. However when I went to print it, it came out all crappy. For some reason these things never print right.

So I got to thinking wouldn’t it be cool if you could output this documentation in flashpaper, since that’s one of the features of Blackstone. Then I took a look at the url for the documentation, I noticed that the renderer for the cfc documentation was a cfc named “cfcexplorer.cfc,” and the documentation was created using the “getcfcinhtml” mehtod. Since the cfc was installed on myserver I opend it up and saw this:

<cffunction name=”getcfcinhtml” access=”remote”
hint=”Generates html descriptor of a component with the specified name or URI path as the http response.”>
<cfargument name=”name” type=”string” required=”yes” />
<cfargument name=”path” type=”string” required=”no” />

<cftry>
<cfscript>
if ( IsDefined(‘arguments.path’) and arguments.path neq ” ) {
proxy = CreateObject( “java”, “coldfusion.runtime.TemplateProxyFactory” ) ;
comp = proxy.ResolvePath( arguments.path, getPageContext() ) ;
} else {
comp = CreateObject( “component”, name ) ;
}

utils = CreateObject( “component”, “utils” ) ;
WriteOutput( utils.cfcToHTML(comp) ) ;
</cfscript>
<cfcatch type=”coldfusion.runtime.CfJspPage$NoSuchTemplateException”>
<cfoutput><h4>Component not found</h4>
The component definition file for component ‘#name#’ cannot be found on this server.</cfoutput>
</cfcatch>
</cftry>
</cffunction>

So I got to thinking, couldn’t I just wrap the whole thing in a <cfdocument> tag. I tried it, and it worked.

<cffunction name=”getcfcinfp” access=”remote”
hint=”Generates flashpaper descriptor of a component with the specified name or URI path as the http response.”>
<cfargument name=”name” type=”string” required=”yes” />
<cfargument name=”path” type=”string” required=”no” />

<cftry>
<cfdocument format=”flashpaper”>

<cfscript> if ( IsDefined(‘arguments.path’) and arguments.path neq ” ) {
proxy = CreateObject( “java”, “coldfusion.runtime.TemplateProxyFactory” ) ;
comp = proxy.ResolvePath( arguments.path, getPageContext() ) ;
} else {
comp = CreateObject( “component”, name ) ;
}

utils = CreateObject( “component”, “utils” ) ;
WriteOutput( utils.cfcToHTML(comp) ) ;
</cfscript>

</cfdocument>
<cfcatch type=”coldfusion.runtime.CfJspPage$NoSuchTemplateException”>
<cfoutput><h4>Component not found</h4>
The component definition file for component ‘#name#’ cannot be found on this server.</cfoutput>
</cfcatch>

</cftry>
</cffunction>

Once that’s done, all you have to do is change the method called in the URL when you browse your cfc from “cfcexplorer.cfc?method=getcfcinhtml” to “cfcexplorer.cfc?method=getcfcinfp”

Cool huh? From the flashpaper, it prints beautifully.