aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Becker2011-09-26 11:14:07 (EDT)
committerJesse McConnell2011-09-26 18:50:27 (EDT)
commit5e6a9ea78e5030d7dc29de2a1122065f02cb731c (patch)
tree6aa9d8723585dccfe62f1affff1560b22d3aa11d
parent0d8bd99d6a9984358f9b8d62c84b4fb12364cb15 (diff)
downloadorg.eclipse.jetty.project-5e6a9ea78e5030d7dc29de2a1122065f02cb731c.zip
org.eclipse.jetty.project-5e6a9ea78e5030d7dc29de2a1122065f02cb731c.tar.gz
org.eclipse.jetty.project-5e6a9ea78e5030d7dc29de2a1122065f02cb731c.tar.bz2
beautified code, added javadoc
-rw-r--r--jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/ProxyRule.java28
-rw-r--r--jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RewriteHandler.java73
-rw-r--r--jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/ProxyRuleTest.java24
3 files changed, 80 insertions, 45 deletions
diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/ProxyRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/ProxyRule.java
index ca2614f..a913ca6 100644
--- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/ProxyRule.java
+++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/ProxyRule.java
@@ -15,6 +15,7 @@ import org.eclipse.jetty.client.HttpExchange;
import org.eclipse.jetty.http.HttpHeaderValues;
import org.eclipse.jetty.http.HttpHeaders;
import org.eclipse.jetty.http.HttpURI;
+import org.eclipse.jetty.http.PathMap;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.EofException;
import org.eclipse.jetty.server.Request;
@@ -92,14 +93,7 @@ public class ProxyRule extends PatternRule implements Rule.ApplyURI
final InputStream in = request.getInputStream();
final OutputStream out = response.getOutputStream();
- String uri = request.getRequestURI();
- if (request.getQueryString() != null)
- uri += "?" + request.getQueryString();
-
- HttpURI url = proxyHttpURI(uri);
-
- if (debug != 0)
- _log.debug(debug + " proxy " + uri + "-->" + url);
+ HttpURI url = createUrl(request,debug);
if (url == null)
{
@@ -221,6 +215,7 @@ public class ProxyRule extends PatternRule implements Rule.ApplyURI
* we need to set the timeout on the continuation to take into account the timeout of the HttpClient and the HttpExchange
*/
long ctimeout = (_client.getTimeout() > exchange.getTimeout())?_client.getTimeout():exchange.getTimeout();
+ exchange.setTimeout(ctimeout);
_client.send(exchange);
try
@@ -229,11 +224,26 @@ public class ProxyRule extends PatternRule implements Rule.ApplyURI
}
catch (InterruptedException e)
{
- _log.info(e);
+ _log.info("Exception while waiting for response on proxied request", e);
}
return target;
}
+ private HttpURI createUrl(HttpServletRequest request, final int debug) throws MalformedURLException
+ {
+ String uri = request.getRequestURI();
+ if (request.getQueryString() != null)
+ uri += "?" + request.getQueryString();
+ uri = PathMap.pathInfo(_pattern,uri);
+ if(uri==null)
+ uri = "/";
+ HttpURI url = proxyHttpURI(uri);
+
+ if (debug != 0)
+ _log.debug(debug + " proxy " + uri + "-->" + url);
+ return url;
+ }
+
private boolean createHeaders(final HttpServletRequest request, final int debug, HttpExchange exchange)
{
// check connection header
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 98b9d06..e563abd 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
@@ -5,11 +5,11 @@
// 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
+// 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.
+// You may elect to redistribute this code under either of these licenses.
// ========================================================================
package org.eclipse.jetty.rewrite.handler;
@@ -19,25 +19,24 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.eclipse.jetty.http.PathMap;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.HandlerWrapper;
/* ------------------------------------------------------------ */
/**
*<p> 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.
- *
- * <p> The rules can be matched by the either: pattern matching of PathMap
- * (eg {@link PatternRule}), regular expressions (eg {@link RegexRule}) or certain conditions set
+ * 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.
+ *
+ * <p> The rules can be matched by the either: pattern matching of PathMap
+ * (eg {@link PatternRule}), regular expressions (eg {@link RegexRule}) or certain conditions set
* (eg {@link MsieSslRule} - the requests must be in SSL mode).
- *
- * <p> The rules can be grouped into rule containers (class {@link RuleContainer}), and will only
+ *
+ * <p> The rules can be grouped into rule containers (class {@link RuleContainer}), and will only
* be applied if the request matches the conditions for their container
* (e.g., by virtual host name)
- *
+ *
* <p>The list of predefined rules is:
* <ul>
* <li> {@link CookiePatternRule} - adds a new cookie in response. </li>
@@ -46,28 +45,36 @@ import org.eclipse.jetty.server.handler.HandlerWrapper;
* <li> {@link ResponsePatternRule} - sets the status/error codes. </li>
* <li> {@link RewritePatternRule} - rewrites the requested URI. </li>
* <li> {@link RewriteRegexRule} - rewrites the requested URI using regular expression for pattern matching. </li>
+ * <li> {@link ProxyRule} - proxies the requested URI to the host defined in proxyTo. </li>
* <li> {@link MsieSslRule} - disables the keep alive on SSL for IE5 and IE6. </li>
* <li> {@link LegacyRule} - the old version of rewrite. </li>
* <li> {@link ForwardedSchemeHeaderRule} - set the scheme according to the headers present. </li>
* <li> {@link VirtualHostRuleContainer} - checks whether the request matches one of a set of virtual host names.</li>
* </ul>
*
- *
+ *
* Here is a typical jetty.xml configuration would be: <pre>
- *
+ *
* &lt;Set name="handler"&gt;
* &lt;New id="Handlers" class="org.eclipse.jetty.rewrite.handler.RewriteHandler"&gt;
* &lt;Set name="rules"&gt;
* &lt;Array type="org.eclipse.jetty.rewrite.handler.Rule"&gt;
*
- * &lt;Item&gt;
+ * &lt;Item&gt;
* &lt;New id="rewrite" class="org.eclipse.jetty.rewrite.handler.RewritePatternRule"&gt;
* &lt;Set name="pattern"&gt;/*&lt;/Set&gt;
* &lt;Set name="replacement"&gt;/test&lt;/Set&gt;
* &lt;/New&gt;
* &lt;/Item&gt;
*
- * &lt;Item&gt;
+ * &lt;Item&gt;
+ * &lt;New id="rewrite" class="org.eclipse.jetty.rewrite.handler.ProxyRule"&gt;
+ * &lt;Set name="pattern"&gt;/*&lt;/Set&gt;
+ * &lt;Set name="proxyTo"&gt;http://webtide.com:8080&lt;/Set&gt;
+ * &lt;/New&gt;
+ * &lt;/Item&gt;
+ *
+ * &lt;Item&gt;
* &lt;New id="response" class="org.eclipse.jetty.rewrite.handler.ResponsePatternRule"&gt;
* &lt;Set name="pattern"&gt;/session/&lt;/Set&gt;
* &lt;Set name="code"&gt;400&lt;/Set&gt;
@@ -75,7 +82,7 @@ import org.eclipse.jetty.server.handler.HandlerWrapper;
* &lt;/New&gt;
* &lt;/Item&gt;
*
- * &lt;Item&gt;
+ * &lt;Item&gt;
* &lt;New id="header" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule"&gt;
* &lt;Set name="pattern"&gt;*.jsp&lt;/Set&gt;
* &lt;Set name="name"&gt;server&lt;/Set&gt;
@@ -83,7 +90,7 @@ import org.eclipse.jetty.server.handler.HandlerWrapper;
* &lt;/New&gt;
* &lt;/Item&gt;
*
- * &lt;Item&gt;
+ * &lt;Item&gt;
* &lt;New id="header" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule"&gt;
* &lt;Set name="pattern"&gt;*.jsp&lt;/Set&gt;
* &lt;Set name="name"&gt;title&lt;/Set&gt;
@@ -91,28 +98,28 @@ import org.eclipse.jetty.server.handler.HandlerWrapper;
* &lt;/New&gt;
* &lt;/Item&gt;
*
- * &lt;Item&gt;
+ * &lt;Item&gt;
* &lt;New id="redirect" class="org.eclipse.jetty.rewrite.handler.RedirectPatternRule"&gt;
* &lt;Set name="pattern"&gt;/test/dispatch&lt;/Set&gt;
* &lt;Set name="location"&gt;http://jetty.eclipse.org&lt;/Set&gt;
* &lt;/New&gt;
* &lt;/Item&gt;
*
- * &lt;Item&gt;
+ * &lt;Item&gt;
* &lt;New id="regexRewrite" class="org.eclipse.jetty.rewrite.handler.RewriteRegexRule"&gt;
* &lt;Set name="regex"&gt;/test-jaas/$&lt;/Set&gt;
* &lt;Set name="replacement"&gt;/demo&lt;/Set&gt;
* &lt;/New&gt;
* &lt;/Item&gt;
- *
- * &lt;Item&gt;
+ *
+ * &lt;Item&gt;
* &lt;New id="forwardedHttps" class="org.eclipse.jetty.rewrite.handler.ForwardedSchemeHeaderRule"&gt;
* &lt;Set name="header"&gt;X-Forwarded-Scheme&lt;/Set&gt;
* &lt;Set name="headerValue"&gt;https&lt;/Set&gt;
* &lt;Set name="scheme"&gt;https&lt;/Set&gt;
* &lt;/New&gt;
* &lt;/Item&gt;
- *
+ *
* &lt;Item&gt;
* &lt;New id="virtualHost" class="org.eclipse.jetty.rewrite.handler.VirtualHostRuleContainer"&gt;
*
@@ -134,10 +141,10 @@ import org.eclipse.jetty.server.handler.HandlerWrapper;
* &lt;/New&gt;
* &lt;/Arg&gt;
* &lt;/Call&gt;
- *
+ *
* &lt;/New&gt;
* &lt;/ Item&gt;
- *
+ *
* &lt;/Array&gt;
* &lt;/Set&gt;
*
@@ -162,13 +169,13 @@ import org.eclipse.jetty.server.handler.HandlerWrapper;
* &lt;/New&gt;
* &lt;/Set&gt;
* </pre>
- *
+ *
*/
public class RewriteHandler extends HandlerWrapper
{
-
+
private RuleContainer _rules;
-
+
/* ------------------------------------------------------------ */
public RewriteHandler()
{
@@ -179,7 +186,7 @@ public class RewriteHandler extends HandlerWrapper
/**
* To enable configuration from jetty.xml on rewriteRequestURI, rewritePathInfo and
* originalPathAttribute
- *
+ *
* @param legacyRule old style rewrite rule
*/
@Deprecated
@@ -201,7 +208,7 @@ public class RewriteHandler extends HandlerWrapper
/* ------------------------------------------------------------ */
/**
* Assigns the rules to process.
- * @param rules an array of {@link Rule}.
+ * @param rules an array of {@link Rule}.
*/
public void setRules(Rule[] rules)
{
@@ -297,13 +304,13 @@ public class RewriteHandler extends HandlerWrapper
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
if (isStarted())
- {
+ {
String returned = _rules.matchAndApply(target, request, response);
target = (returned == null) ? target : returned;
-
+
if (!baseRequest.isHandled())
super.handle(target, baseRequest, request, response);
}
}
-
+
}
diff --git a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/ProxyRuleTest.java b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/ProxyRuleTest.java
index 8a94158..65a0b4d 100644
--- a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/ProxyRuleTest.java
+++ b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/ProxyRuleTest.java
@@ -41,7 +41,7 @@ public class ProxyRuleTest
private Server _proxyServer = new Server();
private Connector _proxyServerConnector = new SelectChannelConnector();
private Server _targetServer = new Server();
- private Connector _targetServerConnector= new SelectChannelConnector();
+ private Connector _targetServerConnector = new SelectChannelConnector();
private HttpClient httpClient = new HttpClient();
@Before
@@ -51,9 +51,11 @@ public class ProxyRuleTest
_targetServer.setHandler(new AbstractHandler()
{
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException,
- ServletException
+ ServletException
{
baseRequest.setHandled(true);
+ String responseString = "uri: " + request.getRequestURI() + " some content";
+ response.getOutputStream().write(responseString.getBytes());
response.setStatus(201);
}
});
@@ -92,16 +94,32 @@ public class ProxyRuleTest
httpClient.send(exchange);
assertEquals(HttpExchange.STATUS_COMPLETED,exchange.waitForDone());
+ assertEquals("uri: / some content",exchange.getResponseContent());
assertEquals(201,exchange.getResponseStatus());
}
@Test
- public void testProxyNoMatch() throws Exception
+ public void testProxyWithDeeperPath() throws Exception
{
ContentExchange exchange = new ContentExchange(true);
exchange.setMethod(HttpMethods.GET);
String body = "BODY";
+ String url = "http://localhost:" + _proxyServerConnector.getLocalPort() + "/foo/bar/foobar?body=" + URLEncoder.encode(body,"UTF-8");
+ exchange.setURL(url);
+
+ httpClient.send(exchange);
+ assertEquals(HttpExchange.STATUS_COMPLETED,exchange.waitForDone());
+ assertEquals("uri: /bar/foobar some content",exchange.getResponseContent());
+ assertEquals(201,exchange.getResponseStatus());
+ }
+
+ @Test
+ public void testProxyNoMatch() throws Exception
+ {
+ ContentExchange exchange = new ContentExchange(true);
+ exchange.setMethod(HttpMethods.GET);
+ String body = "BODY";
String url = "http://localhost:" + _proxyServerConnector.getLocalPort() + "/foobar?body=" + URLEncoder.encode(body,"UTF-8");
exchange.setURL(url);