ColdFusion Splendor now supports site-level REST applications and enables pluggable serializer and deserializer.
Site-level REST application support
In ColdFusion 10, multiple applications cannot have the same name, even if the applications are residing in different hosts. With the enhanced ColdFusion Splendor REST feature, you can have multiple applications with the same name but available in different hosts. Also, you can set one default application (containing the REST service) for each virtual host.
You can register the directory containing the REST service by using any one of the following options:
- autoregister Application Setting
- ColdFusion Administrator console
- ColdFusion Admin API
- restInitApplication method
Option 1: autoregister Application Setting
A new Application setting autoregister is introduced in ColdFusion Splendor.
- You can set the auto register to true if you want to enable the auto registration for the application:
- Specifying the servicemapping is optional. If servicemapping is not specified, the "this.name"/application name will be taken as default.
- Specify the usehost or hostname, If usehost attribute is set to true, then the host name is parsed from the URL and it is used:
- Explicitly naming the host name will make the host name. If the host name is not mentioned, then the usehost name will be defaulted.
- Set isDefault to true and the application will be made as default app.
When the first request comes to the application and if that request is non-REST request, the application will be started and registered. If both usehost and host are not specified, the apps will be registered without host name.
Option 2: Registering a REST application using ColdFusion Administrator console
- Use the Administrator console to register a directory containing REST-enabled CF components. Select Adobe ColdFusion Splendor Administrator console > Data & Services > REST services.
- Browse and select the root path (or the application path) where ColdFusion will search for the directory containing a set of REST enabled CF components.
(Optional) In the Service Mapping section, specify the virtual mapping in place of application name. If the folder has an Application.cfc file and an application name, use the application name to identify the REST services in the directory.
(Optional) Enter the Host Name. This will enable the mapping of the selected application with the host. Each host can have a default application and multiple applications with the same application name can be mapped to different hosts.
- Set the REST application as a default application by selecting the option Set as default application. By selecting this option, you omit the need to specify the service mapping or the application name in the URI.
- Click the Add Service button to complete registration of the directory.
- View the Active ColdFusion REST Services display table to view the list of active service mappings. You can use the same to edit or delete the service mappings in future.
Option 3: Registering a REST application using the ColdFusion Admin API
You can use functions defined in CFIDE.adminapi.extensions CFC to manage a REST application. The functions are:
- registerRESTService(path[,serviceMapping[,host[,isdef]]]: This function registers the REST application. The root path specifies the directory containing REST-enabled CF component. Optionally, the service mapping for the REST application, host name, and isdefault can be specified.
- getRESTServices(): This function returns an array of REST services registered with the ColdFusion Administrator.
- deleteRESTService(rootPath): This function deletes the specified REST application registered with the ColdFusion Administrator.
- refreshRESTService(rootPath): If you make any changes to the REST-enabled CF component, you can refresh the registered application by calling this function.
- getDefaultRestService(): Returns the server wide default REST application.
- getAllDefaultRESTServices: Returns all the default REST services. It is an array of path – host pair.
Option 4: Registering a REST application using the restInitApplication method
You can also register a REST application by calling the method restInitApplication
The syntax is:
The options are an optional argument. In the options struct you can pass the:
For registering by specifying the host explicitly:
For registering by specifying the UseHost attribute: The host name will be extracted from the request URL and will be used for registration.
- In the options you can specify host, useHost and isDefault. The option usage is same as autoRegister feature.
- If you have already registered the application using the Administrator module, call restInitApplication to refresh the REST service.
- Use the restDeleteApplication function to delete the REST service. The syntax is restDeleteApplication(rootPath)
Support pluggable serializer and deserializer
In the application.cfc, you can register your own handler for serializing and deserializing the complex types. If the serializer is not specified, ColdFusion uses the default mechanism for serialization.
Example: If you have a phone directory to be serialized on the following parameters:
- The main data structure is an Array.
- Each element of the array is a struct.
- The Struct contains 2 elements.
- The first is the name of the contact and the second is the struct which contains the code and the phone number.
And in this example, you want to serialize only struct in a simple format and want the result as follows:
With the enhanced ColdFusion Splendor REST feature, the user can use the plugged-in custom serializer instead of using the default serialization function. The custom serializer has four functions:
- canSerialize - Returns a boolean value and takes the "Accept Type" of the request as the argument. You can return true if you want the customserialzer to serialize the data to the passed argument type.
- serialize - The main serialization logic must be implemented in this function. If canSerialize returns "True" for a request, then ColdFusion will use this function to serialize. If canSerialize returns false, then ColdFusion will use the default serialization to serialize.
- CanDeserialize - Returns a boolean value and takes the "Content Type" of the request as the argument. You can return true if you want the customserialzer to deserialize the data.
- DeSerialize - The main deserialization logic must be implemented in this function. If canDeSerialize returns "True" for a request, then ColdFusion will use this function to deserialize. If canDeSerialize returns false, then ColdFusion will use the default de-serialization to deserialize.
The below CustomSerializer code sample will help you to achieve the result for the scenario explained above (Customizing serialization/deserialization of phone directory):
The CustomSerializer that you have implemented can be specified through application.cfc.