Friday, July 27, 2012

Creating sample Javabacked webscript

Webscript is widely used in alfresco customization or integration with third party portals to communicate.

Lets create java backed webscript with sample code.

For creating webscript we required following files :

1) Java class ( for java backed webscript which is action class)
2) Freemarker template
3) webscript bean entry in content xml file

we will create sample script as an example

1. CustomWebscript.java

public class CustomWebscript extends DeclarativeWebScript
{
@Override
protected Map<String, Object> executeImpl(final WebScriptRequest req, final Status status, final Cache cache) {
Map<String,Object> model;
model = new HashMap<String,Object>();
// final processing object should be placed in result
model.put("result","myfinalResultObject");
}
}

2. Descriptor and Freemarker

    customContent.get.desc.xml

<webscript>

  <shortname>Custom Contents</shortname>

  <description>Get Custom Contents</description>

  <url>/getCustomContents</url>

  <format default="json"></format>

  <authentication>user</authentication>

</webscript>


  customContent.get.json.ftl

{

"rows": 

[

<#if (contents??)>

 <#list result as agreement> 

{ 

"name":"${agreement.properties["cm:name"]!""}",

"description":"${agreement.properties["cm:description"]!""}",

"createdBy":"${agreement.properties["cm:creator"]!""}",

<#if (agreement.properties["cm:created"])??>

"createdDate":"${(agreement.properties["cm:created"])?string("yyyy-MM-dd")}"

<#else>

"createdDate":""

</#if>

} 

<#if agreement_has_next>,

</#if>

</#list>

 </#if>

]

}



custom-webscript-context.xml

<bean id="webscript.com.poc.custom.customContent.get" class="com.poc.webscript.CustomWebscript" parent="webscript">

<property name="serviceRegistry">

<ref bean="ServiceRegistry" />

</property>

</bean>

No comments:

Post a Comment