Download - Optaros Surf Code Camp Api

Transcript
Page 1: Optaros Surf Code Camp Api

Alfresco Surf Code Camp

Remoting and Component API

Page 2: Optaros Surf Code Camp Api

12/01/09 Optaros and Client confidential. All rights reserved. 2

Objectives

By the end of this module you should know• Useful JavaScript objects• Surf remoting concepts• How & why you'd use Surf's proxy• How to retrieve and work with XML and JSON returned by scripts

in the Repository tier

Page 3: Optaros Surf Code Camp Api

12/01/09 3

Objects and Methods available to Renderers

Renderer Types• Web Script

• FreeMarker

• Java

• JSP

Surf API

Page 4: Optaros Surf Code Camp Api

12/01/09 4

JavaScript Example

FreeMarker Example

JSP Example

var username = context.user.id;

<#assign username = context.user.id>${username}

<% String username = RequestUtil.getRequestContext().getUser().getId();%><%=username%>

Surf API Brief Examples

Page 5: Optaros Surf Code Camp Api

12/01/09 5

Focus only on the Web Script and FreeMarker API• http://wiki.alfresco.com/wiki/Surf_Platform_-_Freemarker_Template_and_JavaScript_API

Java API not yet documented online

Web Scripts and FreeMarker

Page 6: Optaros Surf Code Camp Api

12/01/09 6

context• The request context

user• The current user

content• The content object being rendered

instance• The renderer instance (equivalent of "this") ‏

sitedata• Site Construction helper

remote• Connection management helper

Renderer Root Scoped Objects

Page 7: Optaros Surf Code Camp Api

12/01/09 7

The request context for the currently executing page

Useful variables:• page The page being rendered

• template The template being used to render

• user The current user

• content The content object being dispatched (if available)‏

Examples:var page = context.page;

model.pageName = page.title;model.pageDescription = page.description;

model.templateId = context.template.id;

Root-scoped: context ScriptRequestContext

Page 8: Optaros Surf Code Camp Api

12/01/09 8

The current user

Useful variables:• firstName The user’s first name

• lastName The user’s last name

• fullName A full name representation of the user

• properties An associative array of custom properties

Examples:var user = context.user;

model.fullName = user.fullName;model.email = user.companyEmail;model.team = user.properties[“{http://www.alfresco.org/model/content/1.0}team”];

Root-scoped: userScriptUser

Page 9: Optaros Surf Code Camp Api

12/01/09 9

A remote connection helper

Access to Alfresco Web Framework remoting faciltiies

Stateless Connections

Stateful Connections (scoped to user) ‏

Credential Management and Binding (stateful) ‏

Credential Vault (persistent and non-persistent) ‏

Customizable (XML config driven) ‏

Root-scoped: remoteScriptRemote

Page 10: Optaros Surf Code Camp Api

12/01/09 10

Configurable definitions for:• Endpoints

• Connectors

• Authenticators

These are entirely configurable

webscript-framework-config-remote.xml

• /WEB-INF/classes/alfresco

Remoting Configurations

Page 11: Optaros Surf Code Camp Api

12/01/09 11

Get a connector for a given endpoint

Endpoints• any arbitrary id

• defines connection information tothe remote location

Connector• knows how to “talk” with specific types

of back end servers

Authenticator• knows how to “handshake” for authentication

with back end servers

• Alfresco Ticket, MediaWiki, WordPress, etc.

Endpoint

Authenticator

Connector

Remoting Configurations

Page 12: Optaros Surf Code Camp Api

12/01/09 12

HTTP

alfresco

RMI Alfresco

www.wikipedia.org

mediawiki wordpress

Endpoint

Connectors

Authenticators

alfresco.com

Remoting Configurations

Page 13: Optaros Surf Code Camp Api

12/01/09 13

Endpoints

Properties• id The endpoint ID

• connector-id The ID of the connector when connecting

• endpoint-url The base URL for the connection

• identity How to manage user connection state

<config evaluator="string-compare" condition="Remote"> <remote> <endpoint> <id>alfresco</id> <name>Alfresco - user access</name> <connector-id>alfresco</connector-id> <endpoint-url>http://localhost:8080/alfresco/s</endpoint-url> <identity>user</identity> </endpoint> </remote></config>

Remote Configuration Endpoints

Page 14: Optaros Surf Code Camp Api

12/01/09 14

Connectors

Properties• id The connector ID

• class The Java implementation class name

• authenticator-id The ID of the authenticator to use

<config evaluator="string-compare" condition="Remote"> <remote> <connector> <id>alfresco</id> <name>Alfresco Connector</name> <class>org.alfresco.connector.AlfrescoConnector</class> <authenticator-id>alfresco-ticket</authenticator-id> </connector> </remote></config>

Remote Configuration Connectors

Page 15: Optaros Surf Code Camp Api

12/01/09 15

Authenticator

Properties• id The authenticator ID

• class The Java implementation class name

<config evaluator="string-compare" condition="Remote"> <remote> <authenticator> <id>alfresco-ticket</id> <name>Alfresco Authenticator</name> <class>org.alfresco.connector.AlfrescoAuthenticator</class> </authenticator> </remote></config>

Remote Configuration Authenticators

Page 16: Optaros Surf Code Camp Api

12/01/09 16

connect(endpointId)‏

• Retrieves a ScriptRemoteConnector instance for the given endpoint

• Creates a new Connector

• Binds it to the given endpoint

• Binds it to the current user

ScriptRemoteConnector Methods• get(uri)‏• post(uri, body) ‏• post(uri, body, contentType) ‏• put(uri, body) ‏• put(uri, body, contentType) ‏• delete(uri)‏

Root-scoped: remoteScriptRemote

Page 17: Optaros Surf Code Camp Api

12/01/09 17

JavaScript

FreeMarker

// get a connector to the Alfresco Endpoint// this endpoint has a base URI of /alfresco/servicevar connector = remote.connect(“alfresco”);

// retrieve the index of web scriptsvar html = connector.get(“/index”);

// store html onto modelmodel.html = html;

${html}

Example #1: HTML

Page 18: Optaros Surf Code Camp Api

12/01/09 18

JavaScript

// get a connector to the Alfresco Endpointvar connector = remote.connect(“alfresco”);

// retrieve the company home foldervar cmis = connector.get(“/api/path/workspace/SpacesStore”);

Example #2: CMIS XML

Page 19: Optaros Surf Code Camp Api

12/01/09 19

<?xml version="1.0" encoding="UTF-8"?><entry xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns:cmis="http://www.cmis.org/2008/05" xmlns:alf="http://www.alfresco.org"> <author><name>System</name></author> <content>56923743-7436-482e-b1cf-eda326d11dc2</content> <id>urn:uuid:56923743-7436-482e-b1cf-eda326d11dc2</id> <link rel="self" href="http://localhost:8080/alfresco/service/api/node/workspace/SpacesStore/56923743-7436-482e-b1cf-eda326d11dc2"/> <link rel="edit" href="http://localhost:8080/alfresco/service/api/node/workspace/SpacesStore/56923743-7436-482e-b1cf-eda326d11dc2"/> <link rel="cmis-allowableactions" href="http://localhost:8080/alfresco/service/api/node/workspace/SpacesStore/56923743-7436-482e-b1cf-eda326d11dc2/permissions"/> <link rel="cmis-relationships" href="http://localhost:8080/alfresco/service/api/node/workspace/SpacesStore/56923743-7436-482e-b1cf-eda326d11dc2/associations"/> <link rel="cmis-children" href="http://localhost:8080/alfresco/service/api/node/workspace/SpacesStore/56923743-7436-482e-b1cf-eda326d11dc2/children"/> <link rel="cmis-descendants" href="http://localhost:8080/alfresco/service/api/node/workspace/SpacesStore/56923743-7436-482e-b1cf-eda326d11dc2/descendants"/> <link rel="cmis-type" href="http://localhost:8080/alfresco/service/api/type/folder"/> <link rel="cmis-repository" href="http://localhost:8080/alfresco/service/api/repository"/> <published>2008-09-10T09:16:32.421-05:00</published> <summary>The company root space</summary> <title>Company Home</title> <updated>2008-09-10T09:17:19.328-05:00</updated> <cmis:object> <cmis:properties> <cmis:propertyId cmis:name="ObjectId"><cmis:value>workspace://SpacesStore/56923743-7436-482e-b1cf-eda326d11dc2</cmis:value></cmis:propertyId> <cmis:propertyString cmis:name="BaseType"><cmis:value>folder</cmis:value></cmis:propertyString> <cmis:propertyString cmis:name="ObjectTypeId"><cmis:value>folder</cmis:value></cmis:propertyString> <cmis:propertyString cmis:name="CreatedBy"><cmis:value>System</cmis:value></cmis:propertyString> <cmis:propertyDateTime cmis:name="CreationDate"><cmis:value>2008-09-10T09:16:32.421-05:00</cmis:value></cmis:propertyDateTime> <cmis:propertyString cmis:name="LastModifiedBy"><cmis:value>System</cmis:value></cmis:propertyString> <cmis:propertyDateTime cmis:name="LastModificationDate"><cmis:value>2008-09-10T09:17:19.328-05:00</cmis:value></cmis:propertyDateTime> <cmis:propertyString cmis:name="Name"><cmis:value>Company Home</cmis:value></cmis:propertyString> <cmis:propertyId cmis:name="ParentId"/> </cmis:properties> </cmis:object> <cmis:terminator/> <app:edited>2008-09-10T09:17:19.328-05:00</app:edited> <alf:icon>http://localhost:8080/alfresco/images/icons/space-icon-default-16.gif</alf:icon></entry>

Example #2: CMIS XML

Page 20: Optaros Surf Code Camp Api

12/01/09 20

<?xml version="1.0" encoding="UTF-8"?><entry xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns:cmis="http://www.cmis.org/2008/05" xmlns:alf="http://www.alfresco.org">

<cmis:object> <cmis:properties>

<cmis:propertyId cmis:name="ObjectId"> <cmis:value> workspace://SpacesStore/56923743-7436-482e-b1cf-eda326d11dc2 </cmis:value> </cmis:propertyId>

<cmis:propertyString cmis:name="Name"> <cmis:value>Company Home</cmis:value> </cmis:propertyString>

</cmis:properties> </cmis:object>

<cmis:terminator/>

</entry>

Essentially…

Example #2: CMIS XML

Page 21: Optaros Surf Code Camp Api

12/01/09 21

JavaScript

FreeMarker

// get a connector to the Alfresco Endpointvar connector = remote.connect(“alfresco”);

// retrieve the company home foldervar cmis = connector.get(“/api/path/workspace/SpacesStore”);

// load XML into E4Xvar xml = new XML(cmis);

// populate the modelvar properties = xml.*::object.*::properties;model.id = properties.(@name=“ObjectId”).*::value; model.name = properties.(@name=“Name”).*::value;

Object id: ${id}<br/>Object name: ${name}

Example #2: CMIS XML

Page 22: Optaros Surf Code Camp Api

12/01/09 22

JavaScript

// get a connector to the Alfresco Endpointvar connector = remote.connect(“alfresco”);

// retrieve a content objectvar nodeRef = “workspace://SpacesStore/56923743-7436-482e-b1cf-eda326d11dc2”;var data = connector.get(“/api/metadata?nodeRef=” + nodeRef);

Example #3: JSON

Page 23: Optaros Surf Code Camp Api

12/01/09 23

{ "type": "{http://www.alfresco.org/model/content/1.0}folder", "aspects": [ "{http://www.alfresco.org/model/content/1.0}auditable", "{http://www.alfresco.org/model/application/1.0}uifacets", "{http://www.alfresco.org/model/system/1.0}referenceable" ], "nodeRef": "workspace://SpacesStore/56923743-7436-482e-b1cf-eda326d11dc2", "properties": { "{http://www.alfresco.org/model/application/1.0}icon": "space-icon-default", "{http://www.alfresco.org/model/content/1.0}description": "The company root space", "{http://www.alfresco.org/model/system/1.0}node-uuid": "56923743-7436-482e-b1cf-eda326d11dc2", "{http://www.alfresco.org/model/system/1.0}node-dbid": 15, "{http://www.alfresco.org/model/content/1.0}title": "Company Home", "{http://www.alfresco.org/model/content/1.0}created": "Wed Sep 10 09:16:32 CDT 2008", "{http://www.alfresco.org/model/content/1.0}modifier": "System", "{http://www.alfresco.org/model/content/1.0}modified": "Wed Sep 10 09:17:19 CDT 2008", "{http://www.alfresco.org/model/content/1.0}creator": "System", "{http://www.alfresco.org/model/system/1.0}store-protocol": "workspace", "{http://www.alfresco.org/model/content/1.0}name": "Company Home", "{http://www.alfresco.org/model/system/1.0}store-identifier": "SpacesStore" }, "mimetype": "application/octet-stream"}

Example #3: JSON

Page 24: Optaros Surf Code Camp Api

12/01/09 24

JavaScript

FreeMarker

// get a connector to the Alfresco Endpointvar connector = remote.connect(“alfresco”);

// retrieve a content objectvar nodeRef = “workspace://SpacesStore/56923743-7436-482e-b1cf-eda326d11dc2”;var data = connector.get(“/api/metadata?nodeRef=” + nodeRef);

// create a javascript object from the jsonvar json = eval('(' + data + ')');

// populate the modelmodel.id = json.nodeRef;model.name = json.properties[“{http://www.alfresco.org/model/content/1.0}name“];

Object id: ${id}<br/>Object name: ${name}

Example #3: JSON

Page 25: Optaros Surf Code Camp Api

12/01/09 25

You can either use connectors directly or via a proxy

Alfresco Surf provides a proxy servlet that uses this

connector facility underneath the hood

http://labs3c:8580/sample/proxy/{endpointId}/{uri}

Proxy Services

Page 26: Optaros Surf Code Camp Api

12/01/09 26

http://labs3c:8580/sample/proxy/alfresco/index• Identifies the “alfresco” endpoint

• The base URI on the endpoint is “/alfresco/service”

• This proxies through to: http://labs3c:8080/alfresco/service/index

• User connector session state is stamped onto the proxied connection

Optimizations• No buffering

• Headers are captured and some retained if related to connector sessions

• Input and output streams flow through

Proxy Services

Page 27: Optaros Surf Code Camp Api

12/01/09 Optaros and Client confidential. All rights reserved. 27

Wrap-up

In this module, you learned...• Useful JavaScript objects• Surf remoting concepts• How & why you'd use Surf's proxy• How to retrieve and work with XML and JSON returned by scripts

in the Repository tier