Java-backed controller

If the script and template are not sufficient enough to achieve the required functionality, you can have a Java Bean where you will have full control over the Alfresco Repository and also the Java API. With Web script, you can bind a Java Bean as a Spring Bean in Alfresco. When you have a Java bean, you can also have a combination of web Java script with it. The sequence of execution will be first Java script will be executed first and then Java bean if you have both used for any web script.

How to declare a Java Bean

To bind a Java Bean as a Spring Bean for any web script, you need to make a bean entry in the web-script-custom-context.xml file in the /WEB-INF/classes/alfresco/extension folder. Also, you have to follow a proper naming convention for that bean ID, which will be:

id="webscript.<packageId>.<serviceId>.<httpMethod>"

where packageId is the package in which the web script descriptor file is located, serviceId is the web script ID, and httpMethod can be GET, POST, PUT, or DELETE.

Also, the parent of this bean should be webscript while configuring this:

<bean id="webscript.org.cignex.news.getNewsHeadline.get"
class="com.cignex.web.scripts.bean.news.GetNewsHeadline"
parent="webscript" />

Creating a Java Bean class

When creating a Java Bean class, GetNewsHeadline, as in the previous section, this class must implement the org.alfresco.web.scripts.WebScript interface, which has the method:

public void execute(WebScriptRequest req, WebScriptResponse res)

You need to implement this method in your Java Bean class. There are two helper classes provided to simplify the development of a Java-backed web script: org.alfresco.web.scripts.AbstractWebScript and org.alfresco.web.scripts.DeclarativeWebScript.

Implementing web scripts

Implementation of a web script consists of mainly four parts. These are explained in the following sections.

Creating a web script

Create the required files for a web script. A description document and a rendering template are required. You can also have a controller script and other components described in the earlier sections of this chapter, if required.

Storing the web script

There are two ways of storing a web script in Alfresco. We will discuss both these methods:

Storing it on the filesystem

If you want to store it on the filesystem, you need to store it in the<<alfresco_server>>/tomcat/shared/extension/templates/webscripts folder. You can create the desired folder structure inside this folder and then put all of the web script files in there.

Storing it in Alfresco Explorer

You can store the web script files in the Company Home | Data Dictionary | Web Scripts Extensions page. Create the required folder structure under this path as shown in the next screenshot:

Storing it in Alfresco Explorer

Click on Create Space and provide a name for the folder you want to create and click on the Create Space button.

Storing it in Alfresco Explorer

Once done with the creation of folders, place the web script files, description document, rendering template, and controller script (if you have one) in that folder with the help of the Add Content option, as shown in the following screenshot:

Storing it in Alfresco Explorer

To upload the files, click on Add Content as shown in the previous screenshot. Then browse to the file on your local desktop and click on OK to upload that content to the specified space:

Storing it in Alfresco Explorer

Registering the web script

Once you are done with storing the required web script files, you need to register them in Alfresco. For registering the web scripts stored in Alfresco Explorer, navigate to http://localhost:8080/alfresco/service/index and click on the Refresh Web Scripts button, as shown in the following screenshot:

Registering the web script

You will see a message about the completion of maintenance of web scripts, which also specifies the number of web scripts:

Registering the web script

Click on the Refresh Web Scripts button. You will see a message showing how many web scripts have recently been found and registered.

While registering web scripts stored on the filesystem for the first time, you need to restart the Alfresco server. After the web script is registered, if you change something in the execution script or presentation template, you can click on the Refresh Web Scripts button on the index page of web scripts; there is no need to restart the server.

In this way, Alfresco supports Hot Deployment of web scripts.

Listing the web scripts for external access

Once the web script is registered, you can start using it with the URL. Navigate to http://localhost:8080/alfresco/service/index, and you will see the different browse options there. If you want to check it, you can click on any of the available browse options. Clicking on Browse by Web Script URI will browse all of the web scripts' URI. By doing so, you can verify your web script is there:

Listing the web scripts for external access
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset