Need to use the smtp settings of coldfusion 10 - coldfusion

There is a new settings of Application.cfc in the coldfusion 10 which is:
this.smtpServersettings = as a structure
I can hard code the values here, but i am saving my mail settings in the database and i want to use those values here, is it possible or not to use the database settings here or not.
here is my Update:
<cfcomponent hint="File for the Website" output="false">
<cfsetting showdebugoutput="no">
<cfset this.name = "myProject1">
<cfset this.applicationTimeout = createTimeSpan(2,0,0,0)>
<cfset this.clientmanagement= "yes">
<cfset this.loginstorage = "session">
<cfset this.sessionmanagement = "yes">
<cfset this.sessiontimeout = CreateTimeSpan(0,0,40,0) />
<cfset this.setClientCookies = "yes">
<cfset this.scriptProtect = "all">
<cfset this.setDomainCookies = true>
<cfset this.customTagPaths = ExpandPath('customtags')>
<cfset this.datasource = {name="myDB"}>
<cfset this.smtpServersettings = {structur as username,password and mail settings}>
but that value needs to be through database, can you show me an example please

Yes, you should be able to set the value of the this.smtpServersettings variable from a database query. And for what it's worth, I think that setting as been available since ColdFusion 9.
All you would need to do is something like the following.
Run your query to get the values from the database
Create a new structure
Assign the three variables to that structure; server, username, and password
Assign the this.smtpServersettings variable to your new structure
Of course you will need to add some code to do something if/when your database query fails.
Update after the question was updated
Just do something like the following (pseudo code):
<cfcomponent hint="File for the Website" output="false">
<cfsetting showdebugoutput="no">
<cfset this.name = "myProject1">
<cfset this.applicationTimeout = createTimeSpan(2,0,0,0)>
<cfset this.clientmanagement= "yes">
<cfset this.loginstorage = "session">
<cfset this.sessionmanagement = "yes">
<cfset this.sessiontimeout = CreateTimeSpan(0,0,40,0) />
<cfset this.setClientCookies = "yes">
<cfset this.scriptProtect = "all">
<cfset this.setDomainCookies = true>
<cfset this.customTagPaths = ExpandPath('customtags')>
<cfset this.datasource = {name="myDB"}>
<!--- run your query here --->
<!--- check your query and do something appropriate if/when it fails --->
<cfset this.MyStructure = StructNew()>
<cfset this.MyStructure.server = this.MyQuery.ServerNameVariable>
<cfset this.MyStructure.username = this.MyQuery.UserNameVariable>
<cfset this.MyStructure.password = this.MyQuery.PasswordVariable>
<cfset this.smtpServersettings = this.MyStructure>
You also need to be aware that doing it this way will run that query on every page request. Make sure it runs fast.

Related

Invalid Component Definition

I'm having an issue in the logs I cannot replicate on the browser. I'm getting hundreds of these per day
invalid component definition, can't find component [cfc.udf]
The cfc are stored in a cfc folder one level above the app. This is so that many apps can use the same cfc.
Folder structure:
---- cfc
--------- udf.cfc
---- myApp
--------- application.cfc
In the application.cfc, I'm using application-specific mappings because this is set on a lot of different load-balanced-servers in production as well as a QA environment and local testing environment and keeping them all synced would be difficult.
At onRequestStart, I have a function that restarts the application every 5 minutes. It was supplied by a consultant. I suspect that this is the culprit because the logs show these errors coming in at exactly 5 minute intervals
<cfcomponent>
<cfset This.name = "myApp">
<cfset This.Sessionmanagement=true>
<cfset This.Sessiontimeout="#createtimespan(0,0,30,0)#">
<cfset this.mappings['/cfc'] = ExpandPath('../cfc')>
<cffunction name="onApplicationStart">
<cfset Application.udf = createObject("component", "cfc.udf").init()>
</cffunction>
<cffunction name="onRequestStart">
<cfset appRefreshMinutes = 5>
<cfif Not IsDefined("Application.refreshTime")>
<cfset currentMinute = Minute(Now())>
<cfset Application.refreshTime = DateAdd("n", -(currentMinute MOD appRefreshMinutes)+appRefreshMinutes, Now())>
<cfset Application.refreshTime = DateAdd("s", -(Second(Application.refreshTime)), Application.refreshTime)>
</cfif>
<cfif Now() GTE Application.refreshTime Or IsDefined("URL.reload")>
<cflock name="ApplicationInit" type="exclusive" timeout="5" throwontimeout="false">
<cfif Now() GTE Application.refreshTime Or IsDefined("URL.reload")>
<cfset OnApplicationStart()>
<cfset Application.refreshTime = DateAdd("n", appRefreshMinutes, Application.refreshTime)>
</cfif>
</cflock>
</cfif>
</cffunction>
</cfcomponent>
Promoted from the comments
Have you tried using a mapping name other than /cfc? Like:
<cfset this.mappings['/somethingelse'] = ExpandPath('../cfc')>
so that you can then call it like:
<cfset Application.udf = createObject("component", "somethingelse.udf").init()>
Maybe it just looks odd to me or maybe that is causing your issue (cfc being a reserved word or somehow getting special treatment in this case).

ColdFusion session variables are not accessible in other pages

My dept. has just bought CF10 and I'm finally updating my existing web app to CF10 from CF8.
My first problem with CF10 is using session variables. When using the lower versions of CF (CF4 to CF8) I have never had any problem setting up session variables and getting or using these variables in other pages after a successful login. With CF 10 it seems setting session variables and using them in other pages are major problems. I'm not sure what have I done wrong in the code.
First I'm setting the session in my Application.cfc this way:
<cfcomponent displayname="Application" output="true">
<cfset THIS.Name ="MyNewApp"/>
<cfset THIS.ApplicationTimeout = CreateTimeSpan(0,0,20,0) />
<cfset THIS.SessionManagement ="YES"/>
<cfset THIS.SessionTimeout = CreateTimeSpan( 0, 0, 20, 0 ) />
<cfset THIS.SetClientCookies = false />
<cffunction name="OnApplicationStart" access="public" returntype="boolean"
output="false">
<cfset application.Main_DSN = "TESTDB">
<cfreturn true />
</cffunction>
<cffunction name="onApplicationEnd" output="false">
<cfargument name="applicationScope" required="true">
</cffunction>
<cffunction name="OnSessionStart" access="public" returntype="void" output="false"
hint="Fires when user session initializes.">
<cfset session.loggedin = "NO">
<cfset session.username = "">
<cfset session.userrights = "">
<cfset session.usergroup = "">
</cffunction>
</cfcomponent>
After login, user is validated and set values to those session.variables:
........user validation codes here......................
<cfif mylogin NEQ true>
<cflocation url="/login/login.cfm">
<cfabort
<cfelse>
<cfset session.loggedin="Yes">
<cfset session.username="#Trim(Form.username)#">
<CFSET qUserRights = LoginObj.getUserRights('#Trim(Form.username)#')>
<cfset session.userrights = qUserRights><!--- it's a query --->
<CFSET qUserGroup = LoginObj.getUserGroup('#Trim(Form.username)#')>
<cfloop query="qUserGroup">
<cfset session.usergroup = user_group>
<cfbreak>
</cfloop>
<!--- ******* ?????????????????????????
When I do cfdump in at this level, I can see that all of these session variables have
been assigned to their values.
But these session variables are not accessible from other pages. Other pages still show
these session variable without its value.
So, when I use these cfdumps in the index.cfm it is shown as they're not yet assigned
with any values ********* --->
<cfdump var="#session.loggedin#">
<cfdump var="#session.username#">
<cfdump var="#session.userright#">
<cfdump var="#session.usergroup#">
</cfif>
In index.cfm, Before Login I got:
session.loggedin = NO
session.username = ["empty string"]
session.userrights = ["empty string"]
session.usergroup = ["empty string"]
After a successful Login:
session.loggedin = NO
session.username = ["empty string"]
session.userrights = ["empty string"]
session.usergroup = ["empty string"]
Have I done something wrong? This code works on CF8.
I need to mention: CF10 is in Linux and my web app is under https not http. But these session variables should be shared between http and https because some older pages are still in http and can not be moved to https.
I do not have a direct answer to this question. However, the problem is not only in CF10. I am experiencing this in CF8. I create session variables in my application.cfc however they cannot be seen (accessed) in other pages. I believe in your case you may want to consider case sensitive checks since you are on a Linux platform as rudimentary as this may seem if all else fails.

Application.CFC variable not available

