PortalURL plays a similar role at the portal level. Its main role is to abstract the creation of an URL for a resource managed by the portal.
public abstract class PortalURL<R, U extends PortalURL<U>>
{
...
}
The PortalURL declaration may seem a bit strange at first sight with two generic types: U and R.
The R generic type represents the type of the resource managed by the portal.
The U generic type is also described as self bound generic type. This design pattern allows a class to return subtypes of itself in the class declaring the generic type. Java Enums are based on this principle (class Enum<E extends Enum<E>>).
A portal URL has various methods but certainly the most important method is the toString() method that generates an URL targeting to the resource. The remaining methods are getter and setter used to mutate the URL configuration, those options will affect the URL representation when it is generated.
resource: the mandatory resource associated with the URL.
locale: the optional locale used in the URL allowing the creation of bookmarkable URL containing a language.
confirm: the optional confirmation message displayed by the portal in the context of the portal UI.
ajax: the ajax option allowing an ajax invocation of the URL.
Obtaining a PortalURL
PortalURL objects are obtained from RequestContext instance, such as the PortalRequestContext, or the PortletRequestContext. Usually, those are obtained thanks to the getCurrentInstance method of the RequestContext class:
RequestContext ctx = RequestContext.getCurrentInstance();
PortalURL are created via to the createURL method that takes an input as a resource type. The resource type is usually a constant and type-safe object that allows to retrieve the PortalURL subclasses:
RequestContext ctx = RequestContext.getCurrentInstance();
PortalURL<R, U> url = ctx.createURL(type);
In reality, you will use a concrete type constant and have instead more concrete code like:
RequestContext ctx = RequestContext.getCurrentInstance();
NodeURL url = ctx.createURL(NodeURL.TYPE);
The NodeURL.TYPE is actually declared as new ResourceType<NavigationResource, NodeURL>() that can be described as a type-literal object emulated by a Java anonymous inner class. Such literal was introduced by Neil Gafter as Super Type Token and popularized by Google Guice as Type Literal. It is an interesting way to create a literal representing a kind of Java type.