ScalaServerPages

ScalaServerPages allow using scala to render a response resource to a particular output format.

Example

The following shows a simple ScalaServerPage:
//a ScalaServePage to render a http://clerezza.org/2009/05/usermanager#UserPermissionPage def um(s: Any) = new UriRef("http://clerezza.org/2009/05/usermanager#"+s) def perm(s: Any) = new UriRef("http://clerezza.org/2008/10/permission#"+s) <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Permissions for user {res/um("user")/FOAF.name}</title> </head> <body> {for (permission <- res/um("permission")) yield <div id="permission"> {permission/perm("javaPermissionEntry")*} </div> } </body> </html>
The following iterates over an rdf:List represented by the root-resource
//a ScalaServePage to render a http://clerezza.org/2009/05/renderletmanager#RenderletManagerPage def typerendering(s: Any) = new UriRef("http://clerezza.org/2009/04/typerendering#"+s) <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Listing renderlets</title> </head> <body> {for (renderlet <- res!!) yield <div id="renderlet"> type: {renderlet*} </div> } </body> </html>
You can sort rdf:List using the sort-method:
//sorting the URIs of a http://clerezza.org/2009/05/renderletmanager#RenderletManagerPage <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Listing renderlets</title> </head> <body> {for (renderlet <- (res!!).sort((a,b) => ((a*) < (b*)))) yield <div id="renderlet"> type: {renderlet*} </div> } </body> </html>
same for properties:
//a ScalaServePage to render a http://clerezza.org/2009/05/usermanager#UserPermissionPage //sorting by java-permission-entry def um(s: Any) = new UriRef("http://clerezza.org/2009/05/usermanager#"+s) def perm(s: Any) = new UriRef("http://clerezza.org/2008/10/permission#"+s) <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Permissions for user {res/um("user")/FOAF.name}</title> </head> <body> {for (permission <- (res/um("permission")) .sort((a,b) => (a/perm("javaPermissionEntry")*) < (b/perm("javaPermissionEntry")*))) yield <div id="permission"> {permission/perm("javaPermissionEntry")*} </div> } </body> </html>

How does it work

A ScalaServerPages is transformed into a Scala Source file and compiled. The content of the ScalaServerPage becomes the content of a method returning AnyRef, the returned Object will be transformed to a String and the to a byte-array to be written to the response stream

Avialable values

  • renderer: CallbackRenderer, used to delegate to another Renderlet usally not used directly but wia the render method
  • res: GraphNode, the main response resource, as a GraphNode it is dynamically converted to a RichGraphNode allowing the functions provided by org.apache.clerezza.utils.scala
  • val context:GraphNode, a GraphNode with contextual information not specifically related to the current request, such as description on the current user
  • val mode: String, the rendering mode
  • val uriInfo: UriInfo, the UriRinf of the request, allows access to the request URI and query parameters
  • val sharedRenderingValues: java.util.Map[String, Object], a map used to share values across the different renderlets and ScalaServerPages involved in the creation of a representation, typically used to prevent repeated computation of the same values. Typically this map is not accessed directly, instead values are retrived with $("key") and set with $("key") = newvalue

Accessing OSGi services

ScalaServerPages are used for rendering information, therefore services are typically accessed when producing the RDF and not from the ScalaServePage. Still you can access designated services from the ScalaServerPages, you get an instance with $[serviceInterface], eg.:
$[AdvertisingService].getBanner
You can only access services that are annotated with @org.apache.clerezza.platform.typerendering.WebRenderingService