In application.cfc I am defining a variable which must be available throughout a session.
<cfcomponent>
<cfset this.applicationTimeout = createTimeSpan(0,4,0,0)>
<cfset this.sessionManagement = true>
<cfset this.setClientCookies = true>
<cfset this.sessionTimeout = createTimeSpan(0,0,90,0)>
<cfset this.setdomaincookies = true>
<cfset this.myVar = "Hello">
</cfcomponent>
I tried several approaches to get the myVar in .cfm but to no avail.
Approach 1: <cfoutput>#myVar#</cfoutput>
Approach 2: <cfoutput>#session.myVar#</cfoutput>
Approach 3: <cfoutput>#application.myVar#</cfoutput>
Any help would be appreciated.
You're confusing the placement of Application.cfc's this-scoped settings with your own application-scoped variables.
Take a look at Ben Nadel's Application.cfc tutorial.
You basically want to put application.myVar in OnApplicationStart():
<cfcomponent>
<cfset this.applicationTimeout = createTimeSpan(0,4,0,0)>
<cfset this.sessionManagement = true>
<cfset this.setClientCookies = true>
<cfset this.sessionTimeout = createTimeSpan(0,0,90,0)>
<cfset this.setdomaincookies = true>
<cffunction name="OnApplicationStart">
<cfset application.myVar = "Hello">
</cffunction>
</cfcomponent>
Then you should be able to reference it in your application as application.myVar.

ColdFusion 8 - Application.cfc interference

I've just had a strange issue with a client's website, my live domain was using my development domains Application settings. I've not had this issue before, and removing cflock around the Application.dsn (amongst other settings) resolved the issue.
As mentioned I have a live site *www.* and development site *dev.*, my development site is in a subfolder of the live site /dev/ and has it's own Application.cfc.
My first question is, if I have variables in my Application scope in the dev folder will this overwrite the Application scope variables in the folder above? Will it consider it the same scope? If so, then this might explain what the issue and if not then I am stumped.
My second question is, how should I correct cflock application scope variables in my application.cfc? Should I be doing this at all?
Here is my Application.cfc, advice would be greatly appreciated:
<cfcomponent output="true">
<cfimport taglib="taglib" prefix="func">
<!---
Check staging directory exists
--->
<cfset THIS.env = "staging">
<!---
Set application vars
--->
<cfset THIS.applicationTimeout = createTimeSpan(0,0,0,0)>
<cfset THIS.sessionManagement="Yes">
<cfset THIS.clientManagement = true>
<cfset THIS.clientStorage = "cookie">
<cfset THIS.loginStorage = "cookie">
<cfset THIS.setDomainCookies = false>
<cfset THIS.setClientCookies = true>
<cfset THIS.scriptProtect = true>
<cfset THIS.secureJSON = true> <!--- Added 12.06.13 --->
<!---
Check environment
Set application name
--->
<cfif THIS.env EQ "staging">
<cfset THIS.applicationName = "devenv">
<cfset THIS.dsn = "devenv">
<cfelse>
<cfset THIS.applicationName = "liveenv">
<cfset THIS.dsn = "liveenv">
</cfif>
<cfif #cgi.HTTP_HOST# NEQ "localhost">
<cfset THIS.dirpath = "http://#cgi.http_host#">
<cfset THIS.componentPath = "cfcs.">
<cfelse>
<cfset urlString = #mid(cgi.PATH_INFO, 2, 200)#>
<cfset THIS.localhostFolderName = #spanexcluding(urlString, "/")#>
<cfset THIS.dirpath = "http://localhost/#THIS.localhostFolderName#">
<cfset THIS.componentPath = "#THIS.localhostFolderName#.cfcs.">
</cfif>
<cfset THIS.name = THIS.applicationName>
<cfset THIS.sessiontimeout = createtimespan(0,0,20,0)>
<cfset THIS.setClientCookies = true>
<cfset THIS.visitor = true>
<cffunction name="onApplicationStart" returntype="void">
<cfset APPLICATION.name = THIS.applicationName>
<cfset APPLICATION.dsn = THIS.dsn>
<cfset APPLICATION.DSN = THIS.dsn>
<cfset APPLICATION.dirpath = THIS.dirpath>
<cfset APPLICATION.componentPath = THIS.componentPath>
<cfif #cgi.HTTP_HOST# EQ "localhost">
<cfset APPLICATION.localhostFolderName = THIS.localhostFolderName>
</cfif>
<!--- USED FOR PATHS AND URLS --->
<!--- Property image upload paths ---->
<cfset APPLICATION.paths = StructNew()>
<!---
Check environment
Set local root
--->
<cfif THIS.env EQ "staging">
<cfset APPLICATION.paths.localRoot = "c:\websites\foobar.co.uk\dev\">
<cfelse>
<cfset APPLICATION.paths.localRoot = "c:\websites\foobar.co.uk\">
</cfif>
<cfset APPLICATION.paths.logs = APPLICATION.paths.localRoot & "logs\">
<cfset APPLICATION.paths.logFile = APPLICATION.paths.logs & "site_log.txt">
<cfset APPLICATION.paths.property = StructNew()>
<cfset APPLICATION.paths.property.image = APPLICATION.paths.localRoot & "images\property\">
<cfset APPLICATION.paths.property.large = APPLICATION.paths.property.image & "large\">
<cfset APPLICATION.paths.property.thumb = APPLICATION.paths.property.image & "thumbs\">
<cfset APPLICATION.paths.property.cmsThumb = APPLICATION.paths.property.image & "thumbs\cms\">
<cfset APPLICATION.paths.property.pdf = APPLICATION.paths.localRoot & "pdf\">
<cfset APPLICATION.paths.property.pdfGenerated = APPLICATION.paths.property.pdf & "generated\">
<cfset APPLICATION.newsUploadPath = APPLICATION.paths.localRoot & "images\news\">
<cfset APPLICATION.articlesUploadPath = APPLICATION.paths.localRoot & "images\articles\">
<cfset APPLICATION.articlesThumbsDir = "../images/articles/thumbs/">
<cfset APPLICATION.articlesContentDir = "../images/articles/assets/">
<cfset APPLICATION.articlesAssetsDir = "../articles/assets/">
<!--- Site URLS ---->
<cfset APPLICATION.urls = StructNew()>
<cfset APPLICATION.urls.root = "http://" & CGI.server_name & "/">
<cfset APPLICATION.urls.com = "com">
<cfset APPLICATION.urls.tagLib = APPLICATION.urls.root & "taglib/">
<cfset APPLICATION.urls.cms.tagLib = "http://" & CGI.server_name & ":" & CGI.server_port & "/admin/tagLib/">
<cfset APPLICATION.RowsPerPage = 10>
<!--- Property URLS --->
<cfset APPLICATION.urls.property.pdf = APPLICATION.urls.root & "pdf/">
<cfset APPLICATION.urls.property.image = APPLICATION.urls.root & "images/property/">
<cfset APPLICATION.urls.property.large = APPLICATION.urls.root & "images/property/large/">
<cfset APPLICATION.urls.property.thumb = APPLICATION.urls.root & "images/property/thumbs/">
<cfset APPLICATION.urls.property.cmsThumb = APPLICATION.urls.root & "images/property/thumbs/cms/">
<cfset APPLICATION.urls.news.image = APPLICATION.urls.root & "images/news/">
<cfset APPLICATION.urls.articles.image = APPLICATION.urls.root & "images/articles/">
<cflock scope="Application" timeout="5" type="Exclusive">
<cfscript>
/* Commonly used objects and queries */
// DAOs
APPLICATION.propertyDAO = CreateObject("component", "cfcs.dataobjects.propertyDAO").init(APPLICATION.dsn);
APPLICATION.propertyImageDAO = CreateObject("component", "cfcs.dataobjects.property_imageDAO").init(APPLICATION.dsn);
APPLICATION.propertyToPropertyImageDAO = CreateObject("component", "cfcs.dataobjects.property_to_property_imageDAO").init(APPLICATION.dsn);
APPLICATION.propertyToPropertyLocationDAO = CreateObject("component", "cfcs.dataobjects.property_to_property_locationDAO").init(APPLICATION.dsn);
APPLICATION.propertyToPropertyTypeDAO = CreateObject("component", "cfcs.dataobjects.property_to_property_typeDAO").init(APPLICATION.dsn);
APPLICATION.propertyToPropertyTenureDAO = CreateObject("component", "cfcs.dataobjects.property_to_property_tenureDAO").init(APPLICATION.dsn);
APPLICATION.propertyGroupDAO = CreateObject("component", "cfcs.dataobjects.property_groupDAO").init(APPLICATION.dsn);
// Gateways
APPLICATION.propertyGateway = CreateObject("component", "cfcs.dataobjects.propertyGateway").init(APPLICATION.dsn);
APPLICATION.propertyImageGateway = CreateObject("component", "cfcs.dataobjects.property_imageGateway").init(APPLICATION.dsn);
APPLICATION.propertyToPropertyImageGateway = CreateObject("component", "cfcs.dataobjects.property_to_property_imageGateway").init(APPLICATION.dsn);
APPLICATION.propertyLocationGateway = CreateObject("component", "cfcs.dataobjects.property_locationGateway").init(APPLICATION.dsn);
APPLICATION.propertyImageGateway = CreateObject("component", "cfcs.dataobjects.property_imageGateway").init(APPLICATION.dsn);
APPLICATION.propertyTypeGateway = CreateObject("component", "cfcs.dataobjects.property_typeGateway").init(APPLICATION.dsn);
APPLICATION.propertyToPropertyTypeGateway = CreateObject("component", "cfcs.dataobjects.property_typeGateway").init(APPLICATION.dsn);
APPLICATION.propertyTenureGateway = CreateObject("component", "cfcs.dataobjects.property_tenureGateway").init(APPLICATION.dsn);
APPLICATION.propertyToPropertyTenureGateway = CreateObject("component", "cfcs.dataobjects.property_to_property_tenureGateway").init(APPLICATION.dsn);
APPLICATION.partnerGateway = CreateObject("component", "cfcs.dataobjects.partnerGateway").init(APPLICATION.dsn);
// Business Objects
APPLICATION.propertyBO = CreateObject("component", "cfcs.businessobjects.propertyBO").init(APPLICATION.dsn);
// Common queries
APPLICATION.qPartners = APPLICATION.partnerGateway.getAllRecords();
APPLICATION.qPropertyTypes = APPLICATION.propertyTypeGateway.getAllRecords();
APPLICATION.qPropertyTenures = APPLICATION.propertyTenureGateway.getAllRecords();
APPLICATION.qPropertyMinMaxSize = APPLICATION.propertyGateway.getMinMaxSize();
APPLICATION.qPropertyLocations = APPLICATION.propertyLocationGateway.getAllRecords();
</cfscript>
</cflock>
</cffunction>
<cffunction name="onSessionStart" returntype="void">
<cflock scope="Session" timeout="5" type="Exclusive">
<cfscript>
SESSION.propertySearchCriteria = CreateObject("component", "cfcs.beans.property_search_criteria").init();
SESSION.propertySearch = CreateObject("component", "cfcs.beans.property_search").init(SESSION.propertySearchCriteria);
</cfscript>
</cflock>
</cffunction>
Why do you change your application name based on the environment it's in? That makes no sense. The application is still the same app, irrespective of whether it's in prod, or you're staging it, or developing it.
Equally, same with the source code. You've got your dev site within your live website? How does this make any sense?
I dunno what's going on with your application variables, but they're bound to the application name (this.name), not where the Application.cfc is in the directory structure.
I suspect your zero applicationTimeout is not helping matters much here. This basically means your application never actually persists... it'll be timing out every request. This makes no sense either.
You do not need to <cflock> that code in onApplicationStart(). ColdFusion will only allow one request to run onApplicationStart(): all other requests will be queued until it's completed, by which time the application has started, so the queued requests won't even attempt to run it.
I think your problem is most likely causes by your poor application framework design, and this weird thing you're doing having dev and prod in the same source code tree (which defies all logic).
Sort your source code out, and the problem won't arise.
You can use conditional logic to set your application name.
if ( YouAreHappy and YouKnowIt ) {
This.name = "ClapYourHands";
}
else {
This.name = "StompYourFeet";
}
In real life, your if condition would be something that differented the live and test environments. cgi.http_host is a good candidate.

