Skip to content

Develop a custom CQ selector

November 14, 2012

An important feature of CQ5 is the ability to natively use as many selectors as you want in a URL.
This means the following URLs will work:

Following code helps to create a custom selectors which return an image layer instance to the incoming request.

@Component(immediate = true, metatype = false)
@Service(value = javax.servlet.Servlet.class)
@Properties({@Property(name = Constants.SERVICE_DESCRIPTION, value = "Custom Image Selector"),
 @Property(name = Constants.SERVICE_VENDOR, value = OsgiServiceProperties.SERVICE_VENDOR),
 @Property(name = "sling.servlet.resourceTypes",
 value = {"foundation/components/parbase", "cq:Page", "myproject/components/custom-rich-content" }),
 @Property(name = "sling.servlet.selectors", value = {"invokeSelector1", "invokeSelector2" }),
 @Property(name = "sling.servlet.extensions", value = {"jpg", "jpeg", "png", "gif", "bmp" }) })
public class CustomImageSelector extends AbstractImageServlet {
 protected Layer createLayer(ImageContext ctx) throws RepositoryException {
 // this will returns the custom image layer object to the incoming request
 // A sample implementation of a custom image selector can be found at /libs/foundation/components/page/ in CQ5
 String selectors = ctx.request.getRequestPathInfo().getSelectorString();
 Layer layer = null;
 if (selectors.contains("invokeSelector1")) {
 } else {

The property “sling.servlet.resourceTypes” defines the incoming resource types of the sling servlet request. It can be CQ and custom resource types like “cq:Page”, “cq:Component” etc.

The property “sling.servlet.selectors” defines the name of the selectors that are defined on this servlet. And the request selectors can be retrieved in the code for processing them individually.

The property “sling.servlet.extensions” defines the extensions that are supported on this selector.

So the image request URL for this selector can be as follows:

The above URL can be resolved as follows:


Displaying Page location: /content/site-1/en_GB/homePageImage

Selectors used in the above URL: invokeSelector1, helloworld, 1350657093639

Selector extension: png

Finally the developed image selector can be used for following URLs:

But cannot be used in following URLs:<b>htm</b>

As the above URLs selectors extensions do not match with the desired/expected extensions.



From → Day/Adobe CQ

  1. Uhm, could you please rewrite this to use instead of I get a lot of 404s in my logs because Google tries to find the PNG on my blog. =P

  2. Akhter permalink

    Is there any way I can get the source code

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: