Obsolete Pages{{Obsolete}}
The official documentation is at: http://docs.alfresco.com
Needs to be Reviewed
<start -------------------------------------------------->
(from 3.1)
The content of a Web Script Description document is defined as follows (Note: this demonstrates all possible settings):
<webscript>
<shortname>Blog Search</shortname>
<description>Find 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>
Where:
The description provides Alfresco with all the necessary information to bind the Web Script to one or more URLs. It also provides enough to document the Web Script.
</start -------------------------------------------------->
<start ------------------------------------------------------>
A URL template is simply a URL containing tokens which may be substituted with actual values. The URL template syntax is minimal and simple. Common forms of URL template include:
/content/a/b/c/d.txt
/blog/search?q={searchTerm}
/blog/search?q={searchTerm}&n={numResults}
/blog/category/{category}?n={itemsperpage?}
/user/{userid}
A token of the form {<name>?} means it is optional. Typically, tokens are used for URL arguments, but may also be used for path segments.
Note the third example passes two parameters to the Web Script. Normally an ampersand (&) is used to separate parameters, though in your template one must use the escaped literal '&' instead.
For now, Web Scripts do not enforce mandatory arguments. However, it's still useful to provide a full URL template for documentation purposes.
URL templates are specified relative to http://<host>:<port>/alfresco/service.
When a URL request is made, Alfresco locates the appropriate Web Script by finding the closest match to a URL template in a Web Script Description document. The part of the URL template that's used for matching is the static part. This is from the start of the URL template up to either the URL arguments or the first path token, whichever comes first. For example, the above URL templates are matched on:
/content/a/b/c/d.txt (exact match)
/blog/search (starts with match)
/blog/category (starts with match)
/user/ (starts with match)
The following URLs match one of the above:
/content/a/b/c/d.txt
/blog/search
/blog/search?q=tutorial
/blog/category
/blog/category/web20
/user/fred
however, the following do not:
/content/a/b/c/d.txt.plain
/blog
/usr/fred
</start ------------------------------------------------------------>
<start ------------------------------------------------------------>
(from 3.2)
The full Alfresco JavaScript API is available within Web Scripts. However, there are some differences with regards to available root objects.
Web Scripts also have access to the following additional root objects:
</start ------------------------------------------------------------>
<start ------------------------------------------------------------>
(from 3.3)
The full Alfresco Template API is available within Web Scripts. However, there are some differences with regards to available root objects.
Web Scripts also have access to the following additional root objects:
And remember, the template also has access to any root objects created by the 'execute' script , if one has been associated with the Web Script.
Web Script templates also have access to the following additional methods:
</start ------------------------------------------------------------>
<start -------------------------------------------------------------------------->
(from 3.4)
Status Response templates have access to the same root objects as standard Web Script response templates with the addition of the 'status' object. The exception is that the default templates /<code>.ftl and /status.ftl only have access to the root objects 'url', 'status', 'server' and 'date'.
To provide a custom 'Bad Request' status response for our blog search we create the following file in the same folder as the Web Script Description document
blogsearch.get.html.400.ftl
whose content is
${status.message}
We can also provide a meaningful response for atom requests which can be machine read:
blogsearch.get.atom.400.ftl
<response>
<code>${status.code}</code>
<codeName>${status.codeName}</codeName>
<codeDescription>${status.codeDescription}</codeDescription>
<message>${status.message}</message>
</response>
Remember, these templates are optional, as the search for an appropriate status template will always eventually find the default template /status.ftl.
</start -------------------------------------------------------------------------->
<start --------------------------------------------------->
Not all clients can issue all HTTP methods. In the most severe case, a client may be restricted to GET and POST only. In this situation, Web Scripts provide two mechanisms for issuing any HTTP method via POST:
In either case, the value is set to the HTTP method name to be invoked.
For example, if the HTTP DELETE method is not supported by a client, but the client wishes to delete a blog entry, the following may be issued:
POST X-HTTP-Method-Override=DELETE /alfresco/service/sample/blog/2007/03/04/new-release
POST /alfresco/service/sample/blog/2007/03/04/new-release?alf_method=DELETE
</start --------------------------------------------------->
<start --------------------------------------------------->
Not all clients can issue all HTTP methods. In the most severe case, a client may be restricted to GET and POST only. In this situation, Web Scripts provide two mechanisms for issuing any HTTP method via POST:
In either case, the value is set to the HTTP method name to be invoked.
For example, if the HTTP DELETE method is not supported by a client, but the client wishes to delete a blog entry, the following may be issued:
POST X-HTTP-Method-Override=DELETE /alfresco/service/sample/blog/2007/03/04/new-release
POST /alfresco/service/sample/blog/2007/03/04/new-release?alf_method=DELETE
</start --------------------------------------------------->
<start --------------------------------------------------->
Not all clients can gracefully handle non-success HTTP Responses e.g. the Flash runtime player - the runtime for Adobe Flex applications.
In this situation, Web Scripts provide a mechanism to force all HTTP Responses to indicate success in their Response Header. However, the Response body will still represent the content as if a non-success status had occurred.
To force HTTP Response Header success set the following HTTP Request Header:
</start --------------------------------------------------->
<start --------------------------------------------------->
If your Web Scripts provide JSON responses, it's likely they'll be invoked directly from within a Web Browser via the XMLHttpRequest object. However, due to security reasons, you may run into cross-domain issues, a restriction that requires you to proxy your requests on the server side. Typically, to 'workaround' these issues, public services such as Yahoo! JSON Services provide a callback mechanism.
Web Scripts also provide this mechanism which is invoked by adding the following URL parameter on JSON URL requests:
where function is the name of a client-side javascript function to be invoked.
The following simple HTML page demonstrates this:
</start --------------------------------------------------->
<start --------------------------------------------------->
(sorry, available 2.1 Enterprise onwards)
A Web Script may specify how its response is cached. See example. Each Web Script Runtime may or may not adhere to the caching requirements of the Web Script; if the runtime does not support caching, the requirements are just ignored. Alfresco 2.1 caching is only supported by the Servlet Runtime, by virtue of caching built into the Web Browser or a Proxy server.
</start --------------------------------------------------->
<start --------------------------------------------------->
Definition-time cache control is optionally specified via the Web Script Description. An example follows:
<webscript>
...
<cache>
<never>false</never>
<public/>
<mustrevalidate/>
</cache>
</webscript>
Where:
</start --------------------------------------------------->
<start --------------------------------------------------->
Run-time Cache control is optionally specified via the root object cache which is available to the Web Script 'Execution' script. At run-time, expiry criteria may be set, and definition-time controls may be overridden.
</start --------------------------------------------------->
<start --------------------------------------------------->
Just start the TestWebScriptServer application (it supports a main entry point) and interact with it via the one of the following commands:
##
## Meta commands
##
ok> help
List this help.
ok> r
Repeat last command.
ok> user [<userName>]
Switch to specified <userName>. If <userName> is omitted, the currently
selected user is shown.
A ticket may be specified instead of username.
ok> quit | exit
Quit this Web Script server.
##
## HTTP Requests
##
ok> get <path>
Issue a HTTP GET request to the Web Script located at <path>. The response
is dumped to the console.
<path> URL relative to /alfresco/service
e.g. get /blog/search?q=tutorial
ok> put <path>
Issue a HTTP PUT request to the Web Script located at <path>. The response
is dumped to the console.
<path> URL relative to /alfresco/service
ok> post <path>
Issue a HTTP POST request to the Web Script located at <path>. The response
is dumped to the console.
<path> URL relative to /alfresco/service
ok> delete <path>
Issue a HTTP DELETE request to the Web Script located at <path>. The response
is dumped to the console.
<path> URL relative to /alfresco/service
ok> tunnel <encoding> <method> <path>
Tunnel a request via POST.
The actual <method> is encoded as either the URL parameter named alf:method or
the request header named X-HTTP-Method-Override as specified via the <encoding>
parameter:
param - encode method as URL parameter
header - encode method in Request Header
e.g. to tunnel 'get /index' via post (where method is encoded in header) issue
tunnel header get /index
##
## Request Header Commands
##
ok> header
Show all defined headers.
ok> var <headerName>=<headerValue>
Define or update a header value.
<headerName> header name
<headerValue> header value
e.g.
header alf-force-success=true
ok> header <headerName>=
Delete an existing header value.
<headerName> header name
##
## end
##
For example (to invoke our example blog search Web Script):
ok> get /blog/search?q=tutorial
<html>
<body>
<img src='/alfresco/images/logo/AlfrescoLogo32.png' alt='Alfresco' />
Blog search: tutorial
<br>
<table>
<tr>
<td><img src='/alfresco/images/filetypes/_default.gif'/>
<td><a href='/alfresco/download/direct/workspace/SpacesStore/d0ea49aa-cda3-11db-a118-718e716a085b/Alfresco-Tutorial.pdf'>Alfresco-Tutorial.pdf</a>
</tr>
</table>
</body>
</html>
78ms
</start --------------------------------------------------->
<start --------------------------------------------------->
Within a unit test, the following pattern may be used:
// retrieve an instance of the test server
TestWebScriptServer server = TestWebScriptServer.getTestServer();
// submit a request
MockHttpServletResponse res = server.submitRequest('get', '/blog/search?q=tutorial');
// process response
byte[] content = res.getContentAsByteArray();
String contentAsString = res.getContentAsString();
</start --------------------------------------------------->
<start --------------------------------------------------->
A host object providing access to multipart/form-data requests allowing file upload from a Web Script. See example. (sorry, 2.1 Enterprise onwards).
A host object providing access to a form field within a multipart/form-data request.
A host object providing access to the URL (or parts of the URL) that triggered the Web Script.
For example, imagine a Web Script URL template of
/user/{userid}
and a Web Script request URL of
/alfresco/service/user/fred?profile=full&format=html
The url root object will respond as follows:
An associative array of response status properties that allow control over the status and content of the Web Script Response.
An associative array of cache control properties that allow control over how the Web Script Response is cached. See example. (sorry, 2.1 Enterprise onwards).
An associative array of meta-data properties that describe the hosting Alfresco server.
An associative array of meta-data properties describing the Web Script.
</start --------------------------------------------------->