Archive

Posts Tagged ‘snippets’

ColdFusion + twitter4j 2.2.4 Authentication

October 15, 2011 Leave a comment

After seeing an interesting question about twitter authentication on stackoverflow.com, I decided to take my first look at the Twitter4j library.  The Twitter4j Code Examples and Adding support for automated tweets with OAuth  were particularly helpful.  But apparently version 2.2.4 changed a few things.  So I thought I would post the small tweaks to the example that worked for me:

Code:

<!--- the factory and consumer keys could be stored in the application scope --->
<cfset Twitter = createObject("java", "twitter4j.TwitterFactory").getInstance()>
<cfset Twitter.setOAuthConsumer( "MyConsumerKey", "MyConsumerSecret")>

<cfif NOT structKeyExists(url, "oauth_verifier")>
<!--- // 2. Authorize --->
<cfset Session.RequestToken = Twitter.getOAuthRequestToken( "MyCallbackURL")>
<cflocation url="#Session.RequestToken.getAuthorizationURL()#" addtoken="No">

<cfelse>
<!--- // 3. Authenticate // --->
<!--- Twitter returns a "verifier" key in the callback URL --->
<!--- Use it along with the <span pre="the ">requestToken</span> to extract the accessToken --->
<cfset AccessToken = Twitter.getOAuthAccessToken(Session.RequestToken, url.oauth_verifier)>
<cfset session.StoredAccessToken = AccessToken.getToken()>
<cfset session.StoredAccessSecret = AccessToken.getTokenSecret()>

<!--- Delete the one time <span pre="time ">RequestToken</span> as it is no longer needed --->
<cfset structDelete(session, "RequestToken")>
<cfdump var="#session#" label="Stored AccessToken Values">
<cfabort>
</cfif>
Categories: ColdFusion, Java Tags: ,

ColdFusion: Creating Print Only Watermarks

While <cfpdf> supports watermarks that are visible on screen, but invisible when printing, the reverse does not seem to be supported.  At least not as far as I can tell.  However, with a bit of DDX you can create invisible watermarks.  Using the <Watermark> element and its showOnScreen and showWhenPrinting attributes, you can create watermark’s that are only visible when printed.

Cracker - I Hate My Generation

Cracker - I Hate My Generation

Code:

<cfsavecontent variable="ddxText">
<?xml version="1.0" encoding="UTF-8"?>
<DDX xmlns="http://ns.adobe.com/DDX/1.0/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ns.adobe.com/DDX/1.0/ coldfusion_ddx.xsd">
<PDF result="TargetPdf">
<PDF source="SourcePdf">
<Watermark showOnScreen="false" showWhenPrinting="true" opacity="95%">
<StyledText>
<p font-size="49pt" font-weight="bold" color="#99001A" font="Algerian">
I Hate My Generation
</p>
</StyledText>
</Watermark>
</PDF>
</PDF>
</DDX>
</cfsavecontent>

<cfif IsDDX( ddxText )>
<cfset input.SourcePdf  = ExpandPath("./cracker.pdf") />
<cfset output.TargetPdf = ExpandPath("./invisibleWatermark.pdf") />

<cfpdf action="processDDX"
    ddxfile="#ddxText#"
    inputFiles="#input#"
    outputFiles="#output#"
    name="ddxResult" />

    <cfdump var="#ddxResult#" />
<cfelse>
    Invalid DDX String
</cfif>
Categories: ColdFusion Tags: ,

ColdFusion 9: Another Way to Extract FLV Duration

July 29, 2011 5 comments

I saw an interesting question this week about using Tika with Railo.   If you are like me, and know very little about Tika,  it is described as a library that

    … detects and extracts metadata and structured text content from various documents..

Now despite knowing very little about it, I was pretty sure it was included in ColdFusion 9 (with its  Solr integration).  I took a look around and sure enough ColdFusion 9 includes a slightly older version of Tika (0.6) under the hood.

While reviewing the API I noticed it mentioned a simple FLVParser.  Interesting.  So I decided to give it a whirl.  First I created an instance of Metadata to store the details retrieved from my test file.  Next I loaded my sample file as an input stream.  Finally, I ran the objects through the Tika parser to actually populate the metadata.  Then with a simple call to Metadata.get() I had the file’s duration.

<cfscript>
    path   = "c:/myFile.flv";
    Tika   = createObject("java", "org.apache.tika.Tika");
    meta   = createObject("java", "org.apache.tika.metadata.Metadata").init();
    stream = createObject("java","java.io.FileInputStream").init( path );

    try {
        Tika.parse(stream, meta);
        WriteOutput("<p>duration="& meta.get("duration") &"</p>");
        // ....
    }
    finally {
        stream.close();
    }
</cfscript>

The parser also extracts a few other interesting details like framerate, width, height and a few others. To review all of the properties found,  just invoke MetaData.names() . Then iterate through the array of property names returned.  Keep in mind this class is just a simple parser. But no extra jars are required to use it!


<cfscript>
    // get all properties names returned
    names = meta.names();
    for (i = 1; i <= arrayLen(names); i++) {
       key = names[i];
       writeOutput("<p>"& key &"="& meta.get( javacast("string", key)) &"</p>");
    }
</cfscript>
Categories: ColdFusion, Java Tags: , , ,

ColdFusion 9 – How to Stream a CFSpreadSheet to the Browser

July 29, 2011 1 comment

I have seen this question raised a few times, but found no examples in the documentation.  So in case you were wondering how to stream a spreadsheet to a browser:

Code:

<!--- create your spreadsheet --->
<cfset sheet = SpreadSheetNew()>
<cfset SpreadsheetAddRow(sheet, "foo,bar")>

<!--- stream it to the browser --->
<cfheader name="Content-Disposition" value="inline; filename=foo.xls">
<cfcontent type="application/vnd.ms-excel" variable="#SpreadSheetReadBinary(sheet)#">

 

Categories: ColdFusion Tags: ,