Servlet and Filter Plugin - Documentation topics on: filter,osgi,plugins,servlet,.

Servlet and Filter Plugin

This bundle plugin is an example of how to how to register servlets and filters and how to use services provided by other bundles.

How to create a bundle plugin using services and registering servlets and filters Let’s review the file organization on the Servlet (com.dotcms.servlet) example:

META-INF/MANIFEST.MF: The manifest file is very important for the deployment, it lists the Bundle Name, Version and Packages.

In this MANIFEST you must specify (see template plugin- dotCMS/docs/examples/):

PropertyDescription
Bundle-NameA descriptive name for your bundle.
Bundle-SymbolicNameA short and unique name for the bundle.
Bundle-ActivatorPackage and name of your Activator class (example: com.dotmarketing.osgi.servlet.Activator).
DynamicImport-PackageSet this value to .* to dynamically add required imports the plugin may need without adding them explicitly.
Import-PackageA comma separated list of package names you are using inside the bundle plugin, which are exported by dotCMS at runtime.

$ more META-INF/MANIFEST.MF Manifest-Version: 1.0 Bundle-Name: Servlet for Hello World Bundle-SymbolicName: com.dotmarketing.osgi.servlet Bundle-Description: Servlet for Hello World Bundle-Version: 1.0.0 Bundle-Activator: com.dotmarketing.osgi.servlet.Activator DynamicImport-Package: * Import-Package: org.osgi.framework, org.osgi.util.tracker, javax.servlet, javax.servlet.http, org.osgi.service.http, org.apache.felix.http.api, com.dotmarketing.osgi.service


Important

In order to work inside the Apache Felix OSGI runtime, the import and export directives must be bi-directional.

You must declare the set of packages that will be available to the OSGI plugins in this file: dotCMS/WEB-INF/felix/osgi-extra.conf.

This is possible also using the dotCMS UI (System → Dynamic Plugins → Exported Packages).

Only after the exported packages are defined in this list, can a plugin Import the packages to use them inside the OSGI bundle.

The src Folder

The Java classes needed to create a new Servlet are contained in the src folder:

$ pwd
/dotCMS/docs/examples/osgi/com.dotcms.servlet
$ ls -l src/com/dotmarketing/osgi/servlet/
-rw-r--r-- 1 Activator.java
-rw-r--r-- 1 HelloWorldServlet.java
-rw-r--r-- 1 TestFilter.java

com.dotmarketing.osgi.servlet.HelloWorldServlet

This is a simple and standard implementation of a HttpServlet that will use the HelloWorld service provided by the com.dotcms.service bundle plugin.

com.dotmarketing.osgi.servlet.TestFilter

Simple and standard implementation of a Filter.

Activator

This bundle activator extends from com.dotmarketing.osgi.GenericBundleActivator and implements BundleActivator.start().

The Activator gets a reference for the HelloWorldService via HelloWorld interface (com.dotcms.service bundle plugin) and registers the HelloWorldServlet servlet and TestFilter filter.

Testing

The HelloWorldServlet is registered under the url pattern “/helloworld” can be tested while running using the following URL (assuming your dotcms url is http://localhost:8080):

http://localhost:8080/app/helloworld

The TestFilter filter is registered for the url pattern “/helloworld/.*” and can be tested using the following URL's (assuming your dotcms url is http://localhost:8080):

http://localhost:8080/app/helloworld/ http://localhost:8080/app/helloworld/testing.dot

To verify the filter is running, review the Tomcat log file (catalina.out) for log messages generated by the filter:

[testFilter] Filter request [/app/helloworld/]
[testFilter] Filter request [/app/helloworld/testing.html]
[testFilter] Filter request [/app/helloworld/testing.html]
[testFilter] Filter request [/app/helloworld/testing.html]

Note:

  • This plugin uses classes provided by the com.dotcms.service plugin.
    • Therefore the com.dotcms.service plugin must be installed and loaded before you can build this plugin.