Liferay Portlet using Axis WebService client – Eclipse development

 

Create Liferay MVC Portlet

Follow the wizard to create Liferay portlet. Remember to check “create portlet class”.

Step 1) Create new Liferay portlet project

Use wizard to create new Liferay Portlet project. Make sure to choose Advanced project configuration… options on the first page and check Axis2:

Wizard creates the following project structure:

Step 2) Edit Java class

Sample Java class:

package accountList.liferay.hp.com;

 

import java.io.IOException;

import javax.portlet.PortletException;

import javax.portlet.RenderRequest;

import javax.portlet.RenderResponse;

import com.liferay.util.bridges.mvc.MVCPortlet;

 

/**

* Portlet implementation class AccountList

*/

public
class AccountList extends MVCPortlet {

 


/**

* @see MVCPortlet#MVCPortlet()

*/


public AccountList() {


super();

}

 


@Override


public
void doView(RenderRequest renderRequest, RenderResponse renderResponse)


throws IOException, PortletException {

String[] array = new String[] { “1”, “2”, “3” };

renderRequest.setAttribute(“myTable”, array);


super.doView(renderRequest, renderResponse);

}

}

Step 3) Edit JSP and configuration files

Add JSTL2 taglib

Add the following definitions to your JSP file

<%@ taglib uri=”http://java.sun.com/jsp/jstl/core&#8221; prefix=”c” %>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/fmt&#8221; prefix=”fmt” %>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/functions&#8221; prefix=”fn” %>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/sql&#8221; prefix=”sql” %>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/xml&#8221; prefix=”x” %>

Edit web.xml

Add the following references to \liferay-portal-6.0.6\tomcat-6.0.29\webapps\ROOT\WEB-INF\web.xml file. Check whether those references do not exist yet.

<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/tld/c.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/fmt</taglib-uri>
<taglib-location>/WEB-INF/tld/fmt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/functions</taglib-uri>
<taglib-location>/WEB-INF/tld/fn.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/sql</taglib-uri>
<taglib-location>/WEB-INF/tld/sql.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/xml</taglib-uri>
<taglib-location>/WEB-INF/tld/x.tld</taglib-location>
</taglib>

 

Edit  liferay-plugin-package.properties

This reference must be included in local file in the project.

portal-dependency-jars=jstl-api.jar,jstl-impl.jar

Add web service to your portlet (method 1)

Step 1) prepare and validate WSDL

I use SOAP-UI to validate WSDL. If SOAP-UI is able to generate project based on WSDL, hence the file and surrounding schemas are OK. Once validated OK, copy WSDL and surrounding XSD files to your project, e.g. into newly created \wsclient folder.

Step 2) generate client stub

Right-click on WSDL and select Web Services > Generate Client

Step 3) move generated sources

Move generated sources from /src to /docroot/WEB-INF/src directory

Step 4) Create and move JAR for XMLBeans

Axis2 is creating XMLBeans in \resources directory. In order to make them available to the run-time, do the following:

  1. Zip schemaorg_apache_xmlbeans
  2. Rename Zip to Jar
  3. Move schemaorg_apache_xmlbeans.jar to \docroot\WEB-INF\lib\ directory

After deployment, file will be available to the run-time environment.

Step 5) Call web service

Service call is managed by the following code in the portlet class:

xxxx.ICSOBPortStub stub = new xxxx.ICSOBPortStub();

 

try {

 

// initialize data objects

xxxx.LogonRequestDocument logonRequestDocument = LogonRequestDocument.Factory.newInstance();

xxxx.LogonResponseDocument logonResponseDocument = null;

xxxx.LogonRequestDocument.LogonRequest logonRequest = logonRequestDocument.addNewLogonRequest();

 


// set request parameters

logonRequest.setUserId(“md”);

logonRequest.setPassword(“111111”);

    
 

    // create request document

logonRequestDocument.setLogonRequest(logonRequest);

    
 

// execute service

    logonResponseDocument = stub.logon(logonRequestDocument);

    
 

    System.out.println(“Response : “ + logonResponseDocument.getLogonResponse());

 

} catch (java.lang.Exception e){

    System.out.println(“exception occured: “ + e.toString());

}