You are here

Creating multiple response templates

A web script can support multiple response formats to allow it to be used by a variety of clients. For example, it can 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.
  1. Log in to SkyVault Share:
    1. In your browser, enter: http://localhost:8080/share/
    2. If prompted, log in with the user name admin and password admin.
  2. Click on the Repository link in the Share header.
  3. Navigate to Data Dictionary > Web Scripts Extensions > org > example.
  4. Create a JSON response template for your Folder Listing example:
    1. In the Create menu, select Plain Text.
    2. Enter the name for the web script in the Name field: dir.get.json.ftl
    3. Type the following in the 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>
        ]
      }
    4. Click Create.
    5. Navigate back to the org/example folder using the breadcrumb trail.
  5. Register the web script again:
    1. In a new browser tab, enter: http://localhost:8080/alfresco/service/index
    2. If prompted, log in with the user name admin and password admin.
    3. Click Refresh Web Scripts. A message indicates all web scripts have refreshed.
  6. Test your response template:
    1. Type the following in your command line:

      curl -uadmin:admin "http://localhost:8080/alfresco/service/dir/Company%20Home.json"

    2. 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.

  7. 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 by using the web script index.