The Web Script Framework lets you create a web script using familiar technologies, such as scripting and template languages.
Each web script comprises only the following components:
- A description document
- An optional controller script
- One or more FreeMarker response templates
Each component is implemented in its own file. The Web Script Framework dictates where the files are located and how they are named. This allows the framework to automatically locate and register web scripts without having to tell the framework where they are. In some cases, a web script can fall back to Java or rely on advanced Web Script Framework features where scripting alone cannot support the requirements of the web script.
Users of a web script only interact through the web script interface, which comprises its URI, HTTP method, and request/response document types. All of these are described in the web script description document, which is defined by the web script creator.
Web script description document
A web script description document is an XML file that describes the URI and HTTP method that initiates the web script. For example, the web script is given a short name and description, along with authentication and transactional needs. URI bindings are described as URI templates.
An example of a web script description document follows:
<webscript> <shortname>Blog Search Sample</shortname> <description>Sample that finds all blog entries whose content contains the specified search term</description> <url>/sample/blog/search?q={searchTerm}</url> <url>/sample/blog/search.atom?q={searchTerm}</url> <url>/sample/b/s?q={searchTerm}</url> <url>/sample/b/s.atom?q={searchTerm}</url> <format default="html">extension</format> <authentication>guest</authentication> <transaction>required</transaction> </webscript>
Web script controller script
A web script controller script is a JavaScript file that contains the actual logic of a web script.
The controller script can query the repository to build a set of data items, known as a model, to render in the response. It might also update the repository for URIs that intend to modify the repository (PUT, POST, and DELETE method bindings). The JavaScript has access to the URI query string, services, and repository data entry points.
// check that search term has been provided if (args.q == undefined || args.q.length == 0) { status.code = 400; status.message = "Search term has not been provided."; status.redirect = true; } else { // perform search var nodes = search.luceneSearch("TEXT:" + args.q); model.resultset = nodes; }
Web script response template
Known as views, web script response templates render output in the correct format for specific needs, such as HTML, Atom, XML, RSS, JSON, CSV, or any combination of these.
The HTTP response is rendered by using one of the supplied templates, where the chosen template is based on the required response content type or status outcome. The template has access to the URI query string, common repository data entry points, and any data items built by the optional controller script.
<html> <body> <img src="${url.context}/images/logo/AlfrescoLogo32.png" alt="Alfresco" /> Blog query: ${args.q} <br/> <table> <#list resultset as node> <tr> <td><img src="${url.context}${node.icon16}"/></td> <td><a href="${url.serviceContext}/api/node/content/${node.nodeRef.storeRef.protocol}/${node.nodeRef.storeRef.identifier}/${node.nodeRef.id}/${node.name?url}">${node.name}</a></td> </tr> </#list> </table> </body> </html>