Saturday, November 21, 2015

Consume Soap Web Service Using Eclipse Tool In Liferay Custom Portlet Plugin

Consume Soap Web Service Using JAX-WS  In Liferay Custom Portlet Plugin

STEPS:

Step 1: Create one liferay portlet plugin project, name: SoapClientUsing_ECLIPSE, after appending suffix full project name will be : SoapClientUsing_ECLIPSE-portlet
Create one portlet name: Soapconsumer inside created project.

Step 2: Import wsdl file using eclipse to generate required artifacts for consuming web service.Follow below screen.
SAMPLE WSDL:

http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?wsdl


Step 3: Paste below code in portlet controller, Soapconsumer.java
package com.test;


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

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.axis.client.Service;
import org.oorsprong.www.websamples_countryinfo.CountryInfoServiceSoapBindingStub;
import org.oorsprong.www.websamples_countryinfo.TCountryCodeAndName;
import org.oorsprong.www.websamples_countryinfo.TCountryInfo;

import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class Soapconsumer
 */
public class Soapconsumer extends MVCPortlet {
@Override
public void render(RenderRequest request, RenderResponse response)
throws PortletException, IOException {

Service service = new Service();
URL url = new URL("http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso");
CountryInfoServiceSoapBindingStub  stub = new CountryInfoServiceSoapBindingStub(url, service);

TCountryCodeAndName[] countriesCodeList =  stub.listOfCountryNamesByCode();

request.setAttribute("COUNTRIES_CODE_LIST", countriesCodeList);
super.render(request, response);
}


public void getCountryInfoByCountryCode(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {

System.out.println("inside getCountryInfoByCountryCode method...");

String countryCode = ParamUtil.getString(actionRequest, "countryCode");

Service service = new Service();
URL url = new URL("http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso");
CountryInfoServiceSoapBindingStub  stub = new CountryInfoServiceSoapBindingStub(url, service);

TCountryInfo countryFullInfo =  stub.fullCountryInfo(countryCode);
actionRequest.setAttribute("COUNTRY_FULL_INFO",countryFullInfo);
actionResponse.setRenderParameter("jspPage", "/html/soapconsumer/countryFullInfo.jsp");
}

}

Step 4: Paste below code in respective jsp's: view.jsp, countryFullInfo.jsp

view.jsp

<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="org.oorsprong.www.websamples_countryinfo.TCountryCodeAndName"%>
<%@page import="javax.portlet.PortletURL"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects />


<p style="border:1px solid green;">Soap web service Consumed using Eclipse</p>


<%
PortletURL getcountryInfoByCode = renderResponse.createActionURL();
getcountryInfoByCode.setParameter(actionRequest.ACTION_NAME, "getCountryInfoByCountryCode");

TCountryCodeAndName[] countriesCodeList = Validator.isNotNull((TCountryCodeAndName[])request.getAttribute("COUNTRIES_CODE_LIST")) ? (TCountryCodeAndName[])request.getAttribute("COUNTRIES_CODE_LIST") : null;    
%>

<form method="post" action="<%=getcountryInfoByCode.toString()%>">
   <select name='<portlet:namespace/>countryCode' id="countryCode">
    <option value="">Please Select Country Code</option>
   <%
     for(TCountryCodeAndName countryCode : countriesCodeList){
   %> 
       <option value="<%=countryCode.getSISOCode()%>"><%=countryCode.getSName()%></option>
   <%
     }
   %>
   </select>
   <input type="submit" value="GetCountryDetail"/>
</form>

countryFullInfo.jsp

<%@page import="org.oorsprong.www.websamples_countryinfo.TLanguage"%>
<%@page import="org.oorsprong.www.websamples_countryinfo.TCountryInfo"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="javax.portlet.PortletURL"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects />


<%

TCountryInfo countryFullInfo = Validator.isNotNull((TCountryInfo)request.getAttribute("COUNTRY_FULL_INFO")) ? (TCountryInfo)request.getAttribute("COUNTRY_FULL_INFO") : null;

TLanguage[] languages = Validator.isNotNull(countryFullInfo) ? countryFullInfo.getLanguages() : null; 
%>

<html>
<head>
<style>
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
th, td {
    padding: 5px;
}
</style>
</head>
<body>

<table style="width:100%">
  <tr>
    <th>Country Code</th>
    <th>Continent Code</th>
    <th>Country Name</th>
    <th>Country Capital</th>
    <th>Country Language</th>
    <th>Country Phone Code</th>
    <th>Country Flag</th>
  </tr>
  <tr>
    <td><%=countryFullInfo.getSISOCode()%></td>
    <td><%=countryFullInfo.getSContinentCode()%></td>
    <td><%=countryFullInfo.getSName()%></td>
    <td><%=countryFullInfo.getSCapitalCity()%></td>
    <td>
    <%
     for(TLanguage language : languages){
    %> 
       <%=language.getSName()%>
    <%}
    %>
    </td>
    <td><%=countryFullInfo.getSPhoneCode()%></td>
    <td><img src="<%=countryFullInfo.getSCountryFlag()%>"/></td>
  </tr>
</table>

</body>
</html>

OUTPUT: Deploy portlet and add on portal page.

1 comment: