Consuming Liferay Web Services

Company Blogs January 19, 2012 By Antônio Junior Staff

What is web service

The W3C defines a web service as a software system designed to support interoperable machine-to-machine interaction over a network. It has an interface described in a machine-processable format (specifically WSDL). Other systems interact with the Web service in a manner prescribed by its description using SOAP messages, typically conveyed using HTTP with an XML serialization in conjunction with other Web-related standards.

Web services are a set of tools that can be used in a number of ways. The three most common styles of use are RPC, SOA and REST.

Liferay enable us to build our custom methods locally and then expose them to be accessed remotely as Web Services (generated by the Service Layer).
In this post I will not talk how to create these services, but I will show how to consume the existing ones.

Enabling web services

First of all, the host must be allowed to access the remote services by configuring the portal-ext.properties file. By default, the portal.properties has the following settings to enable the services be accessed remotely:

axis.servlet.hosts.allowed=127.0.0.1,SERVER_IP
axis.servlet.https.required=false

The code above shows the IPs whick can access the services. You can input a list of comma delimited IPs that can access this servlet. You also can input a blank list to allow any IP to access this servlet. SERVER_IP will be replaced with the IP of the host server. This is the reason that you can access web services only locally if you don't change this list.

Besides, the user must have permission to access the portal resources.
To authenticate an user remotely we have to change the authentication method to screenName or userId:

company.security.auth.type=screenName

Type http://localhost:8080/api/axis in your browser and see a list of SOAP services.
To access the wsdl, click on the wsdl for any of the service.

PS: In old Liferay versions you could access that services by http://localhost:8080/tunnel-web/axis, however in Liferay 6.1 this path has changed and when you type it, you are redirected to the new one.

Example

Now, to access these services we must follow some steps:

1. Add a Web Service Client to your Project by typing a service definition.
Example: http://localhost:8080/api/axis/Portal_UserService?wsdl
After this, it will be added some auxiliar files to your project automatically.

 

2. Add a client code to call a service.

To call the web service using credentials, you would use the following URL syntax:

http://" + [userId / sreenName] + ":" + password + "@<server.com>:<port>/api/secure/axis/" + serviceName

If we use the default authentication method (emailAddress), we will get the (401) Unauthorized error from the server.

And if we use the previous URL (with /tunnel-web/secure/axis) we will get the (301) Moved Permanently error.

So, the final URL will be something like this:

http://2:test@localhost:8080/api/secure/axis/Portal_UserService

Follows a sample cliente code:

That's it. :)

Showing 1 result.