1. Consume Soap Web Service Using JAX-WS In Liferay Custom Portlet Plugin
2.
Consume Soap Web Service Using Eclipse Tool In Liferay Custom Portlet Plugin
1. Consume Soap Web Service Using JAX-WS In Liferay Custom Portlet Plugin
What is JAX-WS?
The Java API for XML Web Services (JAX-WS) is a Java programming language API for creating web services. JAX-WS is one of the Java XML programming APIs. It is part of the Java EE platform.
JAX-WS architecture is an easier-to-understand architecture for web services development.
We will create a client which communicates with the web service and displays the result.
STEPS:
Step 1: Create one portlet plugin project, name: SoapClientUsing_JAX-WS
After appending -portlet suffice, project full name in this example will be
SoapClientUsing_JAX-WS-portlet .
Now create one portlet inside this created project, name: Soapclient in this example.
Step 2: Now we will import wsdl in our project for generating artifacts for consuming service.
JAX-WS provides a tool called 'wsimport' for generating artifacts required for consuming web services. 'wsimport' takes a wsdl file as input.
I)Take project folder path , as shown below
In this example project folder path:
F:\WebSevice\liferay-plugins-sdk-6.2.0\portlets\SoapClientUsing_JAX-WS-portlet
II)Open command and go to above project path, as shown below
III)Now issue below wsimport command to generate required service artifacts.
wsimport -s docroot/WEB-INF/src -d docroot/WEB-INF/classes http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL
-s:
docroot/WEB-INF/src (Path of generated .java files
)
-d:
docroot/WEB-INF/classes (Path of generated .class files
)
wsdl:
http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?WSDL
Step 3: Paste below java code in portlet controller:
Soapclient.java
package com.test;
import java.io.IOException;
import java.util.List;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.oorsprong.websamples.ArrayOftCountryCodeAndName;
import org.oorsprong.websamples.CountryInfoService;
import org.oorsprong.websamples.CountryInfoServiceSoapType;
import org.oorsprong.websamples.TCountryCodeAndName;
import org.oorsprong.websamples.TCountryInfo;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
/**
* Portlet implementation class Soapclient
*/
public class Soapclient extends MVCPortlet {
@Override
public void render(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
CountryInfoService countryinfo = new CountryInfoService();
CountryInfoServiceSoapType stub = countryinfo.getCountryInfoServiceSoap();
ArrayOftCountryCodeAndName countriesArrayList = stub.listOfCountryNamesByCode();
List<TCountryCodeAndName> countriesList = countriesArrayList.getTCountryCodeAndName();
request.setAttribute("COUNTRIES_CODE_LIST", countriesList);
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");
CountryInfoService countryinfo = new CountryInfoService();
CountryInfoServiceSoapType stub = countryinfo.getCountryInfoServiceSoap();
TCountryInfo countryFullInfo = stub.fullCountryInfo(countryCode);
actionRequest.setAttribute("COUNTRY_FULL_INFO",countryFullInfo);
actionResponse.setRenderParameter("jspPage", "/html/soapwsclient/countryFullInfo.jsp");
}
}
Step 4: Paste below respective jsp's code:view.jsp, CountryFullInfo.jsp
view.jsp
<%@page import="java.util.List"%>
<%@page import="org.oorsprong.websamples.TCountryCodeAndName"%>
<%@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 />
<p style="border:1px solid green;">Soap Web Service Consumed using Jax-WS</p>
<%
PortletURL getcountryInfoByCode = renderResponse.createActionURL();
getcountryInfoByCode.setParameter(actionRequest.ACTION_NAME, "getCountryInfoByCountryCode");
List<TCountryCodeAndName> countriesCodeList = Validator.isNotNull((List<TCountryCodeAndName>)request.getAttribute("COUNTRIES_CODE_LIST")) ? (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="java.util.List"%>
<%@page import="org.oorsprong.websamples.ArrayOftLanguage"%>
<%@page import="org.oorsprong.websamples.TLanguage"%>
<%@page import="org.oorsprong.websamples.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;
ArrayOftLanguage arrayOfLanguages = Validator.isNotNull(countryFullInfo) ? countryFullInfo.getLanguages() : null;
List<TLanguage> tLanguage = Validator.isNotNull(arrayOfLanguages) ? arrayOfLanguages.getTLanguage() : 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 : tLanguage){
%>
<%=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.