Powered By Blogger

Thursday, May 26, 2011

Steps to use the WebService client in your Liferay portlet

1. Use the seperate tomcat instance to generate the stub classes from the
url
2. Create the portlet in your sdk
3. The lib folder of the portlet should have below jars
axis.jar
commons-discovery-0.2.jar
jaxrpc.jar
saaj.jar
wsdl4j-1.5.1.jar
4. Inside your portlet class we can write the logic to consume the service

package com.cignex;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.model.UserSoap;
import com.liferay.portal.service.http.UserServiceSoap;
import com.liferay.portal.service.http.UserServiceSoapServiceLocator;

import java.io.IOException;
import java.net.URL;

import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

/**
* Portlet implementation class WebServiceConsumePortlet
*/
public class WebServiceConsumePortlet extends GenericPortlet {

public void init() {
editJSP = getInitParameter("edit-jsp");
helpJSP = getInitParameter("help-jsp");
viewJSP = getInitParameter("view-jsp");
}

public void doEdit(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {

include(editJSP, renderRequest, renderResponse);
}

public void doHelp(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {

include(helpJSP, renderRequest, renderResponse);
}

public void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {

System.out.println("Inside the doView mode************ service");


try {

System.out.println("service locator*************8");
UserServiceSoapServiceLocator locatorUser = new UserServiceSoapServiceLocator();
UserServiceSoap soapUser = locatorUser.getPortal_UserService(new URL("http://test:test@127.0.0.1:8080/tunnel-web/secure/axis/Portal_UserService"));

//http://abc:abc@127.0.0.1:8080/tunnel-web/secure/axis/Portal_UserService
System.out.println("Calling from portlet");
if(null!=soapUser){
System.out.println("Inside soapUser not null*********");
UserSoap soapUserModel = soapUser.getUserById(10134);
System.out.println(" getEmailAddress:" + soapUserModel.getEmailAddress());

}

} catch (Exception e) {
e.printStackTrace();
}


include(viewJSP, renderRequest, renderResponse);
}

protected void include(
String path, RenderRequest renderRequest,
RenderResponse renderResponse)
throws IOException, PortletException {

PortletRequestDispatcher portletRequestDispatcher =
getPortletContext().getRequestDispatcher(path);

if (portletRequestDispatcher == null) {
_log.error(path + " is not a valid include");
}
else {
portletRequestDispatcher.include(renderRequest, renderResponse);
}
}

protected String editJSP;
protected String helpJSP;
protected String viewJSP;

private static Log _log = LogFactoryUtil.getLog(WebServiceConsumePortlet.class);

}

3 comments:

  1. Hi Basu,

    I have a customer specific WSDL file using which i need to create a portlet to consume the WS.

    I did create Stubs using a vanilla tomcat as LR Tomcat doesnot support it.
    So can you please tell me, how to make use of the stubs that i created in my LR Portlet?

    ReplyDelete
  2. Hi Basu ! very impressive post..thnx.

    my manager had given me two tasks in Liferay portlet:

    1. to connect with mysql database in which the data entered by the user in liferay should persist in db.

    2. to consume a webservice from the portlet.

    fortunately got succeeded in the 1st part.
    but second part still remaining buddy.

    plz help me in this. if u can provide me step by step procedure to consume the wbservice..plz.

    also i am in bangalore only. if u can give me ur no. or catch up with me smtimes, it wud gr8 gesture from ur side.

    thnx and waiting for ur reply...

    ReplyDelete
  3. Hi,
    I am new to liferay .
    I am struck near invoking a sample web service from a liferay portlet.

    I have created initially a simple web service( to print Hello). Now i have to create stubs/clients of this particular web service inside my portlet.

    Can you help me on how to acheive this.?
    TIA.

    ReplyDelete