Showing posts with label rest. Show all posts
Showing posts with label rest. Show all posts

Monday, January 13, 2014

Introduction to Liferay Portal

Liferay is Portlet technology which follows the JSR 168 and JSR 286 Compliants.

Liferay is very good open source for portlet technology have very good working group and community.

Portlet technology is similar to servlet technology but portlet is small part of dynamic content in web page.

In normal web application when we send request then server serve the response to the client or browser. Here entire page will be served by one response.

Coming to portlet technology in one web page there may be many portlets and each portlet have its own request and response.

Each portlet can send request and get the response from server then the response belongs to only the portlet which send the request.

We can say portlet is small fragment of dynamic content in the page. Page is aggregation of multiple fragments of dynamic content and each dynamic fragment is managed by one portlet in the web page.

In one page we can accommodate multiple portlet and each portlet is responsible for generate dynamic content for page. Multiple portlet together generate the dynamic content and all the portlet dynamic content aggregation will be generated as one web page.


Liferay

Liferay is implemented portlet technology and it exposed as opens source.

Liferay portlet technology  follows the JSR 168 and JSR 286 Compliant which was given by java community people. Portlet technology should follow the JSR portlet specification.

Like that liferay have implemented portlet specification and give to us as open source. Anybody can get source code and they can customize and add new features to Liferay portal.

Liferay portal consist set of portlets and ready to build web application and we can also develop new portlet application using liferay. Liferay have its own frame work and standards to add new feature and customize existed portal.

Liferay have given two kinds of portal

  1. Liferay Enterprise Edition
  2. Liferay Community Edition


Liferay Enterprise Edition:

Liferay Enter Price Edition is paid portal which have enterprise license from liferay Inc. Liferay Enter Price Edition  have support from Liferay Inc and it’s have more benefits than Liferay Community Edition version.

Liferay Community Edition

Liferay Community Edition is available to public and anybody can get source code and then can add or customize. Very good working group and community people working for this. We can also contribute our effort to liferay community.

The following is Difference between CE and EE


Major Liferay Versions:


Liferay 6.2.x CE&EE, Liferay 6.1.x CE&EE, Liferay 6.0.x CE&EE and Liferay 5.2.x CE&EE


Features of Liferay
  • Liferay is ready to use web application
  • Liferay have many portlet to full fill dynamic web application functionality
  • Very Good Enterprise Web Content Management System
  • Sites, Organization and User Groups
  • Social Network Portlet
  • Document Library Management
  • Collaboration (Blogs, Wikis and Forums)
  •  Service Oriented Architecture Support.
  • Dynamic Look and Feel from Themes
  • Changing page layouts
  • Good Portal Administration User Interface.
  • Roles and Permission System
  • Easy Customization and Development
  • Support Many Integrations
  • Different Application Servers Support.
  • Device based Page View
  • Liferay Market Place

Liferay is ready to use web application

Liferay already is web application so that simple we can download and deploy in any machines. It’s ready to use application. Once we deploy we can directly.

The following tutorial for setup liferay


Liferay have many portlet to full fill dynamic web application functionality

Liferay have given many portlet to  build dynamic web application they have give more than 40 portlet in source code along with that we can also get many portlets from liferay market place.
Liferay market place is place where we get many real time portlets to use in our liferay web application. There we can get CE and EE portlet. Simple we can down lad and deploy in our liferay portal.

The following are links to get portlets



Very Good Enterprise Web Content Management System

 Web application consists of many web pages. Each web pages its own design and content. In real world application to manage content in application people came up with Web Content Management Systems. There are many web content management systems in market and liferay also have very good content management support. With this feature we can create web content, design content and publish content.

The WCM provide content management, content creation and content publishing.
Liferay have its own great feature that is called structures and templates from which we can design content without knowledge of CSS and html.

Admin will provide structures and templates by using that anyone can add web content without knowledge of CSS and HTML.

The following is information about liferay web content management


Sites, Organization and User Groups

Liferay has given sites, organization and User Groups. Based on our requirement we can take any one, so that we will get some generic feature to our application.
The following is link to get more about Sites and Organization


Social Network Portlet

Liferay have given many social networking portlet so that we can build social network web sites. These portlet have functionality to full fill basic needs for social network platforms.

The following is link to get more information


Document Library Management

Document Management system is for managed documents. Liferay have very good document management system winch implemented by Java Content Repository (JCR).
Liferay already integrated with Apache Jackrabbit JCR implementation and we can also integrate with other document management system like Alfresco.

The following is link to get more information



Collaboration (Blogs, Wikis and Forums)

Liferay have support for managing Blogs, Wikis and Forums. They have given portlet to meat wikis, blogs and forum functionality.

The following is link to get more information


Service Oriented Architecture (SOA) Support

Liferay have very good support for SOA. Liferay can support SOAP and REST web service. We can expose liferay services in either ways. In liferay creating web services and accessing web services very easy.

Because of these web services we can integrate liferay portal with other applications. We can also develop mobile application by using these web services. In recent days liferay have came up with mobile SDK to build mobile applications
.
The following are links to get more information.





Dynamic Look and Feel from Themes

Liferay Look and feel can be changes by themes. Themes are very easy way to change liferay look and feel. In liferay we can develop themes very easy.

The following is link to get more information about themes in liferay.


Page layouts

Layout is page template and its portlet arrangement in page. Liferay have many page layouts apart from that we can develop our own layouts.

The following is more information about layouts.


Good Portal Administration User Interface.

Liferay have very good User Interface to manage liferay portal. Manage liferay portal is nothing but manage users, manage sites, manage organization, manage role and permissions and server management.

Liferay have control panel there we can manage all the things

The following is more information about liferay administration control panel.


Roles and Permission System

Liferay have very good permission system which archives from user roles. In liferay each user has role and each role have permissions. Permissions will decide accessibility or scope of user in the website.

Liferay by default have default roles and permission apart from that we can create roles and assign permission to role.

The following links is from more information about Roles and Permissions


Easy Customization and Development

Liferay have its own mechanisms to customize or develop new thing for portal.

Liferay have Plugins SDK from which we can do customization and development.

Liferay have following Plugins SDK to customize and development in liferay portal


Portlets Hook, Themes, Ext and Layouts


The following is link to get more information about Plugins SDK


Support Many Integrations

Liferay support many integration like Service Oriented Integration, SSO Integration, DAP Integration and Learning Management Tools Integration.

The following are links to get more information about integrations



Different Application Servers Support

Liferay have support for many application servers. Liferay releases portal with different application server flavors

The followings is liferay portal available in different servers support


Device based Page View

Liferay can render pages based on devices this is very useful when we show application in mobiles and tablets.

Liferay have responsive theme mechanism based on the device the view will be rendered in browser.

Liferay have Bootstrap theme support and its having own administration panel there we can define device rules. Based on device rules page will be rendered.

The following is link to get more information about liferay view in mobiles


Liferay Market Place

Liferay market place is the liferay applications hub there we can get liferay applications. We can get CE and EE liferay applications. We can also place applications in market place. In market place liferay applications will be packaged and once we download the application we can deploy in portal this task is very easy.

The following is link to get more information about market place


Resources

Liferay official releases and downloads


Liferay CE Public SVN Repository (username: guest & password: guest)


Liferay CE Current working source code


Liferay Documentation

https://www.liferay.com/documentation/liferay-portal/6.2/user-guide

Author

Tuesday, November 12, 2013

Liferay Custom JSON Web Services on Multiple Tables

Liferay Custom JSON web services on Multiple Tables


Objective:

Create JSON web services for combination of multiple tables means prepare JSON web services so that data should come from multiple tables which are related each other.

Download CustomJsonWebservices portlet from following location

You can find source and war file


Note: 

Portlet developed in Liferay 6.1GA2 EE version
If you want deploy in CE version you just do changes in liferay-plugin-package.properties

Liferay 6.1 EE version

name= CustomJsonWebservices
module-group-id=liferay-ee
module-incremental-version=1
tags=
short-description=
change-log=
page-url=http://www.liferay.com
author=Liferay, Inc.
licenses=EE
liferay-versions=6.1.20

Liferay 6.1 CE version

name = CustomJsonWebservices
module-group-id=liferay
module-incremental-version=1
tags=
short-description=
change-log=
page-url=http://www.liferay.com
author=Liferay, Inc.
licenses=LGPL
liferay-versions=6.1.1

Procedure for deploy portlet:

You can use war file and directly place in your portal deploy folder and test or you can also use source to deploy portlet.
Once portlet is deployed successfully insert data in the following table as shown in screens

json_employee



json_address



And test web service URLs

Liferay provide JSON web services so that we can use those services in other systems.

Liferay providing two types of web services SOAP based and REST bases web services.

Liferay JSON web services come under REST based web services.

Here we are talking about only rest based web services i.e. liferay JSON web services.

Liferay have built in ability can generate JSON web services by default when we make remote-service true for service builder.

But the services which created by default we can apply on only one table means data can served from only one table or web services related to single entity.

But in real scenario the default web services not enough to full fill real requirement there we have write our custom methods to produce JSON data. This pretty easy in liferay because liferay also provides write custom methods for JSON web services in pluin portlet.

Note:

Liferay have provided to generate service layer using service builder. We will use servce builder to generate service layer classes and methods

Steps to produce Custom JSON web services in Liferay Plugin portlet environment

The Following are the steps write custom methods to produce JSON web services:

Step: 1

Create Plugin portlet in Liferay using liferay IDE and portlet is liferay MVC portlet.
Step: 2

Create service bolder for plugin portlet

Open service.xml file and define data base tables and put remote-service is true. When we put remote service true then it will create default JSON web services.

<?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.meera.jsonwebservices.db">
            <author>E5410</author>
            <namespace>JSON</namespace>
            <entity name="Employee" local-service="true" remote-service="true">
                        <column name="emplyeeId" type="long" primary="true" />
                        <column name="emplyeeName" type="String" />
                        <column name="employeeDesignation" type="String" />
                        <order by="asc">
                                    <order-column name="emplyeeId" />
                        </order>
            </entity>
            <entity name="Address" local-service="true" remote-service="true">
                        <column name="Id" type="long" primary="true" />
                        <column name="emplyeeId" type="long" />
                        <column name="employeeAddress" type="String" />
                        <order by="asc">
                                    <order-column name="emplyeeId"/>
                        </order>
                        <finder name="emplyeeId" return-type="Collection">
                                    <finder-column name="emplyeeId" />
                        </finder>
            </entity>
</service-builder>

Step: 3

