A web script may support multiple response formats to allow it to be used by a
variety of clients. For example, it may render an HTML response for human consumption in a web
browser, and a JSON response for machine consumption by other clients.
This task adds support for JSON to the Folder Listing web script, in addition to HTML.
-
Log in to SkyVault Explorer:
- In your browser, enter: http://localhost:8080/alfresco/
- If prompted, log in with the user name admin and password admin.
- Navigate to Company Home > Data Dictionary > Web Scripts Extensions > org > example.
-
Create a JSON response template for your Folder Listing example:
- In the Create menu, click Create Content.
- Enter the name for the web script in the Name field as follows: dir.get.json.ftl
- In the Content Type list, select Plain Text.
- Click Next.
-
Type the following in the Enter Content box:
{ "server" : "SkyVault ${server.edition} Edition v${server.version}", "folder" : { "path" : "${folder.displayPath}", "name" : "${folder.name}" }, "children" : [ <#list folder.children as child> { "isfolder" : <#if child.isContainer>true<#else>false</#if>, <#if verbose> "modifier" : "${child.properties.modifier}", "size" : <#if child.isDocument> ${child.properties.content.size?c}<#else>null</#if>, "modified" : "${child.properties.modified?date}", </#if> "name" : "${child.name}" }<#if child_has_next>,</#if> </#list> ] }
- Click Next.
- Click Finish.
-
Register the web script again:
- In your browser, enter: http://localhost:8080/alfresco/service/index
- If prompted, log in with the user name admin and password admin.
- Click Refresh Web Scripts. A message indicates all web scripts have refreshed.
-
Test your response template:
-
Type the following in your command line:
curl -uadmin:admin "http://localhost:8080/alfresco/service/dir/Company%20Home.json"
- If you see the contents of the Company Home folder, your response template is working.
Each web script supports an unlimited number of response templates; however, there can only be one response template for each format. This is enforced by the naming convention for response templates. Your Folder Listing web script now supports two formats: HTML and JSON.
-
Type the following in your command line:
-
Type the following in your command line to request the contents of a folder that does
not exist in JSON format:
curl -uadmin:admin "http://localhost:8080/alfresco/service/dir/doesnotexist.json"
The web script responds with an error response, but in JSON format, as the client requested.
Note: Whenever you change a web script implementation, including adding and removing response templates, you must re-register the web script via the web script index.