Before start this Article please go through
following Article
Call Plugin Portlet JSON
Web Services Using Portal Context
We already developed portlet (in part-I) and once we deployed the portlet
we will use portal context to call Plugin portlet JSON web services.
We can get web service by accession JSONWS API screen as Admin and there
you can see all your web services information like what is return type and what
are the parameters need for web service.
We can also get code snippets for different clients like Java script code
and web service URL
The general URL pattern to call Plugin portlet JSON web services using
Portal Context as follows
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.
portal-context:
It’s our liferay portal context and
we will represent portal context with slash (/)
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 portal
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 it is simple
the 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:
Portal Context
|
/
|
Plugin context path
|
LiferayJSONWebservices-portlet
|
Web service servelet path
|
/api/jsonws
|
Service Class Name
|
EmployeeServiceImpl.java
converted as employee
|
Service Method Name
|
getEmplyee(--)
converted as get-employee
|
When we access Plugin portlet JSON web services form portal context all
authentication mechanism applies by liferay when we access web services.
This is general URL pattern to call JSON web services and we can see all
web service URL information form admin screen
Passing parameters and
its values:
We can pass parameters in two was
- Query String
- URL Parameters
Query String URL Notation
Example:
public
com.meera.db.model.Employee getEmployee(long emplyeeId){
}
Web service URL with Query String Parameters As follows
Result:
{"employeeDesignation":"SoftwareEngineer","emplyeeId":1, "emplyeeName":"meera"} |
URL Parameters
We can pass parameters to web service as URL parameters and here param
name and values separated by slash (/). And here parameter notation will be based
on the param names we have used in java implementation method and each capital
letter in name will be separated by – and capital letter will become small
latter.
URL pattern Web service
Notation:
Example:
public
com.meera.db.model.Employee getEmployee(long emplyeeId){
}
Web service URL with Query String Parameters As follows
http://test@liferay.com:test@localhost:8080/api/jsonws/
LiferayJSONWebservices-portlet.employee/get-employee/emplyee-id/1
Result:
{"employeeDesignation":"SoftwareEngineer","emplyeeId":1, "emplyeeName":"meera"} |
For Our Example Complete URL to access employee data by passing employee
id is following like this
http://test@liferay.com:test@localhost:8080 /api/jsonws/LiferayJSONWebservices-portlet.employee/get-employee/
emplyee-id/1
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"} |
Access Plugin Portlet Web service Information using Admin Screen
Well we understand how JSON web service URL will be
generated and passing parameters. We will have JOSNWS API admin screen there you
can get all information and also we can some code snippets to call web service
Note:
To access JSONWS API screen you need login as portal
admin then only you can access web services information.
The following are steps to get web service information
from admin screen
Step:
1
We need access web services admin screen from
following URL
Note:
When we access above URL by default we can see all
liferay portal web services.
The following is screen to show
JSONWS API
Step:
2
Once we get the screen in left hand side we have Context
Path Select box there we need to select our Plugin portlet context
The following screen shows you select
the context path
Step:
3
Once we select Plugin portlet context path we can
see all entities and its service in the panel and each service is hyper link when
you click on web service method you can see all information about method
The following is screen show the web
service method information
Step:
4
Now we need click on invoke button so that we can
see all code snippets and URL to call web service
Step: 4
Now we can see web service URL when we click on URL
example tab. This URL we will use in any client to access web service. When we
call from other client we need to pass authorized use credentials to access data
using web service URL
The following is screen shows you to web
Service URL
Note:
When get web service URL we need pass parameter values
as input so that we can see full URL and it can get the JSON data
The URL we accessed from admin screen and we already login as admin that why we no need provide any authorization
information but when we access URL from other clients we need to pass authorization
information i.e. we need pass admin credentials as URL http headers.
The following is example URL that contains Authorization
Information
With authentication The URL like follow
http://test@liferay.com:test@localhost:8080/api/jsonws/
LiferayJSONWebservices-portlet.employee/get-employee?emplyeeId=1
OR
http://test@liferay.com:test@localhost:8080/api/jsonws/
LiferayJSONWebservices-portlet.employee/get-employee/emplyee-id/1
Result:
{"employeeDesignation":"SoftwareEngineer","emplyeeId":1,
"emplyeeName":"meera"}
|
Important
Portal Properties when we work with Liferay JSON web services
We need to override following portal properties so
that it will allow calling liferay JSON web services, we need register host
names and we need to tell what are methods allow to public access
The following properties we need to
write in portal-ext.properties file.
json.service.auth.token.hosts.allowed=127.0.0.1,SERVER_IP
json.service.auth.token.enabled=false
jsonws.web.service.public.methods=*
jsonws.servlet.hosts.allowed=127.0.0.1,SERVER_IP
|
More information Please have look into following
link
Note:
Sometime browser not allows pass credentials in URL
so we need pass in the program as Authorization
header and type is basic.
Calling
Plugin Portlet JSON Web Service from Ajax
The following is simple Ajax Program Call Portal
Service with Basic Authorization Header
The following example uses the get Employee JSON Web
Services of Plugin portlet and in the alert it will show employee name. This is
just example
Here you need look into the part how to pass
Authorization header in the request this is very important.
Here we are passing Authorization header type basic
and we will encode the user credentials.
<%@ include file="init.jsp"%>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).on('ready',function(){
var
username ="test@liferay.com";
var
password ="test";
function
make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = btoa(tok);
return "Basic " +
hash;
}
$.ajax({
url: 'http://localhost:8080/api/jsonws/
LiferayJSONWebservices-portlet.employee/get-employee/emplyee-id/1',
dataType: "json",
type: "get",
success: function(data){
alert(data.employeeName);
},
beforeSend: function(xhr){
xhr.setRequestHeader('Authorization',make_base_auth(username,
password));
},
complete: function(){
},
error: function(){
}
});
});
</script>
|
Passing
Parameters Values in Ajax call
<%@ include file="init.jsp"%>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).on('ready',function(){
var
username ="test@liferay.com";
var
password ="test";
function
make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = btoa(tok);
return "Basic " +
hash;
}
$.ajax({
url: 'http://localhost:8080/api/jsonws/
LiferayJSONWebservices-portlet.employee/get-employee',
data:{ emplyeeId:"1"}
dataType: "json",
type: "get",
success: function(data){
alert(data.employeeName);
},
beforeSend: function(xhr){
xhr.setRequestHeader('Authorization',make_base_auth(username,
password));
},
complete: function(){
},
error: function(){
}
});
});
</script>
|
Note:
When we pass parameters values in Ajax call its
nothing but passing parameters as query string URL so here we need to consider
notation of passing parameters and its values in web service URL.
Passing parameters values as URL parameters are
different from passing as query string.
Example:
Query String
?employeeId=1
URL Parameter
/employee-id/1
|
We can use Ajax code in HTML page or JSP page then
you can see the result in alert.
Related
Articles:
0 comments :
Post a Comment