Installation

Obtain the plugin binary package from the Liferay Marketplace and deploy using the Liferay control panel Apps page in the usual way.

If you have pre-existing content in your portal, tiny URLs for them will be automatically generated during portal startup. This operation may take some time to complete, depending on how much content you have on your portal. The portal will not come up until it has finished.

From this point on, tiny URLs will be automatically generated when content is created or edited.

The resulting Tiny URLs have the format portal_url/t/xyzabc.

The following optional setting controls whether the user must be logged in before an attempt is made to display the resource corresponding to a tiny URL. (If not logged in, he will be prompted to log in.) The default is true, i.e. the user must be logged in. If set to false, the user's browser URL bar will show the decoded friendly URL of the resource corresponding to the tiny URL even if it is on a private page. This can result in undesired disclosure of privileged information contained in the friendly URL.

In Liferay 6, it can be set in portal-ext.properties:

tinyurl.secure.decode=true

In Liferay 7, this setting is available in the System Settings control panel page.

Usage

A typical scenario goes like this:

To display the tiny URL to users, a tool included in the plugin can be used. Most commonly, you would place a special web content article on the page.

Step 1: Create a web content structure. It does not need to have any fields.

Step 2: Create a template for the structure, such as the following Velocity code snippet:

For Liferay version 6 (Velocity example):

#set ($tinyURLTools = $utilLocator.findUtil('tinyurl-portlet', 'org.hopeconsultants.portlet.tinyurl.util.TinyURLTools'))

#set ($friendly_url = $request.get('attributes').get('FRIENDLY_URL')) #set ($theme_display = $request.get('theme-display'))

#set ($tiny_url = $tinyURLTools.getTinyURL($!friendly_url, $theme_display))

<div> <label>Tiny URL for this article</label> <input onclick="Liferay.Util.selectAndCopy(this);" readonly="true" value="$!tiny_url" /> </div>

For Liferay version 7 (Freemarker example):

<#assign tiny_url = tinyURLTools.getTinyURL(friendly_url, themeDisplay)>

<div> <label>Tiny URL for this article</label> <input onclick="Liferay.Util.selectAndCopy(this);" readonly="true" value="${tiny_url}" /> </div>

Step 3: Create a web content article using the template. It does not need to have any data.

Step 4: Use a web content display to show the article on the pages you wish.

Similar code can be used a JSP page. Note that in Liferay version 6, the TinyURLTools class expects the flattened ThemeDisplay object available in a web content template, so we have to construct one:

Object tinyURLToolsBean = PortletBeanLocatorUtil.locate("tinyurl-portlet", "org.hopeconsultants.portlet.tinyurl.util.TinyURLTools");

MethodKey getTinyURLMethodKey = new MethodKey(tinyURLToolsBean.getClass(), "getTinyURL", String.class, Map.class);

MapflatThemeDisplay = new HashMap();

flatThemeDisplay.put("server-name", themeDisplay.getServerName()); flatThemeDisplay.put("plid", plid); flatThemeDisplay.put("url-portal", themeDisplay.getURLPortal());

String tinyURL = (String)getTinyURLMethodKey.getMethod().invoke(tinyURLToolsBean, DLUtil.getPreviewURL(fileEntry, null, null, StringPool.BLANK, false, true), flatThemeDisplay);

In Liferay version 7, a Liferay Workspace module project just has to declare a dependency on the package org.hopeconsultants.tinyurl.tools and use code such as:

String friendlyURL = request.getAttribute(_tinyURLTools.FRIENDLY_URL);

String tinyURL = _tinyURLTools.getTinyURL(friendlyURL, themeDisplay);

@Reference

private TinyURLTools _tinyURLTools;

// Or outside of a component class, obtain using the service tracker API.

Usage Example

On a page, place the special web content display and an asset publisher with default settings (i.e. dynamic mode, showing all your assets as abstracts).

When first visiting the page, the tiny URL corresponds to the page. When a user enters the tiny URL into his browser, he will be taken to the same page with the asset publisher showing the abstracts.

If you now click on an asset to show its full content, the tiny URL changes to correspond to that asset. When a user enters this tiny URL into his browser, he will be taken to the same page with the asset publisher showing that asset as full content.

Maintenance

To clean up the tiny URL database table, deleting orphaned entries and creating missing entries, in Liferay 6, add the following line to portal-ext.properties and restart the portal.

tinyurl.verify.on.startup=true

In Liferay 7, this setting is available in the System Settings control panel page.

Supported content types and portlets

Notes:

1: At present, Documents and Media and Documents and Media Display portlets are not supported. Use an Asset Publisher portlet instead.

2: At present only site scope is supported.