css
Thursday, May 30, 2013
Slow response IIS with long running requests
Last week i got myself in a little trouble. I have a back-end website that does a lot of processing stuff with very long running requests (some take days to complete). The website got slow if about 8-9 long running requests were active. These request do not consume a lot of cpu or memory so the problem had to be somewhere in IIS or Coldfusion. I posted a blog about the maximum number of simultaneous templates in coldfusion earlier this month (maximum number of simultaneous templates). Although this could have been an issue here it was not. In the IIS application pool each pool has 1 worker process assigned to it by default. In most cases (read 99%) this is fine, but if you are working with multiple long running request this can become a bottleneck. To resolve the problem you can assign more worker processes to the application pool.
Note: In general it is not a good idea to increase the number of maximum worker processes. This should only be done in very specific circumstances.
Wednesday, May 15, 2013
Quit long running regular expression in coldfusion (aka backtracking)
A problem you might encouter when working with regular expressions is you can't quit a regex currently in progress. There is no timeout setting. This can potentially freeze your server. The function below uses coldfusion's relatively new cfthread function to bypass this problem. In the example below the regex is quit if it is running for more then 3000 miliseconds.
<cffunction name="timedRegex" access="public" returntype="any" output="true">
<cfargument name="txt" type="string">
<cfargument name="pattern" type="string">
<cfset result = structNew()>
<cfset result.success = false>
<cfset result.found = arrayNew(1)>
<cfset var threadName = "thread_" & rereplace(createUuid(),'-','','all')>
<cfthread action="run" name="#threadName#" pattern=#pattern# txt=#txt# result=#result#>
<cfset var local = structNew()>
<cfset local.objPattern = ''>
<cfset local.objMatcher = ''>
<cfset local.str = structNew()>
<cfset local.c = 0>
<cfset local.d = 0>
<cfset local.objPattern = CreateObject("java","java.util.regex.Pattern").Compile('#trim(pattern)#') />
<cfset local.objMatcher = local.objPattern.Matcher(txt) />
<cfloop condition="local.objMatcher.Find()">
<cfset result.found[arraylen(result.found)+1] = structNew()>
<cfset result.found[arraylen(result.found)].string = objMatcher.Group() />
<cfset result.found[arraylen(result.found)].groups = arrayNew(1) />
<cfloop from=1 to="#objMatcher.groupCount()#" index=local.d>
<cfset result.found[arraylen(result.found)].groups[local.d] = objMatcher.Group(local.d) />
</cfloop>
</cfloop>
<cfset result.success = true>
<cfset THREAD.response = result>
</cfthread>
<cfthread action="join" name="#threadName#" timeout="3000"/>
<cfif evaluate(threadName & ".status") eq 'COMPLETED'>
<cfreturn evaluate(threadName & ".response")>
<cfelse>
<cfthread action="terminate" name="#threadName#"/>
<cfreturn result>
</cfif>
</cffunction>
<cfdump var="#timedRegex(txt="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy",pattern="(x+x+)+(y)")#">
<cfdump var="#timedRegex(txt="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",pattern="(x+x+)+(y)")#">
Tuesday, May 14, 2013
Maximum number of simultaneous Template requests
John: How do you increase the number of concurrent template requests in coldfusion 9?
Jesse: I am thinking. Isn't this a setting in the coldfusion administrator under request tuning?
John: Are you sure?
Jesse: Yes I am sure it is there i looked it up see "Maximum number of simultaneous Template requests" and "Maximum number of running JRun threads".
John: Okay have you tested it?
Jesse: No, wait i will test it. Just hang on a sec.
John: (taking a small cup of coffee and reading a post on his favourite website)
Jesse: Hmmm, John i tried setting up 35 simultaneous requests but for some reason i can only get up to 25 simultaneous template requests.
John: I know you are running IIS 6.0 right?
Jesse: Yes.
John: Read this article and try again: Adobe website
Jesse: What the heck.
John: (smiling)
Jesse: Why is there a setting called "Maximum number of simultaneous Template requests" in the coldfusion administrator when this does not work. And why do i have to set this in some config file placed deep inside coldfusion.
John: (still smiling) Only the coldfusion God knows.
---- End of conversation ----
If for some reason you do not wish to goto the adobe official website, below the article they posted on how to increase the number of simultaneous template requests.
http://blogs.adobe.com/cfdoc/2009/12/iis_6_for_coldfusion_9_increasing_the_number_of_worker_threads.html
IIS 6/IIS 7 for ColdFusion 9: Increasing the Number of Worker Threads
While using IIS6 for ColdFusion 9 (or ColdFusion 9.0.1), owing to heavy load, if you encounter performance issues, you may increase the number of worker threads.
Go to the directory cf_root\runtime\lib\wsconfig\1.
Note: In multiserver scenarios, the location is jrun_root/lib/wsconfig/1. 1 indicates a single IIS website. If you have configured multiple IIS websites, the number can change accordingly.
Open the file jrun_iis6_wildcard.ini (jrun_iis7_wildcard.ini if you use IIS 7).
Uncomment maxworkerthreads=25.
Note: By default, maxworkerthreads is commented.
Change the value to 50 (maxworkerthreads=50) from the default value of 25.
Restart IIS 6/IIS 7.
Note: The Maximum number of Simultaneous Template requests (Admin > Server Settings > Request Tunings > Request Limits) must also be increased accordingly.
Jesse: I am thinking. Isn't this a setting in the coldfusion administrator under request tuning?
John: Are you sure?
Jesse: Yes I am sure it is there i looked it up see "Maximum number of simultaneous Template requests" and "Maximum number of running JRun threads".
John: Okay have you tested it?
Jesse: No, wait i will test it. Just hang on a sec.
John: (taking a small cup of coffee and reading a post on his favourite website)
Jesse: Hmmm, John i tried setting up 35 simultaneous requests but for some reason i can only get up to 25 simultaneous template requests.
John: I know you are running IIS 6.0 right?
Jesse: Yes.
John: Read this article and try again: Adobe website
Jesse: What the heck.
John: (smiling)
Jesse: Why is there a setting called "Maximum number of simultaneous Template requests" in the coldfusion administrator when this does not work. And why do i have to set this in some config file placed deep inside coldfusion.
John: (still smiling) Only the coldfusion God knows.
---- End of conversation ----
If for some reason you do not wish to goto the adobe official website, below the article they posted on how to increase the number of simultaneous template requests.
http://blogs.adobe.com/cfdoc/2009/12/iis_6_for_coldfusion_9_increasing_the_number_of_worker_threads.html
IIS 6/IIS 7 for ColdFusion 9: Increasing the Number of Worker Threads
While using IIS6 for ColdFusion 9 (or ColdFusion 9.0.1), owing to heavy load, if you encounter performance issues, you may increase the number of worker threads.
Go to the directory cf_root\runtime\lib\wsconfig\1.
Note: In multiserver scenarios, the location is jrun_root/lib/wsconfig/1. 1 indicates a single IIS website. If you have configured multiple IIS websites, the number can change accordingly.
Open the file jrun_iis6_wildcard.ini (jrun_iis7_wildcard.ini if you use IIS 7).
Uncomment maxworkerthreads=25.
Note: By default, maxworkerthreads is commented.
Change the value to 50 (maxworkerthreads=50) from the default value of 25.
Restart IIS 6/IIS 7.
Note: The Maximum number of Simultaneous Template requests (Admin > Server Settings > Request Tunings > Request Limits) must also be increased accordingly.
Tuesday, March 20, 2012
Custom cfhttp tag
Some extra capabilities:
*) This tag automatically unzips gzipped documents.
*) Better control over request headers
*) Return structure is very similar to the cfhttp tag but has some extra data:
- request headers
- url of redirection
*) Converts body to utf-8
Based on feedback i upgraded the function. Below the second version of my cfhttp replacement function.
Example calls
<cfset rehttp = structNew()> <cfset rehttp.url = "http://www.microsoft.com"> <cfset rehttp.variable = arrayNew(2)> <!--- Syntax rehttp.variable: [x][1] = [field name]; [x][2] = [field value] [not url encoded]; [x][3] = [url|formfield|setRequestProperty]; url = variable in the url like http://www.google.com?q=test formfield= posted variables setRequestProperty = variables like header, body etc. ---> <cfinvoke component="cfc.httprequest" method="init" returnvariable="objectHTTP"> <cfinvokeargument name="pass_url" value="#rehttp.url#"/> <cfinvokeargument name="pass_cookie" value=""/> <cfinvokeargument name="pass_variables" value="#rehttp.variable#"/> <cfinvokeargument name="pass_httpuseragent" value=""/> <cfinvokeargument name="pass_httphost" value=""/> <cfinvokeargument name="pass_httpport" value=""/> <cfinvokeargument name="pass_debug" value="false"/> <cfinvokeargument name="pass_autoredirect" value="true"/> <cfinvokeargument name="pass_remember_cookie" value=""/> </cfinvoke> <cfdump var=#objectHTTP#> <cfset rehttp = structNew()> <cfset rehttp.url = "http://forums.adobe.com/community/search.jspa"> <cfset rehttp.variable = arrayNew(2)> <cfset rehttp.variable[1][1] = "peopleEnabled"> <cfset rehttp.variable[1][2] = "true"> <cfset rehttp.variable[1][3] = "url"> <cfset rehttp.variable[2][1] = "userID"> <cfset rehttp.variable[2][2] = ""> <cfset rehttp.variable[2][3] = "url"> <cfset rehttp.variable[3][1] = "spotlight"> <cfset rehttp.variable[3][2] = "false"> <cfset rehttp.variable[3][3] = "url"> <cfset rehttp.variable[4][1] = "containerType"> <cfset rehttp.variable[4][2] = "14"> <cfset rehttp.variable[4][3] = "url"> <cfset rehttp.variable[5][1] = "container"> <cfset rehttp.variable[5][2] = "2223"> <cfset rehttp.variable[5][3] = "url"> <cfset rehttp.variable[6][1] = "rankBy"> <cfset rehttp.variable[6][2] = "date"> <cfset rehttp.variable[6][3] = "url"> <cfset rehttp.variable[7][1] = "q"> <cfset rehttp.variable[7][2] = "cfhttp"> <cfset rehttp.variable[7][3] = "url"> <cfinvoke component="cfc.httprequest" method="init" returnvariable="objectHTTP"> <cfinvokeargument name="pass_url" value="#rehttp.url#"/> <cfinvokeargument name="pass_cookie" value=""/> <cfinvokeargument name="pass_variables" value="#rehttp.variable#"/> <cfinvokeargument name="pass_httpuseragent" value=""/> <cfinvokeargument name="pass_httphost" value=""/> <cfinvokeargument name="pass_httpport" value=""/> <cfinvokeargument name="pass_debug" value="false"/> <cfinvokeargument name="pass_autoredirect" value="true"/> <cfinvokeargument name="pass_remember_cookie" value=""/> </cfinvoke> <cfdump var=#objectHTTP#>The actual component.
<cfcomponent name="httprequest">
<cffunction name="init" access="remote" returntype="struct" output="no">
<cfargument name="pass_url" type="any">
<cfargument name="pass_cookie" type="string">
<cfargument name="pass_variables" type="array" >
<cfargument name="pass_httpuseragent" type="string" default="Mozilla/5.0 (Windows; U; Windows NT 6.1; nl; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28">
<cfargument name="pass_httphost" type="string" >
<cfargument name="pass_httpport" type="string" >
<cfargument name="pass_debug" type="boolean" >
<cfargument name="pass_autoredirect" type="boolean">
<cfargument name="pass_remember_cookie" type="any" default="">
<cfargument name="pass_referer" default="">
<cfset var websitedata = '' />
<cfset var i = '' />
<cfset websitedata=structnew()>
<cfset websitedata.timer = structNew()>
<cfset websitedata.timer.init = gettickcount()>
<!--- Save get parameters to Array ---->
<cfset var send_getParameters = arrayNew(2)>
<cfloop from=1 to=#arrayLen(pass_variables)# index="i">
<cfif #pass_variables[i][3]# eq 'url'>
<cfset send_getParameters[evaluate(arraylen(send_getParameters)+1)][1] = pass_variables[i][1]>
<cfset send_getParameters[arraylen(send_getParameters)][2] = pass_variables[i][2]>
</cfif>
</cfloop>
<!--- End get parameters to Array ---->
<!--- Save post parameters to Array ---->
<cfset var send_postParameters = arrayNew(2)>
<cfloop from=1 to=#arrayLen(pass_variables)# index="i">
<cfif #pass_variables[i][3]# eq 'formfield'>
<cfset send_postParameters[evaluate(arraylen(send_postParameters)+1)][1] = pass_variables[i][1]>
<cfset send_postParameters[arraylen(send_postParameters)][2] = pass_variables[i][2]>
</cfif>
</cfloop>
<!--- End post parameters to Array ---->
<!--- Save RequestProperties parameters to Array ---->
<cfset var send_requestsParameters = arrayNew(2)>
<cfloop from=1 to=#arrayLen(pass_variables)# index="i">
<cfif #pass_variables[i][3]# eq 'setRequestProperty'>
<cfset send_requestsParameters[evaluate(arraylen(send_requestsParameters)+1)][1] = pass_variables[i][1]>
<cfset send_requestsParameters[arraylen(send_requestsParameters)][2] = pass_variables[i][2]>
</cfif>
</cfloop>
<!--- End post parameters to Array ---->
<!--- Save cookies to Array ---->
<cfset var send_cookies = arrayNew(2)>
<cfloop from=1 to=#arrayLen(pass_variables)# index="i">
<cfif #pass_variables[i][3]# eq 'cookie'>
<cfset send_cookies[evaluate(arraylen(send_cookies)+1)][1] = pass_variables[i][1]>
<cfset send_cookies[arraylen(send_cookies)][2] = pass_variables[i][2]>
</cfif>
</cfloop>
<cfloop from=1 to="#listlen(pass_remember_cookie,chr(1000))#" index="i">
<cfset founditem="#listgetat(pass_remember_cookie,i,chr(1000))#">
<cfset send_cookies[evaluate(arraylen(send_cookies)+1)][1] = #left(founditem,find('=',founditem)-1)#>
<cfset send_cookies[arraylen(send_cookies)][2] = #founditem#>
</cfloop>
<!--- End save cookies to Array ---->
<cfsavecontent variable="httpRequest">
<cfoutput>
<cfinvoke component="cfc.httprequest" method="connect" returnvariable="objectHTTP">
<cfinvokeargument name="in_url" value="#pass_url#">
<cfinvokeargument name="in_max_redirects" value="5">
<cfinvokeargument name="in_UserAgent" value="#pass_httpuseragent#">
<cfinvokeargument name="in_ProxyPort" value="#pass_httpport#">
<cfinvokeargument name="in_ProxyHost" value="#pass_httphost#">
<cfinvokeargument name="in_Cookies" value="#SerializeJSON(send_cookies,true)#">
<cfinvokeargument name="in_getParameters" value="#SerializeJSON(send_getParameters,true)#">
<cfinvokeargument name="in_postParameters" value="#SerializeJSON(send_postParameters,true)#">
<cfinvokeargument name="in_requestParameters" value="#SerializeJSON(send_requestsParameters,true)#">
<cfloop from=1 to=#arrayLen(pass_variables)# index="i">
<cfif #pass_variables[i][3]# eq 'setRequestProperty'>
<cfinvokeargument name="in_#replace(pass_variables[i][1],'-','','all')#" value="#pass_variables[i][2]#">
</cfif>
</cfloop>
</cfinvoke>
</cfoutput>
</cfsavecontent>
<cfset var args = structNew()>
<cfset args.in_url = pass_url>
<cfset args.in_max_redirects = 5>
<cfset args.in_UserAgent = pass_httpuseragent>
<cfset args.in_ProxyPort = pass_httpport>
<cfset args.in_ProxyHost = pass_httphost>
<cfset args.in_Cookies = send_cookies>
<cfset args.in_getParameters = send_getParameters>
<cfset args.in_postParameters = send_postParameters>
<cfset args.in_requestParameters =send_requestsParameters>
<cfloop from=1 to=#arrayLen(pass_variables)# index="i">
<cfif #pass_variables[i][3]# eq 'setRequestProperty'>
<cfset args["in_#replace(pass_variables[i][1],'-','','all')#"] = #pass_variables[i][2]#>
</cfif>
</cfloop>
<cfset objectHTTP = connect(argumentCollection=args)>
<cfset websitedata.mimeType = #objectHTTP.mimeType#>
<cfset websitedata.header = #objectHTTP.header#>
<cfset websitedata.requestheader = #objectHTTP.requestheader#>
<cfset websitedata.responseheader = #objectHTTP.responseheader#>
<cfset websitedata.responseBody = #rereplace(objectHTTP.fileContent,'[^\x09-\xFF]',' ','all')#>
<cfset websitedata.status = #left(objectHTTP.statuscode,3)#>
<cfset websitedata.httpRequest = #httpRequest#>
<cfset websitedata.CONNECTION = objectHTTP.connection>
<cftry>
<cfset websitedata.cookies = #objectHTTP.ResponseHeader["Set-Cookie"]#>
<cfcatch type="any"><cfset websitedata.cookies = ""></cfcatch>
</cftry>
<cfset websitedata.uri = #pass_url#>
<cfset websitedata.charSet = #objectHTTP.charSet#>
<cfset websitedata.errors = #objectHTTP.errors#>
<cfset websitedata.timer.connect = objectHTTP.timing>
<cfset websitedata.timer.end = gettickcount()>
<cfreturn websitedata>
</cffunction>
<cffunction name="connect">
<cfargument type="string" name="in_url" required="yes">
<cfargument type="array" name="in_getParameters" required="no" default="#arrayNew(2)#" hint="Get variables url not encoded">
<cfargument type="array" name="in_postParameters" required="no" default="#arrayNew(2)#" hint="Post variables url not encoded">
<cfargument type="array" name="in_requestParameters" required="no" default="#arrayNew(2)#" hint="Request variables url">
<cfargument type="array" name="in_Cookies" required="no" default="#arrayNew(2)#" hint="Post cookies url not encoded">
<cfargument type="string" name="in_max_redirects" required="no" default="5">
<cfargument type="string" name="in_requestMethod" required="no" default="GET">
<cfargument type="string" name="in_Accept" required="no" default="*/*">
<cfargument type="string" name="in_ContentType" required="no" default="application/x-www-form-urlencoded">
<cfargument type="string" name="in_AcceptEncoding" required="no" default="gzip, x-gzip, identity, *;q=0">
<cfargument type="string" name="in_AcceptCharset" required="no" default="ISO-8859-1,utf-8;q=0.7,*;q=0.7">
<cfargument type="string" name="in_AcceptLanguage" required="no" default="nl,en-us;q=0.7,en;q=0.3">
<cfargument type="string" name="in_UserAgent" required="no" default="Mozilla/5.0 (Windows; U; Windows NT 6.1; nl; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28">
<cfargument type="string" name="in_CacheControl" required="no" default="no-cache">
<cfargument type="string" name="in_ConnectTimeout" required="no" default="15000">
<cfargument type="string" name="in_ReadTimeout" required="no" default="15000">
<cfargument type="string" name="in_ProxyPort" required="no" default="">
<cfargument type="string" name="in_ProxyHost" required="no" default="">
<cfargument type="boolean" name="in_RememberRedirectedCookies" required="no" default="true">
<cfargument type="string" name="out_Charset" required="no" default="UTF-8">
<!--- Set local variables --->
<cfset var local_results = structNew()>
<cfset var local_i = 0>
<cfset var local_j = 0>
<Cfset var local_current_redirect = 0>
<Cfset var local_current_redirect_including_charset = 0>
<Cfset var local_PostVariables = "">
<Cfset var local_Cookies = "">
<Cfset var local_JavaEncoder = createObject("java","java.net.URLEncoder")>
<Cfset var local_RedirectCookiesAsString = "">
<Cfset var local_RedirectCookiesAsArray = arrayNew(2)>
<cfset var local_timer = arrayNew(2)>
<cfset var local_timer_temp = gettickcount()>
<cfset var local_redirectBasesOnWrongCharset = false>
<cfset var local_System = createObject("java", "java.lang.System").getProperties()>
<!--- End set local variables --->
<cfset local_timer_temp = gettickcount()>
<!--- Set defaults --->
<cfif in_requestMethod eq ""><cfset in_requestMethod="GET"></cfif>
<cfif in_Accept eq ""><cfset in_Accept="*/*"></cfif>
<cfif in_ContentType eq ""><cfset in_ContentType="application/x-www-form-urlencoded"></cfif>
<cfif in_AcceptEncoding eq ""><cfset in_AcceptEncoding="gzip, x-gzip, identity, *;q=0"></cfif>
<cfif in_AcceptCharset eq ""><cfset in_AcceptCharset="ISO-8859-1,utf-8;q=0.7,*;q=0.7"></cfif>
<cfif in_AcceptLanguage eq ""><cfset in_AcceptLanguage="nl,en-us;q=0.7,en;q=0.3"></cfif>
<cfif in_UserAgent eq ""><cfset in_UserAgent="Mozilla/5.0 (Windows; U; Windows NT 6.1; nl; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"></cfif>
<cfif in_CacheControl eq ""><cfset in_CacheControl="no-cache"></cfif>
<cfif in_ConnectTimeout eq ""><cfset in_ConnectTimeout="15000"></cfif>
<cfif in_ReadTimeout eq ""><cfset in_ReadTimeout="15000"></cfif>
<cfif in_ProxyPort eq ""><cfset in_ProxyPort=""></cfif>
<cfif in_ProxyHost eq ""><cfset in_ProxyHost=""></cfif>
<cfif arraylen(in_postParameters) neq 0><cfset in_requestMethod = "POST"></cfif>
<!--- End set defaults --->
<!--- Set get variables --->
<cfloop from=1 to="#arraylen(in_getParameters)#" index="i">
<cfif i eq 1><cfset in_url = "#in_url#?"></cfif>
<cfif i gt 1><cfset in_url = "#in_url#&"></cfif>
<cfset in_url = "#in_url##in_getParameters[i][1]#=#trim(local_JavaEncoder.encode(in_getParameters[i][2],"utf-8"))#">
</cfloop>
<!--- End set get variables --->
<!--- Set post variables --->
<cfloop from=1 to="#arraylen(in_postParameters)#" index="i">
<cfif i gt 1><cfset local_PostVariables = "#local_PostVariables#&"></cfif>
<cfset local_PostVariables = "#local_PostVariables##in_postParameters[i][1]#=#trim(local_JavaEncoder.encode(in_postParameters[i][2],"utf-8"))#">
</cfloop>
<!--- End set post variables --->
<!--- Set cookies --->
<cfloop from=1 to="#arraylen(in_Cookies)#" index="i">
<cfif i gt 1><cfset local_Cookies = "#local_Cookies#;"></cfif>
<cfset local_Cookies = "#local_Cookies##in_Cookies[i][1]#=#trim(local_JavaEncoder.encode(in_Cookies[i][2],"utf-8"))#">
</cfloop>
<!--- End set cookies --->
<cfset in_max_redirects = in_max_redirects -1>
<!--- Set default output variables --->
<cfset local_timer_temp = gettickcount()>
<cfset local_results.CHARSET = "UTF-8">
<cfset local_results.HEADER = "">
<cfset local_results.MIMETYPE = "">
<cfset local_results.CONNECTION = structNew()>
<cfset local_results.CONNECTION.REDIRECTED = false>
<cfset local_results.CONNECTION.REDIRECT_TIMES = 0>
<cfset local_results.CONNECTION.REQUESTS = arrayNew(1)>
<cfset local_results.CONNECTION.URL_START = in_url>
<cfset local_results.CONNECTION.URL_END = in_url>
<cfset local_results.REQUESTHEADER = structNew()>
<cfset local_results.RESPONSEHEADER = structNew()>
<cfset local_results.STATUSCODE = "">
<cfset local_results.FILECONTENT = "">
<cfset local_results.ERRORS = structNew()>
<cfset local_results.SUCCESS = true>
<!--- End set default output variables --->
<cfset local_timer[arraylen(local_timer)+1][1] = "Set variables">
<cfset local_timer[arraylen(local_timer)][2] = gettickcount()-local_timer_temp>
<cftry>
<!--- Build connection --->
<cfset var local_Url = createObject("java", "java.net.URL")>
<cfif in_ProxyPort neq '' or in_ProxyHost neq ''>
<cfif in_ProxyHost neq ''><cfset local_System.setProperty("http.proxyHost","#in_ProxyHost#")></cfif>
<cfif in_ProxyPort neq ''><cfset local_System.setProperty("http.proxyPort","#in_ProxyPort#")></cfif>
</cfif>
<cfset var local_Connection = createObject("java", "java.net.HttpURLConnection")>
<cfloop condition="local_current_redirect lte in_max_redirects and local_current_redirect_including_charset lt 10">
<cfset local_timer_temp = gettickcount()>
<cfset local_Url.init(in_url)>
<cfset local_Connection = local_Url.openConnection()>
<cfset local_Connection.setConnectTimeout(JavaCast("int",in_ConnectTimeout))>
<cfset local_Connection.setReadTimeout(JavaCast("int",in_ReadTimeout))>
<cfset local_Connection.setRequestMethod(in_requestMethod)>
<cfset local_Connection.setFollowRedirects(true)>
<cfset local_Connection.setDoInput(true)>
<cfif local_PostVariables neq ''><cfset local_Connection.setDoOutput(true)></cfif>
<cfset local_Connection.setRequestProperty("Accept", in_Accept)>
<cfset local_Connection.setRequestProperty("Content-Type", in_ContentType)>
<cfset local_Connection.setRequestProperty("Accept-Encoding", in_AcceptEncoding)>
<cfset local_Connection.setRequestProperty("Accept-Charset", in_AcceptCharset)>
<cfset local_Connection.setRequestProperty("Accept-Language",in_AcceptLanguage)>
<cfset local_Connection.setRequestProperty("User-Agent",in_UserAgent)>
<cfset local_Connection.setRequestProperty("Cache-Control", in_CacheControl)>
<cfset local_Connection.setRequestProperty("Cookie", local_Cookies)>
<cfif local_current_redirect eq 0>
<cfloop from=1 to="#arraylen(in_requestParameters)#" index="i">
<cfset local_Connection.setRequestProperty("#in_requestParameters[i][1]#", in_requestParameters[i][2])>
</cfloop>
</cfif>
<cfset local_Connection.setInstanceFollowRedirects(false)>
<cfset local_results.url = local_Connection.getURL().toString()>
<!--- Get request headers --->
<cfset var local_RequestHeadersStruc = structNew()>
<cfset var local_RequestHeadersArrayNew = arrayNew(2)>
<cfset var local_RequestHeadersArrayOriginal = local_Connection.getRequestProperties().entrySet().toArray()>
<cfloop from=1 to="#arraylen(local_RequestHeadersArrayOriginal)#" index="i">
<Cfif not isnull(local_RequestHeadersArrayOriginal[i].getKey())>
<cfset local_RequestHeadersArrayNew[i][1] = local_RequestHeadersArrayOriginal[i].getKey()>
<cfloop from=1 to="#arrayLen(local_RequestHeadersArrayOriginal[i].getValue())#" index="j"><cfset local_RequestHeadersArrayNew[i][j+1] = local_RequestHeadersArrayOriginal[i].getValue()[j]></cfloop>
<cfelse>
<cfset local_RequestHeadersArrayNew[i][1] = "">
<cfloop from=1 to="#arrayLen(local_RequestHeadersArrayOriginal[i].getValue())#" index="j"><cfset local_RequestHeadersArrayNew[i][j+1] = local_RequestHeadersArrayOriginal[i].getValue()[j]></cfloop>
</cfif>
</cfloop>
<cfloop from=1 to="#arraylen(local_RequestHeadersArrayNew)#" index="i">
<cfif arraylen(local_RequestHeadersArrayNew[i]) eq 2>
<cfset local_RequestHeadersStruc[local_RequestHeadersArrayNew[i][1]] = local_RequestHeadersArrayNew[i][2]>
<cfelse>
<cfset local_ResponseHeadersStruc[local_RequestHeadersArrayNew[i][1]] = structNew()>
<cfloop from=1 to="#arraylen(local_RequestHeadersArrayNew[i])#" index="j">
<Cftry><cfset local_RequestHeadersStruc[local_RequestHeadersArrayNew[i][1]][""&j&""] = local_RequestHeadersArrayNew[i][j+1]><cfcatch type="any"></cfcatch></cftry>
</cfloop>
</cfif>
</cfloop>
<!--- <cfset results.requestHeader_asArray = local_RequestHeadersArrayNew> --->
<!--- End get request headers --->
<!--- Post post variables --->
<cfif local_PostVariables neq ''>
<cfset var local_uploadStream = local_Connection.getOutputStream() />
<cfset var local_uploadWriter = createObject( "java", "java.io.OutputStreamWriter" ).init(local_uploadStream) />
<cfset local_uploadWriter.write(javaCast( "string", (local_PostVariables) )) />
<cfset local_uploadWriter.close() />
</cfif>
<!--- End post post variables --->
<!--- Store get variables in structure to return --->
<cfset local_RequestHeadersStruc.urlVariables = structNew()>
<cfloop from=1 to="#arraylen(in_getParameters)#" index="i">
<cfset local_RequestHeadersStruc.urlVariables[""&i&""] = structNew()>
<cfset local_RequestHeadersStruc.urlVariables[""&i&""].name = "#in_getParameters[i][1]#">
<cfset local_RequestHeadersStruc.urlVariables[""&i&""].value = "#in_getParameters[i][2]#">
</cfloop>
<!--- End store get variables in structure to return --->
<!--- Store post variables in structure to return --->
<cfset local_RequestHeadersStruc.postVariables = structNew()>
<cfloop from=1 to="#arraylen(in_postParameters)#" index="i">
<cfset local_RequestHeadersStruc.postVariables[""&i&""] = structNew()>
<cfset local_RequestHeadersStruc.postVariables[""&i&""].name = "#in_postParameters[i][1]#">
<cfset local_RequestHeadersStruc.postVariables[""&i&""].value = "#in_postParameters[i][2]#">
</cfloop>
<!--- End store post variables in structure to return --->
<!--- Store cookies in structure to return --->
<cfset local_RequestHeadersStruc.Cookies = structNew()>
<cfloop from=1 to="#arraylen(in_Cookies)#" index="i">
<cfset local_RequestHeadersStruc.Cookies[""&i&""] = structNew()>
<cfset local_RequestHeadersStruc.Cookies[""&i&""].name = "#in_Cookies[i][1]#">
<cfset local_RequestHeadersStruc.Cookies[""&i&""].value = "#in_Cookies[i][2]#">
</cfloop>
<!--- Stop store cookies in structure to return --->
<cfset local_RequestHeadersStruc.requestMethod = in_requestMethod>
<cfset local_results.requestHeader = local_RequestHeadersStruc>
<cfset local_timer[arraylen(local_timer)+1][1] = "Set request">
<cfset local_timer[arraylen(local_timer)][2] = gettickcount()-local_timer_temp>
<!--- Get response headers --->
<cfset local_timer_temp = gettickcount()>
<cfset var local_ResponseHeadersStruc = structNew()>
<cfset var local_ResponseHeadersString = "">
<cfset var local_ResponseHeadersArrayNew= arrayNew(2)>
<cfset var local_ResponseHeadersArrayOriginal = local_Connection.getHeaderFields().entrySet().toArray()>
<cfloop from=1 to="#arraylen(local_ResponseHeadersArrayOriginal)#" index="i">
<Cfif not isnull(local_ResponseHeadersArrayOriginal[i].getKey())>
<cfset local_ResponseHeadersString = "#local_ResponseHeadersString##local_ResponseHeadersArrayOriginal[i].getKey()#: ">
<cfset local_ResponseHeadersArrayNew[i][1] = local_ResponseHeadersArrayOriginal[i].getKey()>
<cfloop from=1 to="#arrayLen(local_ResponseHeadersArrayOriginal[i].getValue())#" index="j"><cfset local_ResponseHeadersString = "#local_ResponseHeadersString##local_ResponseHeadersArrayOriginal[i].getValue()[j]# "><cfset local_ResponseHeadersArrayNew[i][j+1] = local_ResponseHeadersArrayOriginal[i].getValue()[j]></cfloop>
<cfelse>
<cfset local_ResponseHeadersArrayNew[i][1] = "">
<cfloop from=1 to="#arrayLen(local_ResponseHeadersArrayOriginal[i].getValue())#" index="j"><cfset local_ResponseHeadersString = "#local_ResponseHeadersString##local_ResponseHeadersArrayOriginal[i].getValue()[j]# "><cfset local_ResponseHeadersArrayNew[i][j+1] = local_ResponseHeadersArrayOriginal[i].getValue()[j]></cfloop>
</cfif>
</cfloop>
<cfloop from=1 to="#arraylen(local_ResponseHeadersArrayNew)#" index="i">
<cfif arraylen(local_ResponseHeadersArrayNew[i]) eq 2 and not local_ResponseHeadersArrayNew[i][1] eq 'Set-Cookie'>
<cfset local_ResponseHeadersStruc[local_ResponseHeadersArrayNew[i][1]] = local_ResponseHeadersArrayNew[i][2]>
<cfelse>
<cfset local_ResponseHeadersStruc[local_ResponseHeadersArrayNew[i][1]] = structNew()>
<cfloop from=1 to="#arraylen(local_ResponseHeadersArrayNew[i])#" index="j">
<Cftry><cfset local_ResponseHeadersStruc[local_ResponseHeadersArrayNew[i][1]][""&j&""] = local_ResponseHeadersArrayNew[i][j+1]><cfcatch type="any"></cfcatch></cftry>
</cfloop>
</cfif>
</cfloop>
<cfset local_results.CONNECTION.REQUESTS[arrayLen(local_results.CONNECTION.REQUESTS)+1] = structNew()>
<cfset local_results.CONNECTION.REQUESTS[arrayLen(local_results.CONNECTION.REQUESTS)].URL = in_url>
<cfset local_results.CONNECTION.REQUESTS[arrayLen(local_results.CONNECTION.REQUESTS)].COOKIES = local_Cookies>
<cfif StructKeyExists(local_results.RESPONSEHEADER, "Set-Cookie")>
<cfset local_results.CONNECTION.REQUESTS[arrayLen(local_results.CONNECTION.REQUESTS)].COOKIES= #local_results.RESPONSEHEADER["Set-Cookie"]#>
</cfif>
<cfset local_results.CONNECTION.REQUESTS[arrayLen(local_results.CONNECTION.REQUESTS)].REQUEST_HEADER = local_RequestHeadersStruc>
<cfset local_results.CONNECTION.REQUESTS[arrayLen(local_results.CONNECTION.REQUESTS)].RESPONSE_HEADER = local_ResponseHeadersStruc>
<cfset local_results.header = local_ResponseHeadersString>
<cfset local_results.responseHeader = local_ResponseHeadersStruc>
<cfset local_timer[arraylen(local_timer)+1][1] = "Get url header">
<cfset local_timer[arraylen(local_timer)][2] = gettickcount()-local_timer_temp>
<!--- End get response headers --->
<!--- Get CHARSET AND MIMETYPE --->
<cfset local_timer_temp = gettickcount()>
<Cfset var mimetype = local_Connection.getContentType()>
<cftry><cfif find('charset=',mimetype) gt 0 and not local_redirectBasesOnWrongCharset><cfset local_results.Charset = rereplace(mimetype,'.*?charset=(.*)','\1','one')><Cfelseif not local_redirectBasesOnWrongCharset><cfset local_results.Charset ='UTF-8'></cfif><cfcatch type="any"><cfset local_results.Charset ='UTF-8'></cfcatch></cftry>
<cftry><cfset local_results.Mimetype = rereplace(mimetype,'(.*?);.*','\1','one')><cfcatch type="any"></cfcatch></cftry>
<cfset local_timer[arraylen(local_timer)+1][1] = "Get url charset and mimetype: #local_results.Charset#">
<cfset local_timer[arraylen(local_timer)][2] = gettickcount()-local_timer_temp>
<!--- End Get CHARSET AND MIMETYPE --->
<!--- Get response body --->
<cfif not (local_Connection.getResponseCode() eq local_Connection.HTTP_MOVED_PERM OR local_Connection.getResponseCode() eq local_Connection.HTTP_MOVED_TEMP OR local_Connection.getResponseCode() EQ local_Connection.HTTP_SEE_OTHER)>
<cfset var local_ResponseBody = "">
<cfif local_Connection.getContentEncoding() neq "" && (local_Connection.getContentEncoding().equalsIgnoreCase("gzip") || local_Connection.getContentEncoding().equalsIgnoreCase("x-gzip"))>
<cfset var local_BufferedReader = createObject("java","java.io.BufferedReader").init(CreateObject( "JAVA", "java.io.InputStreamReader" ).init( createObject("java", "java.util.zip.GZIPInputStream").init(local_Connection.getInputStream()),local_results.Charset))>
<cfelse>
<cfset var local_BufferedReader = createObject("java","java.io.BufferedReader").init(CreateObject( "JAVA", "java.io.InputStreamReader" ).init( local_Connection.getInputStream(),local_results.Charset))>
</cfif>
<cfset var local_line="">
<cfset var local_lineCheck = false>
<cfset local_line = local_BufferedReader.readLine()>
<cfset local_lineCheck = isDefined("local_line")>
<cfset var local_stringbldr = createObject("java", "java.lang.StringBuilder").init()>
<cfloop condition="#isDefined("local_line")#">
<cfset local_stringbldr.append("#local_line##chr(13)##chr(10)#")>
<cfset local_line = local_BufferedReader.readLine()>
</cfloop>
<cfset local_ResponseBody = local_stringbldr.toString()>
<Cfset local_BufferedReader.close()>
<cfset local_results.Filecontent = local_ResponseBody>
<cfset local_timer[arraylen(local_timer)+1][1] = "Get url body">
<cfset local_timer[arraylen(local_timer)][2] = gettickcount()-local_timer_temp>
<!--- End get response body --->
<!--- check if wrong charset is used --->
<cfset local_redirectBasesOnWrongCharset = false>
<cfif refind("<meta(?!\s*(?:name|value)\s*=)[^>]*?charset\s*=[\s""']*([^\s""'/>]*)",local_ResponseBody) gt 0>
<cfif local_results.Charset neq rereplacenocase(local_ResponseBody,".*<meta(?!\s*(?:name|value)\s*=)[^>]*?charset\s*=[\s""']*([^\s""'/>]*)(.*)","\1","one")>
<cfset local_results.Charset = ucase(rereplacenocase(local_ResponseBody,".*<meta(?!\s*(?:name|value)\s*=)[^>]*?charset\s*=[\s""']*([^\s""'/>]*)(.*)","\1","one"))>
<cfset local_redirectBasesOnWrongCharset = true>
</cfif>
</cfif>
<!--- End check if wrong charset is used --->
</cfif>
<!--- Check if redirection is necessary --->
<cfif (local_Connection.getResponseCode() eq local_Connection.HTTP_MOVED_PERM OR local_Connection.getResponseCode() eq local_Connection.HTTP_MOVED_TEMP OR local_Connection.getResponseCode() EQ local_Connection.HTTP_SEE_OTHER OR local_redirectBasesOnWrongCharset)>
<cfif not local_redirectBasesOnWrongCharset>
<cfif refind('^.{2,5}://',local_Connection.getHeaderField("Location")) eq 0><cfset in_url = local_Url.getProtocol() & "://" & local_Url.getHost() & local_Connection.getHeaderField("Location")>
<Cfelse><cfset in_url = local_Connection.getHeaderField("Location")></cfif>
<cfset local_results.Statuscode = "#local_Connection.getResponseCode()# #local_Connection.getResponseMessage()#">
<cfset local_results.URL = in_url>
<cfset local_results.CONNECTION.REDIRECTED = true>
<cfset local_results.CONNECTION.REDIRECT_TIMES = local_current_redirect+1>
<cfset local_results.CONNECTION.REDIRECT_URL[""&local_current_redirect+1&""] = in_url>
<cfset local_results.CONNECTION.URL_END = in_url>
<cfif StructKeyExists(local_results.RESPONSEHEADER, "Set-Cookie")>
<cfloop collection="#local_results.RESPONSEHEADER["Set-Cookie"]#" item="key">
<cfset var local_cookie_name = "#rereplace(local_results.RESPONSEHEADER["Set-Cookie"][key],'^(.*?)=.*','\1','one')#">
<cfset var local_cookie_value = "#rereplace(local_results.RESPONSEHEADER["Set-Cookie"][key],'^.*?=(.*?)(;.*|$)','\1','one')#">
<cfset var local_cookie_found = false>
<cfloop from=1 to=#arrayLen(in_Cookies)# index="j">
<cfif in_Cookies[j][1] eq local_cookie_name>
<cfset in_Cookies[j][2] = local_cookie_value>
<cfset local_cookie_found = true>
<cfbreak>
</cfif>
</cfloop>
<cfif not local_cookie_found>
<cfset in_Cookies[#evaluate(arrayLen(in_Cookies)+1)#][1] = local_cookie_name>
<cfset in_Cookies[#arrayLen(in_Cookies)#][2] = local_cookie_value>
</cfif>
<cfloop from=1 to=#arrayLen(local_RedirectCookiesAsArray)# index="j">
<cfif in_Cookies[j][1] eq local_cookie_name>
<cfset local_RedirectCookiesAsArray[j][1] = local_cookie_name>
<cfset local_RedirectCookiesAsArray[j][2] = local_results.RESPONSEHEADER["Set-Cookie"][key]>
<cfelseif j eq arrayLen(local_RedirectCookiesAsArray)>
<cfset local_RedirectCookiesAsArray[j+1][1] = local_cookie_name>
<cfset local_RedirectCookiesAsArray[j+1][2] = local_results.RESPONSEHEADER["Set-Cookie"][key]>
</cfif>
</cfloop>
<cfif arrayLen(local_RedirectCookiesAsArray) eq 0>
<cfset local_RedirectCookiesAsArray[1][1] = local_cookie_name>
<cfset local_RedirectCookiesAsArray[1][2] = local_results.RESPONSEHEADER["Set-Cookie"][key]>
</cfif>
</cfloop>
<!--- Set cookies --->
<cfloop from=1 to="#arraylen(in_Cookies)#" index="i">
<cfif i gt 1><cfset local_Cookies = "#local_Cookies#;"></cfif>
<cfset local_Cookies = "#local_Cookies##in_Cookies[i][1]#=#trim(urldecode(local_JavaEncoder.encode(in_Cookies[i][2],"utf-8")))#">
</cfloop>
<!--- End set cookies --->
</cfif>
</cfif>
<cfset local_Connection.disconnect()>
<cfelse>
<cfset local_results.Statuscode = "#local_Connection.getResponseCode()# #local_Connection.getResponseMessage()#">
<cfset local_results.URL = in_url>
<cfset local_results.CONNECTION.REDIRECT_TIMES = local_current_redirect>
<cfset local_Connection.disconnect()>
<Cfbreak>
</cfif>
<!--- End check if redirection is necessary --->
<cfif not local_redirectBasesOnWrongCharset><Cfset local_current_redirect = local_current_redirect + 1></cfif>
<Cfset local_current_redirect_including_charset = local_current_redirect_including_charset + 1>
</cfloop>
<!--- End build connection --->
<cfcatch type="any">
<cfset local_results.ERRORS.MESSAGE = cfcatch.message>
<cftry><cfset local_results.ERRORS.TYPE = cfcatch.Type><cfcatch type="any"></cfcatch></cftry>
<cftry><cfset local_results.ERRORS.StackTrace = cfcatch.StackTrace><cfcatch type="any"></cfcatch></cftry>
<cftry><cfset local_results.ERRORS.TagContext = cfcatch.TagContext><cfcatch type="any"></cfcatch></cftry>
<cfset local_results.success= false>
<cftry><cfset local_results.Statuscode = "#local_Connection.getResponseCode()# #local_Connection.getResponseMessage()#"><cfcatch type="any"></cfcatch></cftry>
<cftry><cfset local_Connection.disconnect()> <cfcatch type="any"></cfcatch></cftry>
</cfcatch>
</cftry>
<cfif in_ProxyPort neq '' or in_ProxyHost neq ''>
<cfset var local_system = createObject("java", "java.lang.System")>
<cfif in_ProxyHost neq ''><cfset local_system.clearProperty("http.proxyHost")></cfif>
<cfif in_ProxyPort neq ''><cfset local_system.clearProperty("http.proxyPort")></cfif>
</cfif>
<cfif in_RememberRedirectedCookies>
<cfif not StructKeyExists(local_results.RESPONSEHEADER, "Set-Cookie")>
<cfset local_results.RESPONSEHEADER["Set-Cookie"] = structNew()>
</cfif>
<Cfset var local_count_keys = 0>
<cftry><cfloop collection="#local_results.RESPONSEHEADER["Set-Cookie"]#" item="key"><cfset local_count_keys = local_count_keys+1></cfloop>
<cfcatch type="any">
<cfif not IsStruct(local_results.RESPONSEHEADER["Set-Cookie"])>
<cfif local_results.RESPONSEHEADER["Set-Cookie"] neq ''><cfset local_count_keys = local_count_keys+1></cfif>
</cfif>
</cfcatch>
</cftry>
<cfloop from=1 to="#arrayLen(local_RedirectCookiesAsArray)#" index="i">
<cfset local_count_keys = local_count_keys+1>
<cfset local_results.RESPONSEHEADER["Set-Cookie"][""&local_count_keys&""] = local_RedirectCookiesAsArray[i][2]>
</cfloop>
</cfif>
<cfset local_results.fileContent = CharsetEncode(ToBinary(ToBase64(local_results.fileContent)), out_Charset)>
<cfset local_results.fileContent = #ToString(local_results.fileContent)#>
<cfset local_results.timing = local_timer>
<cfset local_System.setProperty("http.proxyHost","")>
<cfset local_System.setProperty("http.proxyPort","")>
<cfreturn local_results>
</cffunction>
</cfcomponent>
Sunday, January 29, 2012
How to replace last occurrence of substring in coldfusion
I get a lot of questions lately on how to extract a substring from a string. Below you can find 4 examples on how to extract a specific substring from a string. It is not to difficult if you know how to write regular expressions. If you have no experience with regex writting these rules can give you a major headache.
Replace second occurrence of substring in a string
<!--- Replace the last occurence of test in the string ---> <cfset example_string = "test abc test 123 test def test 456"> <cfoutput> #rereplace(example_string,'(.*)(test )(.*)','\1\3','one')# </cfoutput> <!--- Output: test abc test 123 test def 456 --->Below some other examples of how to replace a specific substring from a string.
Replace second occurrence of substring in a string
<!--- Replace the second occurence of test in the string ---> <cfset example_string = "test abc test 123 test def test 456"> <cfoutput> #rereplace(example_string,'(.*?)(test )(.*?)(test )(.*)','\1\2\3\5','one')# </cfoutput> <!--- Output: test abc 123 test def test 456 --->Replace the first occurrence of substring in a string
<!--- Replace the second occurence of test in the string ---> <cfset example_string = "test abc test 123 test def test 456"> <cfoutput> #rereplace(example_string,'(.*?)(test )(.*)','\1\3','one')# </cfoutput> <!--- Output: abc test 123 test def test 456 --->Replace the penultimate occurrence of substring in a string
<!--- Replace the penultimate occurence of test in the string ---> <cfset example_string = "test abc test 123 test def test 456"> <cfoutput> #rereplace(example_string,'(.*)(test )(.*?)(test )(.*)','\1\3\4\5','one')# </cfoutput> <!--- Output: test abc test 123 def test 456 --->
Thursday, January 26, 2012
Detect the language of text in coldfusion
On occasion i have the opportunity to write something fun. For a project i am currently working on i need to determine / detect the language of text stored in a database. I googled for solutions for determining the language of a string but none of the solutions were easy to implement. So i decided to write a component to solve this problem. Basically i created an array which contains often used words in different languages. Then the function looks for these words in the text for which the language needs to be determined. Based on the number of occurences of the words in the text the language is returned. If you have a question just place a comment.
<cfcomponent name="detectLanguage">
<cfset Variables.dl_languages = arrayNew(1)>
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)+1] = structNew()>
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)].description = "Engels">
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)].lan = "EN">
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)].words = "swimming pool,environment,satisfied,quiet,food,staff,location,delicious,room,rooms,beautiful,nice,friendly,breakfast,bad,well,price,prices,what,she,water,now,one,day,their,look,not,been,word,has,that,are,him,made,than,this,had,get,each,you,his,into,about,will,number,but,your,people,there,many,could,more,out,its,call,use,down,from,these,the,were,part,may,find,for,how,they,and,can,time,them,then,see,her,way,some,all,was,have,said,with,like,which,other,would,make,two,oil,write,did,come,when,who,long,first">
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)+1] = structNew()>
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)].description = "Nederlands">
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)].lan = "NL">
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)].words = "zwembad,omgeving,tevreden,rustig,rustige,eten,personeel,locatie,heerlijk,kamer,kamers,prachtig,leuk,vriendelijk,vriendelijke,ontbijt,slecht,goed,prijs,doen,dag,later,voor,worden,tijd,laatst,werk,zullen,maken,een,zeggen,nieuw,plaats,aan,hebben,dat,goed,jaar,kunnen,hij,aantal,niet,het,klein,die,man,leven,groot,was,van,mensen,moeten,heel,komen,vrouw,ook,zijn,lang">
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)+1] = structNew()>
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)].description = "Duits">
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)].lan = "DE">
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)].words = "schwimmbad,umwelt,zufrieden,ruhig,essen,personal,lage,lecker,zimmer,schön,freundlich,frühstück,schlecht,gut,preis,preise,sehen,über,selber,wenn,mein,mehr,machen,unser,durch,anderer,das,anderes,groß,immer,wissen,sie,sollen,jede,schon,beispiel,gut,eigentlich,sein,weil,werden,wieder,geben,stehen,also,damit,denn,sehr,bei,andere,gehen,selbst,viel,jedes,jeder,lang,können,doch,unter,aus,ihr,auf,haben,sich,der,kein,zeit,oben,ihm,ihn,noch,jetzt,als,hier,aber,all,was,wir,dann,und,jahr,dass,nach,nur,mir,lassen,uns,zwei,sagen,oder,erstes,mit,erster,kommen,ganz,neu,ein,mich,mal,ich,dies,von,wollen,bis,müssen,nicht,erste,auch,für,vor">
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)+1] = structNew()>
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)].description = "Frans">
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)].lan = "FR">
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)].words = "piscine,l'environnement,satisfaits,calme,alimentaires,personnel,lieu,délicieux,chambre,chambres,belle,amicale,petit déjeuner,mauvaise,ainsi,prix,elle,voir,dire,mon,demander,non,quelque,raison,aussi,depuis,sans,pendant,moins,monsieur,peu,même,monde,dont,trouver,celui,heure,premier,alors,être,autre,mettre,bien,dans,leur,qui,donner,que,jour,aucun,dernier,nouveau,passer,suite,falloir,personne,temps,sur,mais,deux,point,pour,avant,part,devoir,très,ainsi,vous,aller,comme,cela,nous,rendre,lequel,bon,notre,par,plus,avec,fois,encore,entre,avoir,lui,savoir,pouvoir,vouloir,venir,grand,déjà ,comprendre,rester,prendre,tout,son,faire,après">
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)+1] = structNew()>
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)].description = "Spaans">
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)].lan = "ES">
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)].words = "piscina,ambiente,satisfecho,tranquilo,comida,personal,ubicación,delicioso,habitación,habitaciones,hermoso,bueno,amistoso,desayuno,malo,bien,precio,precios,pasar,también,hacer,menos,ese,porque,haber,ahora,desde,poder,seguir,llevar,hasta,donde,nuestro,nos,después,primero,nuevo,hablar,dar,querer,vida,para,otro,poner,poco,sin,encontrar,siempre,tan,sobre,por,con,vez,entonces,decir,dos,ver,eso,ella,entre,deber,dejar,asÃ,tanto,grande,cada,cuando,llegar,año,muy,ser,mismo,tiempo,todo,más,como,bien,uno,nada,parte,creer,cosa,tener,saber,que,mucho,parecer,qué,este,hombre,estar,dÃa,pero,quedar,alguno">
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)+1] = structNew()>
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)].description = "Italiaans">
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)].lan = "IT">
<cfset Variables.dl_languages[arraylen(Variables.dl_languages)].words = "piscina,ambiente,soddisfatto,tranquillo,cibo,personale,posizione,delizioso,camera,camere,bello,bella,amichevole,prima colazione,bene,prezzo,prezzi,molto,detto,tutti,dire,tutto,non,noi,quello,più,bene,avere,hai,tuo,con,posso,andare,essere,due,ché,voglio,sono,alla,niente,qui,cosa,così,della,gli,quando,stare,solo,dei,del,fatto,volere,suo,sei,dove,fare,perché,sua,sta,anche,hanno,sapere,allora,questo,vuoi,una,mia,lui,stato,per,questa,che,ancora,chi,mio,grazie,come,uno,ora,mai,nel,era,siamo,lei,potere,abbiamo">
<cffunction name="lan">
<cfargument name="in">
<cfset var result = structNew()>
<cfset var wordsInString = arraylen(reMatch("[[:word:]]+", in))>
<cfset result.languages = arrayNew(1)>
<cfset result.success = false>
<cfset result.absolute = 0>
<cfset result.relative = 0>
<cfset result.message = "">
<cfloop from=1 to="#arrayLen(dl_languages)#" index="l">
<cfset result.languages[arraylen(result.languages)+1] = arrayNew(1)>
<cfset result.languages[arraylen(result.languages)][1] = dl_languages[l].lan>
<cfset result.languages[arraylen(result.languages)][2] = 0>
<cfloop list="#dl_languages[l].words#" index="w">
<cfset result.languages[arraylen(result.languages)][2] = result.languages[arraylen(result.languages)][2] + #evaluate((Len(trim(in)) - Len(reReplace(lcase(trim(in)),'(^|\s)(#w#)(\s|\.|\,|$)','\1\3','all'))) / Len(#w#))#>
</cfloop>
</cfloop>
<cfloop index="outer" from="1" to="#arrayLen(result.languages)#">
<cfloop index="inner" from="1" to="#arrayLen(result.languages)-1#">
<cfif result.languages[inner][2] lt result.languages[outer][2]>
<cfset arraySwap(result.languages,outer,inner)>
</cfif>
</cfloop>
</cfloop>
<cfset result.wordsInString = wordsInString>
<cfif result.languages[1][2] gt 0 and result.languages[1][2] neq result.languages[2][2]>
<cfset result.success = true>
<cfset result.absolute = result.languages[1][2]>
<cfif result.wordsInString gt 0>
<cfset result.relative = ceiling(result.absolute/result.wordsInString*100)>
</cfif>
<cfset result.lan = result.languages[1][1]>
<cfelse>
<cfset result.lan = "NA">
<cfif result.languages[1][2] eq 0>
<cfset result.message = "No matches">
<cfelseif result.languages[1][2] eq result.languages[2][2]>
<cfset result.message = "Multiple matches">
</cfif>
</cfif>
<cfreturn result>
</cffunction>
</cfcomponent>
Friday, January 6, 2012
Javascript objects
I always wanted to create a simple example on how you can create objects in javascript. The example also contains private and public variables and functions.
/* Create a new object */
function testObject()
{
/* Private variables can only be called by functions inside this object */
var privatePropertyValue = 'Private property:1';
/* Public variables can be directly called through objectname */
this.publicPropertyValue = 'Public property:1';
this.objectName = 'testObject';
/*
Private functions can only be called by functions inside this object
Private functions cannot directly access the this method of the object
*/
function privateFunctionSetVariable()
{
/*
Method this is not possible: e.g.
this.publicPropertyValue = 'Private function';
does not refer to the object
*/
alert("This will fail:" + this.objectName);
privatePropertyValue = 'Private property:2';
}
function privateFunctionSetGlobalVariable(c)
{
alert("This will succeed:" + c.objectName);
c.publicPropertyValue = 'Public property:3';
}
/* Public functions can be directly called through objectname */
this.publicFunctionSetVariable = function ()
{
this.publicPropertyValue = 'Public property:2';
};
this.publicCallPrivateFunction = function ()
{
privateFunctionSetVariable();
};
this.publicCallPrivateFunctionGlobal = function ()
{
privateFunctionSetGlobalVariable(this);
};
this.publicAlert = function (t,v)
{
if(t=='private') alert(eval(v));
else alert(eval('this.'+ v));
};
}
var createObject = new testObject();
createObject.publicAlert('public','publicPropertyValue');
createObject.publicFunctionSetVariable();
createObject.publicAlert('public','publicPropertyValue');
createObject.publicCallPrivateFunctionGlobal();
createObject.publicAlert('public','publicPropertyValue');
createObject.publicAlert('private','privatePropertyValue');
createObject.publicCallPrivateFunction();
createObject.publicAlert('private','privatePropertyValue');
Wednesday, October 26, 2011
Google maps load markers into grid
A problem i recently encoutered was displaying more than 400 markers on a single map. Reading through the google maps api documentation i found the following post regarding this problem: http://code.google.com/apis/maps/.... The first solution mentioned is Grid-based Clustering which fits my needs. However the docs do not explain how to create such a grid. Here i explain what data is necessary to create a grid and load the markers into the grid.
First we need the boundaries of the current map:
These variables much be send to the page which returns the markers, this probably is already the case.
Now we are ready to create the grid:
Almost there now we load the markers into the grid
First we need the boundaries of the current map:
var max_lng = map.getBounds().getNorthEast().lng();
var max_lat = map.getBounds().getNorthEast().lat();
var min_lng = map.getBounds().getSouthWest().lng();
var min_lat = map.getBounds().getSouthWest().lat();
These variables much be send to the page which returns the markers, this probably is already the case.
<cfquery name="GetMarkers" datasource="source">
SELECT id, lat, lng
FROM markers
WHERE
(<cfif min_lat lt max_lat>lat <= #max_lat# and lat>= #min_lat#<cfelse>lat <= #max_lat# OR lat>= #min_lat#</cfif>) AND
(<cfif min_lng lt max_lng>lon <= #max_lng# and lon>= #min_lng#<cfelse>lon <= #max_lng# OR lon>= #min_lng#</cfif>)
</cfquery>
<!--- Root of the number of grids: for example 10 means 100 grids --->
<cfset grid_count_root = 30 />
<cfset grid_min_lng = min_lng />
<cfset grid_min_lat = min_lat />
<!--- Array that contains the grid --->
<cfset grid_arr = arrayNew(2) />
<cfset grid_arr_lng = arrayNew(2) />
<cfset grid_arr_lat = arrayNew(2) />
<cfset grid_with_data = arrayNew(1) />
<cfset grid_lng_div = abs((min_lng-max_lng)/grid_count_root) />
<cfset grid_lat_div = abs((min_lat-max_lat)/grid_count_root) />
Now we are ready to create the grid:
<cfloop from=1 to=#grid_count_root# index="i">
<cfset grid_arr_lng[i][1] = grid_min_lng />
<cfset grid_arr_lng[i][2] = grid_min_lng + grid_lng_div />
<cfset grid_arr_lat[i][1] = grid_min_lat />
<cfset grid_arr_lat[i][2] = grid_min_lat + grid_lat_div />
<cfset grid_min_lng = grid_min_lng + grid_lng_div />
<cfset grid_min_lat = grid_min_lat + grid_lat_div />
</cfloop>
<cfloop from=1 to=#arraylen(grid_arr_lng)# index="i">
<cfloop from=1 to=#arraylen(grid_arr_lng)# index="j">
<cfset cur_pos = arrayLen(grid_arr)+1 />
<cfset grid_arr[cur_pos][1] = grid_arr_lng[i][1] />
<cfset grid_arr[cur_pos][2] = grid_arr_lng[i][2] />
<cfset grid_arr[cur_pos][3] = grid_arr_lat[j][1] />
<cfset grid_arr[cur_pos][4] = grid_arr_lat[j][2] />
<cfset grid_arr[cur_pos][5] = '' />
<cfset grid_arr[cur_pos][6] = '' />
<cfset grid_arr[cur_pos][7] = '' />
<cfset grid_arr[cur_pos][8] = 0 />
</cfloop>
</cfloop>
Almost there now we load the markers into the grid
<cfloop from=1 to=#GetMarkers.recordcount# index="i">
<cfset lng_grid_pos = #evaluate((GetMarkers.lng[i] -min_lng)/grid_lng_div)# />
<cfif lng_grid_pos gt 0><cfset lng_grid_pos= #evaluate((ceiling(lng_grid_pos-1)*grid_count_root)+1)#>
<cfelse><cfset lng_grid_pos = 1 /></cfif>
<cfset lat_grid_pos = #evaluate((GetMarkers.lat[i] -min_lat)/grid_lat_div)# />
<cfif lat_grid_pos gt 0><cfset lat_grid_pos= #evaluate((ceiling(lat_grid_pos-1)))#>
<cfelse><cfset lat_grid_pos = 0 /></cfif>
<cfset grid_pos = #evaluate(lng_grid_pos+lat_grid_pos)# />
<cfif grid_arr[grid_pos][5] eq ''>
<cfset grid_arr[grid_pos][5] = GetMarkers.id[i] />
<cfset grid_arr[grid_pos][6] = GetMarkers.lat[i] />
<cfset grid_arr[grid_pos][7] = GetMarkers.lng[i] />
<cfset grid_with_data[arraylen(grid_with_data)+1] = grid_pos />
</cfif>
<cfset grid_arr[grid_pos][8] = grid_arr[grid_pos][8]+1 />
</cfloop>
Wednesday, June 22, 2011
Extracting text / reading text from a secured pdf in coldfusion
So you want to extract text from a pdf that has some form of security on it. For example the pdf has the following properties:
Application
Author someone
CenterWindowOnScreen [empty string]
ChangingDocument Not Allowed
Commenting Not Allowed
ContentExtraction Not Allowed
CopyContent Not Allowed
You probably already figured out that this is not possible with the standard cfpdf tag introduced in coldfusion version 8. You will get the following error:
However it is possible. A coldfusion programmor called Raymond Camden has written a coldfusion component that bypasses the standard security restrictions on a pdf. This component is called pdfutil and you can find the source code here or if you like to read more about the author you can go here. The pdfutils components uses ddx to bypasses any security issues. The installation is very straightforward so it should not take you long before you can see the results yourself.
Application
Author someone
CenterWindowOnScreen [empty string]
ChangingDocument Not Allowed
Commenting Not Allowed
ContentExtraction Not Allowed
CopyContent Not Allowed
You probably already figured out that this is not possible with the standard cfpdf tag introduced in coldfusion version 8. You will get the following error:
An error occurred during EXTRACTTEXT operation in. Error: The password provided is either wrong or does not have sufficient permission to perform this action.
However it is possible. A coldfusion programmor called Raymond Camden has written a coldfusion component that bypasses the standard security restrictions on a pdf. This component is called pdfutil and you can find the source code here or if you like to read more about the author you can go here. The pdfutils components uses ddx to bypasses any security issues. The installation is very straightforward so it should not take you long before you can see the results yourself.
Wednesday, March 9, 2011
Building a coldfusion based spider - Crash course
So you are interested in building a spider in coldfusion. Well i will try to explain the basic principles of building your own spider. First we will have to access data on the web. Below a simple example of downloading data through the cfhttp tag.
You might have noticed that i set the getasbinary argument to yes. The reason for this is to be able to convert ISO-8859-1 (or any other character set) to utf-8. One (of the many) problems you will encouter is determining which character set the spidered page is using. In many cases the cfhttp tag won't return the proper character set. The code below finds the proper character set and if necessary converts the content to utf-8.
Okay we have downloaded content from an url and converted this content to utf-8. The next step is applying patterns to the content. This is where the fun begins. As of coldfusion version 8 you can use the REMatch function to find and extract patterns. However i will advice you not to use this tag because is has some limitations. Use the following function instead:
Now you have learned some basic principles about spidering and extracting data from an webpage. If you like this information and like to know more about building a web spider in coldfusion please leave a comment.
<cfhttp method="get" timeout="30" redirect="no" getasbinary="yes" url="http://coldfusion9.blogger.com" charset="utf-8" userAgent="Mozilla/5.0 (Windows; U; Windows NT 6.1; nl; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15"> <cfhttpparam type="header" name="Accept-Encoding" Value="no-compression"> <cfhttpparam type="header" name="Cache-Control" value="no-cache"> </cfhttp>
You might have noticed that i set the getasbinary argument to yes. The reason for this is to be able to convert ISO-8859-1 (or any other character set) to utf-8. One (of the many) problems you will encouter is determining which character set the spidered page is using. In many cases the cfhttp tag won't return the proper character set. The code below finds the proper character set and if necessary converts the content to utf-8.
<cfif cfhttp.errorDetail eq ''>
<cfset mycharset = "">
<cfset mycharset = #cfhttp.Charset#>
<cfset filec = #ToString(cfhttp.fileContent)#>
<cfif len(mycharset) eq 0>
<cfset mycharset = "ISO-8859-1">
<cfset pat = "(?i)<META(.*)charset=\s*([^\s|^""|^']*)">
<cfif #refindnocase(pat,filec)# gt 0>
<cfset local_re = #refindnocase(pat,filec,1,true)#>
<cfif local_re.len[3] gt 0>
<cfset mycharset = #Mid(filec,local_re.pos[3],local_re.len[3])#>
<cfelse>
<cfset mycharset = #Mid(filec,local_re.pos[2],local_re.len[2])#>
<cfset mycharset = trim(rereplacenocase(mycharset,'.*charset=([^>^' & "'" & '^"]+).*','\1','one'))>
</cfif>
</cfif>
</cfif>
<cfif mycharset neq 'utf-8'>
<cftry>
<cfset filec = CharsetEncode(cfhttp.fileContent, mycharset)>
<cfset filec = #ToString(filec)#>
<cfcatch type="any">
<cfset filec = #ToString(cfhttp.fileContent)#>
</cfcatch>
</cftry>
</cfif>
</cfif>
Okay we have downloaded content from an url and converted this content to utf-8. The next step is applying patterns to the content. This is where the fun begins. As of coldfusion version 8 you can use the REMatch function to find and extract patterns. However i will advice you not to use this tag because is has some limitations. Use the following function instead:
<cfset objPattern = CreateObject("java","java.util.regex.Pattern").Compile('#yourpattern#') />
<cfset objMatcher = objPattern.Matcher(filec) />
<cfloop condition="objMatcher.Find()">
#objMatcher.Group(1)#
</cfloop>
Now you have learned some basic principles about spidering and extracting data from an webpage. If you like this information and like to know more about building a web spider in coldfusion please leave a comment.
Saturday, January 15, 2011
Convert a relative url to an absolute url in coldfusion
To my surprise there is not a default function in coldfusion that converts relative urls to absolute ones. So i created one. This function relies heavily on regular expressions. If you are not familiar with regular expression i suggest you do not change any of the functions that use it (like rereplace and refind). So use the function simply input the relative url and the absolute url the relative url comes from. E.g.
Relative url = "/example.html"
Absolute url = "http://www.example.com/a/b/c/index.html"
Result = http://www.example.com/example.html
Example call function
Complete function
Relative url = "/example.html"
Absolute url = "http://www.example.com/a/b/c/index.html"
Result = http://www.example.com/example.html
Example call function
#regex_UrlRelToAbs('/example.html','http://www.example.com/a/b/c/index.html')#
Complete function
<cffunction name="regex_UrlRelToAbs">
<cfargument name="RelativePath">
<cfargument name="AbsolutePath">
<cfset var tempstruct = structnew()>
<cfset var AbsolutePath_arr = ''>
<cfset RelativePath=ReplaceNoCase(RelativePath,"'","'","ALL")>
<cfset RelativePath=ReplaceNoCase(RelativePath,""","""","ALL")>
<cfset RelativePath=ReplaceNoCase(RelativePath,"<","<","ALL")>
<cfset RelativePath=ReplaceNoCase(RelativePath,">",">","ALL")>
<cfset RelativePath=ReplaceNoCase(RelativePath,"&","&","ALL")>
<cfif isvalid('url',RelativePath)><cfreturn RelativePath></cfif>
<cfif refind('^\/\/',RelativePath)>
<cfset RelativePath = rereplace(RelativePath,'^\/\/','','one')>
<cfreturn "http://#RelativePath#">
<cfif>
<cfset tempstruct.domain = reReplace(AbsolutePath,"(^\w+://)([^\/:]+)[\w\W]*$","\1\2","one") />
<cfset AbsolutePath_arr = #refind("^((http[s]?|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^##?\s]+)(.+)?(##[\w\-]+)?$",AbsolutePath,1,true)#>
<cftry>
<cfif AbsolutePath_arr.len[5] gt 0>
<cfset tempstruct.folders = #trim(mid(AbsolutePath,AbsolutePath_arr.pos[5],AbsolutePath_arr.len[5]))#>
<cfelse>
<cfset tempstruct.folders = ''>
</cfif>
<cfcatch type="any">
<cfset tempstruct.folders = ''>
</cfcatch>
</cftry>
<cfif left(RelativePath,1) eq '/'>
<cfreturn "#tempstruct.domain##RelativePath#">
<cfelseif left(RelativePath,1) neq '.'>
<cfreturn "#tempstruct.domain##tempstruct.folders##RelativePath#">
<cfelse>
<cfset regex_UrlProperties_oc = #arraylen(rematch('(\.\./|\./)',RelativePath))#>
<cfset regex_UrlProperties_newF = ''>
<cfloop from=1 to=#listlen(tempstruct.folders,'/')-regex_UrlProperties_oc# index="i"><cfset regex_UrlProperties_newF = "#regex_UrlProperties_newF##listgetAt(tempstruct.folders,i,'/')#/"></cfloop>
<cfreturn "#tempstruct.domain#/#regex_UrlProperties_newF##rereplace(RelativePath,'(\.\./|\./)','','all')#">
</cfif>
</cffunction>
Tuesday, May 25, 2010
Enable long text retrieval in coldfusion
If you are ever going to use long text fields in mysql or a SQL server you will get a strange result when outputting this data. By default ColdFusion retrieves the number of characters specified in the Long Text Buffer setting (under the advanced settings of a datasource). When coldfusion stores the information of the database into an structure or variable it limits the number of characters (if you are using utf-8 by default about 4.000 characters will be stored). To prevend this behavior and to be able to display the complete content of the database cell you need to check the Enable long text retrieval (CLOB) in the advance settings of a data source.
Tuesday, May 18, 2010
32 bit components on 64 bit coldfusion
If you try to initiate an 32 bit component on the 64 bit version of coldfusion (e.g. <cfobject action="Create" class="MSXML2.ServerXMLHTTP" name="objSrvHTTP">)
you will get the following error:
The only way to solve this is to install a 32bit version of coldfusion on a different server or workstation (or run it parallel on the same server). I know of two possible solutions for this problem.
Option 1
Install a 32bit version of coldfusion on a different server or workstation (or run it parallel on the same server). You can run the component on a 32 bit server or workstation. In my case i created a web services that executed the 32bit component and returns the result to the caller.
Option 2
Call the component in asp and return the data in a webservice or as html. The following example executes the MSXML2.ServerXMLHTTP component in aspx and writes the response to the page. This pages can be called from a coldfusion cfhttp tag. Please note i know very little of asp so this example could probebly be improved.
you will get the following error:
An exception occurred when instantiating a COM object.Unfortunately there is no easy solution for this problem.
The cause of this exception was that: java.lang.RuntimeException: Can not use native code: Initialisation failed.
Option 1
Install a 32bit version of coldfusion on a different server or workstation (or run it parallel on the same server). You can run the component on a 32 bit server or workstation. In my case i created a web services that executed the 32bit component and returns the result to the caller.
<cfcomponent>
<cffunction name="example" access="remote" returntype="struct" output="no">
<cfset pass_url="http://www.adobe.com/"">
<cfobject action="Create" name="objSrvHTTP" class="MSXML2.ServerXMLHTTP">
<cfset temp = objSrvHTTP.open("GET","#pass_url#")>
<cfset temp = objSrvHTTP.send("")>
<cfset websitedata=structnew()>
<cfset websitedata.responseBody = #toString(objSrvHTTP.responseBody)#>
<cfset websitedata.status = #left(objSrvHTTP.Status,3)#>
<cfreturn websitedata>
</cffunction>
</cfcomponent>Option 2
Call the component in asp and return the data in a webservice or as html. The following example executes the MSXML2.ServerXMLHTTP component in aspx and writes the response to the page. This pages can be called from a coldfusion cfhttp tag. Please note i know very little of asp so this example could probebly be improved.
<%@ Page Language="VB" Debug="false" %>
<%
dim page_url
dim page_status
dim page_responseBody
dim page_responseText
dim xmlhttp
dim mycharset
page_url = Request.QueryString("weburl")
xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open ("GET", page_url, false)
xmlhttp.setRequestHeader("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 ( .NET CLR 3.5.30729)")
xmlhttp.send ("")
' page_status = Cint(xmlhttp.status)
page_responseBody = xmlhttp.responseBody
page_responseText = xmlhttp.responseText
mycharset = ""
mycharset= xmlhttp.getResponseHeader("Content-Type")
If(inStr(mycharset,"charset")=0) Then
mycharset = "ISO-8859-1"
Dim pattern As String = "(?i)>META(.*)charset=\s*([^\s|^""|^']*)"
If( Regex.IsMatch( page_responseText, pattern )) Then
Response.CharSet = Regex.Match( page_responseText, pattern ).Groups(2).ToString()
End If
End If
Response.CharSet = mycharset
response.StatusCode = page_status
Response.BinaryWrite(page_responseBody)
' Response.Write(page_responseText)
xmlhttp = nothing
%>
Monday, May 17, 2010
Compare big integers in Coldfusion
Encountered a strange result in coldfusion when i was comparing big integers in coldfusion. In short you should never use the EQ operator when checking if two big integers are the same. This is because coldfusion almost always sees integers as java.lang.Double. So when comparing something like
<cfdump var="#1111111111111111112 eq 1111111111111111113#">
The result will be YES, instead of No. The solution for this strange behavior is to use the Compare function:
<cfdump var="#compare(1111111111111111112,1111111111111111113)#">
If the two big integers are the same this function will return 0 if they are not the same the function will return -1 or 1. In this case it will return -1.
More info on this topic can be found at Beware ColdFusion Floating Point Integers a blog by Barney Boisvert.
<cfdump var="#1111111111111111112 eq 1111111111111111113#">
The result will be YES, instead of No. The solution for this strange behavior is to use the Compare function:
<cfdump var="#compare(1111111111111111112,1111111111111111113)#">
If the two big integers are the same this function will return 0 if they are not the same the function will return -1 or 1. In this case it will return -1.
More info on this topic can be found at Beware ColdFusion Floating Point Integers a blog by Barney Boisvert.
Wednesday, May 5, 2010
cfimage and mod rewrite
If you are Encountering problems serving cfimage images and using some sort of url rewriting program you might be interested in the following article from the bennadel site:
URL Rewriting And ColdFusion's WriteToBrowser Image Functionality (CFFileServlet)
In short you need to prevent your url rewriting program from redirecting URLs with the CFFileServlet folder in it.
URL Rewriting And ColdFusion's WriteToBrowser Image Functionality (CFFileServlet)
In short you need to prevent your url rewriting program from redirecting URLs with the CFFileServlet folder in it.
Lyla Captcha and coldfusion 9
If you need to have a compatible script for captcha (e.g. running the script on a coldfusion mx server and on a coldfusion 8 and above server). You might be interested in the following solution i have used. First of all Lyla captcha is not compatible with coldfusion version 9 (haven't tested it on coldfusion 8 but i think it isn't compatible). As of coldfusion version 8 there is a cfimage tag which isn't compatible with coldfusion mx. To get a captcha script to work on both coldfusion mx and coldfusion 9 you can try the following.
The captcha-cf8.cfm file:
<cfif left(server.coldfusion.productversion,1) gte 8> <cflocation url="captcha-cf8.cfm"> <cfelse> #Your Lyla captcha code here </cfif>
The captcha-cf8.cfm file:
<cfset captchaimagepath=" getdirectoryfrompath(getcurrenttemplatepath())"> <cfimage fonts="Arial black" difficulty="medium" destination="#captchaimagepath#" text="12345" height="60" width="192" action="captcha"> <cfcontent deletefile="yes" file="#captchaimagepath#" type="image/png" reset="yes">
Connect to a microsoft database on another server
Accesing a microsoft database on another server also has some pitfalls. You may get the following error message:
java.sql.SQLException: [Macromedia][SequeLink JDBC Driver][ODBC
Socket][Microsoft][ODBC Microsoft Access Driver] '(unknown)
Adobe offers the following solution:
This error occurs when permissions are not set properly and you try to use theIf you are using a 64 bit version of coldfusion and you want to access a 32 bit microsoft access database you also need to do the following:
ODBC Socket or the drive letter of the mapped drive to the database. Follow the
steps below to avoid the issue:
1.Make sure the user who logged onto the machine
has administrator rights.
2.Set that user to be the logon account for the
ColdFusion MX ODBC Server and ColdFusion MX ODBC Agent services in the Windows
services control panel.
3.Map a drive to the remote database.
4.Create the data
source in the ColdFusion Administrator using the Microsoft Access option, not
ODBC Socket.
5.Set the path to the database using the UNC path, such as the
following example: file://machine_name/databases/test.mdb
- Read http://coldfusion9.blogspot.com/2010/05/connect-to-local-microsoft-access-32.html
- open C:\Windows\System32\odbcad32.exe
- click file dns
- click add
- select microsoft access driver
- click finish
- click select
- in the database name field enter the full unc path (e.g. file://server/folder/database.mdb)
- click ok
- enter desired datasource name (same name as in the coldfusion administrator datasources)
Connect to a local Microsoft Access 32 bit database from coldfusion 9 64 bit.
Follow the instruction on the following link http://forums.adobe.com/message/2000549. In short copy the Windows\SysWOW64\odbcad32.exe to Windows\System32\odbcad32.exe. Make sure you have backup of both files. Enter the new Windows\System32\odbcad32.exe file. Add the dns. Then add the dns through the coldfusion administrator (for the System Database File use - C:\windows\system32\system.mdw) . It will give an error don't worry it will work ( at least for me).
Running coldfusion on IIS 7.5 on windows 7 enterprise will not run coldfusion files.
You can encounter problems if IIS is not installed properly. Dale Fraser has written an excellent solution for this: http://dale.fraser.id.au/2008/05/coldfusion-8-vista-sp1-solution.html. In short you need to add a couple of iis features installed. Go to the control panel --> Programs and features --> click 'windows features on or off'. Install the features in the image below.
Kick-off
Hi there,
Well i thought about writing a blog for some time now. Finally i found some time to set it up. This blog will be about coldfusion and javascript solutions and problems i encounter during my daily work.
Greetings Nebu
Well i thought about writing a blog for some time now. Finally i found some time to set it up. This blog will be about coldfusion and javascript solutions and problems i encounter during my daily work.
Greetings Nebu
Subscribe to:
Posts (Atom)

