pygmy.handlers
Class RedirectHandler

java.lang.Object
  extended by pygmy.core.AbstractHandler
      extended by pygmy.handlers.RedirectHandler
All Implemented Interfaces:
Handler

public class RedirectHandler
extends AbstractHandler

This redirects or rewrites URLs based on a regular expression. It tests the requested URLs against a regular expression. If it finds a match it then uses the substiution expression to rewrite a new URL. For a description of the regular expression language see http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html. This handler operates in two modes either using external redirects (i.e. 302 HTTP code), or internal redirects. The new URL expression can reference groups in the regular expression using ${<group number>}. Substitution expressions can also reference configuration properties by using the notation. For example, ${http.port} would return the port of the pygmy server.

Here is an example of a URL rule and substition expression for creating a URL to user's home directories. Remember to escape "e;\"e; character in your properties files otherwise your expression will not work. To help you debug these problems this Handler will log a message at the debug level so you can see what the regular expression has been set to.

aRedirect.class=pygmy.handlers.RedirectHandler aRedirect.rule=/~(\\w+) aRedirect.subst=/home/${1}/public_html

The new URL built from the subst expression by default will be sent back to the client in a 302 HTTP status code. But, there are cases when you don't want to expose the rewritten URL to the outside world. This handler can internally redirect so that the new URL won't be sent back to the client. In our example above we might want to keep the URL to the user's directory private. Using internal redirects the browser won't see the new URL containing: /home/chuck/public_html. It could be a serious security hole if someone is allowed to request /home/chuck!

RedirectHandler only responds to non-internal requests. This handler will not redirect or rewrite interal requests. This is so redirects don't get into an infinite loop when processing. Something to look out for when using external redirects. Most clients fail if they are redirected too many times.

Parameter NameExplanationDefault ValueRequired
ruleThe regular expression rule to use for matching on the requested URL.NoneYes
substThe string to use for rewriting a new URL that will be used in another request.NoneYes
useInternalIndicates the new URL will be internally redirected. If it is true, then the new URL will be used internally redirected. If false, then the new URL will be sent back to the client with the HTTP code specified by redirectCode.falseNo
redirectCodeThis defines the HTTP code that will be sent back when we substitue or rewrite a URL.302No, but ignored if useInternal is true.


Field Summary
static ConfigOption INTERNAL_OPTION
           
static ConfigOption REDIRECT_CODE_OPTION
           
static ConfigOption RULE_OPTION
           
static ConfigOption SUBST_OPTION
           
 
Fields inherited from class pygmy.core.AbstractHandler
handlerName, server, URL_PREFIX_OPTION, urlPrefix
 
Constructor Summary
RedirectHandler()
           
 
Method Summary
protected  boolean handleBody(HttpRequest request, HttpResponse response)
           
 boolean initialize(java.lang.String handlerName, Server server)
           
protected  boolean isRequestdForHandler(HttpRequest request)
           
 
Methods inherited from class pygmy.core.AbstractHandler
getMimeType, getName, getUrlPrefix, handle, shutdown
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RULE_OPTION

public static final ConfigOption RULE_OPTION

SUBST_OPTION

public static final ConfigOption SUBST_OPTION

INTERNAL_OPTION

public static final ConfigOption INTERNAL_OPTION

REDIRECT_CODE_OPTION

public static final ConfigOption REDIRECT_CODE_OPTION
Constructor Detail

RedirectHandler

public RedirectHandler()
Method Detail

initialize

public boolean initialize(java.lang.String handlerName,
                          Server server)
Specified by:
initialize in interface Handler
Overrides:
initialize in class AbstractHandler

isRequestdForHandler

protected boolean isRequestdForHandler(HttpRequest request)
Overrides:
isRequestdForHandler in class AbstractHandler

handleBody

protected boolean handleBody(HttpRequest request,
                             HttpResponse response)
                      throws java.io.IOException
Overrides:
handleBody in class AbstractHandler
Throws:
java.io.IOException