Consuming a webservice code simplification

UPDATED CODE TO LATEST ITERATION
The following function consumes a webservice that returns address details based on zip code (CEP). I'm using this function to parse the xml and populate an empty query with the address details. I would like to know if there is a more elegant way to achieve the same result. It seems to be a waste to create an empty query and populate it...
Any ideas could my method be modified or the code factored/simplified?
<!--- ****** ACTION: getAddress (consumes web-service to retrieve address details) --->
<cffunction name="getAddress" access="remote" returntype="any" output="false">
<!--- Defaults: strcep (cep (Brazilian zip-code) string webservice would look for), search result returned from webservice --->
<cfargument name="cep" type="string" default="00000000">
<cfset var searchResult = "">
<cfset var nodes = "">
<cfset var cfhttp = "">
<cfset var stateid = 0>
<cfset var tmp = structNew()>
<!--- Validate cep string --->
<cfif IsNumeric(arguments.cep) AND Len(arguments.cep) EQ 8>
<cftry>
<!--- Consume webservice --->
<cfhttp method="get" url="http://www.bronzebusiness.com.br/webservices/wscep.asmx/cep?strcep=#arguments.cep#"></cfhttp>
<cfset searchResult = xmlparse(cfhttp.FileContent)>
<cfset nodes = xmlSearch(searchResult, "//tbCEP")>
<!--- If result insert address data into session struct --->
<cfif arrayLen(nodes)>
<cfset tmp.streetType = nodes[1].logradouro.XmlText>
<cfset tmp.streetName = nodes[1].nome.XmlText>
<cfset tmp.area = nodes[1].bairro.XmlText>
<cfset tmp.city = nodes[1].cidade.XmlText>
<cfset tmp.state = nodes[1].uf.XmlText>
<cfset tmp.cep = arguments.cep>
<!--- Get state id and add to struct --->
<cfset stateid = model("state").findOneByStateInitials(tmp.state)>
<cfset tmp.stateid = stateid.id>
<cfreturn tmp>
</cfif>
<!--- Display error if any --->
<cfcatch type="any">
<cfoutput>
<h3>Sorry, but there was an error.</h3>
<p>#cfcatch.message#</p>
</cfoutput>
</cfcatch>
</cftry>
</cfif>
</cffunction>
<!--- ****** END ACTION getAddress --->
The calling code:
<!--- Get address data based on CEP --->
<cfset session.addressData = getAddress(cep=params.newMember.cep)>
I can't test this because I don't have an example XML file / CEP to test with, but here is a minor rewrite that addresses four things:
Instead of using cfparam and some strange "params" structure, you should pass the CEP into the function as an argument.
The function shouldn't directly modify session data. Instead, you should return the result and let the calling code assign it to the session (or wherever else it might be needed). I'll show this in a 2nd code example.
Cache the xml result per CEP -- assuming this doesn't change often. (You'll have to improve it further if you want time-based manual cache invalidation, but I can help add that if necessary)
Don't use StructInsert. It's not necessary and you're just writing it the long way for the sake of writing it the long way. There is no benefit.
Again, this isn't tested, but hopefully it's helpful:
<cffunction name="getAddress" access="remote" returntype="any" output="false">
<cfargument name="cep" type="string" default="00000000" /><!--- (cep (Brazilian zip-code) string webservice would look for) --->
<cfset var searchResult = "">
<cfset var nodes = "">
<cfset var cfhttp = "">
<cfset var stateid = 0 />
<cfset var tmp = structNew()>
<!--- Validate cep string --->
<cfif IsNumeric(arguments.cep) AND Len(arguments.cep) EQ 8>
<cfif not structKeyExists(application.cepCache, arguments.cep)><!--- or cache is expired: you'd have to figure this part out --->
<!--- Consume webservice --->
<cftry>
<cfhttp method="get" url="http://www.bronzebusiness.com.br/webservices/wscep.asmx/cep?strcep=#arguments.cep#" />
<cfset searchResult = xmlparse(cfhttp.FileContent)>
<cfset nodes = xmlSearch(searchResult, "//tbCEP")>
<!--- If result insert address data into session struct --->
<cfif arrayLen(nodes)>
<cfset tmp.streetType = nodes[1].logradouro.XmlText />
<cfset tmp.streetName = nodes[1].nome.XmlText />
<cfset tmp.area = nodes[1].bairro.XmlText />
<cfset tmp.city = nodes[1].cidade.XmlText />
<cfset tmp.state = nodes[1].uf.XmlText />
<cfset tmp.cep = arguments.cep />
<!--- Get state id and add to struct --->
<cfset stateid = model("state").findOneByStateInitials(session.addressData.state)>
<cfset tmp.stateid = stateid.id />
</cfif>
<cfreturn duplicate(tmp) />
<!--- Display error if any --->
<cfcatch type="any">
<h3>Sorry, but there was an error.</h3>
<p>#cfcatch.message#</p>
</cfcatch>
</cftry>
<cfelse>
<!--- cache exists and is not expired, so use it --->
<cfreturn duplicate(application.cepCache[arguments.cep]) />
</cfif>
</cfif>
<!---
<!--- Redirect to page two of the sign up process --->
<cfset redirectTo(controller="assine", action="perfil")>
--->
</cffunction>
Notice that I commented out the redirect you had at the end. That's because with my function, you'll be returning a value, and the redirect should be done after that, like so:
<cfset session.addressData = getAddress("some-CEP-value") />
<cfset redirectTo(controller="assine", action="perfil")>
If you're going to leave out the caching (As you say in a comment you will), then here is a version that makes no attempt at caching:
<cffunction name="getAddress" access="remote" returntype="any" output="false">
<cfargument name="cep" type="string" default="00000000" /><!--- (cep (Brazilian zip-code) string webservice would look for) --->
<cfset var searchResult = "">
<cfset var nodes = "">
<cfset var cfhttp = "">
<cfset var stateid = 0 />
<cfset var tmp = structNew()>
<!--- Validate cep string --->
<cfif IsNumeric(arguments.cep) AND Len(arguments.cep) EQ 8>
<!--- Consume webservice --->
<cftry>
<cfhttp method="get" url="http://www.bronzebusiness.com.br/webservices/wscep.asmx/cep?strcep=#arguments.cep#" />
<cfset searchResult = xmlparse(cfhttp.FileContent)>
<cfset nodes = xmlSearch(searchResult, "//tbCEP")>
<!--- If result insert address data into session struct --->
<cfif arrayLen(nodes)>
<cfset tmp.streetType = nodes[1].logradouro.XmlText />
<cfset tmp.streetName = nodes[1].nome.XmlText />
<cfset tmp.area = nodes[1].bairro.XmlText />
<cfset tmp.city = nodes[1].cidade.XmlText />
<cfset tmp.state = nodes[1].uf.XmlText />
<cfset tmp.cep = arguments.cep />
<!--- Get state id and add to struct --->
<cfset stateid = model("state").findOneByStateInitials(session.addressData.state)>
<cfset tmp.stateid = stateid.id />
</cfif>
<cfreturn duplicate(tmp) />
<!--- Display error if any --->
<cfcatch type="any">
<h3>Sorry, but there was an error.</h3>
<p>#cfcatch.message#</p>
</cfcatch>
</cftry>
</cfif>
<!---
<!--- Redirect to page two of the sign up process --->
<cfset redirectTo(controller="assine", action="perfil")>
--->
</cffunction>
Note that I did leave in the use of duplicate(). What this does is return a duplicate of the object (in this case, the struct). This is much more important when you start to work on applications where you're passing complex values into and out of functions over and over again. Using duplicate() causes things to be passed by value instead of by reference. It may not bite you in this case, but it's a good habit to get into.
I would also still use the function argument and return a value -- but it's arguable that this is my personal preference. In a way it is. I believe that a function should be fully encapsulated; a total "black box". You give it some input and it gives you back some output. It should not modify anything outside of itself. (Again, just my opinion.)
So assuming you're using this function as part of a larger multi-step process, you should still use it the same way I've described above. The only difference is that you're setting the session variable outside of the function body. Just as previously:
<cfset session.addressData = getAddress("some-CEP-value") />
<cfset redirectTo(controller="assine", action="perfil")>
That looks pretty straightforward. CF doesn't (yet?) have any magical XML-to-Query functions, but that would be pretty cool. If you wanted, you could probably write up an XSL transform to go from XML to WDDX so that you could use the cfwddx tag ... but that's probably putting the cart before the horse.
You need to move your arrayLen() if block into the try block. As it stands, if the cfhttp tag throws an error, the nodes variable will be a string and not an array, thus causing the arrayLen() to throw another error.
Minor nitpick: I wouldn't add a row to the query until inside the arrayLen() block. That way, the calling code can check recordCount to see if the result was a success.
Beyond that ... that's pretty much how it's done.