Wednesday, September 3, 2008

Sling OSGI Track pt 6: Sling Servlets I

This is the continuation of

In this part, I will show how to create your own Sling Servlet and deploy it as an OSGi Bundle.

As you may have seen from the sling documentation, there is a pretty straightforward way to create scripts on the search paths (/apps, /libs) that will be executed depending on properties of the requested resource (most notably sling:resourceType.

You can do the same using servlets, if you prefer.
Using servlets, you can also do a lot more than with scripts, and we'll have a first look at what can be done right now.
As a first easy example, I will show how to create a servlet that uses the service created in the previous parts and makes the output available at /system/osgitest/info .


  • Show how to create a Sling Servlet and register it to respond to a preset path.




  • Project structure
  • The servlet implementation
  • The pom: nothing special
  • Maven dependencies: oops
  • Build, Deploy, Test


Project structure

The project structure is simpler than anything we had so far. We actually need no more than the servlet implementation and the pom. Let's have a look:

|-- pom.xml
`-- src
    `-- main
        `-- java
            `-- mh
                `-- osgitest
                    `-- client
The servlet implementation

Let's have a look at the servlet implementation:

  • The servlet extends SlingSafeMethodsServlet (l. 25).
    This is a sling-provided class that can be extended to implement servlets that do not have side-effects, i.e. do not modify data. See for the details.
  • The servlet overrides the doGet method (l. 31ff).
    In this method, it checks whether it's "service" variable is set (which is of the type 'SampleService' from our previous parts of this track), and if so calls sayHello() on the object to write the result to the output stream.
  • OK, so how can the service variable ever be anything but null? That happens through the src.reference directive in line 27. This will create an OSGi service descriptor, and the OSGi container will inject the reference to a SampleService instance, if available.
  • In line 22, the sling.servlet.paths property is set to /system/osgitest/info. This creates the mapping to the URL /system/osgitest/info, which the servlet will respond to.
    There are many other options of creating mappings depending on the resource that is being accessed, depending on method, etc. Part of this is explained in, but that description is outdated. I'll look into the other options in further posts.
package mh.osgitest.client;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import mh.osgitest.SampleService;


 * A Servlet that uses the service provided in the mh.osgitest to retrieve the
 * phrase "hello".
 * @scr.component immediate="true" metatype="no"
 * @scr.service interface="javax.servlet.Servlet"
 * name="service.description" value="Sample OSGi Client Servlet"
 * name="service.vendor" value="Moritz Havelock"
 * name="sling.servlet.paths" value="/system/osgitest/info"
public class OsgiTestClientServlet extends SlingSafeMethodsServlet {
	/** @scr.reference */
	private final SampleService service = null;

	protected void doGet(SlingHttpServletRequest request,
			SlingHttpServletResponse response) throws ServletException,
			IOException {
		if (service == null) {
					"no reference to service available");
		} else {
The pom: nothing special

The pom looks quite familiar. What's different this time?
On lines 93-97, we have a dependency on the artifact. That is because we are using sling specific types in the servlet implementation. I have here used the version 2.0.0.incubator-SNAPSHOT, which is what comes with CRX Cup edition.
On lines 98-102, we depend on the artifact. This should be in your m2 repository from the previous tracks.

<project xmlns="" xmlns:xsi=""
   <name>A Servlet Client for the OSGi Test Bundle</name>
   <description />
         <name>Apache Incubating Repository</name>
         <name>Apache Incubating Plugin Repository</name>
Maven dependencies: oops

Everything looks easy so far, but there's one catch: no public m2 repository can satisfy the dependency on, so that when you now run mvn package, you will end up with the following:

D:\projekte\workspace-sling\>mvn package
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building A Servlet Client for the OSGi Test Bundle
[INFO]    task-segment: [package]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] snapshot checking for updates from apache.incubating


[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Failed to resolve artifact.


  Try downloading the file manually from the project website.

  Then, install it using the command:
      mvn install:install-file -Dversion=2.0.0.incubator-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file

  Alternatively, if you host your own repository you can deploy the file there:
      mvn deploy:deploy-file -Dversion=2.0.0.incubator-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

  Path to dependency:

1 required artifact is missing.

for artifact:

from the specified remote repositories:
  central (,
  apache.incubating (

[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 seconds
[INFO] Finished at: Wed Sep 03 16:56:39 CEST 2008
[INFO] Final Memory: 5M/9M
[INFO] ------------------------------------------------------------------------

Don't let it bring you down -- the problem is easily solved, even if it takes a few steps. Note the lines 21 - 26 above. They already show that there is a solution at hand. Now all we need is the jar file. Don't look on the sling website, it's already on your machine. Go to the place you started CRX Quickstart from, and you'll find it here: ./crx-quickstart/server/runtime/0/_/WEB-INF/resources/bundles/ Now, you only need to execute the following:

mvn install:install-file -Dversion=2.0.0.incubator-SNAPSHOT -Dpackaging=jar \
  -Dfile=<crx quickstart dir>/crx-quickstart/server/runtime/0/_/WEB-INF/resources/bundles/
Build, Deploy, Test

All that remains is mvn clean install, and go to http://localhost:7402/system/osgitest/info

No comments: