3.0 Web Scripts Kinds

From alfrescowiki

Jump to: navigation, search


Back to Web Scripts.

DRAFT DRAFT DRAFT

NOTE: This document describes features found in Alfresco v3 onwards


Contents

Introduction

This page describes alternative "Kinds" of Web Script.

See Using a Kind of Web Script and Creating a new Kind Web Script for further info.

Available Kinds of Web Script

Content Streaming

It is often desirable for a Web Script to stream back binary content directly from the repository back to the client. This is possible using a purely Java backed Web Script, but a more declarative style provides more implementation flexibility.

This can be achieved by using the StreamContent kind of webscript. This allows a JavaScript file to be executed, as per a normal declarative web script. Once the script has been executed it used information placed within the shared model to determine what content to stream back to the client. There is, therefore, no corresponding template for this kind of declarative web script.

Using a ContentStream Web Script

When describing a content stream Web Script the kind attribute of the root Web Script tag must be set to "org.alfresco.repository.content.stream". This will ensure the correct web script implementation is used, see the following example:

<webscript kind="org.alfresco.repository.content.stream">
  <shortname>Thumbnails</shortname>
  <description>Get a named thumbnail for a content resource</description>
  <url>/api/node/{store_type}/{store_id}/{id}/content{property}/thumbnails/{thumbnailname}</url>  
  <url>/api/path/{store_type}/{store_id}/{id}/content{property}/thumbnails/{thumbnailname}</url>
  <format default="">argument</format>
  <authentication>guest</authentication>
  <transaction>required</transaction>
</webscript>

Note that the format has not been set. This is because the resulting format will be matched to the mimetype of the content being returned to the client.

Setting the Content to Stream in JavaScript

Example:

function main()
{
   // Get the node from the URL
   var pathSegments = url.match.split("/");
   var reference = [ url.templateArgs.store_type, url.templateArgs.store_id ].concat(url.templateArgs.id.split("/"));
   var node = search.findNode(pathSegments[2], reference);
   
   // 404 if the node to thumbnail is not found
   if (node == null)
   {
      status.setCode(status.STATUS_NOT_FOUND, "The thumbnail source node could not be found");
      return;
   }
   
   // Get the thumbnail name from the JSON content 
   var thumbnailName = url.templateArgs.thumbnailname; 
   
   // 404 if no thumbnail name found
   if (thumbnailName == null)
   {
      status.setCode(status.STATUS_NOT_FOUND, "Thumbnail name was not provided");
      return;
   }
   
   // Get the queue/force create setting
   var qc = false;
   var fc = false;
   if (args.c != null)
   { 
   	if (args.c == "queue")
   	{
   		qc = true;
   	}
   	else if (args.c == "force")
   	{
   		fc = true;
   	}
   }
   
   // Get the place holder flag
   var ph = false;
   var phString = args.ph;
   if (phString != null)
   {
      ph = utils.toBoolean(phString);
   }
   
   // Get the thumbnail
   var thumbnail = node.getThumbnail(thumbnailName);
   if (thumbnail == null || thumbnail.size == 0)
   {
      // Queue the creation of the thumbnail if appropriate
      if (fc)
      {
      	model.contentNode = node.createThumbnail(thumbnailName, false);
      }
      else
      {
	      if (qc)
	      {
	         node.createThumbnail(thumbnailName, true);
	      }
	      
	      if (ph == true)
	      {
	         // Try and get the place holder resource
	         var phPath = thumbnailService.getPlaceHolderResourcePath(thumbnailName);
	         if (phPath == null)
	         {
	            // 404 since no thumbnail was found
	            status.setCode(status.STATUS_NOT_FOUND, "Thumbnail was not found and no place holde resource set for '" + thumbnailName + "'");
	            return;
	         }
	          else
	          {
	             // Set the resouce path in the model ready for the content stream to send back to the client
	             model.contentPath = phPath;
	          }
	       }
	       else
	       {         
	          // 404 since no thumbnail was found
	          status.setCode(status.STATUS_NOT_FOUND, "Thumbnail was not found");
	          return;
 	       }
        }
    }
    else
    {
   	  // Place the details of the thumbnail into the model, this will be used to stream the content to the client
         model.contentNode = thumbnail;
    } 
}

main();

Overview of ContentStream Model Properties

TODO:

Personal tools
Download and go
© 2014 Alfresco Software, Inc. All Rights Reserved. Legal | Privacy | Accessibility