Creating and Using Custom Filters

                                                                                                                                                                                                                                                                                                                                      Back to [Book]     [Topic]

Creating and Using Custom Filters

Custom filters are defined in shared libraries that are loaded and called by the server. The general steps for creating a custom filter are as follows:

These steps are described in greater detail in the following sections.

Writing the Source Code

Write your custom filter methods using NSAPI functions. For a summary of the NSAPI functions specific to filter development, see NSAPI Functions for Filter Development and Filter Methods for the filter method prototypes.

The filter must be created by a call to filter_create. Typically, each plug-in defines an nsapi_module_init function that is used to call filter_create and perform any other initialization tasks. For more information, see nsapi_module_init() Function and filter_create() Function.

Filter methods are invoked whenever the server or an SAF calls certain NSAPI functions such as net_write or filter_insert. As a result, filter methods can be invoked from any thread and should only block using NSAPI functions. For example, crit_enter and net_read. If a filter method blocks using other functions, for example, the Windows WaitForMultipleObjects and ReadFile functions, the server could hang. Also, shared objects that define filters should be loaded with the NativeThread="no" flag, as described in Loading and Initializing the Filter.

If a filter method must block using a non-NSAPI function, KernelThreads 1 should be set in magnus.conf. For more information about KernelThreads, see the description in Syntax and Use of magnus.conf in the Sun Java System Web Server Administrator's Configuration File Reference.

Keep the following in mind when writing your filter:

  • Write thread-safe code.
  • IO should only be performed using the NSAPI functions documented in File I/O.
  • Thread synchronization should only be performed using NSAPI functions documented in Threads.
  • Blocking might affect performance.
  • Carefully check and handle all errors.

For examples of custom filters, see Examples of Custom SAFs and Filters.

Compiling and Linking

Filters are compiled and linked in the same way as SAFs. For more information, see Compiling and Linking.

Loading and Initializing the Filter

For each shared library (plug-in) containing custom filters to be loaded into the server, add an Init directive that invokes the load-modules SAF to magnus.conf. The syntax for a directive that loads a filter plug-in is:

Init fn=load-modules shlib=_path_ NativeThread="no"
  • shlib is the local file system path to the shared library (plug-in).
  • NativeThread indicates whether the plug-in requires native threads. Filters should be written to run on any type of thread, as described in Writing the Source Code.

When the server encounters such a directive, it calls the plug-in's nsapi_module_init function to initialize the filter.

Instructing the Server to Insert the Filter

Add an Input or Output directive to obj.conf to instruct the server to insert your filter into the filter stack. The format of the directive is as follows:

Directive fn=insert-filter filter="filter-name" [ name1= "value1"]...[ nameN= "valueN"]

  • Directive is Input or Output.
  • filter-name is the name of the filter, as passed to filter_create, to insert.
  • nameN="valueN" are the names and values of parameters that are passed to the filter's insert filter method.

Filters that process incoming data should be inserted using an Input directive. Filters that process outgoing data should be inserted using an Output directive.

To ensure that your filter is inserted whenever a client sends a request, add the Input or Output directive to the default object. For example, the following portion of obj.conf instructs the server to insert a filter named example-replace and pass it two parameters, from and to:

<Object name="default">
Output fn=insert-filter
       filter="example-replace"
       from="Old String"
       to="New String"
...
</Object>       

Restarting the Server

For the server to load your plug-in, you must restart the server. A restart is required for all plug-ins that implement SAFs and/or filters.

Testing the Filter

Test your filter by accessing your server from a web browser. You should disable caching in your web browser so that the server is sure to be accessed. In Mozilla Firefox, you may hold the shift key while clicking the Reload button to ensure that the cache is not used. Examine the access and error logs to help with debugging.

Labels

java java Delete
server server Delete
sun sun Delete
webserver webserver Delete
application application Delete
system system Delete
webtier webtier Delete
guide guide Delete
webserver70 webserver70 Delete
sunjava sunjava Delete
developers developers Delete
web web Delete
+nsapiguide +nsapiguide Delete
developersguide developersguide Delete
nsapi nsapi Delete
nsapidevelopersguide nsapidevelopersguide Delete
nsapiguide nsapiguide Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Sign up or Log in to add a comment or watch this page.


The individuals who post here are part of the extended Sun Microsystems community and they might not be employed or in any way formally affiliated with Sun Microsystems. The opinions expressed here are their own, are not necessarily reviewed in advance by anyone but the individual authors, and neither Sun nor any other party necessarily agrees with them.

Copyright 1994-2009 Sun Microsystems, Inc.
Powered by Atlassian Confluence
Sun Guidelines on Public Discourse Privacy Policy Terms of Use Trademarks Site Map Employment Investor Relations Contact