How to Expose and Consume a Method

Table of Contents [-]

Use Case: a remote app (CAS for SSO) to authenticate against Liferay Portal

Out of box, Liferay does not expose authentication methods so this is what I did to expose one of their methods in Extension Environment.

Questions/Comments can be posted at http://www.liferay.com/web/guest/community/forums/message_boards/message/227226

1. edit service.xml to include the following

<entity name="MyUser" local-service="false" remote-service="true"></entity>

2. build service, add a method in MyUserServiceImpl, and rebuild service

public int authenticateByEmailAddress(
		long companyId, String emailAddress, String password)
	throws PortalException, SystemException {

	Map headerMap = null;
	Map parameterMap = null;
	
	int isAuthenticated = UserLocalServiceUtil.authenticateByEmailAddress(companyId, emailAddress, password, headerMap, parameterMap);
	
	return isAuthenticated;
}

3. Generate server-config.wsdd

  • For Liferay versions prior to v5.1.x: "ant build-wsdd" in ext-impl/ to generate /ext-web/docroot/WEB-INF/server-config.wsdd for SOAP calls
  • For Liferay versions v5.1.x: Edit the /ext-impl/build.xml adding appropriate antcall tasks to execute the "build-wsdd" task. Within the build.xml, modify each service task as follows, then execute "ant build-services" in ext-impl/ to generate services and server-config.wsdd:
	<target name="build-service-servicename">
		<antcall target="build-service">
			<param name="service.file" value="src/com/ext/path/to/your/service.xml" />
		</antcall>
		<antcall target="build-wsdd">
			<param name="service.file" value="src/com/ext/path/to/your/service.xml" />
		</antcall>
	</target>

4. ant clean deploy and you are done exposing a method. Check it out at /tunnel-web/secure/axis. :)


To consume the newly exposed method, you can generate ext-client.jar just like Liferay did with portal-client.jar

1. create build.xml in ext-client/

<?xml version="1.0"?>

<project name="ext-client" basedir="." default="compile">
	<import file="../build-common-java.xml" />

	<property name="jar.file" value="${ant.project.name}" />
	<property name="client.url" value="http://localhost:8080/tunnel-web/axis" />

	<target name="jar" depends="compile">
		<jar
			jarfile="${jar.file}.jar"
		>
			<fileset dir="classes" />
			<fileset dir="src" />
		</jar>

		<copy file="ext-client.jar" todir="${project.dir}/ext-lib/development" />
	</target>

	<target name="build-client" depends="clean">
		<echo message="Make sure the server is listening on ${client.url}." />
		<echo message="" />

		<delete dir="src" />
		<mkdir dir="src" />

		<java
			classname="com.liferay.portal.tools.PortalClientBuilder"
			classpathref="project.classpath"
			failonerror="true"
			fork="true"
			newenvironment="true"
		>
			<arg value="${project.dir}/ext-web/docroot/WEB-INF/server-config.wsdd" />
			<arg value="src" />
			<arg value="namespaceMapping.properties" />
			<arg value="${client.url}" />
		</java>

		<antcall target="jar" />
	</target>
</project>

2. create namespaceMapping.properties and put in a mapping (package=namespace). Hint: use namespace from the value of wsdlTargetNamespace in server-config.wsdd

com.company.portal.service.http=http.service.portal.company.com

3. While localhost is up and running, run "ant build-client" to generate ext-client.jar

4. Test consuming with ext-client.jar

import java.net.URL;

import com.company.portal.service.http.MyUserServiceSoap;
import com.company.portal.service.http.MyUserServiceSoapServiceLocator;

public class LiferayClient {
	public static void main(String [] args) {
		long userId = 54321L;
		long companyId = 12345L;
		String email = "me@company.com";
		String password = "notTellingYou";
		
		try {
			MyUserServiceSoapServiceLocator locator = new MyUserServiceSoapServiceLocator();
			MyUserServiceSoap soap = locator.getPortal_MyUserService(_getURL(Long.toString(userId), "Portal_MyUserService"));
			int isAuthenticated = soap.authenticateByEmailAddress(companyId, email, password);
			System.out.println("is user authenticated? " + isAuthenticated);
		} catch (Exception e) {
			System.err.println(e.toString());
		}
		
	}
	private static URL _getURL(String remoteUser, String serviceName) throws Exception {
		String password = "secret";
		url = "http://" + remoteUser + ":" + password + "@localhost:8080/tunnel-web/secure/axis/" + serviceName;

		return new URL(url);
	}
}

Related Articles #

How To's

0 Attachments
27940 Views
Average (3 Votes)
The average rating is 3.66666666666667 stars out of 5.
Comments
Threaded Replies Author Date
Thanks for the detailed steps you have... Jay Patel July 29, 2010 11:30 PM

Thanks for the detailed steps you have mentioned..

But some points I would like to add are:

- You will have to copy build-common-java.xml in ext-client from portal source as it is included in build.xml of ext-client.

- Also ant call "ant build-client" takes too much time to execute & most of the time you have to suspend it & run "ant deploy" to get generated ext-client.jar
Posted on 7/29/10 11:30 PM.