In this blog­post I want to give you an overview of my favourite CQ5 util classes.

Some of these classes might already be known to you, hope­full you will find a new one that will help you dur­ing your CQ5 devel­op­ment work.

With every class I will give a small code exam­ple on how you can use it.

ResourceU­til

http://​dev​.day​.com/​d​o​c​s​/​e​n​/​c​q​/​c​u​r​r​e​n​t​/​j​a​v​a​d​o​c​/​o​r​g​/​a​p​a​c​h​e​/​s​l​i​n​g​/​a​p​i​/​r​e​s​o​u​r​c​e​/​R​e​s​o​u​r​c​e​U​t​i​l​.​h​tml

// checks if the resource exists
ResourceUtil.isNonExistingResource(myResource);

// always gets a Val­ueMap from a Resource
ResourceUtil.getValueMap(myResource);

DamU­til

http://​dev​.day​.com/​d​o​c​s​/​e​n​/​c​q​/​c​u​r​r​e​n​t​/​j​a​v​a​d​o​c​/​c​o​m​/​d​a​y​/​c​q​/​d​a​m​/​c​o​m​m​o​n​s​/​u​t​i​l​/​D​a​m​U​t​i​l​.​h​tml

//check if the Resource is an Asset
DamUtil.isAsset(myResource);

ISO8601

http://​jackrab​bit​.apache​.org/​a​p​i​/​2​.​4​/​o​r​g​/​a​p​a​c​h​e​/​j​a​c​k​r​a​b​b​i​t​/​u​t​i​l​/​I​S​O​8​6​0​1​.​h​tml

//formats the Cal­en­dar into ISO8601 for­mat
ISO8601.format(calendar);

JcrU­til

http://​dev​.day​.com/​d​o​c​s​/​e​n​/​c​q​/​c​u​r​r​e​n​t​/​j​a​v​a​d​o​c​/​c​o​m​/​d​a​y​/​c​q​/​c​o​m​m​o​n​s​/​j​c​r​/​J​c​r​U​t​i​l​.​h​tml

// cre­ates a valid JCR-name
JcrUtil.createValidName(myCustomName);

JcrU­tils

http://​dev​.day​.com/​d​o​c​s​/​e​n​/​c​q​/​c​u​r​r​e​n​t​/​j​a​v​a​d​o​c​/​o​r​g​/​a​p​a​c​h​e​/​j​a​c​k​r​a​b​b​i​t​/​c​o​m​m​o​n​s​/​J​c​r​U​t​i​l​s​.​h​tml

// if the folder doesn’t exist, it will be cre­ated
JcrUtils.getOrAddFolder(parent, name);

Prop­er­tiesU­til

http://​dev​.day​.com/​d​o​c​s​/​e​n​/​c​q​/​c​u​r​r​e​n​t​/​j​a​v​a​d​o​c​/​o​r​g​/​a​p​a​c​h​e​/​s​l​i​n​g​/​c​o​m​m​o​n​s​/​o​s​g​i​/​P​r​o​p​e​r​t​i​e​s​U​t​i​l​.​h​tml

// gets a boolean prop­erty when start­ing an OSGi-component

@Activate
pro­tected void activate(ComponentContext con­text) {
PropertiesUtil.toBoolean(context.getProperties().get(“myproperty”), false);
}

Ser­vice­Tracker

http://​www​.osgi​.org/​j​a​v​a​d​o​c​/​r​4​v​4​2​/​o​r​g​/​o​s​g​i​/​u​t​i​l​/​t​r​a​c​k​e​r​/​S​e​r​v​i​c​e​T​r​a​c​k​e​r​.​h​tml

// gets the ref­er­ences to ser­vices loaded in the OSGi-container
serviceTracker.getServiceReference()

JcrResourceUtil.html

http://​dev​.day​.com/​d​o​c​s​/​e​n​/​c​q​/​c​u​r​r​e​n​t​/​j​a​v​a​d​o​c​/​o​r​g​/​a​p​a​c​h​e​/​s​l​i​n​g​/​j​c​r​/​r​e​s​o​u​r​c​e​/​J​c​r​R​e​s​o​u​r​c​e​U​t​i​l​.​h​tml

// cre­ates a full path, with autosave if needed
JcrResourceUtil.createPath(… )

TagUtil

http://​dev​.day​.com/​d​o​c​s​/​e​n​/​c​q​/​c​u​r​r​e​n​t​/​j​a​v​a​d​o​c​/​o​r​g​/​a​p​a​c​h​e​/​s​l​i​n​g​/​s​c​r​i​p​t​i​n​g​/​j​s​p​/​u​t​i​l​/​T​a​g​U​t​i​l​.​h​tml

// gets the SlingRequest/SlingResponse when you are cre­at­ing cus­tom tags

Page­Con­text page­Con­text = (Page­Con­text) getJspContext();

SlingHttpServle­tRe­quest slin­gRe­quest = TagUtil.getRequest(pageContext);
SlingHttpServle­tRe­sponse slin­gRe­sponse = TagUtil.getResponse(pageContext);

User­Prop­er­tiesU­til

http://​dev​.day​.com/​d​o​c​s​/​e​n​/​c​q​/​c​u​r​r​e​n​t​/​j​a​v​a​d​o​c​/​c​o​m​/​d​a​y​/​c​q​/​p​e​r​s​o​n​a​l​i​z​a​t​i​o​n​/​U​s​e​r​P​r​o​p​e​r​t​i​e​s​U​t​i​l​.​h​tml

// check to see if a real user is logged in

boolean realuser = ! UserPropertiesUtil.isAnonymous(request);

@heervisscher