Vous êtes sur la page 1sur 17

Alfresco Web Scripts

Scriptable MVC for REST, AJAX, Widgets and Portlets

What are Web Scripts?


Web Scripting
!Agile Script-Driven MVC !Multiple uses
! Roll your own API ! Create new UI components ! Create portlets/widgets ! Expose WCM/AVM features

Consumer / Client

!Script-based implementation
! Server-side JavaScript ! Freemarker

!Limited only by your imagination


! Integration: Create a mashup ! Search: Exposed to other systems. ! Rich Internet Applications ! Cross-language ! Cross-platform

Freemarker (View) JavaScript (Controller) Alfresco Repository (Model)

Two types of Web Scripts


Data Web Script
RESTstyle / RESTful URL Returns structured data:
XML JSON CSV

UI Web Script
Renders self-contained UI. Can leverage Data Web Scripts.

Examples
Web Scripting Examples
!JavaScript that generates JSON output. !AJAX & DHTML Portlet !E.g. OpenSearch is hosted as Web Script

Lightweight Portlet Model


Single Portlet Approach
One portlet can render many scripts.

Service-oriented interfaces
Transformation Templating Workflow

JavaScript extensions
Access to full Alfresco data model.

Templating
FreeMarker for mark-up. Access to compound structures.

REST-style of interface
URL-addressability of all functions.

Richer, Mashable User Experience


Content authored and categorized Content metadata drives mashup

RSS content feeds for both Web Content and Documents from same Alfresco repository

Developing Web Scripts


Where do they live?
Under Data Dictionary In Classpath Under **/alfresco/extension/templates/webscripts

What are the required components?


Descriptor JavaScript FreeMarker Template

How do I (re)deploy them?


Theyre auto-deployed and auto-refreshed

Sample Descriptor
<webscript> <shortname>Sample Alfresco Repository Services</shortname> <description>Repository services and utilities</description> <url>/repo/</url> <url>/repo/node/{nodeid}</url> <url>/repo/node/path/{path}</url> <format default="html"/> <authentication>guest</authentication> </webscript>

reposervices.get.js (part 1)
<import resource= "classpath:alfresco/extension/templates/webscripts/org/alfresco/util/json.js"> var curNode = companyhome; // Check for presence of arguments and locate the referenced node or path. if (url.extension != "") { if (url.match == "/alfresco/service/repo/node/path/") { var expr = "Company Home/"; var str = url.extension.replace(expr,""); // If the path was just "Company Home/" then don"t bother setting curNode to anything. if (str != "") { curNode = companyhome.childByNamePath(str); } } else if (url.match == "/alfresco/service/repo/node/") { curNode = search.findNode("workspace://SpacesStore/" + url.extension); } } // end if

reposervices.get.js (part 2)
// Initialize the variables and arrays var children = curNode.children; var results = {}; var numDocs = 0; var numSpaces = 0; results.node = curNode; results.spaces = new Array(); results.documents = new Array(); // Iterate through children and populate return objects for (var i=0; i < children.length; i++) { var index = 0; if (children[i].isContainer) { results.spaces[numSpaces] = children[i]; index = numSpaces++; } else if (children[i].isDocument) { results.documents[numDocs] = children[i]; index = numDocs++; } // end if } // end for model.results = results;

reposervices.get.html.ftl
<#assign spaces = results.spaces> <#assign docs = results.documents> <html> <body> <b>Spaces:</b><br/> <#list spaces as space> <a href="${url.serviceContext}/repo/node/${space.id}">${space.name}</a><br/> </#list> <hr/> <b>Documents:</b><br/> <#list docs as doc> <a href="${url.serviceContext}/repo/node/${doc.id}">${doc.name}</a><br/> </#list> <hr/> </body> </html>

How does this apply to portlets? Portlets can leverage Web Scripts as a Data API. Portlets can incorporate UI Web Scripts as Widgets.

JSR 168 Portlet


Standard JSR 168 portlet invokes a Web Script via HTTP. Web Script returns JSON text.
Parsers for Java at JSON.org

Portal

JSR 168

JSON over HTTP

Freemarker (View) JavaScript (Controller) Alfresco Repository (Model)

Alfresco Portlet
Single portlet can render any UI Web Script. Currently requires Alfresco to be deployed on the same application server. IFRAME portlet can be a viable alternative for remote Alfresco servers.

Demo

Examine the Services Registry Invoke our Service Use JavaScript Debugger Tour other Web Scripts & Portlets

The End For more information, visit:


http://wiki.alfresco.com/wiki/Web_Scripts http://wiki.alfresco.com/wiki/JavaScript_API http://wiki.alfresco.com/wiki/Template_Guide

Contact Info:
Luis Sala, Sr. Director of Solutions Engineering
luis.sala@alfresco.com http://blogs.alfresco.com/luissala

Alfresco Web Scripts


Scriptable MVC for REST, AJAX, Widgets and Portlets

Vous aimerez peut-être aussi