Class RewriteHandler
- java.lang.Object
-
- org.eclipse.jetty.util.component.AbstractLifeCycle
-
- org.eclipse.jetty.util.component.ContainerLifeCycle
-
- org.eclipse.jetty.server.handler.AbstractHandler
-
- org.eclipse.jetty.server.handler.AbstractHandlerContainer
-
- org.eclipse.jetty.server.handler.HandlerWrapper
-
- org.eclipse.jetty.rewrite.handler.RewriteHandler
-
- All Implemented Interfaces:
Handler
,HandlerContainer
,Container
,Destroyable
,Dumpable
,Dumpable.DumpableContainer
,LifeCycle
public class RewriteHandler extends HandlerWrapper
Rewrite handler is responsible for managing the rules. Its capabilities is not only limited for URL rewrites such as RewritePatternRule or RewriteRegexRule. There is also handling for cookies, headers, redirection, setting status or error codes whenever the rule finds a match.
The rules can be matched by the either: pattern matching of PathMap (eg
PatternRule
), regular expressions (egRegexRule
) or certain conditions set (egMsieSslRule
- the requests must be in SSL mode).The rules can be grouped into rule containers (class
RuleContainer
), and will only be applied if the request matches the conditions for their container (e.g., by virtual host name)The list of predefined rules is:
-
CookiePatternRule
- adds a new cookie in response. -
HeaderPatternRule
- adds/modifies the HTTP headers in response. -
RedirectPatternRule
- sets the redirect location. -
ResponsePatternRule
- sets the status/error codes. -
RewritePatternRule
- rewrites the requested URI. -
RewriteRegexRule
- rewrites the requested URI using regular expression for pattern matching. -
MsieSslRule
- disables the keep alive on SSL for IE5 and IE6. -
ForwardedSchemeHeaderRule
- set the scheme according to the headers present. -
VirtualHostRuleContainer
- checks whether the request matches one of a set of virtual host names.
<New id="RewriteHandler" class="org.eclipse.jetty.rewrite.handler.RewriteHandler"> <Set name="rules"> <Array type="org.eclipse.jetty.rewrite.handler.Rule"> <Item> <New id="rewrite" class="org.eclipse.jetty.rewrite.handler.RewritePatternRule"> <Set name="pattern">/*</Set> <Set name="replacement">/test</Set> </New> </Item> <Item> <New id="response" class="org.eclipse.jetty.rewrite.handler.ResponsePatternRule"> <Set name="pattern">/session/</Set> <Set name="code">400</Set> <Set name="reason">Setting error code 400</Set> </New> </Item> <Item> <New id="header" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule"> <Set name="pattern">*.jsp</Set> <Set name="name">server</Set> <Set name="value">dexter webserver</Set> </New> </Item> <Item> <New id="header" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule"> <Set name="pattern">*.jsp</Set> <Set name="name">title</Set> <Set name="value">driven header purpose</Set> </New> </Item> <Item> <New id="redirect" class="org.eclipse.jetty.rewrite.handler.RedirectPatternRule"> <Set name="pattern">/test/dispatch</Set> <Set name="location">http://jetty.eclipse.org</Set> </New> </Item> <Item> <New id="regexRewrite" class="org.eclipse.jetty.rewrite.handler.RewriteRegexRule"> <Set name="regex">/test-jaas/$</Set> <Set name="replacement">/demo</Set> </New> </Item> <Item> <New id="forwardedHttps" class="org.eclipse.jetty.rewrite.handler.ForwardedSchemeHeaderRule"> <Set name="header">X-Forwarded-Scheme</Set> <Set name="headerValue">https</Set> <Set name="scheme">https</Set> </New> </Item> <Item> <New id="virtualHost" class="org.eclipse.jetty.rewrite.handler.VirtualHostRuleContainer"> <Set name="virtualHosts"> <Array type="java.lang.String"> <Item>eclipse.com</Item> <Item>www.eclipse.com</Item> <Item>eclipse.org</Item> <Item>www.eclipse.org</Item> </Array> </Set> <Call name="addRule"> <Arg> <New class="org.eclipse.jetty.rewrite.handler.CookiePatternRule"> <Set name="pattern">/*</Set> <Set name="name">CookiePatternRule</Set> <Set name="value">1</Set> </New> </Arg> </Call> </New> </Item> </Array> </Set> </New> <Set name="handler"> <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection"> <Set name="handlers"> <Array type="org.eclipse.jetty.server.Handler"> <Item> <Ref id="RewriteHandler"/> </Item> <Item> <New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/> </Item> <Item> <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/> </Item> <Item> <New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler"/> </Item> </Array> </Set> </New> </Set>
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.eclipse.jetty.server.handler.AbstractHandler
AbstractHandler.ErrorDispatchHandler
-
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Container
Container.InheritedListener, Container.Listener
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Dumpable
Dumpable.DumpableContainer
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle
LifeCycle.Listener
-
-
Field Summary
-
Fields inherited from class org.eclipse.jetty.server.handler.HandlerWrapper
_handler
-
-
Constructor Summary
Constructors Constructor Description RewriteHandler()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addRule(Rule rule)
Add a Rulejava.util.EnumSet<javax.servlet.DispatcherType>
getDispatcherTypes()
java.lang.String
getOriginalPathAttribute()
RuleContainer
getRuleContainer()
Rule[]
getRules()
Returns the list of rules.void
handle(java.lang.String target, Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Handle a request.boolean
isRewritePathInfo()
boolean
isRewriteRequestURI()
void
setDispatcherTypes(java.util.EnumSet<javax.servlet.DispatcherType> types)
void
setDispatcherTypes(javax.servlet.DispatcherType... types)
void
setOriginalPathAttribute(java.lang.String originalPathAttribute)
void
setRewritePathInfo(boolean rewritePathInfo)
void
setRewriteRequestURI(boolean rewriteRequestURI)
void
setRuleContainer(RuleContainer rules)
Assigns the rules to process.void
setRules(Rule[] rules)
Assigns the rules to process.void
setRules(RuleContainer rules)
Deprecated.-
Methods inherited from class org.eclipse.jetty.server.handler.HandlerWrapper
destroy, expandChildren, getHandler, getHandlers, insertHandler, setHandler
-
Methods inherited from class org.eclipse.jetty.server.handler.AbstractHandlerContainer
doShutdown, expandHandler, findContainerOf, getChildHandlerByClass, getChildHandlers, getChildHandlersByClass, setServer
-
Methods inherited from class org.eclipse.jetty.server.handler.AbstractHandler
doError, doStart, doStop, getServer
-
Methods inherited from class org.eclipse.jetty.util.component.ContainerLifeCycle
addBean, addBean, addEventListener, addManaged, contains, dump, dump, dump, dump, dump, dumpBeans, dumpObject, dumpObjects, dumpStdErr, dumpThis, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, setStopTimeout, start, stop, unmanage, updateBean, updateBean, updateBeans
-
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.eclipse.jetty.util.component.Dumpable.DumpableContainer
isDumpable
-
Methods inherited from interface org.eclipse.jetty.util.component.LifeCycle
addLifeCycleListener, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
-
-
-
-
Method Detail
-
setRules
public void setRules(Rule[] rules)
Assigns the rules to process.- Parameters:
rules
- an array ofRule
.
-
setRules
@Deprecated public void setRules(RuleContainer rules)
Deprecated.Assigns the rules to process.- Parameters:
rules
- aRuleContainer
containing other rules to process
-
setRuleContainer
public void setRuleContainer(RuleContainer rules)
Assigns the rules to process.- Parameters:
rules
- aRuleContainer
containing other rules to process
-
getRuleContainer
public RuleContainer getRuleContainer()
-
addRule
public void addRule(Rule rule)
Add a Rule- Parameters:
rule
- The rule to add to the end of the rules array
-
isRewriteRequestURI
public boolean isRewriteRequestURI()
- Returns:
- the rewriteRequestURI If true, this handler will rewrite the value
returned by
HttpServletRequest.getRequestURI()
.
-
setRewriteRequestURI
public void setRewriteRequestURI(boolean rewriteRequestURI)
- Parameters:
rewriteRequestURI
- true if this handler will rewrite the value returned byHttpServletRequest.getRequestURI()
.
-
isRewritePathInfo
public boolean isRewritePathInfo()
- Returns:
- true if this handler will rewrite the value
returned by
HttpServletRequest.getPathInfo()
.
-
setRewritePathInfo
public void setRewritePathInfo(boolean rewritePathInfo)
- Parameters:
rewritePathInfo
- true if this handler will rewrite the value returned byHttpServletRequest.getPathInfo()
.
-
getOriginalPathAttribute
public java.lang.String getOriginalPathAttribute()
- Returns:
- the originalPathAttribte. If non null, this string will be used as the attribute name to store the original request path.
-
setOriginalPathAttribute
public void setOriginalPathAttribute(java.lang.String originalPathAttribute)
- Parameters:
originalPathAttribute
- If non null, this string will be used as the attribute name to store the original request path.
-
getDispatcherTypes
public java.util.EnumSet<javax.servlet.DispatcherType> getDispatcherTypes()
-
setDispatcherTypes
public void setDispatcherTypes(java.util.EnumSet<javax.servlet.DispatcherType> types)
-
setDispatcherTypes
public void setDispatcherTypes(javax.servlet.DispatcherType... types)
-
handle
public void handle(java.lang.String target, Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException
Description copied from interface:Handler
Handle a request.- Specified by:
handle
in interfaceHandler
- Overrides:
handle
in classHandlerWrapper
- Parameters:
target
- The target of the request - either a URI or a name.baseRequest
- The original unwrapped request object.request
- The request either as theRequest
object or a wrapper of that request. The
method can be used access the Request object if required.HttpConnection.getCurrentConnection()
.getHttpChannel()
.getRequest()
response
- The response as theResponse
object or a wrapper of that request. The
method can be used access the Response object if required.HttpConnection.getCurrentConnection()
.getHttpChannel()
.getResponse()
- Throws:
java.io.IOException
- if unable to handle the request or response processingjavax.servlet.ServletException
- if unable to handle the request or response due to underlying servlet issue
-
-