Support for server side tags and functions in CF Mobile development

September 17, 2019
Guide 12 posts
Followers: 0 people
0

Support for server side tags and functions in CF Mobile development

Guide 12 posts
Followers: 0 people
September 17, 2019

In this article, we will explain the recent changes made in CF Mobile, which helps in developing end to end mobile applications.

What was supported for Mobile prior to ColdFusion 2018

CF Mobile was introduced in ColdFusion 2011, which provided a server and a development infrastructure to facilitate rapid and robust mobile application development, debugging, packaging, and deployment.

Here is a sample code:

<cfclientsettings enableDeviceAPI=false>

<div id=”result”/>

<cfclient>

<cfset document.getElementById(‘result’).innerHTML=document.getElementById(‘result’).innerHTML+”Current time is : ” & #now()#>

</cfclient>

Wrap the sample code within the cfclient tag. All code present inside this tag will be converted to JavaScript code, which can be ported to mobile devices. The code mixes JavaScript code and ColdFusion’s code (now function).

We can also write code to invoke device specific functionalities like clicking pictures, taking videos, storing files in devices, etc.

function takePictureAndUpload()

{

var imageUrl = cfclient.camera.getPicture();

if (!isDefined(“imageUrl”) || imageUrl.length == 0)

return;

displayMessage(“Temporary image – ” + imageUrl + “<br><hr>”, false);

//This file is in a temporary file system. Move it to persistent file system

var newFilePath = copyFileFromTempToPersistentFileSystem(imageUrl);

//Now upload file to the server

uploadFileToServer(newFilePath);

//Delete temporary file

cfclient.file.remove(imageUrl);

displayMessage(“Deleted temporaty file ” + imageUrl + “<br><hr>”);

}

The above code takes a picture using the mobile’s camera and uploads the image to the server.

For more information about mobile development in ColdFusion, see this Devnet article.

What was missing in CF Mobile

Though mobile development enabled you to build mobile apps at rapid pace and with minimum number of lines, it still lacked few features

  1. Lack of support for server-side tags/functions

 

Only those tags and functions were supported whose corresponding JavaScript could have been generated. In a <cfmail> tag, we cannot generate any corresponding JavaScript code for it. Similarly, most of such tags were not supported, for example, cfschedule, cfcache, cfpdf, cfddocument, cfindex, cfsearch, etc. ColdFusion functions like imageread, spreadsheetread, fileread etc were also not supported.

 

  1. Incomplete implementation leads to confusion

 

Imagine a situation, where you, as a developer, write some code and after compilation you find that half of the tags/functions that you wrote are not even supported. You had to go through the entire documentation to understand what is supported and what is not

How ColdFusion (2018 release) solves this problem

We solved the problems mentioned above by adding support for most of the server-side tags as well as functions. Now while writing mobile development code in ColdFusion, you need not worry about the supported tags/functions as most of them would be supported out of the box without you being asked to do anything.

Internally, we convert all your server-side tags and functions to Ajax API calls, which make calls to some ColdFusion server, and get the code executed remotely.

Specifying your mobile server

As we convert all server-side functionalities to API calls, we need to specify the ColdFusion server where these calls need to be made. The server-side code cannot be executed within the mobile device as we would be required to embed the ColdFusion server within the mobile, which would compile such code and execute it.

It is recommended to keep the server remote and separate, generate API calls within the mobile apps, and get the work done(executing server side tags) remotely.

Here is how we need to specify the mobile server:

<cfclientsettings  mobileserver=’http://localhost:8500′ enabledeviceapi=”false”>

 

We have added a new attribute for cfclientsettings tag, mobileserver. You need to specify the address of the ColdFusion server, which will host the API calls made by the mobile application being developed.

Code to write server-side tags/functions within cfclient

Well, you need not to write some special code/attributes/tags for it to work within the cfclient tags. We need to write the same code which we would have written for normal web development. We will show you some sample codes for interacting with server side tags

CF Tag Code for Caching

<cfclientsettings  mobileserver=’http://localhost:8500′ enabledeviceapi=”false”>

<cfclient>

<cfset value = ‘delhi’>

<cfset key = ‘india’>

<cfoutput>Putting an element<B>(key : #key#, value : #value.z#)</B> into the cache ….. <br><bR>

<cfcache action=”put” id=”#key#” value=”#value#” >

<cfcache action=’get’ id=’#key#’  name=’nn’  metadata=’mm’ >

Retrieving element from cache for key <B>#key# </B>: <br></cfoutput>

<i><cfoutput>#nn#</cfoutput></i><BR><BR>

</cfclient>

As we can see the code is the same that you would write for a normal caching logic.

CFScript code for caching

Both ColdFusion tags as well as ColdFusion functions are supported.

<cfclientsettings  mobileserver=’http://localhost:8500′ enabledeviceapi=”false”>

<cfclient >

<cfscript>

myObj={};

myObj.a=”hello”;

myObj.b=”world”;

x = createTimespan(0,0,30,0);

y = createTimespan(0,0,15,0);

cacheput(“id_1″,myObj,1,1,”aRegion”);

val = cacheget(“id_1″,”aRegion”);

writeOutput(“Element retrieved : ”  & val.a  & “<br>”);

n = cachegetengineproperties();

writeOutput(‘Engine name : ‘ & n.name & ‘<br>’);

ids = cachegetallids(‘aRegion’);

writeOutput(‘ Ids : ‘ & ids& ‘<br>’);

cacheremove(arrayToList(ids),true,’aRegion’);

ids = cachegetallids(‘aRegion’);

writeOutput(‘ Ids after removing : ‘ & ids & ‘<br><hr>’);

</cfscript>

</cfclient>

Code for PDF manipulation

<cfclientsettings  mobileserver=’http://localhost:8500′ enabledeviceapi=”false” >

<cfclient>

<cfpdf action = “extractimage” source = “C:Depotcf_maincfusionwwwrootmobileserverpdf.pdf” pages = “1-200” destination = “C:Depotcf_maincfusionwwwrootmobileservermybookimages” imageprefix = “mybook” overwrite=true>

</cfclient>

Code for scheduling

<cfclientsettings  mobileserver=’http://see-wv-a196:8500′ enabledeviceapi=”false” >

<cfclient>

<CFSET action = ‘list’>

<cfschedule action=’#action#’ result=’r’>

<cfloop query=”r”>

<cfoutput>#GROUP#</cfoutput>

</cfloop>

</cfclient>

Code for HTTP

<cfclientsettings  mobileserver=’http://localhost:8500′ enabledeviceapi=”false”>

<cfclient>

<cfhttp url=’http://localhost:8500/mobile/server/dummy.txt’  method=”get”   result=’n’ charset=”utf-8″> </cfhttp>

<cfoutput>#CFHTTP.Statuscode#</cfoutput>

</cfclient>

Code for spreadsheet

<cfclientsettings  mobileserver=’http://localhost:8500′ enabledeviceapi=”false”>

<cfclient>

<cfquery

name=”centers” datasource=”MYSQL2″

type=”server”>

SELECT * FROM bank

</cfquery>

<cfscript>

theDir=’c:/Downloads/’;

theFile=theDir & “courses.xls”;

thesecondsheet = SpreadsheetNew();

</cfscript>

<cfset row = [‘D’,’DDD’]>

<cfscript>

SpreadsheetAddRows(thesecondsheet,row);

</cfscript>

<cfspreadsheet action=”write” filename=”#theFile#” name=”thesecondsheet”

sheetname=”centers” overwrite=true>

</cfclient>

Code to Mail

<cfclientsettings  mobileserver=’http://localhost:8500′ enabledeviceapi=”false”>

<cfclient>

<cfmail to=”joe@work.com” from=”bob@work.com”

subject=”Sending a mail with the encryption algo

This message is encrypted using the algorithm” >

<cfmailparam name = “Importance” value = “High”>

Please review the new logo. Tell us what you think.

<cfmailparam name=”Disposition-Notification-To” value=”peter@domain.com”>

</cfmail>

</cfclient>

 

Code for LDAP

<cfclientsettings  mobileserver=’http://localhost:8500′ enabledeviceapi=”false”>

<cfclient>

<cfldap action=”modifyDN” attributes=”cn=Accounting Officers” dn=”cn=QA Managers,ou=Groups,dc=example,

dc=com” server=”localhost” port=”10389″ username=”uid=admin,ou=system” password

=”Password@123″ >

</cfclient>

Code for FTP

<cfclientsettings  mobileserver=’http://localhost:8500′ enabledeviceapi=”false”>

<cfclient>

<cfftp action = “open”

username = “anonymous”

connection = “My_query”

password = “youremail@email.com”

server = “ftp.tucows.com”

stopOnError = “Yes”>

<p>Did it succeed? <cfoutput>#cfftp.succeeded#</cfoutput>

</cfclient>

How to use CFQuery

CFQuery is the only tag that needs special handling within the cfclient tag. CFQuery was already supported in the previous ColdFusion mobile version and it also used to interact with the browser database.

<cfclientsettings  enabledeviceapi=”false”>

<cfclient>

<cfquery name=”q1″ datasource = “clientdb”>

select * from birthdates

</cfquery>

<cfoutput>#serializeJSON(q1)#</cfoutput>

<cfoutput>Expected: Database Data</cfoutput>

</cfclient>

The above code ran earlier as well. It fetches data from the database present within the browser.

Now if we want the above code to execute at server side instead of client’s browser, we need to make use of the new Cfquery attribute, type.

<cfclientsettings  enabledeviceapi=”false” mobileserver=’http://your-server:port-number/‘>

<cfclient>

<cfquery name=”q1″ type=”server” datasource = “cfartgallery”>

select * from art

</cfquery>

<cfoutput>#serializeJSON(q1)#</cfoutput>

<cfoutput>Expected: Database Data</cfoutput>

</cfclient>

The above cfquery code gets executed at the ColdFusion server and fetches data from the database present at the server.

Server-side validations

For example, you are using the <Cfmail> tag, which takes an email as input from the user. You will put client-side validations, which will validate the email entered by the user. But a hacker can bypass such client-side validations and therefore you must have validations at server side as well. But with the current design, developers had no control over server side and cannot perform such validations.

We have solved this issue by providing a hook, which developers can implement, and they can provide their validations in it.

Implement this method:

component

{

public boolean function validateparameters(Struct context){

writedump(var=context,output=’console’);

return true;

}

}

Copy the helper cfc in the server. Name it appropriately. In a cfm, specify the path as follows:

<cfclientsettings  mobileserver=’http://localhost:8500′ enabledeviceapi=”false” validationcfc=’path for cfc’>

 

validateParameters: When the return value is false, the server call does not get executed. It returns an error that says parameter validation failed. This method in the hook provided by you would be invoked before executing any serverside tag or function.

 

Secure your calls

 

By default, your ColdFusion 2018 servers are enabled to serve mobile calls. You must enable it via CF admin. You can find the options in the Settings page. If you enable this option, the mobile apps can make calls to the server. Restart the server after enabling the option.

Also, you must enable CORS via admin so that cross platform API calls can be served from your server.

After enabling the server, you must ensure that only the authorized apps make call to your server. You can generate a mobile secret key from the Settings page of CF admin. This is the secret key that you must use in your mobile application. Auto-generate the key or paste an already generated key.

Specify this secret key in the application cfc of the mobile app as shown below:

this.mobilesecretkey = ‘14404385-fe25-461f-8379-e178739d96f7’;

This secret key will be appended to each API call which your mobile app will make. If this key does not match the key present at the server, request will not be served

Server side tags supported

 

Apart from the tags which were supported earlier following server side tags have also been added to the support list.

“cfexchangeconversation”, “cfexchangecalendar”, “cfexchangemail”, “cfexchangetask”,

“cfexchangecontact”, “cfexchangefilter”, “cfexchange”, “cfexchangeconnection”, “cfexchangefolder”,

“cfsharepoint”, “cfpop”, “cfldap”, “cfftp”, “cffeed”, “cflog”, “cflocation”, “cfprint”,

“cfwddx”, “cfxml”, “cfreport”, “cfdirectory”, “cfdocument”, “cfdocumentitem”,

“cfdocumentsection”, “cffile”, “cfpresenter”, “cfpresentation”, “cfpresentationslide”, “cfform”,

“cfslider”, “cfcontent”, “cfschedule”, “cfcache”, “cfhttp”, “cfimage”, “cfmail”, “cfhtmltopdf”,

“cfspreadsheet”, “cfzip”, “cfcollection”, “cfsearch”, “cfindex”, “cfpdf”, “cfchart”, “cfdbinfo”, “cfimap”,

“cfimapfilter”, “cfobjectcache”, “cfstoredproc”, “cfprocparam”, “cfprocresult”, “cftransaction”,

“cfpdfform”, “cfpdfformparam”, “cfpdfsubform”, “cflogin”, “cfloginuser”, “cflogout”

 

 

Conclusion

ColdFusion 2018 completes the mobile story that had started in CF11. Using ColdFusion’s mobile development framework, developers can develop mobile apps quickly. Developers need not to learn JavaScript (for PhoneGap), Java (for Android apps) or C# (for iOS apps).

Comments (0)
Add your comment