Thursday, December 16, 2010

ColdFusion: You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.

If you are receiving an Error with title "You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members." then make sure that your struct variable is structure everywhere.

An easy way to test your variable's datatype is that you put #IsStruct(VariableName)# in few places above and below the error line to check if your variable is consistently a structure. It is possible that you have re-declared your variable somewhere else.

ColdFusion: Function to create HMAC-SHA1 encrypted string



<cffunction name="hmacEncrypt" returntype="binary" access="public" output="false">

<cfargument name="signKey" type="string" required="true" /> <cfargument name="signMessage" type="string" required="true" />

<cfset var jMsg = JavaCast("string",arguments.signMessage).getBytes("ASCII") /> <cfset var jKey = JavaCast("string",arguments.signKey).getBytes("ASCII") />

<cfset var key = createObject("java","javax.crypto.spec.SecretKeySpec") /> <cfset var mac = createObject("java","javax.crypto.Mac") />

<cfset key = key.init(jKey,"HmacSHA1") />

<cfset mac = mac.getInstance(key.getAlgorithm()) /> <cfset mac.init(key) /> <cfset mac.update(jMsg) />

<cfreturn mac.doFinal() /> </cffunction>

ColdFusion: QueryString to Struct Converter

This function converts QueryString to Struct Data Type.


<cffunction name="QueryStringToStruct" output="false">

<cfargument name="QueryString" required="yes" type="string">

<cfset myStruct = StructNew()>


<cfloop list="#QueryString#" delimiters="&" index="i">

<cfset QueryStringParts = ListToArray(i, "=")>


<cfset structInsert(myStruct, Trim(QueryStringParts[1]),Trim(QueryStringParts[2]))>

</cfloop>

<cfreturn myStruct />

</cffunction>