Saturday, November 21, 2015

Expose Restful Web Service From Liferay Custom Portlet Plugin

Exposing soap web service for a custom plugin

STEPS:

Step 1: Create one portlet plugin project, name: RestExposer, after appending suffix of portlet plugin project full name of project will be RestExposer-portlet.
Create one portlet name: Restexpose, inside created project.

Step 2:Create service.xml,define entity  as shown in below screen.
In service defintion don't forget to make remote-service="true",
Once we  make remote-service="true" in service.xml and after building service we will get EmployeeManageServiceImpl.java


Now build service.

Step 3: Paste below code in view.jsp

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@page import="javax.portlet.PortletURL"%>
<portlet:defineObjects />


<p style="border:1px solid green;">ADD Employee Details</p>

<%
PortletURL addEmployeeDetailURL = renderResponse.createActionURL();
addEmployeeDetailURL.setParameter(actionRequest.ACTION_NAME,"addEmployeeDetail");
%>

 <form action="<%=addEmployeeDetailURL.toString()%>"  method="POST">
<b>Employee First Name</b><br/>
<input  type="text" name="<portlet:namespace/>employeeFirstName" /><br/>
<b>Employee Last Name</b><br/>
<input  type="text" name="<portlet:namespace/>employeeLastName" /><br/>
<b>Employee Age</b><br/>
<input  type="text" name="<portlet:namespace/>employeeAge" /><br/>
<b>Employee Number</b><br/>
<input  type="text" name="<portlet:namespace/>employeeNumber" /><br/>
<br/>
<input type="submit" value="AddEmployeeDetails"/>
</form>

Step 4: Paste below code in portlet controller: Exposer.java

package com.test;

import java.io.IOException;

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

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.slayer.model.EmployeeManage;
import com.slayer.model.impl.EmployeeManageImpl;
import com.slayer.service.EmployeeManageLocalServiceUtil;

/**
 * Portlet implementation class Exposer
 */
public class Exposer extends MVCPortlet {

//Adding employee details
public void addEmployeeDetail(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
       
System.out.println("inside addEmployeeDetail method...");
String employeeFirstName = ParamUtil.getString(actionRequest, "employeeFirstName");
String employeeLastName = ParamUtil.getString(actionRequest, "employeeLastName");
int employeeAge = ParamUtil.getInteger(actionRequest, "employeeAge");
String employeeNumber = ParamUtil.getString(actionRequest, "employeeNumber");
//Instanciate an empty object of type EmployeeManageImpl
EmployeeManage employeeManage = new EmployeeManageImpl();
//Generate a unique primary key to be set
long employeeId = 01;
try {
employeeId = CounterLocalServiceUtil.increment();
} catch (SystemException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//set the field for this object
employeeManage.setEmployeeId(employeeId);
employeeManage.setEmpFirstName(employeeFirstName);
employeeManage.setEmpLastName(employeeLastName);
employeeManage.setEmpAge(employeeAge);
employeeManage.setEmpNumber(employeeNumber);
//called service layer API to save the object
try {
EmployeeManageLocalServiceUtil.addEmployeeManage(employeeManage);
System.out.println("New employee detail added successfully...");
actionResponse.setRenderParameter("jspPage", "/html/restexpose/view.jsp");
} catch (SystemException e) {
System.out.println("something happened wrong while adding new employee detail");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Step 5: Paste below java code in EmployeeManageServiceImpl.java and then build service once again.
/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.slayer.service.impl;

import java.util.List;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.slayer.model.EmployeeManage;
import com.slayer.service.EmployeeManageLocalService;
import com.slayer.service.EmployeeManageLocalServiceUtil;
import com.slayer.service.base.EmployeeManageServiceBaseImpl;

/**
 * The implementation of the employee manage remote service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.slayer.service.EmployeeManageService} interface.
 *
 * <p>
 * This is a remote service. Methods of this service are expected to have security checks based on the propagated JAAS credentials because this service can be accessed remotely.
 * </p>
 *
 * @author samsun
 * @see com.slayer.service.base.EmployeeManageServiceBaseImpl
 * @see com.slayer.service.EmployeeManageServiceUtil
 */
public class EmployeeManageServiceImpl extends EmployeeManageServiceBaseImpl {
/*
 * NOTE FOR DEVELOPERS:
 *
 * Never reference this interface directly. Always use {@link com.slayer.service.EmployeeManageServiceUtil} to access the employee manage remote service.
 */
public List<EmployeeManage>  getlistOfEmployee()
{
List<EmployeeManage> employeeList = null;
try {
employeeList = employeeManageLocalService.getEmployeeManages(-1, -1);
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return employeeList; 
}
public EmployeeManage getEmployeeDetailBasedOnEmpId(long employeeId){
EmployeeManage employeeDetails = null;
try {
employeeDetails = employeeManageLocalService.getEmployeeManage(employeeId);
} catch (PortalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return employeeDetails;
}
}

Step 6: Now by using build wsdd we will expose this service to outside world. By doing this a new file 'server-config.wsdd' will get generate inside WEB-INF. This is the web service deployment descriptor that contains information about the web services that we are exposing.
Follow below screen.

Step 7: Now deploy portlet and add on portal page for adding employee data.
As shown in below screen.



Step 8: hit below url on browser, follow below screen                                                                             http://localhost:8080/RestExposer-portlet/api/jsonws                              















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.

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


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.


Expose Soap Web Service From Liferay Custom Portlet Plugin

Exposing soap web service for a custom plugin

STEPS:

Step 1: Create one portlet plugin project, name: SoapExposer, after appending suffix of portlet plugin project full name of project will be SoapExposer-portlet.
Create one portlet name: Exposer, inside created project.

Step 2:Create service.xml,define entity  as shown in below screen.
In service defintion don't forget to make remote-service="true",
Once we  make remote-service="true" in service.xml and after building service we will get EmployeeManageServiceImpl.java


Now build service.

Step 3: Paste below code in view.jsp

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@page import="javax.portlet.PortletURL"%>
<portlet:defineObjects />


<p style="border:1px solid green;">ADD Employee Details</p>

<%
PortletURL addEmployeeDetailURL = renderResponse.createActionURL();
addEmployeeDetailURL.setParameter(actionRequest.ACTION_NAME,"addEmployeeDetail");
%>

 <form action="<%=addEmployeeDetailURL.toString()%>"  method="POST">
<b>Employee First Name</b><br/>
<input  type="text" name="<portlet:namespace/>employeeFirstName" /><br/>
<b>Employee Last Name</b><br/>
<input  type="text" name="<portlet:namespace/>employeeLastName" /><br/>
<b>Employee Age</b><br/>
<input  type="text" name="<portlet:namespace/>employeeAge" /><br/>
<b>Employee Number</b><br/>
<input  type="text" name="<portlet:namespace/>employeeNumber" /><br/>
<br/>
<input type="submit" value="AddEmployeeDetails"/>
</form>

Step 4: Paste below code in portlet controller: Exposer.java

package com.test;

import java.io.IOException;

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

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.slayer.model.EmployeeManage;
import com.slayer.model.impl.EmployeeManageImpl;
import com.slayer.service.EmployeeManageLocalServiceUtil;

/**
 * Portlet implementation class Exposer
 */
public class Exposer extends MVCPortlet {

//Adding employee details
public void addEmployeeDetail(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
       
System.out.println("inside addEmployeeDetail method...");
String employeeFirstName = ParamUtil.getString(actionRequest, "employeeFirstName");
String employeeLastName = ParamUtil.getString(actionRequest, "employeeLastName");
int employeeAge = ParamUtil.getInteger(actionRequest, "employeeAge");
String employeeNumber = ParamUtil.getString(actionRequest, "employeeNumber");
//Instanciate an empty object of type EmployeeManageImpl
EmployeeManage employeeManage = new EmployeeManageImpl();
//Generate a unique primary key to be set
long employeeId = 01;
try {
employeeId = CounterLocalServiceUtil.increment();
} catch (SystemException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//set the field for this object
employeeManage.setEmployeeId(employeeId);
employeeManage.setEmpFirstName(employeeFirstName);
employeeManage.setEmpLastName(employeeLastName);
employeeManage.setEmpAge(employeeAge);
employeeManage.setEmpNumber(employeeNumber);
//called service layer API to save the object
try {
EmployeeManageLocalServiceUtil.addEmployeeManage(employeeManage);
System.out.println("New employee detail added successfully...");
actionResponse.setRenderParameter("jspPage", "/html/exposer/view.jsp");
} catch (SystemException e) {
System.out.println("something happened wrong while adding new employee detail");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Step 5: Paste below java code in EmployeeManageServiceImpl.java and then build service once again.
/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.slayer.service.impl;

import java.util.List;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.slayer.model.EmployeeManage;
import com.slayer.service.EmployeeManageLocalService;
import com.slayer.service.EmployeeManageLocalServiceUtil;
import com.slayer.service.base.EmployeeManageServiceBaseImpl;

/**
 * The implementation of the employee manage remote service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.slayer.service.EmployeeManageService} interface.
 *
 * <p>
 * This is a remote service. Methods of this service are expected to have security checks based on the propagated JAAS credentials because this service can be accessed remotely.
 * </p>
 *
 * @author samsun
 * @see com.slayer.service.base.EmployeeManageServiceBaseImpl
 * @see com.slayer.service.EmployeeManageServiceUtil
 */
public class EmployeeManageServiceImpl extends EmployeeManageServiceBaseImpl {
/*
* NOTE FOR DEVELOPERS:
*
* Never reference this interface directly. Always use {@link com.slayer.service.EmployeeManageServiceUtil} to access the employee manage remote service.
*/
public List<EmployeeManage>  getlistOfEmployee()
{
List<EmployeeManage> employeeList = null;
try {
employeeList = employeeManageLocalService.getEmployeeManages(-1, -1);
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return employeeList; 
}
public EmployeeManage getEmployeeDetailBasedOnEmpId(long employeeId){
EmployeeManage employeeDetails = null;
try {
employeeDetails = employeeManageLocalService.getEmployeeManage(employeeId);
} catch (PortalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return employeeDetails;
}
}

Step 6: Now by using build wsdd we will expose this service to outside world. By doing this a new file 'server-config.wsdd' will get generate inside WEB-INF. This is the web service deployment descriptor that contains information about the web services that we are exposing.
Follow below screen.

Step 7: Now deploy portlet and add on portal page for adding employee data.
As shown in below screen.



Step 8: hit below url on browser, follow below screen                                                                                                           http://localhost:8080/SoapExposer-portlet/api/axis


Below is the soap wsdl generated file.













Sunday, November 15, 2015

How To Use Soap UI For Testing Web Service

SoapUI  Tool:

SoapUI is an open-source web service testing application for service-oriented architectures (SOA) and representational state transfers (REST). Its functionality covers web service inspection, invoking, development, simulation and mocking, functional testing, load and compliance testing

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

STEPS:

Step 1: Open SoapUI Tool, as shown in below screen


 Step 2: Click on File and create one new soap project, as shown in below screen


Step 3: Provide project name and wsdl url, as shown in below screen.
http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso?wsdl 

 Step 4: Newly created soap project will be shown in SoapUI Tool,
Step 5: Expanding created project CountryInfoService, we will get getCapitalCity method,
In getCapital city request by passing country code we will get capital of that country.
Result: Getting Delhi as capital after passing country code IN.



Web Services

Web Service Basics:
 
A web service is a way of calling a function which is inside a software from some other software. lets say there are two softwares- 1.Company management , 2.Employee management. Software one(Company management) gives a call to function which is inside software two(Employee management).The software which gives a call to a web service is called a client(Here client is software one-Company management). The software which gives a  service to the request as a response is called server(Here server is software two- Employee management). This two software(Company management and Employee management) need not to be written in same language. software one(Company management) can be written in java, software two(Employee management) can be written in dot net.  This softwares need not to be on the same machine. software one(Company management) can be on machine one , software two(Employee management) can be on machine two. But there should be a network(LAN,Internet) connecting these two softwares.
Lets say-
Company application gives a call to Employee application and says provide me (listOfEmployees()), Employee
application receives this call and response back sending (listofEmployees()).
In web service there are two things, server-(service provider/publisher/exposer) and client-(service consumer).
In above explained example Employee application is service provider/exposer and Company application is service consumer.
Web Service can be classified as:
1.    Soap Based Web Service
2.    Restful  Web  Service

Tuesday, November 10, 2015

send log messages to different log file

We often come across to situation where we need to spearate our log based on different scenario.
Here i will explain how to create another log file and use it for logging.
we can do this in two way either using xml or using properties. xml is better approach.

Using log4j.properties
cretae a log4j.properties file inside your resources folder of your project and paste below properties

# Root logger option
log4j.rootLogger=INFO, file

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender

#Redirect to Tomcat logs folder
#log4j.appender.file.File=${catalina.home}/logs/ekf.log C:\\EKF.log

log4j.appender.file.File=${catalina.home}/logs/Sample.log
log4j.appender.file.MaxFileSize=1024MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


As we have use org.apache.log4j so we need to add the respecive dependency in our pom.xml

               <dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>

</dependency>

Now all you to do is, write below code in class which logging message you want to send to Sample.log

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

final static Log logger = LogFactory.getLog(Test.class);

As in our properties we have defined INFO as rootLogger so when you use

logger.info("test your logger");

this inside your method log message will come under your Sample.log.

the time you will deploy your portlet that time itself it will create Sample.log file inside tomcat/log folder of your application.
And if you have use final static Log logger = LogFactory.getLog(Test.class); inside your class for logging all the log message will go to Sample.log

Using log4j.xml
cretae a log4j.xml file inside your resources folder of your project and paste below xml code

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>

<appender name="file" class="org.apache.log4j.RollingFileAppender">
  <param name="append" value="false" />
  <param name="maxFileSize" value="10KB" />
  <param name="maxBackupIndex" value="5" />
  <!-- For Tomcat -->
  <param name="file" value="${catalina.home}/logs/XMLBaseSample.log" />
  <layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" 
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
  </layout>
</appender>

<root>
<level value="INFO" />
<appender-ref ref="file" />
</root>

</log4j:configuration>

add below dependency in your pom.xml

        <dependency>
<groupId>log4j</groupId>
               <artifactId>log4j</artifactId>
               <version>1.2.16</version>

</dependency>


in this case it will create different file as  XMLBaseSample.log inside tomcat/log folder of your application.

in your class write below code to send logging messages to XMLBaseSample.log file

import org.apache.log4j.Logger;

private static Logger LOG = Logger.getLogger(Test.class);

and in class if  you have use this logger

private static Logger LOG = Logger.getLogger(Test.class);

and inside method if you use logger.info("test your logger using xml based approach");
 then you will see this log message inside XMLBaseSample.log

i have use different logger in both the case just to explain that we can achieve it with many ways.