Showing posts with label Web service in liferay. Show all posts
Showing posts with label Web service in liferay. Show all posts

Wednesday, 27 June 2012

Consuming web service in liferay

Following are the steps to Consume the web service in the liferay.

1) Now it's time to verify the web service which we have generated is working or not. For that we need to make a client which is going to consume the web service we have published.


Make a sample portlet called "web-service-Consuming-portlet ".  Hit the url "http://127.0.0.1:8080/web-service-publishing-portlet/api/axis/Plugin_WebServiceDemoSample_SampleService?wsdl" copy the wsdl file from the browser.


2) Copy that file into the WEB-INF folder.


3) Right click on the .wsdl file from eclipse and click on the new and make a web service client.then set assemble client and clisk on thegenerate the classes for accessing the service.

4) Write a class ConsumingWebService and write the follwoing code access the web service.

public void render(RenderRequest request,RenderResponse renderResponse){
        SampleServiceSoapServiceLocator serviceLocator = new SampleServiceSoapServiceLocator();
    //SampleServiceSoapService soapService = (SampleServiceSoapService) serviceLocator.getPlugin_WebServiceDemoSample_SampleService();
    SampleServiceSoap serviceSoap;
    try {
        serviceSoap = serviceLocator.getPlugin_WebServiceDemoSample_SampleService();
        SampleSoap[] sampleSoap =  serviceSoap.getUsers(10180);
        for(int i=0 ;i<sampleSoap.length;i++){
            System.out.println("User Id"+sampleSoap[i].getName());
        }
    } catch (ServiceException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
   
    }


In the above code we have cnsumed the web service which are published in the previous example.Which is going to print print the resut on the concel. 

Monday, 25 June 2012

Publish a Web service In liferay.

Steps to publish a SOAP web-service in liferay.


1) Create a portlet called "web-service-publishing-portlet" in liferay.
2) Put the service.xml in the web-inf folder. In the service.xml remote-service="true".

Code-Snippet:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="com.aalap.sample.webservice">
    <namespace>WebServiceDemoSample</namespace>
        <entity name="Sample" local-service="true" remote-service="true">
        <column name="id" type="long" primary="true"></column>
        <column name="name" type="String" primary="true"></column>
        <column name="groupId" type="long"></column>
       
        <finder return-type="Collection" name="groupId">
            <finder-column name="groupId"></finder-column>
        </finder>
    </entity>
</service-builder>

3) Now clean and run the command ant clean-build-service command.By this command appropriate methods has been generated.

4) Form the local service impl class write a method

public  List<Sample> getUsers(long groupId) throws SystemException{
        return samplePersistence.findBygroupId(groupId);
    }
again run the build-service task. Make sure your build task run successful.

5) Now put a method in the serviceImpl class.

public class SampleServiceImpl extends SampleServiceBaseImpl {
    public  List<Sample> getUsers(long groupId) throws SystemException{
        return SampleLocalServiceUtil.getUsers(groupId);
    }
}

Again run the command ant clean build-service.Verify the task has been run successfully.

6) Now run the task build-wsdd.Verify tha task has run successfully.Then run the task ant clean deploy ant. Check in the concel the message one "web-service-publishing-portlet" is available for use.

7) After this command one wsdd(Web Service Deployment Descriptor) file has been generated.In this file the tag <service name> defines the name of the wsdl(Web Service Description Language). In our example name of the wsdl file is "Plugin_WebServiceDemoSample_SampleService". To access this file hit the url "http://127.0.0.1:8080/web-service-publishing-portlet/api/axis/Plugin_WebServiceDemoSample_SampleService?wsdl" . If some one want to access methods published url hit above mention url.

In the wsdl file the tag <wsdl:operation> define the name of the methods we have published. In our example we have nethod "getUsers" which is in the tag "<wsdl:operation name="getUsers">".