Class FilterHandler
java.lang.Object
sunlabs.brazil.filter.FilterHandler
- All Implemented Interfaces:
Handler
The
FilterHandler captures the output of another
Handler and allows the ouput to
be modified. One or more
Filters
may be specified to change that output
before it is returned to the client.
This handler provides one of the core services now associated with the Brazil Server: the ability to dynamically rewrite web content obtained from an arbitrary source.
For instance, the FilterHandler can be used as a proxy for
a PDA. The wrapped Handler would go to the web to
obtain the requested pages on behalf of the PDA. Then, a
Filter would examine all "text/html" pages and rewrite the
pages so they fit into the PDA's 200 pixel wide screen. Another
Filter would examine all requested images and dynamically
dither them to reduce the wireless bandwidth consumed by the PDA.
The following configuration parameters are used to initialize this
Handler:
- prefix, suffix, glob, match
- Specify the URL that triggers this handler.
(See
MatchString). -
handler - The name of the
Handlerwhose output will be captured and then filtered. This is called the "wrapped handler". -
filters - A list of
Filternames. The filters are applied in the specified order to the output of the wrapped handler. -
exitOnError - If set, the server's
initFailurewill set any of the filters fail to initialize. No handler prefix is required.
handler=filter port=8081 filter.class=sunlabs.brazil.filter.FilterHandler filter.handler=proxy filter.filters=noimg proxy.class=sunlabs.brazil.proxy.ProxyHandler noimg.class=sunlabs.brazil.filter.TemplateFilter noimg.template=sunlabs.brazil.template.NoImageTemplateThese parameters set up a proxy server running on port 8081. As with a normal proxy, this proxy server forwards all HTTP requests to the target machine, but it then examines all HTML pages before they are returned to the client and strips out all
<img> tags. By applying
different filters, the developer could instead build a server - to automatically dither embedded images down to grayscale (instead of simply stripping them all out)
- to apply pattern recognition techniques to strip out only the advertisements
- to examine and change arbitrary URLs on the page
- to extract the content from an HTML page and dynamically combine it with another file to produce a different look-and-feel.
responnd
for a more detailed explaination.- Version:
- 2.3
- Author:
- Stephen Uhler (stephen.uhler@sun.com), Colin Stevens (colin.stevens@sun.com)
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanStart the handler and filter classes.booleanResponds to an HTTP request by the forwarding the request to the wrappedHandlerand filtering the output of thatHandlerbefore sending the output to the client.
-
Field Details
-
handler
-
filters
-
-
Constructor Details
-
FilterHandler
public FilterHandler()
-
-
Method Details
-
init
Start the handler and filter classes.- Specified by:
initin interfaceHandler- Parameters:
server- The HTTP server that created thisHandler. TypicalHandlers will useServer.propsto obtain run-time configuration information.prefix- The handlers name. The string thisHandlermay prepend to all of the keys that it uses to extract configuration information fromServer.props. This is set (by theServerandChainHandler) to help avoid configuration parameter namespace collisions.- Returns:
trueif thisHandlerinitialized successfully,falseotherwise. Iffalseis returned, thisHandlershould not be used.
-
respond
Responds to an HTTP request by the forwarding the request to the wrappedHandlerand filtering the output of thatHandlerbefore sending the output to the client.At several stages, the
Filtersare given a chance to short-circuit this process:- Each
Filteris given a chance to examine the request before it is sent to theHandlerby invoking its respond() method. TheFiltermay decide to change the request's properties. AFiltermay even return some content to the client now, by (callingrequest.sendResponse()and returning true), in which case, neither theHandlernor any furtherFilters are invoked at all. - Assuming the respond() methods of all the filters
returned false (which is normally the case),
the
handler's respond()method is called, and is expected to generate content. If no content is generated at this step, this handler returns false. - After the
Handlerhas generated the response headers, but before it has generated any content, eachFilteris asked if it would be interested in filtering the content. If noFilteris, then the subsequent content from theHandlerwill be sent directly to the client. - On the other hand, if any
Filteris interested in filtering the content, then the output of theHandlerwill be sent to each of the interestedFilters in order. The output of each interestedFilteris sent to the next one; the output of the finalFilteris sent to the client. - At this point, any one of the invoked
Filters can decide to reject the content completely, instead of rewriting it.
Filterfor a description of how to cause filters to implement the various behaviors defined above.- Specified by:
respondin interfaceHandler- Parameters:
request- The HTTP request to be forwarded to one of the sub-servers.- Returns:
trueif the request was handled and content was generated,falseotherwise.- Throws:
IOException- if there was an I/O error while sending the response to the client.
- Each
-