0% found this document useful (0 votes)
151 views

Learning Alfresco Web Scripts Sample Chapter

Chapter No. 1 Getting Familiar with Web Scripts Learn how to successfully implement unique integration solutions with Alfresco

Uploaded by

Packt Publishing
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views

Learning Alfresco Web Scripts Sample Chapter

Chapter No. 1 Getting Familiar with Web Scripts Learn how to successfully implement unique integration solutions with Alfresco

Uploaded by

Packt Publishing
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Learning Alfresco Web Scripts

Ramesh Chauhan

Chapter No. 1
"Getting Familiar with Web Scripts"

In this package, you will find:


The authors biography
A preview chapter from the book, Chapter no.1 "Getting Familiar with Web Scripts"
A synopsis of the books content
Information on where to buy this book

About the Author


Ramesh Chauhan is presently working as a lead consultant at CIGNEX Datamatics.
He has core IT experience of around 9 years. Having strong expertise in Alfresco, he
has implemented and delivered customized business solutions in Alfresco for customers
across the globe and has extensively used Alfresco in multiple production projects. He
earned his Bachelor of Engineering degree in Information and Technology from Nirma
Institute of Technology, Gujarat University, India. He also contributes to Alfresco
community forums.

Acknowledgments
I would first like to extend my gratitude to Lord Swaminarayana and my guru, H. H.
Pramukh Swami Maharaj, for all the blessings. I would like to thank my mom and dad
for their unconditional love and support. Special thanks to my loving and caring wife,
Priyanka Chauhan, who has always encouraged me and supported me throughout the
journey of this book. Big thanks to my one-year-old daughter, Misri, who has been the
source of inspiration for me.

For More Information:


www.packtpub.com/big-data-and-business-intelligence/learning-alfresco-webscripts

I would like to thank Packt Publishing for this opportunity. I would like to thank all the
technical reviewers for their valuable feedback and give my thanks to Kevin Colaco,
Priyanka Shah, and Nikhil Potdukhe from the Packt Publishing team. It was really nice
working with the entire Packt Publishing team. I sincerely appreciate Priyanka Shah,
the content development editor, and Nikhil Potdukhe, the technical editor, who helped
a lot in fixing my writing style with their valuable feedback.
I would like to thank our management team at CIGNEX Datamatics for encouraging me
to write this book as a part of the community contribution from our organization. My
sincere thanks to Mr. Munwar Shariff (CTO, CIGNEX Datamatics) for all his support.
I would also like to thank the entire Alfresco practice team at CIGNEX Datamatics for
being very supportive. Thanks to Aadit Majmudar for helping me whenever needed
while writing this book. I would like to thank Dhaval Joshi especially, who has been
my very good friend and colleague at CIGNEX Datamatics. He has always been with
me whenever I needed his help and input. Thanks to him and my friend, Chirayu Joshi,
for believing in me, supporting me, and motivating me to write this book.

For More Information:


www.packtpub.com/big-data-and-business-intelligence/learning-alfresco-webscripts

Learning Alfresco Web Scripts


The first thought that came to my mind when I got the opportunity to write this book was
whether I will be able to take this up or not, as implementing web scripts in projects is
completely different from writing a book on it. However, at the same time, I was curious
and excited as well to write my first book. What I always believe isif you think you can
do it, then just go for it and give your best try to achieve it. Hence, I took this opportunity
and started my new journey of writing my first book to share my knowledge, learning,
and experience with you.
To give you a brief account of the book, web scripts is one of the key features of
Alfresco and is a must know for each and every developer working on Alfresco. It is
really essential for a developer working on Alfresco to be familiar with the powerful
web script framework and understand how to practically implement web scripts. In this
book, you will get to learn all the basic details required to work with web scripts in
Alfresco and get yourself competent with web script development for your projects to
implement useful integration solutions with Alfresco.
A few years back, I was also a beginner. Now, I have vast experience of using web
scripts in different real-time business implementations. Based on my learning and
experience from my journey as a beginner to my current level of expertise in regards to
implementing web scripts, I have tried to provide the most useful information that will
help a lot to new or experienced developers who are looking forward to exploring and
learning about web scripts.
I have attempted to share the knowledge and learning that I have gained throughout my
experience working on Alfresco web scripts. I have tried to explain the key concepts
about web scripts in a simple way and have used practical examples to understand how to
implement web scripts. I have also gone deep into technical details where I felt it would
be required to help you learn about the web script framework in a better way.
By the end of this book, you will be able to say that you are familiar with web scripts and
are ready to take up new assignments to develop custom web scripts as you have gained
the required knowledge to practically implement them in Alfresco. You will also be able
to debug, troubleshoot, and fi x the issues that you might come across while working on
web scripts. Also, you will be able to share the core technical knowledge about the
powerful web script framework with your colleagues.
My journey of writing this book has reached its destination as the book is now available
in your hand. Now, you are about to begin your journey of reading this book and learning
about Alfresco web scripts. Hope you have a wonderful journey ahead!

For More Information:


www.packtpub.com/big-data-and-business-intelligence/learning-alfresco-webscripts

What This Book Covers


Chapter 1, Getting Familiar with Web Scripts, introduces you to web scripts by
exploring answers to some of the basic questions about web scripts, which will give you a
clear idea about how web scripts can be useful for implementing integration solutions
with Alfresco.
Chapter 2, It's Time for the First Web Script, walks you through implementing your first
web script in Alfresco, which will give you the knowledge required about the core
fundamentals along with some key points to take care of when practically implementing
web scripts in your projects.
Chapter 3, Understanding the Web Script Framework, covers the powerful web script
framework, which will make you well acquainted with the detailed technical knowledge
of web script execution and the backbone pillars of the web script framework.
Chapter 4, Building Blocks of Web Scripts, provides you complete details about all the
components to build web scripts.
Chapter 5, Invoking Web Scripts, describes the various ways you can execute a web
script that will help you to choose the way to execute web scripts as required.
Chapter 6, Creating Java-backed Web Scripts, helps you understand how to practically
implement Java-backed web scripts through the implementation of a sample use case.
Chapter 7, Understanding JavaScript-based Web Scripts in Detail, explains what you can
do with JavaScript APIs in Alfresco web scripts and makes you aware about the different
root objects to access these APIs and useful root objects exposed by the web script
framework. It also takes you through some code examples for some of the common
functionalities in JavaScript-based web scripts.
Chapter 8, Deployment, Debugging, and Troubleshooting Web Scripts, guides you
through the options to deploy web scripts in detail and also walks you through some
useful debugging techniques and troubleshooting pointers along with some important
points to execute web scripts in a production environment.
Chapter 9, Mavenizing Web Scripts, gives you basic knowledge of setting up the
development environment in Eclipse using the Alfresco Maven SDK to develop
web scripts.
Chapter 10, Extending the Web Script Framework, familiarizes you with the possibility
of extending the core components of the web script framework in Alfresco to help you
understand the power of working in an open source technology.

For More Information:


www.packtpub.com/big-data-and-business-intelligence/learning-alfresco-webscripts

Getting Familiar with


Web Scripts
In this chapter, we will cover the following topics:

Understanding web scripts

Reasons to use web scripts

Understanding when to use a web script

Understanding where web scripts can be used

Understanding how web scripts work

Types of web scripts

For More Information:


www.packtpub.com/big-data-and-business-intelligence/learning-alfresco-webscripts

Getting Familiar with Web Scripts

Consider a scenario where you have to work on a project to develop a customized


business solution with a backend repository such as an Alfresco content management
system. It is going to be an integration project with the Alfresco content repository.
The end goal is to build up a business implementation that has a custom frontend
application interacting with the Alfresco repository in a secure way in order to allow
the end users to access the content on demand and to provide access to features of
the Alfresco repository. Basically, you want to create, update, and delete content
in the Alfresco repository and are interested in retrieving the required content on
demand from the Alfresco repository from the custom frontend application in a
secure way.
Now, you might have a lot of questions on your mind. You must be curious to find
out how it is possible to access the Alfresco repository from the custom frontend
application. What are the possible alternatives to access the content residing in the
Alfresco repository from the custom frontend application? Is it going to be a complex
implementation to build up an API accessing the Alfresco repository? Is it going to
be a secure communication from the frontend application to the backend repository?
How is it going to maintain the client state during the communication with the
Alfresco repository in order to access, update, or delete content from the repository
based on the user's access? And the list of questions still goes on
Whenever there is a question, there is an answer. Also, the answer to all of your
previous questions is Alfresco web scripts. Using web scripts, you can build up a
customized integration solution using Alfresco as the backend content repository.
It is always a good idea when we start learning about any new topic to always start
with the "5Ws and 1H". It's basically the who, what, when, why, where, and how
questions. This helps us to understand the topic in a better way. In this chapter, we are
going to find out the useful information about web scripts by discovering the answers
to some of the basic questions such as what, why, when, where, and how. Let's start
and find out the answers to them and understand web scripts in a better way.

Understanding web scripts


In order to understand web scripts in Alfresco, let's begin with finding out the
answer to the question "What is a web script?" from multiple perspectives.
In simple words, web scripts can be explained as follows:

Web scripts are powerful and extremely useful services supported


by Alfresco

They are a way to interact with the Alfresco repository securely

[8]

For More Information:


www.packtpub.com/big-data-and-business-intelligence/learning-alfresco-webscripts

Chapter 1

They are reusable across different platforms

Web scripts provide uniform access of the content to a wide range of


client applications

In technical terms, web scripts can be defined as follows:

They are RESTful web services


What is RESTful?
REST stands for REpresentational State Transfer, which is basically
an architectural style. Well-defined and uniform access to the
resources through HTTP request methods, uniquely identified
resources through the URI, and representation of these resources
are the key principles of REST. Any implementation following this
architectural style is known as RESTful.

They are bound to a specific Uniform Resource Identifier (URI)

They respond to HTTP methods such as GET, POST, PUT, and DELETE

They are a lightweight implementation

From a developer's point of view, web scripts have the following properties:

They are easy to understand and learn

They are easy to develop

They are easy to debug

They are easy to maintain

They are easy to deploy

They are faster to implement

They would be the first choice when it comes to accessing the Alfresco
repository securely from external applications

From a business user's point of view, web scripts are useful as follows:

They bridge the gap between the business requirement and technical
implementation for building up business solutions with Alfresco

They empower Alfresco in its integration capabilities to develop useful


Alfresco integration solutions with external systems

They are a unique way to implement the integration solutions on top of the
Alfresco repository

They are the backbone of the integration solution implementation with the
Alfresco repository
[9]

For More Information:


www.packtpub.com/big-data-and-business-intelligence/learning-alfresco-webscripts

Getting Familiar with Web Scripts

In essence, web scripts can be talked about as follows:

Web scripts are built on the idea of URL addressability

Web scripts are simply a service, mapped to a human-readable form, and


developed using a piece of code as their backend implementation

For example, in a contract management system built using Alfresco as


the backend repository, you can have a web script to retrieve all the draft
agreements from the repository and then display them on the custom frontend
application. This repository web script to get all the draft agreements will
simply be accessed through its URL. This web script will also have a piece
of code in the backend to retrieve the data from the Alfresco repository to
produce the results in the formats as required. The URL for this web script
might look like the following:
/alfresco/service/contract/get_draft_agreements

Reasons to use web scripts


It's now time to discover the answer to the next questionwhy web scripts? There
are various alternate approaches available to interact with the Alfresco repository,
such as CMIS, SOAP-based web services, and web scripts. Generally, web scripts are
always chosen as a preferred option among developers and architects when it comes
to interacting with the Alfresco repository from an external application. Let's take
a look at the various reasons behind choosing a web script as an option instead of
CMIS and SOAP-based web services.
In comparison with CMIS, web scripts are explained as follows:

In general, CMIS is a generic implementation, and it basically provides


a common set of services to interact with any content repository. It does
not attempt to incorporate the services that expose all features of each and
every content repository. It basically tries to cover a basic common set of
functionalities for interacting with any content repository and provide the
services to access such functionalities.

Alfresco provides an implementation of CMIS for interacting with the


Alfresco repository. Having a common set of repository functionalities
exposed using CMIS implementation, it may be possible that sometimes
CMIS will not do everything that you are aiming to do when working with
the Alfresco repository. While with web scripts, it will be possible to do the
things you are planning to implement and access the Alfresco repository
as required. Hence, one of the best alternatives is to use Alfresco web
scripts in this case and develop custom APIs as required, using the
Alfresco web scripts.
[ 10 ]

For More Information:


www.packtpub.com/big-data-and-business-intelligence/learning-alfresco-webscripts

Chapter 1

Another important thing to note is, with the transaction support of web
scripts, it is possible to perform a set of operations together in a web script,
whereas in CMIS, there is a limitation for the transaction usage. It is possible
to execute each operation individually, but it is not possible to execute a set
of operations together in a single transaction as possible in web scripts.

SOAP-based web services are not preferable for the following reasons:

It takes a long time to develop them

They depend on SOAP

Heavier client-side requirements

They need to maintain the resource directory

Scalability is a challenge

They only support XML

In comparison, web scripts have the following properties:

There are no complex specifications

There is no dependency on SOAP

There is no need to maintain the resource directory

They are more scalable as there is no need to maintain session state

They are a lightweight implementation

They are simple and easy to develop

They support multiple formats

In a developer's opinion:

They can be easily developed using any text editor

No compilations required when using scripting language

No need for server restarts when using scripting language

No complex installations required

In essence:

Web scripts are a REST-based and powerful option to interact with the
Alfresco repository in comparison to the traditional SOAP-based web
services and CMIS alternatives

They provide RESTful access to the content residing in the Alfresco


repository and provide uniform access to a wide range of client applications

[ 11 ]

For More Information:


www.packtpub.com/big-data-and-business-intelligence/learning-alfresco-webscripts

Getting Familiar with Web Scripts

They are easy to develop and provide some of the most useful features such
as no server restart, no compilations, no complex installations, and no need
of a specific tool to develop them

All these points make web scripts the most preferred choice among developers
and architects when it comes to interacting with the Alfresco repository

Understanding when to use a web script


Having understood what a web script is and the reasons for using web scripts,
let's now understand when to use web scripts. When working with Alfresco, it is
important to know in which scenarios web scripts can be used. Web scripts allow
you to build custom URI-identified and HTTP-accessible Content Management
Web services to access the Alfresco content repository, and hence they can be used
in various kinds of implementations to build useful business solutions with Alfresco
content management systems.
In development projects:

You can use web scripts to develop business implementations integrating


Alfresco with external applications

You can develop customized web scripts as per your business requirement

You can leverage on the out-of-the-box web scripts available in Alfresco


wherever they fit in your custom implementation

In support projects:

Web scripts could be your savior when you are working on a support or
maintenance project to maintain the live system for your customers that
is built on Alfresco and has millions of records.

You might come across a range of issues, for example, updating existing
content to fix some data issues, deleting some unneeded content, getting the
required content matching with the criteria specified by the business team,
and so on. Web scripts can be used in such scenarios to serve these purposes.

On a live production system, it is not possible to restart the server, and


restarting a server could impact the business of a customer. Consider a
scenario where you need to fix some critical issues on a production system
and you are looking for an option to have the issue fixed without restarting
the server. The powerful feature of web scripts to interact with the Alfresco
repository without restarting the server will help you in this case, and you
can fix some critical issues using web scripts to provide a good content
management experience to the business users.
[ 12 ]

For More Information:


www.packtpub.com/big-data-and-business-intelligence/learning-alfresco-webscripts

Chapter 1

In general, web scripts can be used in all kinds of solutions, such as:

Integrating Alfresco with external applications that can communicate with


Alfresco using HTTP

Developing JSR-168 portlets to build up UI services

Developing data services

Providing feeds for repository content

Alfresco integration with Office

Developing Facebook applications

Building UI components in Alfresco SURF

Understanding where web scripts can be


used
Now, it's time to understand where web scripts can be used. There are a number of
different environments from where a web script can be accessed and used.
In different environments:

They can be used from an HTTP client such as a web browser and HTTP
client APIs

It is also possible to access web scripts in JSF pages

They can be used in JSR-168 portals

They can be used from Facebook applications

Web scripts can be used in the Alfresco SURF platform as well

Understanding how web scripts work


Web scripts in Alfresco mostly use the model-view-controller pattern. However, it
isn't mandatory to follow this pattern all the time. The way MVC pattern works in
web scripts is as follows:

The controller is responsible for performing the required business


logic as per the business requirement. After processing the business logic,
controller populates the model object with the required data. Once this is
done, controller will then forward the request to the view.

