Portlet Development

Document created by resplin Employee on Jun 6, 2015
Version 1Show Document
  • View in full screen mode

Obsolete Pages{{Obsolete}}

The official documentation is at: http://docs.alfresco.com




Developing Portlets


It is not uncommon for developers to want to integrate Alfresco into portals and similar platforms. What follows are some thoughts exploring possible approaches to portlet development.


Using the Web Script Container


Alfresco has de-coupled the Web Script Container from the core Alfresco application such that it can be deployed independently as a portlet into Liferay or other portals.

The basic process is documented the Alfresco Web Script Runtime Wiki page and more detailed instructions are being authored. The advantage of this approach is that any existing remote-enabled WebScript can be easily adapted to run as a portlet.


Using the CMIS Proposed Standard


Recently, Alfresco, IBM, Microsoft, Documentum and others announced the submission of a new content management standard proposal called 'CMIS' or 'Content Management Interoperability Service'. Alfresco has released an initial implementation of this proposed standard which includes support for REST-like RPC and SOAP-based web services. This provides the Liferay community with a cross-platform mechanism of integrating not just with Alfresco, but with any other content management system that supports the specification.

The Apache Adbdera library may be used to parse the ATOM-formated CMIS responses but here's a sample of a portlet that uses a basic XML parser to do the same:




package training;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletSecurityException;
import javax.portlet.PortletSession;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class AlfrescoTrainingPortlet extends GenericPortlet {

@Override
protected void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
  response.setContentType('text/html');
 
  HttpClient client = new HttpClient();
  client.getParams().setAuthenticationPreemptive(true);
  Credentials defaultcreds = new UsernamePasswordCredentials('admin', 'admin');
  client.getState().setCredentials(AuthScope.ANY, defaultcreds);
  String url;
 
  String objectIdParam = (String) request.getPortletSession().getAttribute('objectId', PortletSession.PORTLET_SCOPE);
  if (objectIdParam == null) {
   url = 'http://localhost:8080/alfresco/s/api/path/workspace/SpacesStore/Company%20Home/children';
  } else {
   url = 'http://localhost:8080/alfresco/s/api/node/workspace/SpacesStore/' + objectIdParam + '/children';
  }
 
  GetMethod method = new GetMethod(url);
  client.executeMethod(method);
 
  PortletURL actionURL = response.createActionURL();
 
  PrintWriter writer = response.getWriter();
  try {
   Document dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(method.getResponseBodyAsStream());
   NodeList list = dom.getElementsByTagName('cmis:propertyId');
   int len = list.getLength();
  
   for (int i = 0; i

' + objectId);
    }
    if (objectId == null) {
     continue;
    }
    NodeList stringList = ((Element) element.getParentNode()).getElementsByTagName('cmis:propertyString');
    int stringSize = stringList.getLength();
    for (int j = 0; j <a href='' + actionURL.toString() + ''>' + strElem.getTextContent() + '</a>');
      break;
     }
    }
   }
  } catch (Exception exc) {
   exc.printStackTrace();
  }
 
  //response.getWriter().println(method.getResponseBodyAsString());
}

@Override
public void processAction(ActionRequest request, ActionResponse response)
   throws PortletException, PortletSecurityException, IOException {
  String objectId = request.getParameter('objectId');
  if (objectId != null) {
   request.getPortletSession().setAttribute('objectId', objectId, PortletSession.PORTLET_SCOPE);
  }
}
}


Other REST APIs


In addition to the proposed CMIS standard, Alfresco has exposed a myriad of REST-like APIs for services such as workflow, tagging, thumbnailing, user management and more. These services are documented in the Alfresco 3.0 REST API wiki page.




Web Services


Some developers (like Gonzalo Garcia) have succesfully written their own API for communicating Liferay with Alfresco.

This method consists on creating an API which communicates with Alfresco, this API is independent from Liferay, you can use it in a portlet, a servlet or a normal java application and this API allows you to search, delete, send documents, etc.  to Alfreso through Web Services.

You can find all the libraries you need for communicating with Alfresco in the Alfresco SDK.

For example, you can search in a space (filtering by one property) and you obtain a list with all the documents found. Then, in your portlet you can work with that list as you prefer.

Some examples can be found at the following link: http://svn.alfresco.com/repos/alfresco-open-mirror/alfresco/HEAD/root/projects/sdk/samples/WebServiceSamples/source/org/alfresco/sample/webservice/

Some references from Alfresco can be found here: Alfresco Content Management Web Services


Attribution


I initially wrote most of this content on the Liferay Wiki but have republished and updated it here for convenience. The original article may be found at:
http://www.liferay.com/web/guest/community/wiki/-/wiki/Main/Alfresco+integration
CMIS
Integrations



Attachments

    Outcomes