Consuming Liferay Web Services

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. :)

16
Blogs
Great Post! this saved my day!
Authentication is done using HTTP Basic Authentication, which of course is not appropriate for a production environment, since the password is unencrypted! what about SAML, WS-SECURITY or other advanced security/authentication scenario?
Nice post but it would also be nice to know (i.e. without using the ext environment- http://www.christysering.com/blog/liferay/liferay-allow-user-to-sign-in-with-email-or-screen-name/), how to accomplish this when all my active users are already logging into the portlet UI with emailAddress?
(401) Unauthorized error from the server.Im not able to find a resolution .please helop.
Thanks in advance
Hi Antônio,
a very nice article.
Recently on my blog I posted an article on how to use Liferay Portal Client. On my GitHub repository I published the full sample. Repository at https://github.com/amusarra/liferay-portal-client-example and article at http://musarra.wordpress.com/2013/05/23/liferay-portal-client-example-en/.

Bye,
Antonio.
Greate Post!
Just in case anyone is interested in using email address as username instead of screenName.
Replace these lines
<code>
String screenName = "test";
.
.
.
sb.append(screenName);
</code>

with these ones

<code>
String emailAddress = URLEncoder.encode("test@liferay.com", "UTF-8");
.
.
.
sb.append(emailAddress );
</code>

It works to authenticate an user remotely as well.
Obviously, u need to remove this line: "company.security.auth.type=screenName" from portal-ext.properties.
Hope it helps.