[ 13 ]

For More Information:


www.packtpub.com/big-data-and-business-intelligence/learning-alfresco-webscripts

Getting Familiar with Web Scripts

MVC in web scripts


The model-view-controller pattern in web scripts is explained as follows:

The controller is a server-side JavaScript or Java class or it could be both


as well

The model is a data structure object passed between the controller and view

The view is a FreeMarker template that is responsible for generating the


response in the required format

The mapping of the web script URI to the controller is done through a descriptor file.
It is mainly an XML file that will have the required details for a web script such as
URL, description, arguments, transaction, authentication, and response formats.
Response formats are mapped to FreeMarker templates through naming conventions.
For example, a FreeMarker template that returns an HTML response will have the
extension html.ftl.
Web scripts are registered and executed by the web script engine in Alfresco.
A descriptor, optional controller, and one or more FreeMarker
response templates collectively make a web script in general scenarios.
All these components are tied together through a specific document
naming convention.

Making web scripts work


To make web scripts work, it is important to deploy the web script files at an
appropriate location.
The descriptor file and the FreeMarker template as per the response format for a web
script can be either placed on a filesystem or put into the Alfresco repository.
If a JavaScript-based controller is used for a web script, then it could be placed
along with the descriptor and FreeMarker template on a filesystem or in the
Alfresco repository.

[ 14 ]

For More Information:


www.packtpub.com/big-data-and-business-intelligence/learning-alfresco-webscripts

Chapter 1

If a Java-based controller is used for a web script, then the class file for the controller
must be available on the class path.
There are two types of controllers available for web scripts:
JavaScript-based controllers and Java-based controllers. It is
possible to have none of them, either of them, or even both
of them for a web script.

Types of web scripts


There are two types of web script in Alfresco: Data web scripts and Presentation
web scripts.

Data web script


Data web scripts provide an interface to the repository for the client applications
to create, retrieve, update, and delete content/data in the repository. These web
scripts typically send the response in formats such as XML and JSON, and client
applications will have to parse it in order to use it further.
Data web scripts encapsulate access and modification of content/data residing in the
repository. These web scripts are provided and exposed by the Alfresco repository
server only.

Presentation web script


Presentation web scripts can be used to build user interfaces such as dashlets
for Alfresco explorer, dashlets for Alfresco share, portlets for a JSR-168 portal,
a UI component within Alfresco SURF, or a custom application.
These kinds of web scripts generally render HTML responses.
It is possible to host these web scripts on a separate presentation server or they can
also be exposed by the Alfresco repository server.

[ 15 ]

For More Information:


www.packtpub.com/big-data-and-business-intelligence/learning-alfresco-webscripts

Getting Familiar with Web Scripts

Presentation web scripts generally make a call to Data web scripts in order to get the
required data from the repository.
Out-of-the-box web scripts available in an Alfresco installation can
be found at ALFRESCO_HOME\tomcat\webapps\alfresco\
WEB-INF\classes\alfresco\templates\webscripts\org\
alfresco, where ALFRESCO_HOME is the base directory where
Alfresco is installed.
If you are using Alfresco Community 5, you can find out-of-thebox web scripts under the alfresco package inside alfrescoremote-api-*.jar, which is available at ALFRESCO_HOME\
tomcat\webapps\alfresco\WEB-INF\lib.

Summary
In this chapter, we gained a better understanding of web scripts through some
of the basic questions about web scripts. We learned what web scripts in Alfresco
are, why they are the preferred choice of developers, when and where we can use
web scripts, how web scripts work, and also the different types of web scripts
available in Alfresco.
In the next chapter, we are going to do a hands-on exercise to learn how to
implement our first web script in Alfresco.

[ 16 ]

For More Information:


www.packtpub.com/big-data-and-business-intelligence/learning-alfresco-webscripts

Where to buy this book


You can buy Learning Alfresco Web Scripts from the Packt Publishing website:
.
Free shipping to the US, UK, Europe and selected Asian countries. For more information, please
read our shipping policy.

Alternatively, you can buy the book from Amazon, BN.com, Computer Manuals and
most internet book retailers.

www.PacktPub.com

For More Information:


www.packtpub.com/big-data-and-business-intelligence/learning-alfresco-webscripts

You might also like