Adam Tuttle

ColdFusion Bug with image functions on OSX with Java7

Yesterday afternoon I found myself getting the very same exception message as this bug report from May 2014: java.lang.ClassNotFoundException:

What sort of horrible monstrosity of code was I attempting to slip by ColdFusion?

var img = imageRead( imagePath );

Oh, the humanity.

So, what crimes am I guilty of, thus causing this error? I'm using Java 7 (build 1.7.0_60-b19, to be precise) on OSX Mavericks (10.9.4 build 13E28, to be precise).

Let's see:

I guess my JDK is slightly ahead of the officially supported version, but something tells me that downgrading my JDK would be a waste of my time: I doubt it would work on 1.7.0_15. Here's the download, if anyone wants to try it, though. The reason I believe it would be a waste of my time is that the issue is marked Unverified and otherwise has had no adobe contact, at all.

If you also happen to be a ColdFusion developer that uses a Mac with OSX 10.9 (Mavericks), could you maybe lend a hand and go vote for this bug? I'd appreciate it.

Published 2014-08-12 @ 08:00 in ColdFusion

To Scope, or Not To Scope

Adam Cameron has another of his surveys going, about your personal taste in variable scoping. It's something I've written about in the past, but my approach has evolved since then, so I figure it's worth writing about again.

Previously my position was that all references (if-statements, display, etc.) should be scoped, while it was not strictly necessary for variable definition (foo=42) — unless required to actually put the variable where you want it, e.g. in Request scope. I still think this approach has merit, and for the same reasons (you never find yourself asking, "ok, but WHERE is the variable foo coming from?") but its primary drawback is verbosity.

Since then, CFScript has matured (which is not to say that it "is mature"), and I find myself writing more JavaScript than CFML/CFScript, anyway. So, it's only natural that my personal style when writing CFML/CFScript is starting to take on the appearance of my JavaScript, too.

This means that I am tending to use traditional var foo = 42; statements in my CFCs over my previously-preferred style of var local = {}; = 42;. I preferred the latter because of its explicitness. As of CF9 we no longer needed the var local = {}; but I still find littering my code with dozens of local. prefixes to be a bit verbose, cumbersome, and at times distracting.

Another part of the evolution of my style has been that my code gets smaller and more decoupled (and in many cases, functional), which means the surface area of any given function is smaller and more manageable, so we don't need as much brain power to keep everything in our heads at once. Since I've got some additional brain bandwidth available from that change, I find myself preferring the more terse var-scoped approach, because I love terseness. This sort of creates a feedback loop: By writing smaller, more discrete functions, I'm able to use a more terse syntax, which takes up less space (mentally and physically), which itself makes the functions smaller and more manageable.

In the end, my preference is still that if you can't look at a variable and immediately know where its value was set — even when reviewing the code 2 years later — then you need to be more explicit. It's just that my tooling and my general style have enabled me to reduce the amount of time that extreme specificity is required.

Tools help! I can look at a FW/1 controller and tell you that a variable named is input from the user: a form or url variable. Likewise, this means that things not prefixed with rc. are not user-generated and are then more likely to be var-scoped. My controllers don't use anything variables-scoped except injected services, so those service methods are prefixed by the service name: fooService.getFoos();. I could write variables.fooService.getFoos() but what's the point? All in all, those controllers are very easy to follow. (Well done, Sean!)

Likewise, my FW/1 services use injected service dependencies and no other variables-scoped data. If I need a config setting, it's available through my configService. So knowing this, all unscoped variables inside my functions are (better be!) var-scoped locals.

But that's just my personal style. What is yours like? (And hey, take a minute to fill out Adam Cameron's Survey, too!)

Published 2014-07-24 @ 09:33 in Best Practices ColdFusion

Idiosyncrasies of using CFContent to send files to the client in ColdFusion

I've found some idiosyncrasies in using CFContent to deliver files to the browser with ColdFusion (e.g. shielded downloads), so I thought I would share them here.

I have only tested what I'm about to share in ColdFusion 10, but I have no reason to believe that it will be any different with other versions, as it appears to be entirely how things are handled client-side.

I can never remember what the appropriate header name is for specifying the file name, so the first thing I did was google for it. That took me to Ben Nadel's blog post on streaming files -- a slightly different matter, but it had the answer I was after:

<cfheader name="content-disposition" value="attachment; filename='filename.xlsx'" />

This worked great, at first, in Chrome. When I got around to various browser tests, I found that if my file name contained a space, as in file name.xlsx then Firefox would save it as 'file (no extension -- nothing after the space). Equally as odd, Safari got the name correct (file name.xlsx) but appended .html.

Changing the single quotes to double quotes fixes FireFox's quirk, and adding a Content-Type header fixes Safari's, so the final result looks like this:

<cfheader name="Content-Disposition" value="attachment; filename=""#rc.fileName#""" />
<cfheader name="Content-Type" value="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
<cfcontent reset="true" file="#rc.filePath#" deletefile="true" />

It's 2014... We can make an almost-convincing hoverboard hoax but we still need to test everything in every browser on every platform.

Published 2014-07-18 @ 09:05 in ColdFusion

CFHTTP SSL Stopped Working after OSX Mavericks Upgrade? Here's Why!

Like so many before me, I've recently been banging my head against issues with CFHTTP and SSL, particularly only on Mac OS X. Eventually I landed on the right Google phrase and read enough links to find this gem, which while only tangentially related, brought my memory back in a flash.

Apple, in their infinite wisdom, decided to stop distributing Java with OSX. And I hadn't realized it at the time that I upgraded, because ColdFusion continued to function, but the upgrade deleted much of the Java JDK, including (you're probably guessing right...) the cacerts file. Not even Verisign signed certificates are trusted if you don't have any cacerts file.

It wasn't until I figured out that my cacerts file (and many other bits of the JDK) were missing that I found the Jira ticket that solved the puzzle.

Thanks, Apple!

Here's what you can do about it: ColdFusion 10 now officially supports Java 7 (as of update 8), so you need to upgrade.

Being a fan of minimalism (e.g. not installing tons of useless junk) I opted to download the Java SE 7 JRE package -- because the JRE is for people who want to RUN java apps, while the JDK is for people who want to WRITE java apps -- for OSX, and installed it; though I was perplexed that it didn't let me choose an install location, or even tell me what it was after it was complete. I had to find out via Stack Overflow -- and as indicated in the comments there, using a JRE installed into the Internet Plugins folder to run your app server isn't exactly the brightest idea.

Right then... Thanks, Oracle!

With that great idea flushed down the toilet, I gave up any hope for sanity and downloaded the Java SE 7 JDK. Again, it didn't let me choose an install location, or bother telling me where it installed. Thankfully, someone else in the same Stack Overflow thread mentioned that the command /usr/libexec/java_home -v 1.7 will let you find the location of an installed JDK by version number; and it turned out that mine was installed to: /Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home.

Next we need to tell ColdFusion to use this new JDK instead of the kneecapped one that Apple left behind during the Mavericks upgrade. ColdFusion gives us a decent UI for updating the value, but stores it in the file {CF_ROOT}/cfusion/bin/jvm.config, so make a backup of this file before you change anything. And don't delete your old JDK yet. If something goes wrong, you can just put back your backup of jvm.config to point CF back at the old JDK.

With your backup made, open your CF Administrator and navigate to Server Settings > Java and JVM. Next to Java Virtual Machine Path tap the Browse Server button. (Sure, you could paste it in, but why risk a stray space or missing slash, etc screwing things up?) Find the folder listed as the result of the java_home command earlier and select it.

Then just restart ColdFusion and you're good to go! (If you're not, copy your backup of jvm.config back into {CF_ROOT}/cfusion/bin/jvm.config and restart CF again...)

Verify that you're now on Java 7 by clicking the small "i" icon in the upper-right of the CF Administrator, and checking the value of Java Version -- you want to see 1.7.0_60 or later.

I hope I never have to go through that again, and I hope maybe it helps a few of you out there.

Published 2014-06-11 @ 08:25 in Apple ColdFusion