Skip to sending parameter in ColdFusion REST Service - web-services

I was writing a REST component but having difficulties when I do not want to pass argument for a parameter. My code and URL (PATH style) look like this.
Component definition:
<cfcomponent rest="true" restPath="SearchRestAPI">
<cffunction name="restaurantResults" access="remote" httpMethod="get"
output="false" returntype="Query" produces="application/json"
restPath="Search/{city : ([a-zA-Z])*}/{address}/{type}">
<cfargument name="city" default="" type="string" restargsource="path">
<cfargument name="address" default="" type="string" restargsource="path">
<cfargument name="type" default="" type="string" restargsource="path">
Calling URL/Path to consume the REST service. Notice I do not want to send any value for type parameter so there is double slash at the end of the path:
http://localhost:8502/backend/section/SearchResAPI/Search/Calcutta/Lindsay Street//
But this is throwing a "Not Found" error. Any suggestion would be highly appreciated.

I am not sure about this ColdFusion functionality, but looks like "restPath" works with regular expressions (which you are already using when capturing the city parameter
([a-zA-Z])* // your city parameter matches the specified character sets zero or more times.
Similarly to the above, you could try:
restPath="Search/{city : ([a-zA-Z])*}/{address}/{type: ([a-zA-Z])*}"
Here is a ColdFusion page that has some examples as well (search the page for "restPath"):
https://wikidocs.adobe.com/wiki/display/coldfusionen/cfcomponent

Related

String concatenation issue with Railo4

I'm working on an application built on Railo4 and am running into an interesting issue. I'm not doing anything new here as far as ColdFusion code goes. simply taking some strings, concatenating where needed, and returning a string.
<cffunction name="customBuildURL" access="public" returntype="string">
<cfargument name="subsystem" type="string" required="true" />
<cfargument name="section" type="string" required="true" />
<cfargument name="item" type="string" required="true" />
<cfargument name="args" type="string" required="true" />
<cfset var url = "index.cfm?action=" & ARGUMENTS.subsystem & ":" & ARGUMENTS.section />
<cfif Ucase(ARGUMENTS.item) NEQ "DEFAULT" >
<cfset url &= "." & ARGUMENTS.item />
</cfif>
<cfif ARGUMENTS.args NEQ "" >
<cfset url &= ARGUMENTS.args />
</cfif>
<cfreturn url />
</cffunction>
However, I'm getting two unusual errors.
1) The first is: Can't cast Complex Object Type Struct to String and is being reported for the following two lines:
<cfset url &= "." & ARGUMENTS.item />
<cfset url &= ARGUMENTS.args />
2) The second is the function customBuildURL has an invalid return value , can't cast Object type [url] to a value of type [string] upon return of the url variable.
As you can see, I'm not doing anything elaborate here. Just setting some strings, concatenating them and then returning it. I don't see where an 'Object' is being created and being cast as a string. I double checked the use of the &= operator and that doesn't appear to be the issue because if I do a url = url & "." & ARGUMENTS.item the same error is reported.
Any ideas?
Sly,
Railo does not allow you to use ANY scope as a variable inside functions. This is an intentional incompatibility, since Coldfusion does allow that. But after doing that, you will not be able to access the URL scope anymore. That's why we don't allow that.
Just call the variable sUrl for instance.
HTH
Gert Franz
Railo ltd.
Url is a reserved word in ColdFusion, so even though you're var-ing it in the function it's still picking up the actual structure of url variables.
Here's a complete list of reserved words in ColdFusion

How to pass a struct to Coldfusion CFC using CFINVOKE?

I have a CFC file which handles all of the emails I'm sending form an application (using Coldfusion8).
I was using CFINVOKE to call the respective function inside this CFC and passed a struct with all user data along like so:
<cfscript>
var User.data = {};
User.data.name = "John Doe";
User.data.email = "john#doe.com";
...
</cfscript>
// call mailer
<cfinvoke component="mailer_user" method="say_hi">
<cfinvokeargument name="userData" value="#User.data#">
</cfinvoke>
And inside my mailer.cfc
<cffunction name="say_hi" access="public" output="false">
<cfargument name="userData" type="struct" required="true" />
....
For some reason this now fails and I can only get it to work if I pass fields seperately as cfargument, which is a a pain, since I'm passing a lot of data.
Question:
How can I get this to work using argumentCollection.
Even if I CFINVOKE like this:
<cfinvoke component="mailer_user" argumentcollection="#User.data#" method="say_hi"></cfinvoke>
it still doesn't do a thing. I'm setting output flags right before the cfinvoke and after, as well as inside the "say_hi" function going in and out. I'm only getting the flag before CFINVOKE.
Side note: This is all done through AJAX and I'm only getting back success="false" if my CFC has an error somewhere. I only work remotely on the system, so I can't set AJAX debugging in CFADMIN
As I typed the comment above it occurred to me what the problem is likely to be.
You are passing in a structure to your function. You pass User.data which has name,email,blah,etc as keys in that structure. Those keys need to match the arguments in your function
<cffunction name="say_hi" access="public" output="false">
<cfargument name="name" type="struct" required="true" />
<cfargument name="email" type="struct" required="true" />
<cfargument name="blah" type="struct" required="true" />
<cfargument name="etc" type="struct" required="true" />
If you want to pass in the structure as a argument, you would need to have a user.userData as your structure of user data and your function should be
<cffunction name="say_hi" access="public" output="false">
<cfargument name="userData" type="struct" required="true" />
When you pass the collection as argumentCollection you should do argumentCollection="#user#", so that the userData part matches your cfargument in the function.
Clear as mud?
I think you should stay in cfscript style by writing
// call mailer
mailUser = createObject("component", "mailer_user"); // or new mailer_user(); for CF9+
mailUser.say_hi(User.data);
That should work, if it doesn't, it's somewhere else in your code. Try looking at the error log.
You should map the variable to the data you pass, then no problem sending a struct. Do it this way
<cfset objMailer = createObject("component","mailer_user") />
<cfset objMailer.say_hi(userData:user.data)/>
This works even in CF7.
Ok. There was a typo inside my mailer CFC, where I had a variable with "##". As is was inside my email text
it went unnoticed...
So you can pass a struct allright using this:
<cfinvoke component="mailer_user" method="say_hi">
<cfinvokeargument name="userData" value="#User.userdata#">
</cfinvoke>
and grab it inside your called function like so:
<cffunction name="say_hi" access="public" output="false" hint="">
<cfargument name="userData" type="struct" required="true" hint="user data passed" />
<cfscript>
var internalInfo = "";
var User = {};
User.userdata = userData;
</cfscript>
...
Maybe someone else can use the snippet.

Accessing the Mura Component Bean from a Plugin

I am working on a Mura plugin that uses a mura tag to pull in a component and use it on the page. Essentially my plugin needs to call $.dspObject('component',Arguments.componentid). Since the mura scope isn't available within the plugin method then I am guessing that I need to pull in a component bean, but I have no idea how to do that.
<cffunction name="showPlayer" access="public" output="false" returntype="String">
<cfargument name="component" type="string" required="true" hint="The ID of the component that contains the playlist." />
<!--- Create the body content --->
<cfsavecontent variable="bodycode">
<cfoutput>#$.dspObject('component',Arguments.component)#</cfoutput>
</cfsavecontent>
</cffunction>
That is a very stripped down version of the method (the full eventHandler can be found on GitHub.
So how would I rewrite my code to pull the component bean out (or whatever I need to do for it)?
There are several ways possible:
<cfargument name="$">
OR
var $=application.ServiceFactory.getBean(“muraScope”);
OR
var $=getServiceFactory().getBean(“muraScope”);
OR
var $=getBean(“muraScope”);
See MuraCMS Programmers Guide and cfobject.cfc

Script function for file upload in ColdFusion 9

Is there a a cfscript equivalent for cffile action="upload" in ColdFusion 9? Looking through the docs, there doesn't seem to be.
[Update] This was added in the 9.0.1 update
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSd160b5fdf5100e8f36f73035129d9e70a92-8000.html
You can easily abstract it with a user defined function.
<cffunction name="fileuploader">
<cfargument name="formfield" required="yes" hint="form field that contains the uploaded file">
<cfargument name="dest" required="yes" hint="folder to save file. relative to web root">
<cfargument name="conflict" required="no" type="string" default="MakeUnique">
<cfargument name="mimeTypesList" required="no" type="string" hint="mime types allowed to be uploaded" default="image/jpg,image/jpeg,image/gif,image/png,application/pdf,application/excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.presentation,application/vnd.ms-excel,image/pjpeg">
<cffile action="upload" fileField="#arguments.formField#" destination="#arguments.dest#" accept="#arguments.mimeTypesList#" nameConflict="#arguments.conflict#">
<cfreturn cffile>
</cffunction>
And then use it in cfscript:
<cfscript>
// NOTE: because of how cffile works, put the form field with the file in quotes.
result = fileUploader("FORM.myfield", myDestPath);
WriteOutput(result.fileWasSaved);
</cfscript>
Note: I would be very careful how you rename this function in case Adobe does include this functionality down the road.
Not sure when this was added but CF does support file uploads in CFSCRIPT. I have been using FileUpload() for a little while. I have checked it is not a function in my MVC framework and def seems to be something unique to CF 9.01.
However, Builder 2 does not seem to like it nor can I find reference on CF 9 Docs but it does works and it is part the lasted Adobe ColdFusion 9.01, Ralio I have not checked tho
examples used:
fileUpload(getTempDirectory(),"ImageFile","","makeUnique");
Nope, but it has been requested.

Testing for existence of FORM scope / struct in ColdFusion

Problem: When requesting the WSDL for a CFC, I get the following error: Variable FORM is undefined. It happens in this line of code, in the OnRequestStart method in application.cfc
<cfif structKeyExists(form,'resetappvars')>
<cfset OnApplicationStart() />
</cfif>
If I request a specific method, it works fine. I have considered using cfparam to create a default form struct if none exists, but that seems like an ugly hack and I worry it will actually create the form struct in the variables or this scope of the CFC. Maybe this is a legitimate bug as well?
Note: This only happens when I request the WSDL, if I invoke a method directly - the code executes as expected without problems.
Update: Application.cfc code sample - just add any CFC to your app and request it with ?wsdl to see the issue. This has been tested (and failed) on ColdFusion 7 and ColdFusion 8.
<cfcomponent output="false">
<cffunction name="OnApplicationStart" access="public" returntype="boolean" output="false" hint="Fires when the application is first created.">
<cfset application.dsn = "my_dsn" />
<cfreturn true />
</cffunction>
<cffunction name="OnRequestStart" access="public" returntype="boolean" output="false" hint="Fires at first part of page processing.">
<cfargument name="TargetPage" type="string" required="true" />
<cfif structKeyExists(form,'resetappvars')>
<cfset OnApplicationStart() />
</cfif>
<cfreturn true />
</cffunction>
</cfcomponent>
Maybe try adding a:
<cfif IsDefined("form")>...</cfif>
around the above code?
You could also cfparam the variable you're looking for then just change your logic a little (assuming resetAppVars is a boolean:
<cfparam name="form.resetAppVars" default="false" />
...
<cfif form.resetAppVars>
<cfset OnApplicationStart() />
</cfif>
Edit: I'm not sure if the above code could be considered a hack, but it seems pretty standard CF, to me.
This post of Ben Nadel gives detailed list of scopes available for different types of requests.
By reading it you can easily find out that form scope is not available in given context, but url is.
I've heard it's just a matter of opinion, but it seems to me that it is improper to reference your form scope within a CFC, as there is no guarantee that the form scope will be available when your cfc is invoked and when your method is called. It is better to ensure that any data that needs to be available to the method is provided explicitly to your object. This can be done either by including an argument:
<cfargument name="resetAppVars" type="boolean" required="false" default="false" />
Then you check arguments.resetAppVars, and it is always defined, but defaulted to false.
Or by creating an attribute on your object and creating an explicit set method:
(at the top of your cfc)
<cfset this.resetAppVars = false />
<cffunction name="setResetAppVars" access="public" returnType="void" output="false">
<cfargument name="flagValue" type="boolean" required="true" />
<cfset this.resetAppVars = arguments.flagValue />
</cffunction>
In which case you will check against this.resetAppVars. You can also scope this locally using <cfset var resetAppVars = false /> as the declaration, which makes it a private attribute of your object, and is probably proper, so code that invokes the object cannot improperly overwrite this variable with a non-boolean type. In that case, you would simply refer directly to resetAppvars in your test, instead of using this scope.
You could also do this:
<cfif NOT isSoapRequest()>...
and stick your remaining logic inside that chunk.