content for the masses

Marketing technologist, content management strategist, digital platform architect, technology evangelist.

Non Existing Content in Sling

You’re human. You make mistakes. So, what happens when you enter an incorrect url within a Sling web application? By default, you get a 404 page telling you there is not such resource for the supplied url. However, why not take advantage of the built-in Sling request progress tracker?

It’s like my old maths teacher used to say, show you’re working out and if you get it wrong, no problem, at least we can see how you got there.”

The request progress tracker dumps out Slings’s working out, detailing what it tried and how it failed to render the supplied resource. This was nicely written up on dev.day.com and I’ve tried to condense it down.

Pre-requisite:

Setting up the Non-Existing Script

Entering the url http://localhost:8080/george/bush/is/out results in the following response within Sling.

However, wouldn’t it be good if we could get something a little more informative back like:

Now, lets step through this trace. To start with, the number in the first column is NOT the line number. Throughout the request, the progress tracker maintains a timer and the number of the left tells us when, in milliseconds, the action took place. So on line 3, we see that after 3 ms the /george/bush/is/out resource is mapped to NonExistingResource. All resources have types. The built-in NonExistingResource is of type sling:nonexisting.

Now that we have a resource, Sling needs a script to act on it. Mapping the resource onto a script is seen above by the Starting ServletResolution. Sling looks under the /apps directory for scripts. Furthermore, the search for scripts to act on resources of type sling:nonexisting starts at /apps/sling/nonexisting.

So just to recap, we are performing a GET operation on the NonExistingResource /george/bush/is/out, so its not surprising that the script Sling expects to find a file GET.XXX, where XXX is the script suffix. The suffix for the script, e.g. GET.esp, tells us the language of the script that has been implemented to handle the resource. Other HTTP methods can be supported by providing scripts such as DELETE.XXX, POST.XXX, and so on.

So let’s add GET.esp to /apps/sling/nonexisting that looks like:

<%
	  response.setContentType("text/plain");
	  response.setCharacterEncoding("UTF-8");
	  request.requestProgressTracker.dump(response.getWriter());
%>

And that’s all there is to it to get a full trace for both resource and script resolution within Sling.

More Logging

The script below demonstrates a few extra features that are available to you with the requestProgressTracker, namely setting up timers and writing out more comprehensive log messages.

 <%
		// set up the mime type
		response.setContentType("text/plain");
		response.setCharacterEncoding("UTF-8");

		// start a timer
		request.requestProgressTracker.startTimer("myTimer");

		// log something
		request.requestProgressTracker.log("### Start logging area");
		request.requestProgressTracker.log("### {0} logging area {1}", "Start", "again!");

		// dump out time spent since starting the timer
	        request.requestProgressTracker.logTimer("myTimer");

		// finally dump out log
		request.requestProgressTracker.dump(response.writer);
%>

Here is a very brief run down of the methods available on the requestProgressTracker:

		// start the timer with name timerName
		startTimer(String timerName);

	       // log time elapsed since last timer started with that name
		logTimer(String timerName);

		// better messages
		logTimer(String timeName, String format, Object ... args );

		// log a message
		log(String message);

		// log a more detailed message
		log(String format, Object ...args);