January 9, 2008

The TrueBone Band - Lets Stay Together!

Filed under: ColdFusion — Phillip Holmes @ 11:25 pm

February 6, 2007

Server to Server Certificate Presentation

Filed under: ColdFusion, Java — Phillip Holmes @ 12:25 pm

Some application service providers and payment systems (such as Global Collect) require you to present a mutually agreed upon SSL certificate to them in order to authenticate and use their services. This is certainly a one-off scenario and that is what the I.T. posts on my blog are all about.

The code below is written in Java and called as a servlet via ColdFusion (which is another API for Java). Anyway, it presents the certificate via a direct call to the path. During my research on this challenge, I also saw that you can use the Java certificate store to present the certificate. However, I could never get ColdFusion to do this. I followed recommendations by Sun documentation and other blogs but after spending days trying to figure it out, I just settled for calling the direct path to the certificate.

There are a couple advantages to the 'direct path' method (aside from the fact that it works 100% of the time). First, you could actually just make the path an argument in the function and keep the key wherever on the box. Secondly, the 'direct path' call relieves the server admininistrator of having to import the SSL key to the java certificate store for every new client or update ones that are there. This would make a shared host scenario hard to manage. This way, users can just FTP their certs up and supply the path to the servlet.

Anyway, enough rambling... here it is.. Hope it helps you.

CODE:

  1. /*
  2. *
  3. * Created on April 05, 2006, 10:10 AM
  4. * Author: Phillip B. Holmes
  5. */
  6.  
  7.  
  8. import java.net.*;
  9. import java.io.*;
  10.  
  11. public class ReadHttpsURL {
  12.     public static String uri;
  13.     public static String XML;
  14.    
  15.     public static void main(String args[]){
  16.        ReadHttpsURL obj = new ReadHttpsURL();
  17.        obj.readit(uri,XML);
  18.     }
  19.    
  20.       public String readit(String uri, String XML) {
  21.         try{
  22.             System.setProperty("javax.net.ssl.keyStore", "d:\\cfusion\\runtime\\jre\\lib\\security\\ssmt.pfx");
  23.             System.setProperty("javax.net.ssl.keyStorePassword", "your_certs_password");
  24.             System.setProperty("javax.net.ssl.keyStoreType", "pkcs12");
  25.             System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");
  26.             String message;
  27.             URL url = new URL(uri);
  28.            
  29.             URLConnection conn = url.openConnection();
  30.             conn.setDoOutput(true);
  31.             conn.setRequestProperty("Content-Type", "text/xml");
  32.             BufferedWriter out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
  33.  
  34.             out.write(XML);
  35.             out.flush();
  36.  
  37.             // Get the response
  38.             BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  39.             String line;
  40.             StringBuffer sb = new StringBuffer();
  41.             while ((line = in.readLine()) != null) {
  42.                sb.append(line);
  43.             }
  44.             message = sb.toString();
  45.  
  46.             in.close();
  47.             out.close();
  48.            
  49.             return message;
  50.  
  51.         } catch (Exception e) {
  52.               String message = "exception: " + e.getMessage();
  53.               return message;
  54.         }
  55.    }
  56. }

November 22, 2006

alphaCap() 2.01

Filed under: ColdFusion — Phillip Holmes @ 10:20 am

This bit of code is a total rewrite from some of the code library we used back in the days of my old CF hosting and development operation (Media Resolutions Inc.). Version one was written by Jerry Martilik. However, this version offers substantial structure changes / improvements and a list of words that are exceptions combined with an additional exception argument.

CODE:

  1. <cffunction name="stringToArray" returntype="array" output="false" access="public">
  2.      <cfargument name="str" required="yes" type="string" />
  3.      <cfscript>
  4.         var ar = Array();
  5.         for(i=1; i lte Len(str); i=i+1){
  6.            ar[i] = mid(str,i,1);
  7.         }
  8.         return ar;
  9.      </cfscript>
  10. </cffunction>
  11.  
  12.  
  13. <cffunction name="alphaCap" returntype="string" output="true" access="public">
  14.     <cfargument name="str" required="yes" type="string" />
  15.     <cfargument name="exceptions" required="no" type="string" default="" />
  16.     <cfscript>
  17.     // cap and lowers string handed to it (for titles and such)
  18.         var result ='';
  19.  
  20.         if(len(trim(arguments.exceptions)) EQ 0) {
  21.             lst_exception = 'an,the,at,by,for,of,in,up,on,to,and,as,but,if,or,nor,a';
  22.         } else {
  23.             lst_exception = 'an,the,at,by,for,of,in,up,on,to,and,as,but,if,or,nor,a,' & arguments.exceptions;
  24.         }
  25.  
  26.         for(i=1; i lte ListLen(trim(str),' '); i=i+1) {
  27.             str_first = '';
  28.             str_last = '';
  29.             str_word = ListGetAt(Trim(str),i,' ');
  30.             count = len(trim(str_word));
  31.             ar = ArrayNew(1);
  32.             ar = stringToArray(trim(str_word));
  33.             for(j=1; j LTE count; j=j+1) {
  34.                 if(ar[1] IS ar[j]) {
  35.                     if(i NEQ 1 AND listfind(lst_exception,str_word,',') GT 0) {
  36.                         str_first = ar[1];
  37.                     } else {
  38.                         str_first = UCase(ar[1]);
  39.                     }
  40.                 } else {
  41.                     str_last = str_last & lcase(ar[j]);
  42.                 }
  43.             str_temp = str_first & str_last;
  44.             }
  45.             result = result & ' ' & str_temp;
  46.         }
  47.         return trim(result);
  48.     </cfscript>
  49. </cffunction>

June 29, 2006

Search Your CF OO Hierarchy with getMetaData()

Filed under: ColdFusion — Phillip Holmes @ 11:18 pm

This is a useful little function I wrote that will recursively search your OO class tree by it's meta data. All you have to do is put this in your base class and then you're off to the races.

It will return the instance and state of the properties in the object you're looking for.

CODE:

  1. <cffunction name="getInstance" returntype="any" access="public">
  2.   <cfargument name="nameType" type="string" required="no" default=""/>
  3.   <cfargument name="returnType" type="string" required="no" default=""/>
  4.   <cfargument name="accessType" type="string" required="no" default=""/>
  5.   <cfargument name="roleType" type="string" required="no" default=""/>
  6.   <cfscript>
  7.     var ret = structNew();
  8.     ret.error = arrayNew(1);
  9.     ret.success = true;
  10.     ret.msg = 'SUCCESS';
  11.     ret.data = structNew();
  12.     try {
  13.          if(len(trim(arguments.returnType)) GT 0 OR
  14.             len(trim(arguments.accessType)) GT 0 OR
  15.             len(trim(arguments.roleType)) GT 0 OR
  16.             len(trim(arguments.nameType)) GT 0) {
  17.             for(it in this) {
  18.               retMeta = getMetaData(this[it]);
  19.               if((isDefined('retMeta.returnType') AND
  20.                 retMeta.returnType IS arguments.returnType) OR
  21.                 (isDefined('retMeta.access') AND
  22.                 retMeta.access IS arguments.accessType) OR
  23.                 (isDefined('retMeta.role') AND
  24.                 retMeta.role IS arguments.roleType) OR
  25.                 (isDefined('retMeta.name') AND
  26.                 retMeta.name IS arguments.nameType)) {
  27.                      ret.data[it] = this[it];
  28.             }
  29.           }
  30.         } else {
  31.          ret.success = false;
  32.          ret.msg = 'FAIL';
  33.          arrayAppend(ret.error,'MISSING_ARGUMENT');
  34.         }
  35.        return ret;
  36.     } catch(any excpt) {
  37.       ret.success = false;
  38.       ret.msg = 'FAIL';
  39.       arrayAppend(ret.error,excpt.message);
  40.       return ret;
  41.     }
  42.   </cfscript>
  43. </cffunction>

May 4, 2006

Review of VIUX Hosting

Filed under: ColdFusion, SysAdmin — Phillip Holmes @ 3:29 pm

I wanted to write a quick note to consumers / developers out there considering going with VIUX for hosting. Do NOT host your site with VIUX.com. You will regret it.

Within about 5 months at VIUX, I had to open over 20 trouble tickets, some of which were reopened almost a dozen times.

Here are the problems I experienced:

1. MySQL constantly timing out

2. extremely slow PHP page loads on my site (even those without db calls)

3. excessive downtime caused by failed services that were not counted as 'official downtime' by VIUX, but rendered my site inoperable nonetheless. Since they did not count this as downtime, I received no free month (as described by their own SLA) as a result

4. control panel was extemely slow and also constantly timed-out

5. support staff that went out of their way to try to blame their outages on the client instead of simply investigating the issue and finally resolving it.

6. misses commitment deadlines / dates to resolve these and other issues (by months).

7. webtrends metrics constantly failed (two or three times per week) and would be down several days until I prompted them to kick the box

Summary: Do not host your website with VIUX unless you really dislike your client. :)

March 18, 2006

innerHTML with ColdFusion

Filed under: ColdFusion, JavaScript — Phillip Holmes @ 3:50 pm

I hear lots of hype about AJAX these days but actually this technology is nothing new. While the use AJAX is a great tool, AJAX and the type of functionality it makes available has been around for many years. AJAX has simply standardized the model. Writing dynamic data to the DOM is really no big mystery and does not require anything more than old fashioned JavaScript and your favorite server side language. My advice about AJAX is to carefully consider what your application needs instead of running in and implementing it because it is the new buzzword or just because you can.

Here is an example of data that comes from a couple CF arrays and is then dynamically inserted into a JavaScript array. This data is then passed to a layer which utilizes the innerHTML tag to display it.

CODE:

  1. <cfscript>
  2.      arProdIDs = arraynew(1);
  3.      arProdIDs[1] = 240;
  4.      arProdIDs[2] = 256;
  5.      arProdIDs[3] = 257;
  6.      arPrice = arraynew(1);
  7.      arPrice[1] = 9.99;
  8.      arPrice[2] = 10.99;
  9.      arPrice[3] = 11.99;
  10.     intCount = arrayLen(arProdIDs);
  11.     intDiscount = 100;
  12. </cfscript>
  13. <script>
  14.      var products = new Array();
  15. </script>
  16. <cfscript>
  17.       for(i=1; i lte intCount;i=i+1) {
  18.            if(i EQ 1) {
  19.                 writeOutput('<script>');
  20.            }
  21.            writeOutput('products[#arProdIDs[i]#] = #arPrice[i]#;');
  22.            if(i EQ intCount) {
  23.                 writeOuput('</script>');
  24.            }
  25.       }
  26. </cfscript>
  27. <script>
  28.     function changeText(obj_input) {
  29.          intDiscount = #intDiscount#;
  30.          for(it in products) {
  31.               if(obj_input==it) {
  32.                    document.getElementById('dynaText').innerHTML =  intDiscount - products[it];
  33.                }
  34.           }
  35.      }
  36. </script>
  37. <div id="dynaText"></div>
  38. <form action="thispage.cfm" method="post" name="myform">
  39.      <cfloop from="1" to="#IntCount#" index="i">
  40.           <cfoutput>
  41.                <input type="radio" name="prodid" value="#arProdIDs[i]#" onclick="changeText(this.value)"/> #arProdIDs[i]#<br />
  42.           </cfoutput>
  43.      </cfloop>
  44.      <br />
  45.      <input type="submit"/>
  46. </form>

With ColdFusion 7, the export of dynamic data from ColdFusion to JavaScript has been made much easier with the addition of the toScript() function. I'll be posting more about toScript() and also about how to dynamically populate select menus without refreshing the page soon, so stay tuned!

March 3, 2006

try, catch, finally within cfscript

Filed under: ColdFusion, Java — Phillip Holmes @ 11:40 pm

Whenever I look at my blog's statistics, I always see cf'ers searching for try catch examples within cfscript. So far, the ColdFusion portion of my blog has featured 'one off' code for odd situations and such. I completely forgot that most people that are looking for examples like try catch instead. What I've decided to do here is feature a little more than my stats log has requested by listing a try catch a couple languages so you can see how CF stands up to a full blown programming languages like Java or C#.

-----

A Java or C# try catch would go something like this:

CODE:

  1. try {
  2.      // the code you want to try to run
  3.      } catch (SomeException e) {
  4.     // code to run when their is an error
  5. }
  6. finally {
  7.   // This is code that runs regardless of the error.
  8. }

-----

This is the ColdFusion equivalent:

CODE:

  1. try {
  2.      foo = 1;
  3.      } catch(any excpt) {
  4.      foo = 2;
  5. }

-----

CODE:

  1. function finally() {
  2.      foo = 3;
  3.      return foo;
  4. }
  5. try {
  6.      foo = 1;
  7.      } catch(any excpt) {
  8.      foo = 2;
  9. }
  10. foo = finally();

-----

So, there it is! I hope that helps those folks out there looking for that example! Thanks for visiting and please come back often as I update frequently.

January 29, 2006

Email Validation with CFMX & Java

Filed under: ColdFusion, Java — Phillip Holmes @ 3:59 pm

This little gadget will not only validate the syntax of an email address of a user, but also will use java.net.InetAddress to make sure the domain they are using is valid. This is a handy tool to cut down the number of fake email addresses your application may receive. I have included the source for you to review and change if you like.

You may want to add the ability to actually validate the account as well as the domain. This is possible by using Javamail or other third party classes to instantiate VRFY() function or like functionality on the address, but I do not recommend this because some servers disable this command.

I compiled emailValidator.jar using Netbeans 4.0. I found that the latest version of Netbeans will not create compilations that are compatible with CFMX. I suspect this is due to the latest JVM update. Testing so far has revealed that CFMX will only run under Sun's JVM version 1.4.2_10 and under. So, in other words CFMX does not support J2SE 5.0 as of this post. However, I am sure that will be included in an updater of CFMX 7 or future iteration of CFMX. Here are all the J2SE 1.4.2 downloads and reference docs. That page also has the JRE.

Anyway, enough said.. here it is. You'll need winrar to decompress it.

January 25, 2006

Calculating Age From Date of Birth with Leap Year

Filed under: ColdFusion — Phillip Holmes @ 3:27 am

This is a pesky little task that gets overlooked from time to time in applications that need to do things like notify clients on their birthdays or display an 'age' field based on date of birth. There a few ways to handle this. Here are a couple ways I decided to approach the problem.

By the way, here is a great little article that explains a little bit about fundamental celestial mechanics. Giving that a once over will help you understand what is going on with our scripts.

This first example finds the lapse days based on a simple equation that divides your year difference by 4. The value (intSlippage) is then subtracted from your total days because that is the number of days you have slipped from the year you were born. It compensates for people under 4 years old because they have slipped less than one day. Most likely, your application is not going to care about a few hours.

CODE:

  1. intHours = dateDiff('h',arguments.dob,now());
  2. intDays = intHours / 24;
  3. intSlippage = dateDiff('yyyy',arguments.dob,now()) / 4;
  4. if(intSlippage LTE 1) {
  5.      intYears = (intDays - 1) / 365;
  6. } else {
  7.      intYears = (intDays - intSlippage) / 365;
  8. }
  9. return intYears = fix(intYears);

The second example actually uses the tropical year formula which is the actual equinox calculation. You can find the actual equation used to arrive at 365.242190 in the link I have provided above. This example is actually more efficient:

CODE:

  1. intHours = dateDiff('h',arguments.dob,now());
  2. intDays = intHours / 24;
  3. intYears = intDays / 365.242190;
  4. return intYears = fix(intYears);

January 21, 2006

java.nio.charset.Charset

Filed under: ColdFusion, Java — Phillip Holmes @ 1:48 pm

Sun's Charset class reference

Every so often, you may have to deal with international data that has not been stored properly in an ntext or nvarchar field. Generally, this data will be stored as ascii representations of unicode or NCRs. So, when you need to display this in the browser as a certain language type, you'll need to tell ColdFsuion or Java what charset the data is to be displayed in and what you'll need to store it as when you insert or update.

Here is a scaled down working example of how to do this with ColdFusion using the Java Charset class. This example will loop over your query data and convert it to the Charset you specify. This example is used in the context of a function call and the beginning and ending character sets are passed in as arguments.

CODE:

  1. jcharset = CreateObject('java', 'java.nio.charset.Charset');
  2.     for(i=1;i LTE listLen(qSQL.columnlist); i=i+1 ) {
  3.         // get column
  4.         strTemp = ListGetAt(qSQL.columnlist,i);
  5.         // set variant holder of the data
  6.         vntText = qSQL[strTemp];
  7.         // instantiate instance of jcharset with orginal encoding as arg
  8.         charsetBefore = jcharset.forName(arguments.charsetA);
  9.         // instantiate instance jcharset with new encoding as arg
  10.         charsetAfter =  jcharset.forName(arguments.charsetB);
  11.         // convert text to byte array with original encoding
  12.         charBytes = charsetBefore.encode(vntText);
  13.         // decode with new encoding assign to struct member
  14.         'ret.data.#strTemp#' = charsetAfter.decode(charBytes).ToString();
  15.     }

Keep in mind that you'll need the starting and ending character sets as your arguments. Here are a few charsets as examples
Latin: CP1252:
Chinese Simplified: GB2312
Chinese Traditional: Big5
Chinese Taiwan: Big5
Japanese: Shift_JIS
Korean: EUC-KR
Czech: ISO-8859-2

Next Page »

The Holmes Blog