Introduction

The JSF based component architecture of the web-client does not lend itself to URL addressability or bookmarking of page URLs. To compensate for this, Alfresco web-client provides a set of servlets that provide direct URL based access to any item of content within the repository and to allow direct navigation to pages within the web-client. Servlets are also provided to render templates and execute workflow upon documents.

Security

Access to content or pages in servers is protected by the same node level security as any other API within Alfresco. If a request for node content is made without an appropriate security context, a login page will be presented. If NTLM or other single sign-on authentication is present, then it will be used if applicable. Guest Access or a Ticket URL Argument can also be used if appropriate to avoid the login page.

Guest Access

Guest access is now supported for all Alfresco URLs. The URL parameter guest=true can be appended to any Alfresco URL and the login page will not be displayed before the URL is processed. The Guest user must have access to the item referenced by the URL or the access attempt will fail. For example:

Guest access allows URLs to be sent in emails etc. to provide external access to content or template rendering results to users who do not have an Alfresco login. If the Guest login is successful then the URL will not require a login page, however if guest access fails the Login page is shown as normal. Also, if a permissions check to the template or node referenced by the URL fails then the Login page is shown.

Ticket URL Argument

If the API you are using wishes to use a servlet directly and does not require the Alfresco web-client screen for security authentication, then you must aquire a valid Authentication ticket from the appropriate API call - you can use a Web Script or Web Services call to request a ticket.

This can then be appended to any servlet URL as the alf_ticket argument to provide authentication without login, for example:

DownloadContentServlet

This servlet has the following class name:

org.alfresco.web.app.servlet.DownloadContentServlet

Responsible for streaming node content from the repo directly to the response stream. The NodeRef of the content to retrieve is encoded on the URL. The appropriate mimetype to return in the header of the stream is calculated based on filename extension passed as the final element on the url path.

The 'attach' or 'direct' element is used to indicate whether to display the stream directlyin the browser or download it as a file attachment. Specify attach to attach the content stream as an HTTP1.1 attachment object, or specify direct to return the content stream directly to the output. The URL elements then encode the repository store protocol, the store ID, followed by the content Node Id. The last element is the stream attachment filename to return to the browser.

URL arguments

ticket - can be supplied as a URL argument to allow authentication for external use - see above.

By default, the servlet assumes that the content is on the {http://www.alfresco.org/model/content/1.0}content content property. If you require the content of a different specific model property, provide the fully qualified name of the property as an additional URL argument called property. For example:

CMIS alternatives

UploadContentServlet

This servlet has the following class name:

org.alfresco.web.app.servlet.UploadContentServlet

Responsible for streaming content directly from servers into the repository using the HTTP PUT command. The NodeRef of the node onto which the content will be streamed can be encoded into the URL. In this situation the content of the property can be considered to be updated once the servlet has returned.

If a NodeRef is not specified in the URL then the content is streamed into the content store and the content data (including the content store URL, the mimetype, the encoding and the size) will be returned to the client on the response. This information can later be used to manually update the content of a node by setting it to the values of the content property fo the node in question. This is a technique used when updating content via the web service API.

Example URL with NodeRef provided:

/alfresco/upload/workspace/SpacesStore/0000-0000-0000-0000/myfile.pdf

Example URL with NodeRef not provided:

/alfresco/upload/myfile.pdf

By default the upload servlet assumes, when uploading content, it should do so to the cm:content property. If this is not the case then the content property name should also be encoded into the URL. The property argument should be set to the fully qaulified name of the content property.

The mimetype and encoding of the content being uploaded can also be encoded onto the URL. In cases that they are not the mimetype is determined from the file extension and encoding is defaulted to 'UTF-8'. The 'mimetype' and 'encoding' properties should be set to the required mimetype and encoding value respectivly.

The 'ticket' parameter is used to pass the authentication information to the servlet. Guest access is currently denied.

GuestDownloadContentServlet

This servlet has the following class name:

org.alfresco.web.app.servlet.GuestDownloadContentServlet

This servlet follows exactly the same rules as DownloadContentServlet, the same parameters and usage model applies.

The main difference with this servlet is the handling of sessions. This servlet will always download the content as the 'guest' user, however, the guest user will not be logged in, as is the case when DownloadContentServlet is used. The other difference is if the content referenced by the URL is not accessable to the guest user, a 403 Forbidden error will be returned rather than the login page.

ExternalAccessServlet

This servlet has the following class name:

org.alfresco.web.app.servlet.ExternalAccessServlet

Responsible for redirecting the web-client to display various global JSF views. It also accepts additional URL arguments to setup the context required for certain JSF views.

The URL specifies the JSF 'outcome' to be executed, which provides the JSF View to be displayed. The JSF 'outcome' must equate to a global navigation rule or it will not be displayed. The servlet may also accepts additional elements that encode a space or content NodeRef as part of the view context. A third mechanism is to encode the webdav path of the node instead of the NodeRef.

ticket - can be supplied as a URL argument to allow authentication for external use - see above.

TemplateContentServlet

This servlet has the following class name:

org.alfresco.web.app.servlet.TemplateContentServlet

Responsible for returning the result of processing a template against a specified node and the default template model. Allows direct processing of Templates against nodes (and the default template model) with streaming of the result to the output stream. This allows the use of simple URLs to execute arbitary templates against nodes without the web-client interface.

Servlet URL is of the form:

/alfresco/template/<workspace>/<store>/<nodeid>

The result is the output of the default template (from the 'templatable' aspect set on the specified NodeRef), processed using the standard template model (see Template Guide) and the supplied NodeRef. A template must have been set on the node using the templatable aspect for this to work!

ticket - can be supplied as a URL argument to allow authentication for external use - see above.

mimetype - can be supplied as a URL argument to set the mimetype for the response returned on the outputstream ('text/html' is assumed by default)

The default template assigned to the requested node will be used but it's also possible to assign a different template to the node (useful when no template is set or you need a different template than the default one):

Any other additional arguments can be passed to the servlet, they will be collected and supplied to the templating data-model into a Map object called args. The args object is accessable from the root of the templating data-model and contains all the URL name/value pairs supplied to the servlet.

Available in Alfresco 1.4!

The reference to the template or the context node can now be specified as 'templatePath' and 'contextPath' URL arguments rather than as NodeRef elements. For example:

Paths are specified in a similar way to a WebDav path - they are 'cm:name' based URL encoded strings.

Note that the entire URL must be URL encoded appropriately.

The servlet now also supports POST'ed parameters. As with parameters passed on the URL, these will be collected and supplied to the templating data-model into a Map object called args. The args object is accessable from the root of the templating data-model and contains all the name/value pairs supplied to the servlet (usually via an HTML form). Please note though, that file upload is not supported.

Example to create RSS Feeds

Using URL addressability and Presentation Templates it is possible to define an RSS feed for any Space within Alfresco. All that is needed is to apply the RSS Presentation Template that is distributed with Alfresco to the required Space using a URL - this can either be by explicit reference to the template or implicitly through a Space Dashboard.

The easiest approach is to use the Dashboard feature, which only takes 5 simple steps!

Step 1: In the Alfresco web client, navigate to the space you wish to subscribe to, and choose 'View Details' from the Actions menu.

If you do not want to use the Dashboard approach, then you will need to find the Node Reference for the RSS template (from its Details page) and build a URL that also includes it. The format of that URL will be:

NOTE: You must ensure that the Guest user has access to the space you wish to feed the RSS stream from! This can be achieved using the 'Manage Space Users' dialog and inviting the 'Guest' user into the Space.

GuestTemplateContentServlet

This servlet has the following class name:

org.alfresco.web.app.servlet.GuestTemplateContentServlet

This servlet follows exactly the same rules as TemplateContentServlet, the same parameters and usage model applies.

The main difference with this servlet is the handling of sessions. This servlet will always run the template as the 'guest' user, however, the guest user will not be logged in, as is the case when TemplateContentServlet is used. The other difference is if the node or the template referenced by the URL is not accessable to the guest user, a 403 Forbidden error will be returned rather than the login page.

CommandServlet

This servlet has the following class name:

org.alfresco.web.app.servlet.CommandServlet

A generic servlet that can be configured and customized with custom classes using the well known Command and Strategy design patterns. It is responsible for direct execution of named commands using a configured command processor instance. For example in the default installation of Alfresco 1.3, a Workflow command processor is supplied that can execute 'approve' and 'reject' commands. This mechanism allows the use of simple URLs to execute commands against nodes without the web-client interface.

Servlet URL is of the form:

/alfresco/command/<command-processor>/<command>/<args...>

The args element(s) is optional and depend on the individual command being executed. It is up to the command to specify and process args as appropriate for the implementation (see below). E.g. for the workflow command processor executing the 'approve' command the URL could be of the form:

/alfresco/command/workflow/approve/<workspace>/<store>/<nodeid>

URL arguments:

return-page - if supplied then instead of the status page, the servlet will redirect to the specified URL once the command has been completed.

ticket - can be supplied as a URL argument to allow authentication for external use - see above.

Command Processors

The first element in the URL after the servlet name specifies the command processor to use. Command processors are simple classes responsible for executing a related set of commands. They are configured in web-client-config.xml - or in the appropriate extension config file. The config block is as follows:

/** * This interfaces defines the contract and lifecycle of a Servlet Command Processor. * * A command processor is defined as a class capable of executing a set of related Command * objects. It performs the bulk of the work for the command servlet. The processor impl * is responsible for validating that the command can be processed (given the supplied remaining * URL arguments from the servlet) and processing the command. It is also responsible for * supply an output status page on successfuly execution of the command. * * The arguments passed to a Command Processor are the remaining URL elements from the command * servlet URL after removing the web-app name, servlet name and command processor name. * * @author Kevin Roast */public interface CommandProcessor{ /** * Pass and validate URL arguments for the command processor. Validate if the command can be * executed given the arguments supplied. Generally at this post a Command Processor will * convert the supplied arguments to the objects it expects, and also check any permissions * that are required by the current user to execute the command. * * @param serviceRegistry ServiceRegistry instance * @param command Name of the command the arguments are for * @param args String[] of the remaining URL arguments to the command servlet. * * @return true if the command can be executed by the current user given the supplied args. */ public boolean validateArguments(ServiceRegistry serviceRegistry, String command, String[] args);

/** * Process the supplied command name. It is the responsibility of the Command Processor * to lookup the specified command name using the CommandFactory registry. For that reason * it also has the responsiblity to initially register commands it is responsible for so * they can be constructed later. If the supplied command is unknown to it then an * exception should be thrown to indicate this. * * @param serviceRegistry ServiceRegistry * @param request HttpServletRequest * @param command Name of the command to construct and execute */ public void process(ServiceRegistry serviceRegistry, HttpServletRequest request, String command);

/** * Output a simple status message to the supplied PrintWriter. * It can be assumed that the process() method was successful if this method is called. * * @param out PrintWriter */ public void outputStatus(PrintWriter out);}

The lifecycle of the command processor is as follows:

validateArguments() : The array of args passed to the method represent the remaining URL elements that followed the command processor and command names on the URL - this may be empty. The command processor should validate that the supplied arguments are valid for the command to be executed, throwing a runtime exception if this is not the case. The command processor will generally convert the supplied args to objects it needs (e.g. a NodeRef) ready for command execution later.

process() : Performs the bulk of the work for the command processor. It is passed a named command to execute against the previously arguments as passed to validateArguments(). The command processor should create the named command using the CommandFactory singleton instance and execute it - throwing an approriate runtime exception if any error occurs. An example process() method may look as follows:

outputStatus() : called on successful execution of the command - it should output a simple success status message to the supplied PrintWriter.

In addition, it is the responsibilty of the command processor instance to register the related set of named commands it is expecting to process with the CommandFactory singleton instance. This is generally performed within a static initialiser block, for example:

The named commands are registered by name against class. It should be noted that for each execution of a command a new command processor instance is created and then discarded at the end of the lifecycle.

Available in Alfresco 1.4!

The servlet now supports POST'ed parameters. All supplied parameters will be stored in the map of arguments passed to the command processor. Please note though that file uploads are not supported.

Workflow Command Processor

Responsible for executing 'approve' and 'reject' workflow commands on a Node.The appropriate workflow command will be executed on node specified by the NodeRef elements in the URL. Currently the only available command names are 'approve' and 'reject'.Returns a very simple status HTML page on successful execution of the workflow command.

Task Command Processor

Responsible for managing advanced workflow tasks. The appropriate task command will be executed on the task specified by the taskId element in the URL. Currently, the only available command is 'end' which accepts an optional transition name. Returns a very simple status HTML page on successful execution of the task command. Valid taskId and transition names are those returned by the Workflow Service.

Script Command Processor

Responsible for processing an Alfresco JavaScript through the 'execute' command.The NodeRef supplied as the first 3 arguments of the node specify the script to execute. Optionally an additional NodeRef can be specified to provide a document node context to the script (in addition the parent folder of the document node will then be used as the space context to the script).Returns the result of the script (if any) converted to a string as the HTML response.See the JavaScript API page for more information on the Alfresco JavaScript API.

Any additional URL parameter arguments can be passed to the servlet, they will be collected and supplied to the scripting data-model into an associative JavaScript array object called args. The args object is accessable from the root of the script scope and contains all the URL name/value pairs supplied to the servlet.

Available in Alfresco 1.4!

The reference to the script or the context node can now be specified as 'scriptPath' and 'contextPath' URL arguments rather than as NodeRef elements. For example:

Paths are specified in a similar way to a WebDav path - they are 'cm:name' based URL encoded strings.

Note that the entire URL must be URL encoded appropriately.

It is not not always desirable to allow all users to do this as it is an advanced feature - in Alfresco 3.3 this is changed so that unless configured, only the admin user can perform this action via a command servlet URL.

The Explorer web-client custom config to re-enable this feature for non-admin users is as follows:

Commands

The element in the URL after the command processor name is the command the command processor should execute. Commands are simple classes responsible for encapsulating a single task. Each command processor is responsible for a known set of register commands. Command classes must implement the following interface: