From b087e6db28a723f75e6d69ced894549dd86b2e2d Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Tue, 5 Apr 2016 12:13:29 +1000 Subject: [PATCH] Issue #467 Compact // rule Improved the rewrite classes and modules to make it easier to compact // withing a URL. This can now be done with --module=rewrite,rewrite-compactpath --- examples/embedded/pom.xml | 5 ++ .../eclipse/jetty/embedded/RewriteServer.java | 52 +++++++++++++++++++ .../config/etc/jetty-rewrite-customizer.xml | 17 ++++++ .../src/main/config/etc/jetty-rewrite.xml | 18 ++----- .../main/config/etc/rewrite-compactpath.xml | 10 ++++ .../config/modules/rewrite-compactpath.mod | 10 ++++ .../config/modules/rewrite-customizer.mod | 24 +++++++++ .../src/main/config/modules/rewrite.mod | 3 +- .../jetty/rewrite/RewriteCustomizer.java | 44 ++++++++++++++++ .../rewrite/handler/CookiePatternRule.java | 11 ++++ .../rewrite/handler/HeaderPatternRule.java | 14 ++++- .../rewrite/handler/HeaderRegexRule.java | 16 ++++-- .../jetty/rewrite/handler/PatternRule.java | 11 ++++ .../rewrite/handler/RedirectPatternRule.java | 12 ++++- .../rewrite/handler/RedirectRegexRule.java | 33 +++++++----- .../jetty/rewrite/handler/RegexRule.java | 15 +++++- .../rewrite/handler/ResponsePatternRule.java | 12 ++++- .../jetty/rewrite/handler/RewriteHandler.java | 17 ++++++ .../rewrite/handler/RewritePatternRule.java | 25 +++++++-- .../rewrite/handler/RewriteRegexRule.java | 30 ++++++++--- .../jetty/rewrite/handler/RuleContainer.java | 7 ++- .../handler/TerminatingPatternRule.java | 6 +++ .../rewrite/handler/TerminatingRegexRule.java | 8 +++ .../org/eclipse/jetty/server/HttpChannel.java | 21 +++++--- 24 files changed, 367 insertions(+), 54 deletions(-) create mode 100644 examples/embedded/src/main/java/org/eclipse/jetty/embedded/RewriteServer.java create mode 100644 jetty-rewrite/src/main/config/etc/jetty-rewrite-customizer.xml create mode 100644 jetty-rewrite/src/main/config/etc/rewrite-compactpath.xml create mode 100644 jetty-rewrite/src/main/config/modules/rewrite-compactpath.mod create mode 100644 jetty-rewrite/src/main/config/modules/rewrite-customizer.mod create mode 100644 jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/RewriteCustomizer.java diff --git a/examples/embedded/pom.xml b/examples/embedded/pom.xml index 11b221f24a5..57a17676d1f 100644 --- a/examples/embedded/pom.xml +++ b/examples/embedded/pom.xml @@ -40,6 +40,11 @@ jetty-deploy ${project.version} + + org.eclipse.jetty + jetty-rewrite + ${project.version} + org.eclipse.jetty jetty-jmx diff --git a/examples/embedded/src/main/java/org/eclipse/jetty/embedded/RewriteServer.java b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/RewriteServer.java new file mode 100644 index 00000000000..dd065e68f0b --- /dev/null +++ b/examples/embedded/src/main/java/org/eclipse/jetty/embedded/RewriteServer.java @@ -0,0 +1,52 @@ +// +// ======================================================================== +// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.embedded; + +import org.eclipse.jetty.rewrite.RewriteCustomizer; +import org.eclipse.jetty.rewrite.handler.CompactPathRule; +import org.eclipse.jetty.rewrite.handler.RewriteRegexRule; +import org.eclipse.jetty.server.HttpConfiguration; +import org.eclipse.jetty.server.HttpConnectionFactory; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.servlet.ServletContextHandler; + +public class RewriteServer +{ + public static void main( String[] args ) throws Exception + { + Server server = new Server(8080); + + HttpConfiguration config=server.getConnectors()[0].getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration(); + + RewriteCustomizer rewrite = new RewriteCustomizer(); + config.addCustomizer(rewrite); + rewrite.addRule(new CompactPathRule()); + rewrite.addRule(new RewriteRegexRule("(.*)foo(.*)","$1FOO$2")); + + ServletContextHandler context = new ServletContextHandler( + ServletContextHandler.SESSIONS); + context.setContextPath("/"); + server.setHandler(context); + + context.addServlet(DumpServlet.class, "/*"); + + server.start(); + server.join(); + } +} diff --git a/jetty-rewrite/src/main/config/etc/jetty-rewrite-customizer.xml b/jetty-rewrite/src/main/config/etc/jetty-rewrite-customizer.xml new file mode 100644 index 00000000000..f8584e3d866 --- /dev/null +++ b/jetty-rewrite/src/main/config/etc/jetty-rewrite-customizer.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/jetty-rewrite/src/main/config/etc/jetty-rewrite.xml b/jetty-rewrite/src/main/config/etc/jetty-rewrite.xml index 0f37c7c5bb2..cb6e2b0b6c5 100644 --- a/jetty-rewrite/src/main/config/etc/jetty-rewrite.xml +++ b/jetty-rewrite/src/main/config/etc/jetty-rewrite.xml @@ -8,7 +8,7 @@ - + @@ -21,19 +21,9 @@ - - + + + diff --git a/jetty-rewrite/src/main/config/etc/rewrite-compactpath.xml b/jetty-rewrite/src/main/config/etc/rewrite-compactpath.xml new file mode 100644 index 00000000000..7cd53e64fc2 --- /dev/null +++ b/jetty-rewrite/src/main/config/etc/rewrite-compactpath.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/jetty-rewrite/src/main/config/modules/rewrite-compactpath.mod b/jetty-rewrite/src/main/config/modules/rewrite-compactpath.mod new file mode 100644 index 00000000000..a02e732f113 --- /dev/null +++ b/jetty-rewrite/src/main/config/modules/rewrite-compactpath.mod @@ -0,0 +1,10 @@ +# +# Jetty Rewrite CompactPath module +# +[xml] +etc/rewrite-compactpath.xml + +[ini-template] +## Requires either rewrite or rewrite-customizer module +## with rewritePathInfo==true +jetty.rewrite.rewritePathInfo=true \ No newline at end of file diff --git a/jetty-rewrite/src/main/config/modules/rewrite-customizer.mod b/jetty-rewrite/src/main/config/modules/rewrite-customizer.mod new file mode 100644 index 00000000000..102939dc3f1 --- /dev/null +++ b/jetty-rewrite/src/main/config/modules/rewrite-customizer.mod @@ -0,0 +1,24 @@ +# +# Jetty Rewrite Customizer module +# +# Apply rewrite rules as a request customizer applied to all +# connectors sharing a HttpConfiguration +# +[depend] +server + +[lib] +lib/jetty-rewrite-${jetty.version}.jar + +[xml] +etc/jetty-rewrite-customizer.xml + +[ini-template] +## Whether to rewrite the request URI +# jetty.rewrite.rewriteRequestURI=true + +## Whether to rewrite the path info +# jetty.rewrite.rewritePathInfo=true + +## Request attribute key under with the original path is stored +# jetty.rewrite.originalPathAttribute=requestedPath diff --git a/jetty-rewrite/src/main/config/modules/rewrite.mod b/jetty-rewrite/src/main/config/modules/rewrite.mod index c8a1750618a..3c542578a56 100644 --- a/jetty-rewrite/src/main/config/modules/rewrite.mod +++ b/jetty-rewrite/src/main/config/modules/rewrite.mod @@ -1,7 +1,8 @@ # # Jetty Rewrite module # - +# Install rewrite rules as a handler applied to all requests on a server +# [depend] server diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/RewriteCustomizer.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/RewriteCustomizer.java new file mode 100644 index 00000000000..e2bace34773 --- /dev/null +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/RewriteCustomizer.java @@ -0,0 +1,44 @@ +// +// ======================================================================== +// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.rewrite; + +import java.io.IOException; + +import org.eclipse.jetty.io.RuntimeIOException; +import org.eclipse.jetty.rewrite.handler.RuleContainer; +import org.eclipse.jetty.server.Connector; +import org.eclipse.jetty.server.HttpConfiguration; +import org.eclipse.jetty.server.HttpConfiguration.Customizer; +import org.eclipse.jetty.server.Request; + +public class RewriteCustomizer extends RuleContainer implements Customizer +{ + @Override + public void customize(Connector connector, HttpConfiguration channelConfig, Request request) + { + try + { + matchAndApply(request.getPathInfo(), request, request.getResponse()); + } + catch (IOException e) + { + throw new RuntimeIOException(e); + } + } +} diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/CookiePatternRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/CookiePatternRule.java index 5eeab6b6520..875e5f013dd 100644 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/CookiePatternRule.java +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/CookiePatternRule.java @@ -24,6 +24,8 @@ import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.util.annotation.Name; + /** * Sets the cookie in the response whenever the rule finds a match. @@ -38,8 +40,17 @@ public class CookiePatternRule extends PatternRule /* ------------------------------------------------------------ */ public CookiePatternRule() { + this(null,null,null); + } + + /* ------------------------------------------------------------ */ + public CookiePatternRule(@Name("pattern") String pattern, @Name("name") String name, @Name("value") String value) + { + super(pattern); _handling = false; _terminating = false; + setName(name); + setValue(value); } /* ------------------------------------------------------------ */ diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/HeaderPatternRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/HeaderPatternRule.java index 21c2f6b2a6c..14faefc15a1 100644 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/HeaderPatternRule.java +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/HeaderPatternRule.java @@ -23,6 +23,8 @@ import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.util.annotation.Name; + /** * Sets the header in the response whenever the rule finds a match. @@ -31,13 +33,23 @@ public class HeaderPatternRule extends PatternRule { private String _name; private String _value; - private boolean _add=false; + private boolean _add; /* ------------------------------------------------------------ */ public HeaderPatternRule() { + this(null,null,null); + } + + /* ------------------------------------------------------------ */ + public HeaderPatternRule(@Name("pattern") String pattern, @Name("name") String name, @Name("value") String value) + { + super(pattern); _handling = false; _terminating = false; + _add=false; + setName(name); + setValue(value); } /* ------------------------------------------------------------ */ diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/HeaderRegexRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/HeaderRegexRule.java index 981c0922c6b..4802e4abecc 100644 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/HeaderRegexRule.java +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/HeaderRegexRule.java @@ -24,13 +24,14 @@ import java.util.regex.Matcher; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.util.annotation.Name; + /* ------------------------------------------------------------ */ /** Rule to add a header based on a Regex match */ public class HeaderRegexRule extends RegexRule { - private String _name; private String _value; private boolean _add=false; @@ -38,10 +39,19 @@ public class HeaderRegexRule extends RegexRule /* ------------------------------------------------------------ */ public HeaderRegexRule() { - _handling = false; - _terminating = false; + this(null,null,null); } + /* ------------------------------------------------------------ */ + public HeaderRegexRule(@Name("regex") String regex, @Name("name") String name, @Name("value") String value) + { + super(regex); + setHandling(false); + setTerminating(false); + setName(name); + setValue(value); + } + /* ------------------------------------------------------------ */ /** * Sets the header name. diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/PatternRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/PatternRule.java index 3acc7079718..e3c225bc8b9 100644 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/PatternRule.java +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/PatternRule.java @@ -33,7 +33,18 @@ public abstract class PatternRule extends Rule { protected String _pattern; + /* ------------------------------------------------------------ */ + protected PatternRule() + { + } + /* ------------------------------------------------------------ */ + protected PatternRule(String pattern) + { + this(); + setPattern(pattern); + } + /* ------------------------------------------------------------ */ public String getPattern() { diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectPatternRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectPatternRule.java index 5b4096a8337..89f883c5d24 100644 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectPatternRule.java +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectPatternRule.java @@ -24,6 +24,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.util.annotation.Name; /** * Issues a (3xx) Redirect response whenever the rule finds a match. @@ -39,10 +40,17 @@ public class RedirectPatternRule extends PatternRule public RedirectPatternRule() { - _handling = true; - _terminating = true; + this(null,null); } + public RedirectPatternRule(@Name("pattern") String pattern, @Name("location") String location) + { + super(pattern); + _handling = true; + _terminating = true; + _location=location; + } + /** * Sets the redirect location. * diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRule.java index 0d72002fee1..78be9525b42 100644 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRule.java +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRule.java @@ -25,6 +25,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.util.annotation.Name; /** * Issues a (3xx) Redirect response whenever the rule finds a match via regular expression. @@ -37,23 +38,31 @@ import org.eclipse.jetty.http.HttpStatus; */ public class RedirectRegexRule extends RegexRule { - protected String _replacement; + protected String _location; private int _statusCode = HttpStatus.FOUND_302; - + public RedirectRegexRule() { - _handling = true; - _terminating = true; + this(null,null); + } + + public RedirectRegexRule(@Name("regex") String regex, @Name("location") String location) + { + super(regex); + setHandling(true); + setTerminating(true); + setLocation(location); } - /** - * Whenever a match is found, it replaces with this value. - * - * @param replacement the replacement string. - */ + @Deprecated public void setReplacement(String replacement) { - _replacement = replacement; + _location = replacement; + } + + public void setLocation(String location) + { + _location = location; } /** @@ -77,7 +86,7 @@ public class RedirectRegexRule extends RegexRule protected String apply(String target, HttpServletRequest request, HttpServletResponse response, Matcher matcher) throws IOException { - target=_replacement; + target=_location; for (int g=1;g<=matcher.groupCount();g++) { String group = matcher.group(g); @@ -101,7 +110,7 @@ public class RedirectRegexRule extends RegexRule StringBuilder str = new StringBuilder(); str.append(super.toString()); str.append('[').append(_statusCode); - str.append('>').append(_replacement); + str.append('>').append(_location); str.append(']'); return str.toString(); } diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RegexRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RegexRule.java index 8eb3f95df70..13d96c91bf0 100644 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RegexRule.java +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RegexRule.java @@ -32,7 +32,18 @@ import javax.servlet.http.HttpServletResponse; public abstract class RegexRule extends Rule { protected Pattern _regex; - + + /* ------------------------------------------------------------ */ + protected RegexRule() + { + } + + /* ------------------------------------------------------------ */ + protected RegexRule(String pattern) + { + setRegex(pattern); + } + /* ------------------------------------------------------------ */ /** * Sets the regular expression string used to match with string URI. @@ -41,7 +52,7 @@ public abstract class RegexRule extends Rule */ public void setRegex(String regex) { - _regex=Pattern.compile(regex); + _regex=regex==null?null:Pattern.compile(regex); } /* ------------------------------------------------------------ */ diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/ResponsePatternRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/ResponsePatternRule.java index 89f0b7ef008..d3318b9c7bf 100644 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/ResponsePatternRule.java +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/ResponsePatternRule.java @@ -23,19 +23,29 @@ import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.util.annotation.Name; + /** * Sends the response code whenever the rule finds a match. */ public class ResponsePatternRule extends PatternRule { private String _code; - private String _reason = ""; + private String _reason; /* ------------------------------------------------------------ */ public ResponsePatternRule() + { + this(null,null,""); + } + + /* ------------------------------------------------------------ */ + public ResponsePatternRule(@Name("pattern") String pattern, @Name("code") String code, @Name("reason") String reason) { _handling = true; _terminating = true; + setCode(code); + setReason(reason); } /* ------------------------------------------------------------ */ diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewriteHandler.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewriteHandler.java index 5d403a4b7ed..24798fabe43 100644 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewriteHandler.java +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewriteHandler.java @@ -206,11 +206,28 @@ public class RewriteHandler extends HandlerWrapper * Assigns the rules to process. * @param rules a {@link RuleContainer} containing other rules to process */ + @Deprecated public void setRules(RuleContainer rules) { _rules = rules; } + + /*------------------------------------------------------------ */ + /** + * Assigns the rules to process. + * @param rules a {@link RuleContainer} containing other rules to process + */ + public void setRuleContainer(RuleContainer rules) + { + _rules = rules; + } + /*------------------------------------------------------------ */ + public RuleContainer getRuleContainer() + { + return _rules; + } + /* ------------------------------------------------------------ */ /** * Add a Rule diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewritePatternRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewritePatternRule.java index bced576908a..bf34dbc10c0 100644 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewritePatternRule.java +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewritePatternRule.java @@ -26,6 +26,7 @@ import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.http.PathMap; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.util.URIUtil; +import org.eclipse.jetty.util.annotation.Name; /** * Rewrite the URI by replacing the matched {@link PathMap} path with a fixed string. @@ -37,11 +38,19 @@ public class RewritePatternRule extends PatternRule implements Rule.ApplyURI /* ------------------------------------------------------------ */ public RewritePatternRule() + { + this(null,null); + } + + /* ------------------------------------------------------------ */ + public RewritePatternRule(@Name("pattern") String pattern, @Name("replacement") String replacement) { _handling = false; _terminating = false; + setReplacement(replacement); } - + + /* ------------------------------------------------------------ */ /** * Whenever a match is found, it replaces with this value. @@ -50,9 +59,17 @@ public class RewritePatternRule extends PatternRule implements Rule.ApplyURI */ public void setReplacement(String replacement) { - String[] split = replacement.split("\\?", 2); - _replacement = split[0]; - _query = split.length == 2 ? split[1] : null; + if (replacement==null) + { + _replacement=null; + _query=null; + } + else + { + String[] split = replacement.split("\\?", 2); + _replacement = split[0]; + _query = split.length == 2 ? split[1] : null; + } } /* ------------------------------------------------------------ */ diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewriteRegexRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewriteRegexRule.java index c7f0df3265b..335eb0e9759 100644 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewriteRegexRule.java +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewriteRegexRule.java @@ -25,6 +25,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.util.annotation.Name; /** * Rewrite the URI by matching with a regular expression. @@ -43,8 +44,16 @@ public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI /* ------------------------------------------------------------ */ public RewriteRegexRule() { - _handling = false; - _terminating = false; + this(null,null); + } + + /* ------------------------------------------------------------ */ + public RewriteRegexRule(@Name("regex") String regex, @Name("replacement") String replacement) + { + setHandling(false); + setTerminating(false); + setRegex(regex); + setReplacement(replacement); } /* ------------------------------------------------------------ */ @@ -55,10 +64,19 @@ public class RewriteRegexRule extends RegexRule implements Rule.ApplyURI */ public void setReplacement(String replacement) { - String[] split=replacement.split("\\?",2); - _replacement = split[0]; - _query=split.length==2?split[1]:null; - _queryGroup=_query!=null && _query.contains("$Q"); + if (replacement==null) + { + _replacement=null; + _query=null; + _queryGroup=false; + } + else + { + String[] split=replacement.split("\\?",2); + _replacement = split[0]; + _query=split.length==2?split[1]:null; + _queryGroup=_query!=null && _query.contains("$Q"); + } } diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RuleContainer.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RuleContainer.java index 727d838b796..1db6be5a7f3 100644 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RuleContainer.java +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RuleContainer.java @@ -161,13 +161,18 @@ public class RuleContainer extends Rule { boolean original_set=_originalPathAttribute==null; + // TODO why is this always done? target = URIUtil.compactPath(target); - + + if (_rules==null) + return target; + for (Rule rule : _rules) { String applied=rule.matchAndApply(target,request, response); if (applied!=null) { + // TODO why is this always done? applied = URIUtil.compactPath(applied); LOG.debug("applied {}",rule); diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/TerminatingPatternRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/TerminatingPatternRule.java index 55283d8392d..e182646d7f4 100644 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/TerminatingPatternRule.java +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/TerminatingPatternRule.java @@ -31,6 +31,12 @@ public class TerminatingPatternRule extends PatternRule { public TerminatingPatternRule() { + this(null); + } + + public TerminatingPatternRule(String pattern) + { + super(pattern); super.setTerminating(true); } diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/TerminatingRegexRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/TerminatingRegexRule.java index 32ac893e105..bc5fbc1a847 100644 --- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/TerminatingRegexRule.java +++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/TerminatingRegexRule.java @@ -24,6 +24,8 @@ import java.util.regex.Matcher; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.util.annotation.Name; + /** * If this rule matches, terminate the processing of other rules. * Allowing the request to be processed by the handlers after the rewrite rules. @@ -32,6 +34,12 @@ public class TerminatingRegexRule extends RegexRule { public TerminatingRegexRule() { + this(null); + } + + public TerminatingRegexRule(@Name("regex") String regex) + { + super(regex); super.setTerminating(true); } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java index ac7c30ed55a..f29b615e01a 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java @@ -295,16 +295,23 @@ public class HttpChannel implements Runnable, HttpOutput.Interceptor _request.setHandled(false); _response.getHttpOutput().reopen(); - List customizers = _configuration.getCustomizers(); - if (!customizers.isEmpty()) - { - for (HttpConfiguration.Customizer customizer : customizers) - customizer.customize(getConnector(), _configuration, _request); - } try { _request.setDispatcherType(DispatcherType.REQUEST); - getServer().handle(this); + + List customizers = _configuration.getCustomizers(); + if (!customizers.isEmpty()) + { + for (HttpConfiguration.Customizer customizer : customizers) + { + customizer.customize(getConnector(), _configuration, _request); + if (_request.isHandled()) + break; + } + } + + if (!_request.isHandled()) + getServer().handle(this); } finally {