blob: eb1e09abf9650f8c9a2f83475d5c439230c613eb [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Chapter&nbsp;18.&nbsp;Handlers</title><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><meta name="keywords" content="jetty, servlet-api, cometd, http, spdy, eclipse, maven, java, software"><link rel="home" href="index.html" title="Jetty : The Definitive Reference"><link rel="up" href="advanced.html" title="Part&nbsp;III.&nbsp;Advanced Topics Guide"><link rel="prev" href="advanced-start-matrix.html" title="Start Option Matrix"><link rel="next" href="writing-custom-handlers.html" title="Writing Custom Handlers"><link xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" rel="shortcut icon" href="images/favicon.ico"><script type="text/javascript" src="js/shCore.js"></script><script type="text/javascript" src="js/shBrushJava.js"></script><script type="text/javascript" src="js/shBrushXml.js"></script><script type="text/javascript" src="js/shBrushBash.js"></script><script type="text/javascript" src="js/shBrushJScript.js"></script><script type="text/javascript" src="js/shBrushSql.js"></script><script type="text/javascript" src="js/shBrushProperties.js"></script><script type="text/javascript" src="js/shBrushPlain.js"></script><link type="text/css" rel="stylesheet" href="css/shCore.css"><link type="text/css" rel="stylesheet" href="css/shThemeEclipse.css"><link type="text/css" rel="stylesheet" href="css/font-awesome.min.css"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><tr><td style="width: 25%"><a href="http://www.eclipse.org/jetty"><img src="images/jetty-header-logo.png" alt="Jetty Logo"></a><br><span style="font-size: small">
Version: 9.0.6.v20130930</span></td><td style="width: 50%"><script type="text/javascript"> (function() {
var cx = '016459005284625897022:obd4lsai2ds';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script><gcse:search></gcse:search></td></tr></table><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter&nbsp;18.&nbsp;Handlers</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="advanced-start-matrix.html"><i class="icon-chevron-left"></i> Previous</a>&nbsp;</td><th width="60%" align="center">Part&nbsp;III.&nbsp;Advanced Topics Guide<br><a accesskey="p" href="index.html"><i class="icon-home"></i> Home</a></th><td width="20%" align="right">&nbsp;<a accesskey="n" href="writing-custom-handlers.html">Next <i class="icon-chevron-right"></i></a></td></tr></table><hr></div><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="jetty-callout"><h5 class="callout"><a href="http://www.webtide.com/support.jsp">Contact the core Jetty developers at
<span class="website">www.webtide.com</span></a></h5><p>
private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ...
scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery
</p></div><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a name="jetty-handlers"></a>Chapter&nbsp;18.&nbsp;Handlers</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="jetty-handlers.html#rewrite-handler">Rewrite Handler</a></span></dt><dt><span class="section"><a href="writing-custom-handlers.html">Writing Custom Handlers</a></span></dt></dl></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="rewrite-handler"></a>Rewrite Handler</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="jetty-handlers.html#d0e10610">Quick Start</a></span></dt><dt><span class="section"><a href="jetty-handlers.html#d0e10629">Configuring Rules</a></span></dt><dt><span class="section"><a href="jetty-handlers.html#d0e10645">Embedded Example</a></span></dt><dt><span class="section"><a href="jetty-handlers.html#d0e10653">Rules</a></span></dt></dl></div><p>
The RewriteHandler matches a request against a set of rules, and modifies the request accordingly for any rules that match. The most common use is to rewrite request URIs, but it is capable of much more: rules can also be configured to redirect the response, set a cookie or response code on the response, modify the header, etc.
</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="d0e10610"></a>Quick Start</h3></div></div></div><p>
The standard Jetty distribution bundle contains the <code class="literal">jetty-rewrite</code> module JAR, at <code class="filename">lib/jetty-rewrite-*.jar</code>, and a sample configuration file, at <code class="filename">etc/jetty-rewrite.xml</code>. To enable the rewrite module, using the sample configuration file, start up Jetty with this command:
</p><div class="screenexample"><pre class="screen">
$ java -jar start.jar OPTIONS=default,rewrite etc/jetty.xml etc/jetty-rewrite.xml
</pre></div></div><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><i class="icon-asterisk"></i> Note</h3><p>
If you are running the standard Jetty distribution with the sample test webapp, there will be a demo of the rewrite module at http://localhost:8080/rewrite/
</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="d0e10629"></a>Configuring Rules</h3></div></div></div><p>
The rules are configured using <code class="filename">jetty.xml</code> syntax. This example file shows how to add the rewrite handler for the entire server:
</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: xml;toolbar: false">
<![CDATA[
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- create and configure the rewrite handler -->
<New id="Rewrite" class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
<Set name="rewriteRequestURI">true</Set>
<Set name="rewritePathInfo">false</Set>
<Set name="originalPathAttribute">requestedPath</Set>
<!-- redirect the response. This is a redirect which is visible to the browser.
After the redirect, the browser address bar will show /redirected -->
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RedirectPatternRule">
<Set name="pattern">/redirect/*</Set>
<Set name="replacement">/redirected</Set>
</New>
</Arg>
</Call>
<!-- rewrite the request URI. This is an internal rewrite, visible to server,
but the browser will still show /some/old/context -->
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RewritePatternRule">
<Set name="pattern">/some/old/context</Set>
<Set name="replacement">/some/new/context</Set>
</New>
</Arg>
</Call>
<!-- reverse the order of the path sections. Internal rewrite -->
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RewriteRegexRule">
<Set name="regex">/reverse/([^/]*)/(.*)</Set>
<Set name="replacement">/reverse/$2/$1</Set>
</New>
</Arg>
</Call>
</New>
<!-- add the rewrite handler to the server -->
<Set name="handler"><Ref id="Rewrite" /></Set>
</Configure>
]]>
</script></div><p>
See <code class="filename">etc/jetty-rewrite.xml</code> for more configuration examples.
</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="d0e10645"></a>Embedded Example</h3></div></div></div><p>
This is an example for embedded Jetty, which does the same thing as the configuration file example above:
</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: java;toolbar: false">
<![CDATA[
Server server = new Server();
RewriteHandler rewrite = new RewriteHandler();
rewrite.setRewriteRequestURI(true);
rewrite.setRewritePathInfo(false);
rewrite.originalPathAttribute("requestedPath");
RedirectPatternRule redirect = new RedirectPatternRule();
redirect.setPattern("/redirect/*");
redirect.setReplacement("/redirected");
rewrite.addRule(redirect);
RewritePatternRule oldToNew = new RewritePatternRule();
oldToNew.setPattern("/some/old/context");
oldToNew.setReplacement("/some/new/context");
rewrite.addRule(oldToNew);
RewriteRegexRule reverse = new RewriteRegexRule();
reverse.setRegex("/reverse/([^/]*)/(.*)");
reverse.setReplacement("/reverse/$2/$1");
rewrite.addRule(reverse);
server.setHandler(rewrite);
]]>
</script></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="d0e10653"></a>Rules</h3></div></div></div><p>There are several types of rules that are written extending useful base rule classes.</p><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="d0e10658"></a>PatternRule</h4></div></div></div><p>
Matches against the request URI using the servlet pattern syntax.
</p><div class="variablelist"><dl><dt><span class="term"><a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/CookiePatternRule.html" target="_top">CookiePatternRule</a></span></dt><dd><p>
Adds a cookie to the response.
</p></dd><dt><span class="term"><a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/HeaderPatternRule.html" target="_top">HeaderPatternRule</a></span></dt><dd><p>
Adds/modifies a header in the response.
</p></dd><dt><span class="term"><a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/RedirectPatternRule.html" target="_top">RedirectPatternRule</a></span></dt><dd><p>
Redirects the response.
</p></dd><dt><span class="term"><a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/ResponsePatternRule.html" target="_top">ResponsePatternRule</a></span></dt><dd><p>
Sends the response code (status or error).
</p></dd><dt><span class="term"><a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/RewritePatternRule.html" target="_top">RewritePatternRule</a></span></dt><dd><p>
Rewrite the URI by replacing the matched request path with a fixed string.
</p></dd></dl></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="d0e10699"></a>RegexRule</h4></div></div></div><p>
Matches against the request URI using regular expressions.
</p><div class="variablelist"><dl><dt><span class="term"><a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/RedirectRegexRule.html" target="_top">RedirectRegexRule</a></span></dt><dd><p>
Redirect the response.
</p></dd><dt><span class="term"><a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/RewriteRegexRule.html" target="_top">RewriteRegexRule</a></span></dt><dd><p>
Rewrite the URI by matching with a regular expression. (The replacement string may use Template:$n to replace the nth capture group.)
</p></dd></dl></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="d0e10719"></a>HeaderRule</h4></div></div></div><p>
Match against request headers. Match either on a header name + specific value, or on the presence of a header (with any value).
</p><div class="variablelist"><dl><dt><span class="term"><a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/ForwardedSchemeHeaderRule.html" target="_top">ForwardedSchemaHeaderRule</a></span></dt><dd><p>
Set the scheme on the request (defaulting to https).
</p></dd></dl></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="d0e10732"></a>Others</h4></div></div></div><p>
Oddball rules that defy classification.
</p><div class="variablelist"><dl><dt><span class="term"><a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/MsieSslRule.html" target="_top">MsieSslRule</a></span></dt><dd><p>
Disables the keep alive for SSL from IE5 or IE6.
</p></dd><dt><span class="term"><a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/LegacyRule.html" target="_top">LegacyRule</a></span></dt><dd><p>
Implements the legacy API of RewriteHandler
</p></dd></dl></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="d0e10752"></a>RuleContainer</h4></div></div></div><p>
Groups rules together. The contained rules will only be processed if the conditions for the RuleContainer evaluate to true.
</p><div class="variablelist"><dl><dt><span class="term"><a class="link" href="http://download.eclipse.org/jetty/stable-9/xref/org/eclipse/jetty/rewrite/handler/VirtualHostRuleContainer.html" target="_top">VirtualHostRuleContainer</a></span></dt><dd><p>
Groups rules that apply only to a specific virtual host or a set of virtual hosts
</p></dd></dl></div></div></div></div></div><script type="text/javascript">
SyntaxHighlighter.all()
</script><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="advanced-start-matrix.html"><i class="icon-chevron-left"></i> Previous</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="advanced.html"><i class="icon-chevron-up"></i> Top</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="writing-custom-handlers.html">Next <i class="icon-chevron-right"></i></a></td></tr><tr><td width="40%" align="left" valign="top">Start Option Matrix&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html"><i class="icon-home"></i> Home</a></td><td width="40%" align="right" valign="top">&nbsp;Writing Custom Handlers</td></tr></table></div><p xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><div class="jetty-callout">
See an error or something missing?
<span class="callout"><a href="http://github.com/jetty-project/jetty-documentation">Contribute to this documentation at
<span class="website"><i class="icon-github"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2013-10-30T13:20:28-05:00)</i></span></div></p><script xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1149868-7']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script></body></html>