Featured Post

Event Sourcing Video from Michael Ploed

Event Sourcing I want to share a great video I found few days ago that describes very well what Event Sourcing is.

Monday, April 30, 2012

Web Service with Apache CXF and OpenCMS

The exposing of Web Services from a CMS can be usefull to provide functionality towards external systems. Apache CXF implements JAX-WS API and helps to build services easily in custom applications or in software product as OpenCMS.
The following steps describe how to expose web services using Apache CXF with the assumption that we have already created a module in OpenCMS in the right manner from the console of administration of OpenCMS. Besides there should be configured an Export Point so that when you publish this module, compiled java files are copied to the path <OPENCMS-HOME>/WEB-INF/classes/
In this example you use the credentials of a specific user (username: John, password: Smith) to access the VFS (Virtual File System) of OpenCMS. In this post I don't want to speak of the best approach to access VFS of OpenCMS , but expose a possibile way to access it. Use a single user to access VFS is not a bad practice and giving him a role with particular grant may be a good strategy if your external system interacts with OpenCMS at the same way and you are not interested into different interactions with OpenCMS.

Step 1: Add CXFServlet to web.xml of OpenCMS

<web-app version="2.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">




Step 2: Create WebService inside your OpenCMS module

First of all we need to write an interface AccessResource that we annotate with @WebService. This interface rappresents the contract of the web service.

package my.service;

package my.service;

import javax.jws.WebService;

public interface AccessResource {
   public void accessResourceOpenCMS();

Afterwards we create the class AccessResourceImpl that implements the interface and then we annotate it with @WebService. We have to specify the attribute endpointInterface ( that describes the class providing the abstract contract of the web service) and the attribute serviceName.
As you can see, i get CmsObject using Guest user, but afterwards i make a login as a specific user: John Smith. This user has been created before in the administration console, providing him role and permission to perform the read of CmsResources.

package my.service;

import java.util.List;

import javax.jws.WebService;

import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.main.CmsException;
import org.opencms.main.OpenCms;

@WebService(endpointInterface="my.service.AccessResource",serviceName = "AccessResource")
public class AccessResourceImpl implements AccessResource{

 public void accessResourceOpenCMS() {
   try {

   CmsObject initCmsObject = OpenCms.initCmsObject("Guest");
   String loginUser = initCmsObject.loginUser("John", "Smith");
   List readResources = initCmsObject.readResources("/",CmsResourceFilter.ALL);
   for (CmsResource cmsr:readResources){
  } catch (CmsException e) {


Step 3: Create the configuration file cxf-servlet.xml

In the web.xml we have indicated the cxf-servlet.xml as the file to put the configurations of the web services. In this file it's declared the attribute serviceClass that defines the contract of the web service and the attribute address that defines where the web service it's exposed.

<beans xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans 
      http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd


  <jaxws:server address="/access_resource" id="accessResourceOpencms" serviceclass="my.service.AccessResource">
    <bean class="my.service.AccessResourceImpl">

Step 4: Put Apache CXF library into OpenCMS

Last step is to put Apache CXF libraries into <OPENCMS-HOME>/WEB-INF/lib/
Indipendently if you use JBoss or Tomcat to run OpenCMS , when your container is running, the web service is available at the address http://localhost:8080/opencms/services/access_resource and the WSDL at the address http://localhost:8080/opencms/services/access_resource?wsdl.

No comments :

Post a Comment