beautified code, added javadoc
This commit is contained in:
parent
0d8bd99d6a
commit
5e6a9ea78e
|
@ -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
|
||||
|
|
|
@ -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>
|
||||
*
|
||||
*
|
||||
* <Set name="handler">
|
||||
* <New id="Handlers" class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
|
||||
* <Set name="rules">
|
||||
* <Array type="org.eclipse.jetty.rewrite.handler.Rule">
|
||||
*
|
||||
* <Item>
|
||||
* <Item>
|
||||
* <New id="rewrite" class="org.eclipse.jetty.rewrite.handler.RewritePatternRule">
|
||||
* <Set name="pattern">/*</Set>
|
||||
* <Set name="replacement">/test</Set>
|
||||
* </New>
|
||||
* </Item>
|
||||
*
|
||||
* <Item>
|
||||
* <Item>
|
||||
* <New id="rewrite" class="org.eclipse.jetty.rewrite.handler.ProxyRule">
|
||||
* <Set name="pattern">/*</Set>
|
||||
* <Set name="proxyTo">http://webtide.com:8080</Set>
|
||||
* </New>
|
||||
* </Item>
|
||||
*
|
||||
* <Item>
|
||||
* <New id="response" class="org.eclipse.jetty.rewrite.handler.ResponsePatternRule">
|
||||
* <Set name="pattern">/session/</Set>
|
||||
* <Set name="code">400</Set>
|
||||
|
@ -75,7 +82,7 @@ import org.eclipse.jetty.server.handler.HandlerWrapper;
|
|||
* </New>
|
||||
* </Item>
|
||||
*
|
||||
* <Item>
|
||||
* <Item>
|
||||
* <New id="header" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
|
||||
* <Set name="pattern">*.jsp</Set>
|
||||
* <Set name="name">server</Set>
|
||||
|
@ -83,7 +90,7 @@ import org.eclipse.jetty.server.handler.HandlerWrapper;
|
|||
* </New>
|
||||
* </Item>
|
||||
*
|
||||
* <Item>
|
||||
* <Item>
|
||||
* <New id="header" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
|
||||
* <Set name="pattern">*.jsp</Set>
|
||||
* <Set name="name">title</Set>
|
||||
|
@ -91,28 +98,28 @@ import org.eclipse.jetty.server.handler.HandlerWrapper;
|
|||
* </New>
|
||||
* </Item>
|
||||
*
|
||||
* <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>
|
||||
* <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>
|
||||
*
|
||||
* <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">
|
||||
*
|
||||
|
@ -134,10 +141,10 @@ import org.eclipse.jetty.server.handler.HandlerWrapper;
|
|||
* </New>
|
||||
* </Arg>
|
||||
* </Call>
|
||||
*
|
||||
*
|
||||
* </New>
|
||||
* </ Item>
|
||||
*
|
||||
*
|
||||
* </Array>
|
||||
* </Set>
|
||||
*
|
||||
|
@ -162,13 +169,13 @@ import org.eclipse.jetty.server.handler.HandlerWrapper;
|
|||
* </New>
|
||||
* </Set>
|
||||
* </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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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,13 +94,29 @@ 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 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";
|
||||
|
|
Loading…
Reference in New Issue