Accessing Liferay API from Your Portlet

Configure Dependencies

Download and Install the Liferay portal-service.jar and portal-kernel.jar

  • If you have Liferay installed, the jars can be found in <liferay-install-dir>/lib/ext
  • If you are using Maven, you will probably want to install this jar to you local repository:
mvn install:install-file -Dfile=portal-service.jar \
  -DgroupId=com.liferay \
  -DartifactId=portal-service \
  -Dversion=5.1.2 \
  -Dpackaging=jar

mvn install:install-file -Dfile=portal-kernel.jar \
  -DgroupId=com.liferay \
  -DartifactId=portal-kernel \
  -Dversion=5.1.2 \
  -Dpackaging=jar
  • To install to our repository:
    • make sure the following server is set in your maven settings.xml:
<server>
   <id>intelliwareReleases</id>
   <username>root</username>
   <password>****</password>
</server>
    • run the following commands at the command line:
mvn deploy:deploy-file -DgroupId=com.liferay \
-DartifactId=portal-service -Dversion=5.1.2 -DgeneratePom=true \
-Dpackaging=jar -Dfile=portal-service.jar \
-DrepositoryId=intelliwareReleases \
-Durl=scp://mvnrepo.intelliware.ca/var/www/repo/release

mvn deploy:deploy-file -DgroupId=com.liferay \
-DartifactId=portal-kernel -Dversion=5.1.2 -DgeneratePom=true \
-Dpackaging=jar -Dfile=portal-kernel.jar \
-DrepositoryId=intelliwareReleases \
-Durl=scp://mvnrepo.intelliware.ca/var/www/repo/release
  • Add the following to your portlet pom.xml:
<dependency>
    <groupId>com.liferay</groupId>
    <artifactId>portal-service</artifactId>
    <version>5.1.2</version>
</dependency>
<dependency>
    <groupId>com.liferay</groupId>
    <artifactId>portal-kernel</artifactId>
    <version>5.1.2</version>
</dependency>
  • Add a file called context.xml to your META-INF directory, containing:
<Context>
	<Loader loaderClass="com.liferay.support.tomcat.loader.PortalClassLoader"/>
</Context>
    • NOTE: as soon as you do this, you may encounter collisions between the jars in your portlet and the jars within Liferay itself, as you are now sharing the same classloader. You may need to add <scope>provided</scope> to your Maven dependencies.

Accessing the API

  • once the above setup is complete, you should be able to deploy the portlet and use Liferay’s API to get at Liferay values. For example, to get the user, you would do the following:

Tapestry Example:

private static final String LIFERAY_USER_ID_KEY = "liferay.user.id";

public User getLoggedInUser() {
    UserService service = UserServiceUtil.getService();
    String userId = getLoggedInUserId();
    if (StringUtils.isNotBlank(userId)) {
        return service.getUserById(userId);
    }
    return null; // or throw exception
}

protected String getLoggedInUserId() {
    if (isUserLoggedIn()) {
        Map<String, String> userInfoMap = getUserInfoMap();
        if (userInfoMap != null) {
            return userInfoMap.get(LIFERAY_USER_ID_KEY);
        }
    }
    return StringUtils.EMPTY;
}

public boolean isUserLoggedIn() {
    Map<String, String> userInfoMap = getUserInfoMap();
    if (userInfoMap != null && StringUtils.isNotBlank(userInfoMap.get(LIFERAY_USER_ID_KEY))) {
        return true;
    }
    return false;
}

It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply