Saturday, August 25, 2012

Consuming Liferay JSON Web Services

In previous post, we exposed custom liferay plugin service as JSON web service. In this post, we are consuming that service using sample standalone java class.


public class JsonTest {
public static void main(String[] args) throws Exception {
HttpHost targetHost = new HttpHost("localhost", 8080, "http");
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("username", "password"));
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
BasicHttpContext ctx = new BasicHttpContext();
ctx.setAttribute(ClientContext.AUTH_CACHE, authCache);
HttpPost post = new HttpPost("/<<portlet-context>>/api/secure/jsonws/sample/say-hello");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", “Harish”));
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();
}
}


Thursday, August 23, 2012

Liferay Custom JSON Web Service Development



Following is the step by step description of generating a custom liferay plugin service and exposing it as JSON Web Service.



STEP:1 Create a liferay plugin project and create new service


Here is the sample service.xml


<service-builder package-path="com.test.sample">
<author>harish.kumar</author>
<namespace>sample</namespace>
<entity name="Sample" local-service="true" remote-service="true">
<!-- PK fields -->
<column name="sampleId" type="long" primary="true" />
<!-- Other fields -->
<column name="name" type="String" />
<column name="address" type="String" />
</entity>
</service-builder>

Make sure remote-service=”true” in entity tag declaration.



STEP:2  Build the service.



STEP:3  Add your custom method in SampleLocalServiceImpl class


public void sayHello(Striang name)
{
System.out.println("Hello " + name);
}

STEP:4  Build the service.



STEP:5  Add the method definition to SampleServiceImpl class


public void sayHello(String name)
{
SampleLocalServiceUtil.sayHello(name);
}

STEP:6  build the service.



STEP:7  Add the following <servlet> and <servlet-mapping> Entries to portlet's web.xml file -


<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>
<servlet-mapping>
<servlet-name>JSON Web Service Servlet</servlet-name>
<url-pattern>/api/secure/jsonws/*</url-pattern>
</servlet-mapping>
view raw json-web-xml hosted with ❤ by GitHub

STEP:8  deploy the portlet.


STEP:9  To access your json web services enter the following url -

http://localhost:8080/<<portlet-context>>/api/jsonws





and Its Done!!!