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

Servlet and Filter Plugin

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

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/):

  • Bundle-Name: The name of your bundle
  • Bundle-SymbolicName: A short an unique name for the bundle
  • Bundle-Activator: Package and name of your Activator class (example: com.dotmarketing.osgi.servlet.Activator)
  • DynamicImport-Package: *. Dynamically add required imports the plugin may need without adding them explicitly.
  • Import-Package: This is a comma separated list of package's name. Lists the packages that you are using inside the bundle plugin and that 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

Beware!!!

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

dotCMS 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.

Source

Under the src folder you will find all the Java classes needed to create a new Servlet:

$ 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, check the log file: catalina.out for logging 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, so it requires the com.dotcms.service plugin to be installed and loaded before building it.