Introduction:
Liferay have provided JSON web services to expose
data to outside world. We can use different client to call liferay web services
In
this article we will see the following clients
- Java Client Consuming Liferay Web services
- Ajax Client Consuming Liferay Web services
The
following are Articles which talk about Liferay JSON web services.
Liferay
6.2 and 6.1
Liferay 6.0.6
Note:
Read above all articles so that we will have good experience
with Liferay JSON web services.
Java
Client Consuming Liferay Web services
We can also use pure java client to call Liferay
JSON web services. We will use Apache Http Client to call these web
services.
Liferay also have implements Android SDK which used
Apache Http Client to Liferay JSON web services.
Apache
http client is set of java classes to call normal Http URLs in java code.
Some time we may need to call http URLs from java
code such scenarios we will use Apache Http Client libraries.
Now we will call Liferay JSON web services Using Apache
Http Client.
The following is place you can download required
Apache Http Client Jar files
Assume following is your web service URL and have
one parameter
The
following is Liferay JSON web service URL
Using Plugin Context
http://localhost:8080/LiferayJSONWebservices-portlet/api/jsonws/
employee/get-employee?emplyeeId=1
Using Portal Context
http://localhost:8080/api/jsonws/LiferayJSONWebservices-portlet.employee/get-employee?emplyeeId=1
|
Note:
In Liferay 6.1 we have used Plugin Context in the URL
from Liferay 6.2 we directly used Portal context
The
following is HTTP java client
public static void main(String[]
args) throws ClientProtocolException,
IOException {
ArrayList<HashMap<String,
String>> mylist = new ArrayList<HashMap<String,
String>>();
HttpHost targetHost
= new HttpHost("localhost", 8080, "http");
DefaultHttpClient
httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
new
AuthScope(targetHost.getHostName(), targetHost.getPort()),
new
UsernamePasswordCredentials("test@liferay.com", "test"));
AuthCache authCache
= new BasicAuthCache();
BasicScheme
basicAuth = new BasicScheme();
authCache.put(targetHost,
basicAuth);
BasicHttpContext
ctx = new BasicHttpContext();
//
ctx.setAttribute(ClientContext.AUTH_CACHE,authCache);
// Plugin Context
Use for Liferay 6.1
HttpPost post = new HttpPost("/LiferayJSONWebservices-portlet/api/jsonws/employee/get-employee");
// Portal Context
Use for Liferay 6.2
// HttpPost post =
new
HttpPost("/api/jsonws/LiferayJSONWebservices-portlet.employee/get-employee");
List<NameValuePair>
params = new ArrayList<NameValuePair>();
params.add(new
BasicNameValuePair("emplyeeId", "30722"));
UrlEncodedFormEntity
entity = new UrlEncodedFormEntity(params, "UTF-8");
post.setEntity(entity);
HttpResponse resp =
httpclient.execute(targetHost, post, ctx);
resp.getEntity().writeTo(System.out);
httpclient.getConnectionManager().shutdown();
}
|
When you execute above code if you get Exception that Authenticated Access required then
use following code
package com.meera.liferay;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import
org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import
org.apache.http.client.ClientProtocolException;
import
org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.
import
org.apache.http.message.BasicNameValuePair;
import
org.apache.http.protocol.BasicHttpContext;
public class LiferayWebserviceClient {
public static void main(String[] args) throws
ClientProtocolException,
IOException
{
ArrayList<HashMap<String,
String>> mylist = new ArrayList<HashMap<String, String>>();
HttpHost
targetHost = new HttpHost("localhost", 8080, "http");
BasicHttpContext
ctx = new BasicHttpContext();
// Plugin Context Use for Liferay
6.1
HttpPost
post = new HttpPost("/LiferayJSONWebservices-portlet/api/jsonws/employee/get-employee");
Base64
b = new Base64();
String
encoding = b.encodeAsString(new String("test@liferay.com:test").getBytes());
post.setHeader("Authorization", "Basic
" + encoding);
List<NameValuePair>
params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("emplyeeId", "30722"));
UrlEncodedFormEntity
entity = new UrlEncodedFormEntity(params, "UTF-8");
post.setEntity(entity);
HttpResponse
resp = httpclient.execute(targetHost, post, ctx);
resp.getEntity().writeTo(System.out);
httpclient.getConnectionManager().shutdown();
}
}
|
In the code I have used Plugin context bases on your
choice you can use Plugin Context or Portal Context. If you are using Liferay
6.2 then you can use Portal context in the URL.
Calling Portal Services Using Http Client
package com.meera.liferay;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import
org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import
org.apache.http.client.ClientProtocolException;
import
org.apache.http.client.entity.UrlEncodedFormEntity;
import
org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.
import org.apache.http.protocol.BasicHttpContext;
public class LiferayWebserviceClient {
public static void main(String[] args) throws
ClientProtocolException, IOException {
HttpHost
targetHost = new HttpHost("localhost", 8080, "http");
BasicHttpContext
ctx = new BasicHttpContext();
// Plugin Context Use for Liferay
6.1
HttpPost
post = new HttpPost("/api/jsonws/country/get-countries");
Base64
b = new Base64();
String encoding = b.encodeAsString(new String("test@liferay.com:test").getBytes());
post.setHeader("Authorization", "Basic " + encoding);
List<NameValuePair>
params = new ArrayList<NameValuePair>();
//params.add(new
BasicNameValuePair("emplyeeId", "30722"));
UrlEncodedFormEntity
entity = new UrlEncodedFormEntity(params, "UTF-8");
post.setEntity(entity);
HttpResponse
resp = httpclient.execute(targetHost, post, ctx);
resp.getEntity().writeTo(System.out);
httpclient.getConnectionManager().shutdown();
}
}
|
Required Jar Files
commons-codec.jar
httpclient.jar
httpcore.jar
commons-logging.jar
|
Download Example
and Required Jar files
Ajax
Client Consumer Liferay JSON Web services
The following is simple Ajax Program Call liferay
web 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.
The
following is Liferay JSON web service URL
Using
Plugin Context
http://localhost:8080/LiferayJSONWebservices-portlet/api/jsonws/
employee/get-employee?emplyeeId=1
OR
http://localhost:8080/LiferayJSONWebservices-portlet/api/jsonws/employee/get-employee/employee-id/1
Using
Portal Context
http://localhost:8080/api/jsonws/LiferayJSONWebservices-portlet.employee/get-employee?emplyeeId=1
OR
http://localhost:8080/api/jsonws/LiferayJSONWebservices-portlet.employee/get-employee
/employee-id/1
|
.
Note:
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/LiferayJSONWebservices-portlet/api/jsonws/employee/get-employee/employee-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/LiferayJSONWebservices-portlet/api/jsonws/
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>
|
Author
Army Bharti is seeking candidates for SSC course for law graduates.
ReplyDeleteCandidates who are interested and meet the eligibility criteria should go through the official website which is joinindianarmy.nic.in.
Awesome Article. Candidates who are eager to get the FreeJobAlert. they can get the all information from this page
ReplyDeleteAlso Candidates Can Get Jobs in India Post Office Post Office Recruitment
It's really amazing to have many lists of will help to make thanks a lot for sharing
ReplyDeletevé máy bay từ hcm đi thượng hải
vé máy bay đi anh giá rẻ
vé máy bay đi mỹ giá rẻ eva air
có chuyến bay từ mỹ về việt nam chưa
vé máy bay giá rẻ từ nga về việt nam
I found that site very usefull and this survey is very cirious, I ' ve never seen a blog that demand a survey for this actions, very curious... similar sites like fiverr
ReplyDeleteFantastic!! you are doing good job! I impressed. Many bodies are follow to you and try to some new.. After read your comments I feel; Its very interesting and every guys sahre with you own works. Great!!
ReplyDeletethông tin chuyến bay từ singapore về việt nam
Các chuyến bay từ Nhật về Việt Nam bị hoãn
vé máy bay từ toronto về việt nam
vé máy bay từ san francisco về việt nam
vé máy bay từ los angeles về việt nam
chuyến bay thương mại từ canada về việt nam
I have to convey my respect for your kindness for all those that require guidance on this one field. Your special commitment to passing the solution up and down has been incredibly functional and has continually empowered most people just like me to achieve their dreams. Your amazing insightful information entails much to me and especially to my peers.
ReplyDeletebiệt thự nhà vườn cấp 4
mẫu nhà trọ đẹp
biệt thự nhà vườn 2 tầng
nhà 3 tầng hiện đại
thiết kế nội thất quán cà phê
mẫu nhà ống