Now we need add JSON Web service servelet in web.xml of porltet. So that it will get web services exposing ability.

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
            <display-name>CustomJsonWebservices-portlet</display-name>
            <filter>
         <filter-name>Secure JSON Web Service Servlet Filter</filter-name>
         <filter-class>com.liferay.portal.kernel.servlet.PortalClassLoaderFilter</filter-class>
         <init-param>
          <param-name>filter-class</param-name>
          <param-value>com.liferay.portal.servlet.filters.secure.SecureFilter</param-value>
         </init-param>
         <init-param>
          <param-name>basic_auth</param-name>
          <param-value>true</param-value>
         </init-param>
         <init-param>
          <param-name>portal_property_prefix</param-name>
          <param-value>jsonws.servlet.</param-value>
         </init-param>
       </filter>
       <filter-mapping>
         <filter-name>Secure JSON Web Service Servlet Filter</filter-name>
         <url-pattern>/api/jsonws/*</url-pattern>
       </filter-mapping>

       <servlet>
         <servlet-name>JSON Web Service Servlet</servlet-name>
         <servlet-class>com.liferay.portal.kernel.servlet.PortalClassLoaderServlet</servlet-class>
         <init-param>
          <param-name>servlet-class</param-name>
          <param-value>com.liferay.portal.jsonwebservice.JSONWebServiceServlet</param-value>
         </init-param>
         <load-on-startup>0</load-on-startup>
       </servlet>
       <servlet-mapping>
         <servlet-name>JSON Web Service Servlet</servlet-name>
         <url-pattern>/api/jsonws/*</url-pattern>
       </servlet-mapping>
            <jsp-config>
                        <taglib>
                                    <taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri>
                                    <taglib-location>
                                                /WEB-INF/tld/liferay-portlet.tld
                                    </taglib-location>
                        </taglib>
                        <taglib>
                                    <taglib-uri>http://liferay.com/tld/aui</taglib-uri>
                                    <taglib-location>/WEB-INF/tld/aui.tld</taglib-location>
                        </taglib>
            </jsp-config>
           
</web-app>


Step: 4

Run service builder using ant build-service or in eclipse you can run this from ant view.

Step: 5

Writing Custom methods for produce JSON data

We need to decide for which entity we have to provide custom JSON services .find appropriate entity and write method XXXServiceImpl.java this is under package of
 you-base-package.service.impl package of your source.

Here XXX is entity name which is specified in service.xml

In our example I have implemented in EmployeeServiceImpl.java here I am getting employee object by employeeId. This method return employee object. In liferay any object automatically sterilizes and produces as JSON data.

The following is example for code


public class EmployeeServiceImpl extends EmployeeServiceBaseImpl {   
            public  com.meera.jsonwebservices.db.model.Employee getEmployee(
                                    long emplyeeId)
                                    throws com.liferay.portal.kernel.exception.PortalException,
                                                com.liferay.portal.kernel.exception.SystemException {
                                    return EmployeeLocalServiceUtil.getEmployee(emplyeeId);
                        }
}


Once we completed writing custom method in XXXServiceImpl.java

Then we need to run service builder using ant build-service command or from eclipse ant view you can run same command

Step: 6

Now finally deploy the portlet into server by using ant deploy command or from ant view you can use same command

Now we are ready with custom JSON web services

Note:

For each modification in XXXServiceImpl.java or in service layed we have run ant build-service command  later we have to deploy the portlet using ant deploy then only change will be applied to the services.
  
Accessing JSON web services

To access the services we need to use following URL pattern



Server:

 It’s your domain name or host name if you are in local the its “localhost”

Port:

It’s your application server port number.

plugin-context:

 it’s our plug-in portlet context name its simple as portlet name

api/jsonws:

This is path for call JSON Web Service servlet this is configured in web.xml

service-class-name:

 service-class-name is generated from the service’s class name by removing the Service or ServiceImpl suffix and making it lower case.This is our service class name where we implemented custom method or its simple a entity name which is in service.xml. In our example we implemented our method in EmployeeServiceImpl.java
Finally we have to use in URL as employee when we observe it’s just entity name in service.xml

service-method-name:

Service-method-name is generated from the service’s method name by converting its camel case to lower case and using dashes (-) to separate words

Finally our Complete URL like this for our Example:





Plugin context path
CustomJsonWebservices-portlet
Web service servelet path
/api/jsonws
Service Class Name
EmployeeServiceImpl.java converted as employee
Service Method Name
getEmplyee(--) converted as get-employee



Passing parameters and its values:

We can pass parameters in two ways

Query String

URL pattern and separated param name and value by slash(/)

Complete URL example with query string




Complete URL by URL pattern:




For Our Example Complete URL to access employee data by passing employee id is following like this



OR


result:

{"employeeDesignation":"SoftwareEngineer",
"emplyeeId":1,"emplyeeName":"meera"}



Note:

 Some time its ask Authentication so we should pass admin credentials as URL headers

With authentication The URL like follow

OR


result:

{"employeeDesignation":"SoftwareEngineer",
"emplyeeId":1,"emplyeeName":"meera"}



Note:

 All these URLs you can use from any browser and see the JSON data. Here sometimes browsers not allows @ or # symbols in URL then we have to make this as URL encoding type characters

For encode URL and test in browse use following link and encode


Encoded URL:


OR



Well as of now we just done created Custom service and its calling

Now we will see another way to call same web service method in JSON Web Services Invoker

JSON web service Invoker:

JSON Web Services Invoker is one of the mechanism to call some complex web services very easy way.

Here we can call JSON web service Invoker by following URL pattern



URL explanation is same like i explained previous section here we need to remember is to call invoker we have to use path /api/jsonws/invoke

This call take only one parameter i.e. cmd the value of this parameter is JSON Map.

Here we need to pass parameter as query string only like follow



Our complete URL for example is




What is JSON Map?

JSON Map is simple JSON object it contains key and value here value is another JSON object which contains key and value

In JSON map key will be used as service call path and value is parameters which passing to service calls.
The same example we discussed above we can do like this

Take the example get employee data by passing id.

Our Normal URL call is as follows




In above URL service call is    employee/get-employee/  and parameter is employeeId and its value is 1

Now the following is JSON map for above


{
"/employee/get-employee": {
"emplyeeId": 1,
}
}


Here key is used as Service call and value is another json object contains parameters and its value.

Now call our complete web service call using JSON web service Invoker as follows



result:

{"employeeDesignation":"Software Engineer",
"emplyeeId":1,"emplyeeName":"meera"}




This is way we have to pass JSON map to JSON web service invoker.

We can assign data as some reference object like following .here we have to specify $ before variable


{
"$emp=/employee/get-employee": {
"emplyeeId": 1,
}
}


$emp is reference variable




In above call we will get JSON data have all columns of Employee Table

Fetching required columns from JSON Web service invoker means filtering columns


{
"$emp[emplyeeName,emplyeeId]=/employee/get-employee":
{
"emplyeeId": 1,
}
}


The following is complete web service call to filter columns using JSON web service invoker



result:

{"emplyeeId":1,"emplyeeName":"meera"}



As of now we just call custom web service which is related to one entity or one table
Now we will see how to fetch JSON data so that data comes from multiple Tables
Some times in real requirement the data should fetch from multiple tables.

To make use of custom method we can produce JSON web services so that data come from multiple tables.

Understanding Real Scenario:

I have one employee each employee have multiple addresses.

Here I have two tables Employee and Address tables.

If we generate JSON web services from liferay it will serve only one table data for service. But here data stored in two tables.

So now we have to produce JSON services so that data should come from Employee Tables and Address table

Employee Table:

employeeId
employeeName
employeeDesignation
1
meera
Software Engineer
2
prince
Architect
3
savvy
Developer




Address Table:

Id
emplyeeId
Address
1
1
Hyderabad
2
1
Hong Kong
2
1
Landon




When you observe above data employee meera have 3 addresses

This is where we need to apply custom JSON web services.

Assume my data should be like as followed structure


{
        name:”meera”,
       addresess:[
                          “Hyderabad”,”Landon”,”Hong Kong”
                        ]
}



We have two options

Complete manual preparation of JSON data

Use JSON Web service Invoker Nested calls

Complete manual preparation of JSON data:

In the manual preparation we will prepare JSON structure and will produce data.

In general List and any java object type can be serialize and will be produces as JSON data in liferay web services.Some scenarios some complex object will not be sterilize.If some thing complex structure then we have to prepare JSON data and we will produce as web services.

Default sterilization for list type and any object type:

In this scenario when we return some java object in custom method it will be automatically converted as JSON data. Here we need not do like this

Example for  Java Object to JSON data or JSON Object:


public class EmployeeServiceImpl extends EmployeeServiceBaseImpl {
           
            public  com.meera.jsonwebservices.db.model.Employee getEmployee(
                                    long emplyeeId)
                                    throws com.liferay.portal.kernel.exception.PortalException,
                                                com.liferay.portal.kernel.exception.SystemException {
                                    return EmployeeLocalServiceUtil.getEmployee(emplyeeId);
                        }
}


In above custom method getEmplyee return Employee objects type. Here we need not to serialize the data and it will be converted as JSON data by default this is default behavior of liferay web services.

The following is URL to access method


OR


result:

{"employeeDesignation":"Software Engineer",
"emplyeeId":1,"emplyeeName":"meera"}



Note:

In the above custom method return Employee object but finally object convereted as JSON data.

 Example for  List to JSON data


public class AddressServiceImpl extends AddressServiceBaseImpl {          
            public  List<com.meera.jsonwebservices.db.model.Address> getAddressList(long emplyeeId)
                                    throws com.liferay.portal.kernel.exception.PortalException,
                                                com.liferay.portal.kernel.exception.SystemException {
                                    return AddressUtil.findByemplyeeId(emplyeeId);
                        }
}


In the above custom method return list of Address objects for particular employee id.
Here list will be converted as JSON data by default

The following is URL to call the above service


OR


result:

[{"employeeAddress":"Hyderabad","emplyeeId":1,"id":1},
{"employeeAddress":"Hong Kong","emplyeeId":1,"id":2}]


Here Address is our service class and getAddressList is custom method to give list of address objects for particular employee.
Here List will be converted as JSON array simple.

Some scenarios which data is complex then default sterilization not work all times when default sterilization not work then we will prepare JSON data manually in custom method. Scenarios like list have map objects and map have list objects.

Example for specialized structure:

Take our previous scenario one employee have many address but my data structure should be like follows


{
        name:”meera”,
       addresess:[
                          “Hyderabad”,”Landon”,”Hong Kong”
                        ]
}


We can’t produce this same JSON data by default here we have to prepare JSON data manually from java objects.

The following is Example:


public class EmployeeServiceImpl extends EmployeeServiceBaseImpl {
            public  JSONObject getEmployeeManualJsonData(
                                    long emplyeeId)
                                    throws com.liferay.portal.kernel.exception.PortalException,
                                                com.liferay.portal.kernel.exception.SystemException {
                       
                        JSONObject employeeData=JSONFactoryUtil.createJSONObject();
                        JSONArray addressArray=JSONFactoryUtil.createJSONArray();
                        Employee      empObject=EmployeeLocalServiceUtil.getEmployee(emplyeeId);
                        employeeData.put("name",empObject.getEmplyeeName());
                        for(Address empaddress:AddressUtil.findByemplyeeId(emplyeeId)){
                                    addressArray.put(empaddress.getEmployeeAddress());
                        }
                        employeeData.put("addresess", addressArray);
                       
                                    return employeeData;
                        }
}



Here service class is EmployeeServiceImpl.java it will be converts as employee in URL and method is getEmployeeManualJsonData it will be converted in URL as get-employee-manual-json-data

The following is URL to access above service


OR


result:

{"name":"meera","addresess":["Hyderabad","Hong Kong","Landon"]}


Note:

 liferay providing JSONFactoryUtil class from this we can create JSONArray and JSONObject.

By using above classes we can prepare JSON data manually in custom web service method.

Nested JSON web service Invoker:

We already know JSON Web service Invoker is one of the way to call web services in liferay.
Nested JSON web service invoker has ability serve data from multiple tables.

By using this we can get data from multiple tables which are related to each other.

Same above scenarios I want data fetch all addresses for employee by pass employeeId.
We can call following web service invoker to get employee address. From nested web service invoker we can achieve that.

The following is JSON map for fetch employee addresses


{
"$employee =/employee/get-employee":
{
"emplyeeId": 1,
"$address = /address/get-address-list": {
"@emplyeeId" : "$employee.emplyeeId"
}
 }
}


@emplyeeId  at which parameter we pass to service call

$employee.emplyeeId   we will get employeeId from $employee reference object.

emplyeeId is same as column name  of entity which we mention in service.xml.

Observer above JSON Map:

First we need to fetch employee object and referenced by variable called $employee we use this employee object to get employeeId and this id will be passed to /address/get-address-list service call.

In the first service call i.e. /employee/get-employee   we will get employee object and referenced by variable, in the next service call i.e. /address/get-address-list we use this reference variable to get employeeId and that will passed as parameter value.

Here we are calling two service calls within one JSON Map object that is why we call it as Nested JSON Web Service Invoker.

Note:

Here all parameters which we passed in JSON map should be same as parameter names which we used in custom method implementation in XXXServiceImpl class.

The final web service call using Nested JSON web service invoke



result:

{"emplyeeId":1,"address":[{"employeeAddress":"Hyderabad","emplyeeId":1,"id":1},
{"employeeAddress":"Hong Kong","emplyeeId":1,"id":2},
{"employeeAddress":"Landon","emplyeeId":1,"id":3}],
"emplyeeName":"meera","employeeDesignation":"Software Engineer"}


Filter columns in Nested JSON web service call



result:

{"emplyeeId":1,"address":[{"employeeAddress":"Hyderabad","emplyeeId":1,"id":1},
{"employeeAddress":"Hong Kong","emplyeeId":1,"id":2},
{"employeeAddress":"Landon","emplyeeId":1,"id":3}],"emplyeeName":"meera"}


In the above call we only get employeeId and employeeName from employee object.

Now calling JSON web service with portal context:

As of now we have used plugin portlet context for calling web services. We can also call web services using portal context/

The following is pattern to call from portal context:
.


Note:
Conveniently, requests sent this way can leverage the user’s authentication in his current portal session. Liferay’s JavaScript API for services calls plugin services using this method.
The following is call web service from plugin context.




Note:

This calls the plugin’s service in a separate web application that is not aware of the user’s current session in the portal. As a result, accessing the service in this manner requires additional authentication. You might use this for batch services or other requests that don’t require context.

The following are dependent properties related to JSON web service in liferay


jsonws.web.service.invalid.http.methods=DELETE,POST,PUT

jsonws.web.service.strict.http.method=true

json.service.auth.token.enabled=true
json.service.auth.token.hosts.allowed=255.255.255.255
json.deserializer.strict.mode=false

jsonws.web.service.public.methods=*


Note:

Please consider all above properties in your portal.properties file. If something you  want change you can use portal-ext.properties file to override.

Important points:
  • Liferay provides two kinds of web services SOAP and REST.
  • JSON web services is part REST bases web services which partially follows rest specifications.
  • To implement service layer in liferay we will use service builder to and if we use remote- service is true then we can get data base services and JSON web services.
  • Liferay web services serve only one table data by default.
  • If we want get data which is related multiple tables or some specialize requirement we have to implement customer web services.
  • To implement any custom services we will use XXXServiceImpl.java class.
  • We can access JSON web services from simple URL pattern URL include plugin context, service class name, service method name and its parameters used by service method.
  • We can pass parameter as query string or normal URL pattern so that each param and values should be separated by slash (/)
  • We can access JSON web services using plug-in portlet context and portal context too. But difference is when we call from plug-in context we need implement our own authentication mechanism for services. If we use portal context then it will use authentication mechanism which provided by liferay portal.
  • JSON web service invoker is way to call web services which will fetch data from multiple tables.
  • To fetch data from multiple tables we have to implement custom method so that custom method prepares JSON data manually or we can use Nested JSON web service invoker.
  • By default all java objects, list and map can be sterilize automatically and it will be produced as JSON data. Some scenarios if data has complex structure then we have to prepare JSON data manually.
  • To prepare JSON data manually we can JSONFcatoryUtil class this can create JSONObject and JSONArray.
Screen 1:

The following screen depicts call JSON web services in browser and result will be JSON data.


Note:

In the URL host name and port number change according to you environment and in the web service URL pass your liferay admin username and password.





Reference Links:


Recent Posts

Recent Posts Widget

Popular Posts