diff --git a/VERSION.txt b/VERSION.txt index 92c169a2f30..dfd08003eef 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -9,6 +9,7 @@ jetty-7.0.0.M2-SNAPSHOT + JETTY-1004 CERT VU#402580 Canonical path handling includes ? in path segment + JETTY-1014 Enable start-stop-daemon by default on jetty.sh (START_STOP_DAEMON=1) + JETTY-1015 Reduce BayeuxClient and HttpClient lock contention + + 275396 Added ScopedHandler to set servlet scope before security handler jetty-6.1.17 30 April 2009 + JETTY-936 Make optional dispatching to welcome files as servlets diff --git a/jetty-ajp/src/test/java/org/eclipse/jetty/ajp/Ajp13ConnectionTest.java b/jetty-ajp/src/test/java/org/eclipse/jetty/ajp/Ajp13ConnectionTest.java index 0a27086dc57..5804f81f46e 100644 --- a/jetty-ajp/src/test/java/org/eclipse/jetty/ajp/Ajp13ConnectionTest.java +++ b/jetty-ajp/src/test/java/org/eclipse/jetty/ajp/Ajp13ConnectionTest.java @@ -299,10 +299,9 @@ public class Ajp13ConnectionTest extends TestCase public static class Handler extends AbstractHandler { - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - Request base_request=(request instanceof Request) ? (Request) request : HttpConnection.getCurrentConnection().getRequest(); - base_request.setHandled(true); + baseRequest.setHandled(true); response.setStatus(HttpServletResponse.SC_OK); response.setContentType("text/plain"); response.getWriter().println("success"); diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/ExpireTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/ExpireTest.java index e554eeaeb3c..636984bb1d6 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/ExpireTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/ExpireTest.java @@ -70,7 +70,7 @@ public class ExpireTest extends TestCase server.setConnectors( new Connector[] { connector } ); server.setHandler( new AbstractHandler() { - public void handle( String target, HttpServletRequest servletRequest, HttpServletResponse response ) throws IOException, + public void handle( String target, Request baseRequest, HttpServletRequest servletRequest, HttpServletResponse response ) throws IOException, ServletException { Request request = (Request) servletRequest; diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpExchangeTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpExchangeTest.java index 52f3d9b45e7..a43f6616534 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpExchangeTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpExchangeTest.java @@ -336,13 +336,12 @@ public class HttpExchangeTest extends TestCase newServer(); _server.setHandler(new AbstractHandler() { - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { int i=0; try { - Request base_request=(request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest(); - base_request.setHandled(true); + baseRequest.setHandled(true); response.setStatus(200); _count.incrementAndGet(); diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/SecurityListenerTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/SecurityListenerTest.java index f41b9af06d6..d4d36d9183b 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/SecurityListenerTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/SecurityListenerTest.java @@ -288,11 +288,10 @@ public class SecurityListenerTest extends TestCase Handler testHandler = new AbstractHandler() { - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // System.out.println("passed authentication!"); - Request base_request=(request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest(); - base_request.setHandled(true); + baseRequest.setHandled(true); response.setStatus(200); if (request.getServerName().equals("jetty.eclipse.org")) { diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/SslSecurityListenerTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/SslSecurityListenerTest.java index 83de795d34b..df092eb7208 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/SslSecurityListenerTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/SslSecurityListenerTest.java @@ -168,12 +168,11 @@ public class SslSecurityListenerTest extends TestCase Handler testHandler = new AbstractHandler() { - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // System.err.println("passed authentication!\n"+((Request)request).getConnection().getRequestFields()); - Request base_request = (request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest(); - base_request.setHandled(true); + baseRequest.setHandled(true); response.setStatus(200); response.setContentType("text/plain"); if (request.getServerName().equals("jetty.eclipse.org")) diff --git a/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyConnectors.java b/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyConnectors.java index 714a5f007c9..0bbc9d9260d 100644 --- a/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyConnectors.java +++ b/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyConnectors.java @@ -67,7 +67,7 @@ public class ManyConnectors public static class HelloHandler extends AbstractHandler { - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); diff --git a/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyContexts.java b/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyContexts.java index d408f185f3f..c645df6c2dd 100644 --- a/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyContexts.java +++ b/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyContexts.java @@ -84,7 +84,7 @@ public class ManyContexts _welcome=welcome; } - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { ((Request)request).setHandled(true); response.setStatus(HttpServletResponse.SC_OK); diff --git a/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyHandlers.java b/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyHandlers.java index f62203aef05..a6e72d7ab16 100644 --- a/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyHandlers.java +++ b/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/ManyHandlers.java @@ -72,10 +72,10 @@ public class ManyHandlers Handler param=new ParamHandler(); HandlerWrapper wrapper = new HandlerWrapper() { - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { request.setAttribute("welcome","Hello"); - super.handle(target,request,response); + super.handle(target,baseRequest,request, response); } }; Handler hello=new HelloHandler(); @@ -103,7 +103,7 @@ public class ManyHandlers public static class ParamHandler extends AbstractHandler { - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { Map params = request.getParameterMap(); if (params.size()>0) @@ -117,7 +117,7 @@ public class ManyHandlers public static class HelloHandler extends AbstractHandler { - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); diff --git a/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/OneContext.java b/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/OneContext.java index 468b51b706f..bd9d6f0fd2d 100644 --- a/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/OneContext.java +++ b/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/OneContext.java @@ -55,7 +55,7 @@ public class OneContext public static class HelloHandler extends AbstractHandler { - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setStatus(HttpServletResponse.SC_OK); response.setContentType("text/html"); diff --git a/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/OneHandler.java b/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/OneHandler.java index 5894301cc60..cdd9132b4f4 100644 --- a/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/OneHandler.java +++ b/jetty-embedded-examples/src/main/java/org/eclipse/jetty/embedded/OneHandler.java @@ -41,7 +41,7 @@ public class OneHandler public static class HelloHandler extends AbstractHandler { - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); 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 020595c09bb..57d1572a5dd 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 @@ -20,6 +20,7 @@ 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; /* ------------------------------------------------------------ */ @@ -322,7 +323,7 @@ public class RewriteHandler extends HandlerWrapper /* (non-Javadoc) * @see org.eclipse.jetty.server.handler.HandlerWrapper#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int) */ - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { if (isStarted()) { @@ -331,7 +332,7 @@ public class RewriteHandler extends HandlerWrapper if (!_rules.isHandled()) { - super.handle(target, request, response); + super.handle(target, baseRequest, request, response); } } } diff --git a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewriteHandlerTest.java b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewriteHandlerTest.java index 9c2e2142600..386e2ffe1d0 100644 --- a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewriteHandlerTest.java +++ b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewriteHandlerTest.java @@ -18,6 +18,7 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.handler.AbstractHandler; @@ -35,7 +36,7 @@ public class RewriteHandlerTest extends AbstractRuleTestCase _server.setHandler(_handler); _handler.setHandler(new AbstractHandler(){ - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setStatus(201); request.setAttribute("target",target); @@ -70,7 +71,7 @@ public class RewriteHandlerTest extends AbstractRuleTestCase _handler.setRewritePathInfo(false); _request.setRequestURI("/foo/bar"); _request.setPathInfo("/foo/bar"); - _handler.handle("/foo/bar",_request,_response); + _handler.handle("/foo/bar",_request,_request, _response); assertEquals(201,_response.getStatus()); assertEquals("/foo/bar",_request.getAttribute("target")); assertEquals("/foo/bar",_request.getAttribute("URI")); @@ -83,7 +84,7 @@ public class RewriteHandlerTest extends AbstractRuleTestCase _handler.setOriginalPathAttribute(null); _request.setRequestURI("/aaa/bar"); _request.setPathInfo("/aaa/bar"); - _handler.handle("/aaa/bar",_request,_response); + _handler.handle("/aaa/bar",_request,_request, _response); assertEquals(201,_response.getStatus()); assertEquals("/ddd/bar",_request.getAttribute("target")); assertEquals("/aaa/bar",_request.getAttribute("URI")); @@ -98,7 +99,7 @@ public class RewriteHandlerTest extends AbstractRuleTestCase _handler.setRewritePathInfo(true); _request.setRequestURI("/aaa/bar"); _request.setPathInfo("/aaa/bar"); - _handler.handle("/aaa/bar",_request,_response); + _handler.handle("/aaa/bar",_request,_request, _response); assertEquals(201,_response.getStatus()); assertEquals("/ddd/bar",_request.getAttribute("target")); assertEquals("/ddd/bar",_request.getAttribute("URI")); @@ -111,7 +112,7 @@ public class RewriteHandlerTest extends AbstractRuleTestCase _rule2.setTerminating(true); _request.setRequestURI("/aaa/bar"); _request.setPathInfo("/aaa/bar"); - _handler.handle("/aaa/bar",_request,_response); + _handler.handle("/aaa/bar",_request,_request, _response); assertEquals(201,_response.getStatus()); assertEquals("/ccc/bar",_request.getAttribute("target")); assertEquals("/ccc/bar",_request.getAttribute("URI")); @@ -127,7 +128,7 @@ public class RewriteHandlerTest extends AbstractRuleTestCase _request.setAttribute("info",null); _request.setRequestURI("/aaa/bar"); _request.setPathInfo("/aaa/bar"); - _handler.handle("/aaa/bar",_request,_response); + _handler.handle("/aaa/bar",_request,_request, _response); assertEquals(200,_response.getStatus()); assertEquals(null,_request.getAttribute("target")); assertEquals(null,_request.getAttribute("URI")); diff --git a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/VirtualHostRuleContainerTest.java b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/VirtualHostRuleContainerTest.java index ee9906adbe5..208d94f1d4c 100644 --- a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/VirtualHostRuleContainerTest.java +++ b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/VirtualHostRuleContainerTest.java @@ -178,7 +178,7 @@ public class VirtualHostRuleContainerTest extends AbstractRuleTestCase private void handleRequest() throws Exception { - _server.handle("/cheese/bar", _request, _response); + _server.handle("/cheese/bar", _request, _request, _response); } } diff --git a/jetty-security/src/main/java/org/eclipse/jetty/security/ConstraintSecurityHandler.java b/jetty-security/src/main/java/org/eclipse/jetty/security/ConstraintSecurityHandler.java index 209e5b8f586..4dca9942e9d 100644 --- a/jetty-security/src/main/java/org/eclipse/jetty/security/ConstraintSecurityHandler.java +++ b/jetty-security/src/main/java/org/eclipse/jetty/security/ConstraintSecurityHandler.java @@ -306,7 +306,7 @@ public class ConstraintSecurityHandler extends SecurityHandler implements Constr } - protected boolean isAuthMandatory(Request base_request, Response base_response, Object constraintInfo) + protected boolean isAuthMandatory(Request baseRequest, Response base_response, Object constraintInfo) { if (constraintInfo == null) { diff --git a/jetty-security/src/main/java/org/eclipse/jetty/security/IdentityService.java b/jetty-security/src/main/java/org/eclipse/jetty/security/IdentityService.java index 00d908c4138..a16610ca968 100644 --- a/jetty-security/src/main/java/org/eclipse/jetty/security/IdentityService.java +++ b/jetty-security/src/main/java/org/eclipse/jetty/security/IdentityService.java @@ -17,6 +17,7 @@ import java.security.Principal; import javax.security.auth.Subject; +import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.UserIdentity; /* ------------------------------------------------------------ */ @@ -32,7 +33,7 @@ public interface IdentityService /** * Associate a user identity with the current thread. * This is called with as a thread enters the - * {@link SecurityHandler#handle(String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)} + * {@link SecurityHandler#handle(String, Request, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)} * method and then again with a null argument as that call exits. * @param user The current user or null for no user to associated. */ diff --git a/jetty-security/src/main/java/org/eclipse/jetty/security/SecurityHandler.java b/jetty-security/src/main/java/org/eclipse/jetty/security/SecurityHandler.java index de58139352d..b42d527aa36 100644 --- a/jetty-security/src/main/java/org/eclipse/jetty/security/SecurityHandler.java +++ b/jetty-security/src/main/java/org/eclipse/jetty/security/SecurityHandler.java @@ -381,81 +381,80 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti * javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse, int) */ - public void handle(String pathInContext, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String pathInContext, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - final Request base_request = (request instanceof Request) ? (Request) request : HttpConnection.getCurrentConnection().getRequest(); - final Response base_response = (response instanceof Response) ? (Response) response : HttpConnection.getCurrentConnection().getResponse(); + final Response base_response = baseRequest.getResponse(); final Handler handler=getHandler(); if (handler==null) return; - if (_authenticator!=null && checkSecurity(base_request)) + if (_authenticator!=null && checkSecurity(baseRequest)) { - Object constraintInfo = prepareConstraintInfo(pathInContext, base_request); + Object constraintInfo = prepareConstraintInfo(pathInContext, baseRequest); // Check data constraints - if (!checkUserDataPermissions(pathInContext, base_request, base_response, constraintInfo)) + if (!checkUserDataPermissions(pathInContext, baseRequest, base_response, constraintInfo)) { - if (!base_request.isHandled()) + if (!baseRequest.isHandled()) { response.sendError(Response.SC_FORBIDDEN); - base_request.setHandled(true); + baseRequest.setHandled(true); } return; } // is Auth mandatory? - boolean isAuthMandatory = isAuthMandatory(base_request, base_response, constraintInfo); + boolean isAuthMandatory = isAuthMandatory(baseRequest, base_response, constraintInfo); // check authentication try { final Authenticator authenticator = _authenticator; - Authentication authentication = base_request.getAuthentication(); + Authentication authentication = baseRequest.getAuthentication(); if (authentication==null || authentication==Authentication.NOT_CHECKED) authentication=authenticator.validateRequest(request, response, isAuthMandatory); if (authentication instanceof Authentication.ResponseSent) { - base_request.setHandled(true); + baseRequest.setHandled(true); } else if (authentication instanceof Authentication.User) { Authentication.User userAuth = (Authentication.User)authentication; - base_request.setAuthentication(authentication); + baseRequest.setAuthentication(authentication); _identityService.associate(userAuth.getUserIdentity()); if (isAuthMandatory) { - boolean authorized=checkWebResourcePermissions(pathInContext, base_request, base_response, constraintInfo, userAuth.getUserIdentity()); + boolean authorized=checkWebResourcePermissions(pathInContext, baseRequest, base_response, constraintInfo, userAuth.getUserIdentity()); if (!authorized) { response.sendError(Response.SC_FORBIDDEN, "!role"); - base_request.setHandled(true); + baseRequest.setHandled(true); return; } } - handler.handle(pathInContext, request, response); + handler.handle(pathInContext, baseRequest, request, response); authenticator.secureResponse(request, response, isAuthMandatory, userAuth); } else if (authentication instanceof Authentication.Deferred) { DeferredAuthentication lazy= (DeferredAuthentication)authentication; lazy.setIdentityService(_identityService); - base_request.setAuthentication(authentication); + baseRequest.setAuthentication(authentication); try { - handler.handle(pathInContext, request, response); + handler.handle(pathInContext, baseRequest, request, response); } finally { lazy.setIdentityService(null); } - Authentication auth=base_request.getAuthentication(); + Authentication auth=baseRequest.getAuthentication(); if (auth instanceof Authentication.User) { Authentication.User userAuth = (Authentication.User)auth; @@ -466,8 +465,8 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti } else { - base_request.setAuthentication(authentication); - handler.handle(pathInContext, request, response); + baseRequest.setAuthentication(authentication); + handler.handle(pathInContext, baseRequest, request, response); authenticator.secureResponse(request, response, isAuthMandatory, null); } } @@ -483,7 +482,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti } } else - handler.handle(pathInContext, request, response); + handler.handle(pathInContext, baseRequest, request, response); } @@ -494,7 +493,7 @@ public abstract class SecurityHandler extends HandlerWrapper implements Authenti protected abstract boolean checkUserDataPermissions(String pathInContext, Request request, Response response, Object constraintInfo) throws IOException; /* ------------------------------------------------------------ */ - protected abstract boolean isAuthMandatory(Request base_request, Response base_response, Object constraintInfo); + protected abstract boolean isAuthMandatory(Request baseRequest, Response base_response, Object constraintInfo); /* ------------------------------------------------------------ */ protected abstract boolean checkWebResourcePermissions(String pathInContext, Request request, Response response, Object constraintInfo, diff --git a/jetty-security/src/test/java/org/eclipse/jetty/security/ConstraintTest.java b/jetty-security/src/test/java/org/eclipse/jetty/security/ConstraintTest.java index fd39fa85e3b..3ec84fa8213 100644 --- a/jetty-security/src/test/java/org/eclipse/jetty/security/ConstraintTest.java +++ b/jetty-security/src/test/java/org/eclipse/jetty/security/ConstraintTest.java @@ -570,10 +570,9 @@ public class ConstraintTest extends TestCase class RequestHandler extends AbstractHandler { - public void handle(String target, HttpServletRequest request, HttpServletResponse response ) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response ) throws IOException, ServletException { - Request base_request=(request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest(); - base_request.setHandled(true); + baseRequest.setHandled(true); if (request.getAuthType()==null || "user".equals(request.getRemoteUser()) || request.isUserInRole("user")) { response.setStatus(200); @@ -591,10 +590,10 @@ public class ConstraintTest extends TestCase { /* ------------------------------------------------------------ */ /** - * @see org.eclipse.jetty.server.handler.HandlerWrapper#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) + * @see org.eclipse.jetty.server.handler.HandlerWrapper#handle(java.lang.String, Request, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ @Override - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { UserIdentity.Scope old = ((Request) request).getUserIdentityScope(); @@ -622,7 +621,7 @@ public class ConstraintTest extends TestCase try { - super.handle(target,request,response); + super.handle(target,baseRequest,request, response); } finally { @@ -633,7 +632,7 @@ public class ConstraintTest extends TestCase class RoleCheckHandler extends AbstractHandler { - public void handle(String target, HttpServletRequest request, HttpServletResponse response ) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response ) throws IOException, ServletException { ((Request) request).setHandled(true); if (request.getAuthType()==null || "user".equals(request.getRemoteUser()) || request.isUserInRole("untranslated")) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Dispatcher.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Dispatcher.java index 6afa257cf73..38dc538d38c 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Dispatcher.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Dispatcher.java @@ -126,20 +126,20 @@ public class Dispatcher implements RequestDispatcher */ public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException { - Request base_request=(request instanceof Request)?((Request)request):HttpConnection.getCurrentConnection().getRequest(); + Request baseRequest=(request instanceof Request)?((Request)request):HttpConnection.getCurrentConnection().getRequest(); request.removeAttribute(__JSP_FILE); // TODO remove when glassfish 1044 is fixed // TODO - allow stream or writer???? - DispatcherType old_type = base_request.getDispatcherType(); - Attributes old_attr=base_request.getAttributes(); - MultiMap old_params=base_request.getParameters(); + DispatcherType old_type = baseRequest.getDispatcherType(); + Attributes old_attr=baseRequest.getAttributes(); + MultiMap old_params=baseRequest.getParameters(); try { - base_request.setDispatcherType(DispatcherType.INCLUDE); - base_request.getConnection().include(); + baseRequest.setDispatcherType(DispatcherType.INCLUDE); + baseRequest.getConnection().include(); if (_named!=null) - _contextHandler.doHandle(_named, base_request,(HttpServletRequest)request, (HttpServletResponse)response); + _contextHandler.handle(_named,baseRequest, (HttpServletRequest)request, (HttpServletResponse)response); else { String query=_dQuery; @@ -163,7 +163,7 @@ public class Dispatcher implements RequestDispatcher } } - base_request.setParameters(parameters); + baseRequest.setParameters(parameters); } IncludeAttributes attr = new IncludeAttributes(old_attr); @@ -174,17 +174,17 @@ public class Dispatcher implements RequestDispatcher attr._pathInfo=_path; attr._query=query; - base_request.setAttributes(attr); + baseRequest.setAttributes(attr); - _contextHandler.doHandle(_named==null?_path:_named, base_request,(HttpServletRequest)request, (HttpServletResponse)response); + _contextHandler.handle(_named==null?_path:_named,baseRequest, (HttpServletRequest)request, (HttpServletResponse)response); } } finally { - base_request.setAttributes(old_attr); - base_request.getConnection().included(); - base_request.setParameters(old_params); - base_request.setDispatcherType(old_type); + baseRequest.setAttributes(old_attr); + baseRequest.getConnection().included(); + baseRequest.setParameters(old_params); + baseRequest.setDispatcherType(old_type); } } @@ -195,26 +195,26 @@ public class Dispatcher implements RequestDispatcher */ protected void forward(ServletRequest request, ServletResponse response, DispatcherType dispatch) throws ServletException, IOException { - Request base_request=(request instanceof Request)?((Request)request):HttpConnection.getCurrentConnection().getRequest(); - Response base_response=(Response)base_request.getResponse(); + Request baseRequest=(request instanceof Request)?((Request)request):HttpConnection.getCurrentConnection().getRequest(); + Response base_response=(Response)baseRequest.getResponse(); base_response.fwdReset(); request.removeAttribute(__JSP_FILE); // TODO remove when glassfish 1044 is fixed - String old_uri=base_request.getRequestURI(); - String old_context_path=base_request.getContextPath(); - String old_servlet_path=base_request.getServletPath(); - String old_path_info=base_request.getPathInfo(); - String old_query=base_request.getQueryString(); - Attributes old_attr=base_request.getAttributes(); - MultiMap old_params=base_request.getParameters(); - DispatcherType old_type=base_request.getDispatcherType(); + String old_uri=baseRequest.getRequestURI(); + String old_context_path=baseRequest.getContextPath(); + String old_servlet_path=baseRequest.getServletPath(); + String old_path_info=baseRequest.getPathInfo(); + String old_query=baseRequest.getQueryString(); + Attributes old_attr=baseRequest.getAttributes(); + MultiMap old_params=baseRequest.getParameters(); + DispatcherType old_type=baseRequest.getDispatcherType(); try { - base_request.setDispatcherType(dispatch); + baseRequest.setDispatcherType(dispatch); if (_named!=null) - _contextHandler.doHandle(_named, base_request,(HttpServletRequest)request, (HttpServletResponse)response); + _contextHandler.handle(_named,baseRequest, (HttpServletRequest)request, (HttpServletResponse)response); else { String query=_dQuery; @@ -228,8 +228,8 @@ public class Dispatcher implements RequestDispatcher if( old_params == null ) { - base_request.getParameterNames(); // force parameters to be evaluated - old_params = base_request.getParameters(); + baseRequest.getParameterNames(); // force parameters to be evaluated + old_params = baseRequest.getParameters(); } if (old_params!=null && old_params.size()>0) @@ -290,8 +290,8 @@ public class Dispatcher implements RequestDispatcher } } - base_request.setParameters(parameters); - base_request.setQueryString(query); + baseRequest.setParameters(parameters); + baseRequest.setQueryString(query); } ForwardAttributes attr = new ForwardAttributes(old_attr); @@ -319,14 +319,14 @@ public class Dispatcher implements RequestDispatcher - base_request.setRequestURI(_uri); - base_request.setContextPath(_contextHandler.getContextPath()); - base_request.setAttributes(attr); - base_request.setQueryString(query); + baseRequest.setRequestURI(_uri); + baseRequest.setContextPath(_contextHandler.getContextPath()); + baseRequest.setAttributes(attr); + baseRequest.setQueryString(query); - _contextHandler.doHandle(_path, base_request,(HttpServletRequest)request, (HttpServletResponse)response); + _contextHandler.handle(_path,baseRequest, (HttpServletRequest)request, (HttpServletResponse)response); - if (base_request.getConnection().getResponse().isWriting()) + if (baseRequest.getConnection().getResponse().isWriting()) { try {response.getWriter().close();} catch(IllegalStateException e) { response.getOutputStream().close(); } @@ -340,14 +340,14 @@ public class Dispatcher implements RequestDispatcher } finally { - base_request.setRequestURI(old_uri); - base_request.setContextPath(old_context_path); - base_request.setServletPath(old_servlet_path); - base_request.setPathInfo(old_path_info); - base_request.setAttributes(old_attr); - base_request.setParameters(old_params); - base_request.setQueryString(old_query); - base_request.setDispatcherType(old_type); + baseRequest.setRequestURI(old_uri); + baseRequest.setContextPath(old_context_path); + baseRequest.setServletPath(old_servlet_path); + baseRequest.setPathInfo(old_path_info); + baseRequest.setAttributes(old_attr); + baseRequest.setParameters(old_params); + baseRequest.setQueryString(old_query); + baseRequest.setDispatcherType(old_type); } } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Handler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Handler.java index 3e0c3ff5ab8..34be72e5ea7 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Handler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Handler.java @@ -27,6 +27,7 @@ public interface Handler extends LifeCycle /* ------------------------------------------------------------ */ /** Handle a request. * @param target The target of the request - either a URI or a name. + * @param baseRequest TODO * @param request The request either as the {@link Request} * object or a wrapper of that request. The {@link HttpConnection#getCurrentConnection()} * method can be used access the Request object if required. @@ -36,7 +37,7 @@ public interface Handler extends LifeCycle * @throws IOException * @throws ServletException */ - public void handle(String target, HttpServletRequest request, HttpServletResponse response) + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException; public void setServer(Server server); diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/HandlerContainer.java b/jetty-server/src/main/java/org/eclipse/jetty/server/HandlerContainer.java index 208cdeefcc7..a64c0f8d0bc 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/HandlerContainer.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/HandlerContainer.java @@ -24,8 +24,29 @@ import org.eclipse.jetty.util.component.LifeCycle; */ public interface HandlerContainer extends LifeCycle { + /* ------------------------------------------------------------ */ + /** + * @return array of handlers directly contained by this handler. + */ public Handler[] getHandlers(); + + /* ------------------------------------------------------------ */ + /** + * @return array of all handlers contained by this handler and it's children + */ public Handler[] getChildHandlers(); + + /* ------------------------------------------------------------ */ + /** + * @param byclass + * @return array of all handlers contained by this handler and it's children of the passed type. + */ public Handler[] getChildHandlersByClass(Class byclass); + + /* ------------------------------------------------------------ */ + /** + * @param byclass + * @return first handler of all handlers contained by this handler and it's children of the passed type. + */ public Handler getChildHandlerByClass(Class byclass); } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java index fffae5be074..64a2fa1a808 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java @@ -126,6 +126,7 @@ public class Request implements HttpServletRequest private String _characterEncoding; protected HttpConnection _connection; private ContextHandler.Context _context; + private boolean _newContext; private String _contextPath; private CookieCutter _cookies; private boolean _cookiesExtracted=false; @@ -149,7 +150,6 @@ public class Request implements HttpServletRequest private Object _requestAttributeListeners; private String _requestedSessionId; private boolean _requestedSessionIdFromCookie=false; - private Object _requestListeners; private String _requestURI; private Map _savedNewSessions; private String _scheme=URIUtil.HTTP; @@ -1545,9 +1545,21 @@ public class Request implements HttpServletRequest */ public void setContext(Context context) { + _newContext=_context!=context; _context=context; } + /* ------------------------------------------------------------ */ + /** + * @return True if this is the first call of takeNewContext() since the last {@link #setContext(Context)} call. + */ + public boolean takeNewContext() + { + boolean nc=_newContext; + _newContext=false; + return nc; + } + /* ------------------------------------------------------------ */ /** * Sets the "context path" for this request @@ -1679,14 +1691,6 @@ public class Request implements HttpServletRequest { _requestedSessionIdFromCookie = requestedSessionIdCookie; } - /* ------------------------------------------------------------ */ - /** - * @param requestListeners {@link LazyList} of {@link ServletRequestListener}s - */ - public void setRequestListeners(Object requestListeners) - { - _requestListeners=requestListeners; - } /* ------------------------------------------------------------ */ /** @@ -1790,17 +1794,6 @@ public class Request implements HttpServletRequest return _async; } - /* ------------------------------------------------------------ */ - /** - * @return {@link LazyList} of {@link ServletRequestListener}s - */ - public Object takeRequestListeners() - { - final Object listeners=_requestListeners; - _requestListeners=null; - return listeners; - } - /* ------------------------------------------------------------ */ public String toString() { diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java index 3dbb4546bf6..7a250daf617 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Response.java @@ -302,7 +302,7 @@ public class Response implements HttpServletResponse request.setAttribute(Dispatcher.ERROR_REQUEST_URI, request.getRequestURI()); request.setAttribute(Dispatcher.ERROR_SERVLET_NAME,request.getServletName()); - error_handler.handle(null,_connection.getRequest(),this); + error_handler.handle(null,_connection.getRequest(),_connection.getRequest(),this ); } else { diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java index 0b98dacae78..c3facd5ab97 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Server.java @@ -312,17 +312,17 @@ public class Server extends HandlerWrapper implements Attributes public void handle(HttpConnection connection) throws IOException, ServletException { final String target=connection.getRequest().getPathInfo(); - final HttpServletRequest request=connection.getRequest(); - final HttpServletResponse response=connection.getResponse(); + final Request request=connection.getRequest(); + final Response response=connection.getResponse(); if (Log.isDebugEnabled()) { Log.debug("REQUEST "+target+" on "+connection); - handle(target, request, response); + handle(target, request, request, response); Log.debug("RESPONSE "+target+" "+connection.getResponse().getStatus()); } else - handle(target, request, response); + handle(target, request, request, response); } /* ------------------------------------------------------------ */ @@ -336,36 +336,36 @@ public class Server extends HandlerWrapper implements Attributes final AsyncRequest async = connection.getRequest().getAsyncRequest(); final AsyncRequest.AsyncEventState state = async.getAsyncEventState(); - final Request base_request=connection.getRequest(); + final Request baseRequest=connection.getRequest(); final String path=state.getPath(); if (path!=null) { // this is a dispatch with a path - base_request.setAttribute(AsyncContext.ASYNC_REQUEST_URI,base_request.getRequestURI()); - base_request.setAttribute(AsyncContext.ASYNC_QUERY_STRING,base_request.getQueryString()); + baseRequest.setAttribute(AsyncContext.ASYNC_REQUEST_URI,baseRequest.getRequestURI()); + baseRequest.setAttribute(AsyncContext.ASYNC_QUERY_STRING,baseRequest.getQueryString()); - base_request.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,state.getSuspendedContext().getContextPath()); + baseRequest.setAttribute(AsyncContext.ASYNC_CONTEXT_PATH,state.getSuspendedContext().getContextPath()); final String contextPath=state.getServletContext().getContextPath(); HttpURI uri = new HttpURI(URIUtil.addPaths(contextPath,path)); - base_request.setUri(uri); - base_request.setRequestURI(null); - base_request.setPathInfo(base_request.getRequestURI()); - base_request.setQueryString(uri.getQuery()); + baseRequest.setUri(uri); + baseRequest.setRequestURI(null); + baseRequest.setPathInfo(baseRequest.getRequestURI()); + baseRequest.setQueryString(uri.getQuery()); } - final String target=base_request.getPathInfo(); + final String target=baseRequest.getPathInfo(); final HttpServletRequest request=(HttpServletRequest)async.getRequest(); final HttpServletResponse response=(HttpServletResponse)async.getResponse(); if (Log.isDebugEnabled()) { Log.debug("REQUEST "+target+" on "+connection); - handle(target, request, response); + handle(target, baseRequest, request, response); Log.debug("RESPONSE "+target+" "+connection.getResponse().getStatus()); } else - handle(target, request, response); + handle(target, baseRequest, request, response); } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java index 68a50f0af13..fb244a1ffa9 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java @@ -81,7 +81,7 @@ import org.eclipse.jetty.util.resource.Resource; * * */ -public class ContextHandler extends HandlerWrapper implements Attributes, Server.Graceful, CompleteHandler +public class ContextHandler extends ScopedHandler implements Attributes, Server.Graceful, CompleteHandler { private static ThreadLocal __context=new ThreadLocal(); public static final String MANAGED_ATTRIBUTES = "org.eclipse.jetty.server.servlet.ManagedAttributes"; @@ -116,7 +116,6 @@ public class ContextHandler extends HandlerWrapper implements Attributes, Server private Set _connectors; private EventListener[] _eventListeners; private Logger _logger; - private boolean _shutdown; private boolean _allowNullPathInfo; private int _maxFormContentSize=Integer.getInteger("org.eclipse.jetty.server.Request.maxFormContentSize",200000).intValue(); private boolean _compactPath=false; @@ -127,6 +126,13 @@ public class ContextHandler extends HandlerWrapper implements Attributes, Server private Object _requestListeners; private Object _requestAttributeListeners; private Set _managedAttributes; + + private boolean _shutdown=false; + private boolean _available=true; + private volatile int _availability; // 0=STOPPED, 1=AVAILABLE, 2=SHUTDOWN, 3=UNAVAILABLE + + private final static int __STOPPED=0,__AVAILABLE=1,__SHUTDOWN=2,__UNAVAILABLE=3; + /* ------------------------------------------------------------ */ /** @@ -496,7 +502,32 @@ public class ContextHandler extends HandlerWrapper implements Attributes, Server */ public void setShutdown(boolean shutdown) { - _shutdown = shutdown; + synchronized(this) + { + _shutdown = shutdown; + _availability=isRunning()?(_shutdown?__SHUTDOWN:_available?__AVAILABLE:__UNAVAILABLE):__STOPPED; + } + } + + /* ------------------------------------------------------------ */ + /** + * @return false if this context is unavailable (sends 503) + */ + public boolean isAvailable() + { + return _available; + } + + /* ------------------------------------------------------------ */ + /** Set Available status. + */ + public void setAvailable(boolean available) + { + synchronized(this) + { + _available = available; + _availability=isRunning()?(_shutdown?__SHUTDOWN:_available?__AVAILABLE:__UNAVAILABLE):__STOPPED; + } } /* ------------------------------------------------------------ */ @@ -517,6 +548,8 @@ public class ContextHandler extends HandlerWrapper implements Attributes, Server */ protected void doStart() throws Exception { + _availability=__STOPPED; + if (_contextPath==null) throw new IllegalStateException("Null contextPath"); @@ -547,10 +580,12 @@ public class ContextHandler extends HandlerWrapper implements Attributes, Server if (_errorHandler==null) setErrorHandler(new ErrorHandler()); + // defers the calling of super.doStart() startContext(); - + + _availability=_shutdown?__SHUTDOWN:_available?__AVAILABLE:__UNAVAILABLE; } finally { @@ -561,6 +596,7 @@ public class ContextHandler extends HandlerWrapper implements Attributes, Server { current_thread.setContextClassLoader(old_classloader); } + } } @@ -616,6 +652,8 @@ public class ContextHandler extends HandlerWrapper implements Attributes, Server */ protected void doStop() throws Exception { + _availability=__STOPPED; + ClassLoader old_classloader=null; Thread current_thread=null; @@ -670,19 +708,28 @@ public class ContextHandler extends HandlerWrapper implements Attributes, Server /* * @see org.eclipse.jetty.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ - public void handle(String target, HttpServletRequest request, HttpServletResponse response) + public boolean checkContext(final String target, final Request baseRequest, final HttpServletResponse response) throws IOException, ServletException { - Request base_request=(request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest(); - DispatcherType dispatch=base_request.getDispatcherType(); + DispatcherType dispatch=baseRequest.getDispatcherType(); + + switch(_availability) + { + case __STOPPED: + case __SHUTDOWN: + return false; + case __UNAVAILABLE: + response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); + return false; + default: + if((DispatcherType.REQUEST.equals(dispatch) && baseRequest.isHandled())) + return false; + } - if( !isStarted() || _shutdown || (DispatcherType.REQUEST.equals(dispatch) && base_request.isHandled())) - return; - // Check the vhosts if (_vhosts!=null && _vhosts.length>0) { - String vhost = normalizeHostname( request.getServerName()); + String vhost = normalizeHostname( baseRequest.getServerName()); boolean match=false; @@ -698,7 +745,7 @@ public class ContextHandler extends HandlerWrapper implements Attributes, Server match=contextVhost.equalsIgnoreCase(vhost); } if (!match) - return; + return false; } // Check the connector @@ -706,43 +753,41 @@ public class ContextHandler extends HandlerWrapper implements Attributes, Server { String connector=HttpConnection.getCurrentConnection().getConnector().getName(); if (connector==null || !_connectors.contains(connector)) - return; + return false; } - if (_compactPath) - target=URIUtil.compactPath(target); if (target.startsWith(_contextPath)) { if (_contextPath.length()==target.length() && _contextPath.length()>1 &&!_allowNullPathInfo) { // context request must end with / - base_request.setHandled(true); - if (request.getQueryString()!=null) - response.sendRedirect(URIUtil.addPaths(request.getRequestURI(),URIUtil.SLASH)+"?"+request.getQueryString()); + baseRequest.setHandled(true); + if (baseRequest.getQueryString()!=null) + response.sendRedirect(URIUtil.addPaths(baseRequest.getRequestURI(),URIUtil.SLASH)+"?"+baseRequest.getQueryString()); else - response.sendRedirect(URIUtil.addPaths(request.getRequestURI(),URIUtil.SLASH)); - return; + response.sendRedirect(URIUtil.addPaths(baseRequest.getRequestURI(),URIUtil.SLASH)); + return false; } } else { // Not for this context! - return; + return false; } - doHandle(target,base_request,request,response); + return true; } - + + /* ------------------------------------------------------------ */ - /* - * @see org.eclipse.jetty.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) + /** + * @see org.eclipse.jetty.server.handler.ScopedHandler#doScope(java.lang.String, org.eclipse.jetty.server.Request, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ - public void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) - throws IOException, ServletException - { - boolean new_context=false; + @Override + public void doScope(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + { Context old_context=null; String old_context_path=null; String old_servlet_path=null; @@ -754,15 +799,18 @@ public class ContextHandler extends HandlerWrapper implements Attributes, Server DispatcherType dispatch=baseRequest.getDispatcherType(); old_context=baseRequest.getContext(); - + // Are we already in this context? if (old_context!=_scontext) { - new_context=true; - // check the target. if (DispatcherType.REQUEST.equals(dispatch) || DispatcherType.ASYNC.equals(dispatch)) { + if (_compactPath) + target=URIUtil.compactPath(target); + if (!checkContext(target,baseRequest,response)) + return; + if (target.length()>_contextPath.length()) { if (_contextPath.length()>1) @@ -780,6 +828,14 @@ public class ContextHandler extends HandlerWrapper implements Attributes, Server pathInfo=null; } } + + // Set the classloader + if (_classLoader!=null) + { + current_thread=Thread.currentThread(); + old_classloader=current_thread.getContextClassLoader(); + current_thread.setContextClassLoader(_classLoader); + } } try @@ -799,56 +855,8 @@ public class ContextHandler extends HandlerWrapper implements Attributes, Server baseRequest.setServletPath(null); baseRequest.setPathInfo(pathInfo); } - - ServletRequestEvent event=null; - if (new_context) - { - // Set the classloader - if (_classLoader!=null) - { - current_thread=Thread.currentThread(); - old_classloader=current_thread.getContextClassLoader(); - current_thread.setContextClassLoader(_classLoader); - } - - // Handle the REALLY SILLY request events! - baseRequest.setRequestListeners(_requestListeners); - if (_requestAttributeListeners!=null) - { - final int s=LazyList.size(_requestAttributeListeners); - for(int i=0;i0;) - baseRequest.removeEventListener(((EventListener)LazyList.get(_requestAttributeListeners,i))); - } - } - } + nextScope(target,baseRequest,request,response); } finally { @@ -867,7 +875,69 @@ public class ContextHandler extends HandlerWrapper implements Attributes, Server baseRequest.setPathInfo(old_path_info); } } - } + } + + /* ------------------------------------------------------------ */ + /** + * @see org.eclipse.jetty.server.handler.ScopedHandler#doHandle(java.lang.String, org.eclipse.jetty.server.Request, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) + */ + @Override + public void doHandle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException + { + final DispatcherType dispatch=baseRequest.getDispatcherType(); + final boolean new_context=baseRequest.takeNewContext(); + try + { + if (new_context) + { + // Handle the REALLY SILLY request events! + if (_requestAttributeListeners!=null) + { + final int s=LazyList.size(_requestAttributeListeners); + for(int i=0;i0;) + baseRequest.removeEventListener(((EventListener)LazyList.get(_requestAttributeListeners,i))); + } + } + } + } /* ------------------------------------------------------------ */ /* Handle a runnable in this context @@ -899,7 +969,7 @@ public class ContextHandler extends HandlerWrapper implements Attributes, Server /* ------------------------------------------------------------ */ /** Check the target. - * Called by {@link #handle(String, HttpServletRequest, HttpServletResponse)} when a + * Called by {@link #handle(String, Request, HttpServletRequest, HttpServletResponse)} when a * target within a context is determined. If the target is protected, 404 is returned. * The default implementation always returns false. * @see org.eclipse.jetty.webapp.WebAppContext#isProtectedTarget(String) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandlerCollection.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandlerCollection.java index cd1b5176a5d..a67a367a5a0 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandlerCollection.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandlerCollection.java @@ -166,21 +166,19 @@ public class ContextHandlerCollection extends HandlerCollection /* * @see org.eclipse.jetty.server.server.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int) */ - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { Handler[] handlers = getHandlers(); if (handlers==null || handlers.length==0) return; - - Request base_request = HttpConnection.getCurrentConnection().getRequest(); - AsyncRequest async = base_request.getAsyncRequest(); + AsyncRequest async = baseRequest.getAsyncRequest(); if (async.isAsync()) { ContextHandler context=async.getContextHandler(); if (context!=null) { - context.doHandle(target,base_request,request,response); + context.handle(target,baseRequest,request, response); return; } } @@ -211,8 +209,8 @@ public class ContextHandlerCollection extends HandlerCollection for (int j=0; jFor example if Scoped handlers A, B & C were chained together, then + * the calling order would be:
+ * A.handle(...)
+ *   A.doScope(...)
+ *     B.doScope(...)
+ *       C.doScope(...)
+ *         A.doHandle(...)
+ *           B.doHandle(...)
+ *              C.doHandle(...)   
+ * 
+ * 
+ * 

If non scoped handler X was in the chained A, B, X & C, then + * the calling order would be:

+ * A.handle(...)
+ *   A.doScope(...)
+ *     B.doScope(...)
+ *       C.doScope(...)
+ *         A.doHandle(...)
+ *           B.doHandle(...)
+ *             X.handle(...)
+ *               C.handle(...)
+ *                 C.doHandle(...)   
+ * 
+ * 
+ * 

A typical usage pattern is:

+ *     private static class MyHandler extends ScopedHandler
+ *     {
+ *         public void doScope(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
+ *         {
+ *             try
+ *             {
+ *                 setUpMyScope();
+ *                 super.doScope(target,request,response);
+ *             }
+ *             finally
+ *             {
+ *                 tearDownMyScope();
+ *             }
+ *         }
+ *         
+ *         public void doHandle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
+ *         {
+ *             try
+ *             {
+ *                 doMyHandling();
+ *                 super.doHandle(target,request,response);
+ *             }
+ *             finally
+ *             {
+ *                 cleanupMyHandling();
+ *             }
+ *         }
+ *     }
+ * 
+ */ +public abstract class ScopedHandler extends HandlerWrapper +{ + private static ThreadLocal __outerScope= new ThreadLocal(); + protected ScopedHandler _outerScope; + protected ScopedHandler _nextScope; + + /* ------------------------------------------------------------ */ + /** + * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStart() + */ + @Override + protected void doStart() throws Exception + { + try + { + _outerScope=__outerScope.get(); + if (_outerScope==null) + __outerScope.set(this); + + super.doStart(); + + _nextScope= (ScopedHandler)getChildHandlerByClass(ScopedHandler.class); + + } + finally + { + if (_outerScope==null) + __outerScope.set(null); + } + } + + + /* ------------------------------------------------------------ */ + /* + */ + public final void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + { + if (_outerScope==null) + doScope(target,baseRequest,request, response); + else + doHandle(target,baseRequest,request, response); + } + + /* ------------------------------------------------------------ */ + /* + * Scope the handler + */ + public abstract void doScope(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) + throws IOException, ServletException; + + /* ------------------------------------------------------------ */ + /* + * Scope the handler + */ + public final void nextScope(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) + throws IOException, ServletException + { + if (_nextScope!=null) + _nextScope.doScope(target,baseRequest,request, response); + else if (_outerScope!=null) + _outerScope.doHandle(target,baseRequest,request, response); + else + doHandle(target,baseRequest,request, response); + } + + /* ------------------------------------------------------------ */ + /* + * Do the handler work within the scope. + */ + public abstract void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) + throws IOException, ServletException; + + /* ------------------------------------------------------------ */ + /* + * Do the handler work within the scope. + */ + public final void nextHandle(String target, final Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + { + if (_nextScope!=null && _nextScope==_handler) + _nextScope.doHandle(target,baseRequest,request, response); + else if (_handler!=null) + _handler.handle(target,baseRequest, request, response); + } + +} diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/StatisticsHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/StatisticsHandler.java index 65b411cec94..c1a260387de 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/StatisticsHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/StatisticsHandler.java @@ -80,18 +80,17 @@ public class StatisticsHandler extends HandlerWrapper implements CompleteHandler /* ------------------------------------------------------------ */ - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - final Request base_request=(request instanceof Request)?((Request)request):HttpConnection.getCurrentConnection().getRequest(); - final Response base_response=(response instanceof Response)?((Response)response):HttpConnection.getCurrentConnection().getResponse(); + final Response base_response=baseRequest.getResponse(); - long timestamp0=base_request.getTimeStamp(); + long timestamp0=baseRequest.getTimeStamp(); long timestamp1=timestamp0; try { synchronized(this) { - AsyncRequest asyncContextState=base_request.getAsyncRequest(); + AsyncRequest asyncContextState=baseRequest.getAsyncRequest(); if(asyncContextState==null) { @@ -118,7 +117,7 @@ public class StatisticsHandler extends HandlerWrapper implements CompleteHandler _requestsActiveMax=_requestsActive; } - super.handle(target, request, response); + super.handle(target, baseRequest, request, response); } finally { @@ -138,10 +137,10 @@ public class StatisticsHandler extends HandlerWrapper implements CompleteHandler _requestsActiveDurationMax=duration; - if(base_request.isAsyncStarted()) + if(baseRequest.isAsyncStarted()) { - Object list = base_request.getAttribute(COMPLETE_HANDLER_ATTR); - base_request.setAttribute(COMPLETE_HANDLER_ATTR, LazyList.add(list, this)); + Object list = baseRequest.getAttribute(COMPLETE_HANDLER_ATTR); + baseRequest.setAttribute(COMPLETE_HANDLER_ATTR, LazyList.add(list, this)); } else { diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/session/SessionHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/session/SessionHandler.java index 0f1d58eae97..8ba48d25ae9 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/session/SessionHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/session/SessionHandler.java @@ -130,46 +130,45 @@ public class SessionHandler extends HandlerWrapper /* * @see org.eclipse.jetty.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int) */ - public void handle(String target, HttpServletRequest request, HttpServletResponse response) + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { setRequestedId(request); - Request base_request = (request instanceof Request) ? (Request)request:HttpConnection.getCurrentConnection().getRequest(); SessionManager old_session_manager=null; HttpSession old_session=null; try { - old_session_manager = base_request.getSessionManager(); - old_session = base_request.getSession(false); + old_session_manager = baseRequest.getSessionManager(); + old_session = baseRequest.getSession(false); if (old_session_manager != _sessionManager) { // new session context - base_request.setSessionManager(_sessionManager); - base_request.setSession(null); + baseRequest.setSessionManager(_sessionManager); + baseRequest.setSession(null); } // access any existing session HttpSession session=null; if (_sessionManager!=null) { - session=base_request.getSession(false); + session=baseRequest.getSession(false); if (session!=null) { if(session!=old_session) { HttpCookie cookie = _sessionManager.access(session,request.isSecure()); if (cookie!=null ) // Handle changed ID or max-age refresh - base_request.getResponse().addCookie(cookie); + baseRequest.getResponse().addCookie(cookie); } } else { - session=base_request.recoverNewSession(_sessionManager); + session=baseRequest.recoverNewSession(_sessionManager); if (session!=null) - base_request.setSession(session); + baseRequest.setSession(session); } } @@ -179,13 +178,13 @@ public class SessionHandler extends HandlerWrapper Log.debug("session="+session); } - getHandler().handle(target, request, response); + getHandler().handle(target, baseRequest, request, response); } catch (RetryRequest r) { - HttpSession session=base_request.getSession(false); + HttpSession session=baseRequest.getSession(false); if (session!=null && session.isNew()) - base_request.saveNewSession(_sessionManager,session); + baseRequest.saveNewSession(_sessionManager,session); throw r; } finally @@ -197,8 +196,8 @@ public class SessionHandler extends HandlerWrapper //leaving context, free up the session if (session!=null) _sessionManager.complete(session); - base_request.setSessionManager(old_session_manager); - base_request.setSession(old_session); + baseRequest.setSessionManager(old_session_manager); + baseRequest.setSession(old_session); } } } @@ -210,9 +209,9 @@ public class SessionHandler extends HandlerWrapper */ protected void setRequestedId(HttpServletRequest request) { - Request base_request = (request instanceof Request) ? (Request)request:HttpConnection.getCurrentConnection().getRequest(); + Request baseRequest = (request instanceof Request) ? (Request)request:HttpConnection.getCurrentConnection().getRequest(); String requested_session_id=request.getRequestedSessionId(); - if (!DispatcherType.REQUEST.equals(base_request.getDispatcherType()) || requested_session_id!=null) + if (!DispatcherType.REQUEST.equals(baseRequest.getDispatcherType()) || requested_session_id!=null) { return; } @@ -266,8 +265,8 @@ public class SessionHandler extends HandlerWrapper } } - base_request.setRequestedSessionId(requested_session_id); - base_request.setRequestedSessionIdFromCookie(requested_session_id!=null && requested_session_id_from_cookie); + baseRequest.setRequestedSessionId(requested_session_id); + baseRequest.setRequestedSessionIdFromCookie(requested_session_id!=null && requested_session_id_from_cookie); } /* ------------------------------------------------------------ */ diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncContextTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncContextTest.java index c285d425e9f..e6bc34db530 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncContextTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncContextTest.java @@ -185,11 +185,9 @@ public class AsyncContextTest extends TestCase - public void handle(String target, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException - { - final Request base_request = (request instanceof Request)?((Request)request):HttpConnection.getCurrentConnection().getRequest(); - - if (DispatcherType.REQUEST.equals(base_request.getDispatcherType())) + public void handle(String target, final Request baseRequest, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException + { + if (DispatcherType.REQUEST.equals(baseRequest.getDispatcherType())) { if (_read>0) { @@ -205,9 +203,9 @@ public class AsyncContextTest extends TestCase } if (_suspendFor>0) - base_request.setAsyncTimeout(_suspendFor); - base_request.addEventListener(__asyncListener); - final AsyncContext asyncContext = base_request.startAsync(); + baseRequest.setAsyncTimeout(_suspendFor); + baseRequest.addEventListener(__asyncListener); + final AsyncContext asyncContext = baseRequest.startAsync(); if (_completeAfter>0) { @@ -219,7 +217,7 @@ public class AsyncContextTest extends TestCase Thread.sleep(_completeAfter); response.getOutputStream().print("COMPLETED"); response.setStatus(200); - base_request.setHandled(true); + baseRequest.setHandled(true); asyncContext.complete(); } catch(Exception e) @@ -233,7 +231,7 @@ public class AsyncContextTest extends TestCase { response.getOutputStream().print("COMPLETED"); response.setStatus(200); - base_request.setHandled(true); + baseRequest.setHandled(true); asyncContext.complete(); } @@ -263,13 +261,13 @@ public class AsyncContextTest extends TestCase { response.setStatus(200); response.getOutputStream().print("TIMEOUT"); - base_request.setHandled(true); + baseRequest.setHandled(true); } else { response.setStatus(200); response.getOutputStream().print("RESUMED"); - base_request.setHandled(true); + baseRequest.setHandled(true); } } } diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncStressTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncStressTest.java index ec0b546eed5..3ed07d47a92 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncStressTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncStressTest.java @@ -226,10 +226,8 @@ public class AsyncStressTest extends TestCase _timer=new Timer(); } - public void handle(String target, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException + public void handle(String target, final Request baseRequest, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException { - final Request base_request = (request instanceof Request)?((Request)request):HttpConnection.getCurrentConnection().getRequest(); - int read_before=0; long sleep_for=-1; long suspend_for=-1; @@ -247,7 +245,7 @@ public class AsyncStressTest extends TestCase if (request.getParameter("complete")!=null) complete_after=Integer.parseInt(request.getParameter("complete")); - if (DispatcherType.REQUEST.equals(base_request.getDispatcherType())) + if (DispatcherType.REQUEST.equals(baseRequest.getDispatcherType())) { if (read_before>0) { @@ -265,9 +263,9 @@ public class AsyncStressTest extends TestCase if (suspend_for>=0) { if (suspend_for>0) - base_request.setAsyncTimeout(suspend_for); - base_request.addEventListener(__asyncListener); - base_request.startAsync(); + baseRequest.setAsyncTimeout(suspend_for); + baseRequest.addEventListener(__asyncListener); + baseRequest.startAsync(); } else if (sleep_for>=0) { @@ -281,18 +279,18 @@ public class AsyncStressTest extends TestCase } response.setStatus(200); response.getOutputStream().print("SLEPT"); - base_request.setHandled(true); + baseRequest.setHandled(true); return; } else { response.setStatus(200); response.getOutputStream().print("NORMAL"); - base_request.setHandled(true); + baseRequest.setHandled(true); return; } - final AsyncContext asyncContext = base_request.getAsyncContext(); + final AsyncContext asyncContext = baseRequest.getAsyncContext(); if (complete_after>0) @@ -305,7 +303,7 @@ public class AsyncStressTest extends TestCase { response.setStatus(200); response.getOutputStream().print("COMPLETED"); - base_request.setHandled(true); + baseRequest.setHandled(true); asyncContext.complete(); } catch(Exception e) @@ -323,7 +321,7 @@ public class AsyncStressTest extends TestCase { response.setStatus(200); response.getOutputStream().print("COMPLETED"); - base_request.setHandled(true); + baseRequest.setHandled(true); asyncContext.complete(); } @@ -350,13 +348,13 @@ public class AsyncStressTest extends TestCase { response.setStatus(200); response.getOutputStream().print("TIMEOUT"); - base_request.setHandled(true); + baseRequest.setHandled(true); } else { response.setStatus(200); response.getOutputStream().print("RESUMED"); - base_request.setHandled(true); + baseRequest.setHandled(true); } } } diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/CheckReverseProxyHeadersTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/CheckReverseProxyHeadersTest.java index 1590c7494d4..2ab4d3c99b6 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/CheckReverseProxyHeadersTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/CheckReverseProxyHeadersTest.java @@ -158,7 +158,7 @@ public class CheckReverseProxyHeadersTest extends TestCase return _error; } - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { try { diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/DumpHandler.java b/jetty-server/src/test/java/org/eclipse/jetty/server/DumpHandler.java index 7b173aeda99..d55c44850ae 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/DumpHandler.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/DumpHandler.java @@ -59,10 +59,8 @@ public class DumpHandler extends AbstractHandler /* * @see org.eclipse.jetty.server.server.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int) */ - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException - { - Request base_request = (request instanceof Request) ? (Request)request:HttpConnection.getCurrentConnection().getRequest(); - + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + { if (!isStarted()) return; @@ -73,7 +71,7 @@ public class DumpHandler extends AbstractHandler continuation.suspend(); } - base_request.setHandled(true); + baseRequest.setHandled(true); response.setHeader(HttpHeaders.CONTENT_TYPE,MimeTypes.TEXT_HTML); OutputStream out = response.getOutputStream(); diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java b/jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java index f2298d0e1e6..73f89b6a966 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java @@ -817,10 +817,9 @@ public class HttpServerTestBase extends TestCase { // ~ Methods // ------------------------------------------------------------ - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - Request base_request=(request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest(); - base_request.setHandled(true); + baseRequest.setHandled(true); if (request.getContentType()!=null) response.setContentType(request.getContentType()); @@ -857,10 +856,9 @@ public class HttpServerTestBase extends TestCase private static class HelloWorldHandler extends AbstractHandler { // ------------------------------------------------------------ - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - Request base_request=(request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest(); - base_request.setHandled(true); + baseRequest.setHandled(true); response.setStatus(200); response.getOutputStream().print("Hello world\r\n"); } @@ -870,10 +868,9 @@ public class HttpServerTestBase extends TestCase private static class DataHandler extends AbstractHandler { // ------------------------------------------------------------ - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - Request base_request=(request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest(); - base_request.setHandled(true); + baseRequest.setHandled(true); response.setStatus(200); InputStream in = request.getInputStream(); diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/RequestTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/RequestTest.java index 200eebe8827..93655b7a729 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/RequestTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/RequestTest.java @@ -409,7 +409,7 @@ public class RequestTest extends TestCase RequestTester _checker; String _content; - public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { ((Request)request).setHandled(true); diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java index fdf721e15f6..66b1aae0c83 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/ResponseTest.java @@ -365,7 +365,7 @@ public class ResponseTest extends TestCase server.addConnector(socketConnector); server.setHandler(new AbstractHandler() { - public void handle(String string, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String string, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setStatus(200); response.setContentType("text/plain"); diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/UnreadInputTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/UnreadInputTest.java index 70a4464a1c4..8df97a0bc68 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/UnreadInputTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/UnreadInputTest.java @@ -39,8 +39,8 @@ public class UnreadInputTest extends TestCase public class NoopHandler extends AbstractHandler { - public void handle(String target, HttpServletRequest request, - HttpServletResponse response) throws IOException, + public void handle(String target, Request baseRequest, + HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { //don't read the input, just send something back diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerCollectionTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerCollectionTest.java index b42f4418e76..f6fd314b0aa 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerCollectionTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerCollectionTest.java @@ -160,10 +160,9 @@ public class ContextHandlerCollectionTest extends TestCase return handled; } - public void handle(String s, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String s, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - Request base_request = (request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest(); - base_request.setHandled(true); + baseRequest.setHandled(true); this.handled = true; } diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java index d69f4a65c63..a6396da29bb 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ContextHandlerTest.java @@ -210,10 +210,9 @@ public class ContextHandlerTest extends TestCase return handled; } - public void handle(String s, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + public void handle(String s, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - Request base_request = (request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest(); - base_request.setHandled(true); + baseRequest.setHandled(true); this.handled = true; } diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ScopedHandlerTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ScopedHandlerTest.java new file mode 100644 index 00000000000..c8cb181d7e2 --- /dev/null +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ScopedHandlerTest.java @@ -0,0 +1,162 @@ +package org.eclipse.jetty.server.handler; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.eclipse.jetty.server.Request; + +import junit.framework.TestCase; + +public class ScopedHandlerTest extends TestCase +{ + StringBuilder _history=new StringBuilder(); + + public void testSingle() + throws Exception + { + _history.setLength(0); + TestHandler handler0 = new TestHandler("0"); + handler0.start(); + handler0.handle("target",null,null,null); + handler0.stop(); + String history=_history.toString(); + System.err.println(history); + assertEquals(">S0>W0S0>S1>W0>W1S0>S1>S2>W0>W1>W2S0>S1>W0>HA>W1>HBS0>S1>S2>W0>HA>W1>HB>W2>HCS"+_name); + super.nextScope(target,baseRequest,request, response); + } + finally + { + _history.append("W"+_name); + super.nextHandle(target,baseRequest,request,response); + } + finally + { + _history.append("H"+_name); + super.handle(target,baseRequest,request, response); + } + finally + { + _history.append("0) - chain=getFilterChain(base_request, target, servlet_holder); } } else { // look for a servlet by name! servlet_holder=(ServletHolder)_servletNameMap.get(target); + } + + Log.debug("servlet holder=",servlet_holder); + + try + { + // Do the filter/handling thang + if (servlet_holder!=null) + { + old_scope=baseRequest.getUserIdentityScope(); + baseRequest.setUserIdentityScope(servlet_holder); + + nextScope(target,baseRequest,request, response); + } + } + finally + { + if (old_scope!=null) + baseRequest.setUserIdentityScope(old_scope); + + if (!(DispatcherType.INCLUDE.equals(type))) + { + baseRequest.setServletPath(old_servlet_path); + baseRequest.setPathInfo(old_path_info); + } + } + + } + + /* ------------------------------------------------------------ */ + /* + * @see org.eclipse.jetty.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int) + */ + public void doHandle(String target, Request baseRequest,HttpServletRequest request, HttpServletResponse response) + throws IOException, ServletException + { + DispatcherType type = baseRequest.getDispatcherType(); + + ServletHolder servlet_holder=(ServletHolder) baseRequest.getUserIdentityScope(); + FilterChain chain=null; + + // find the servlet + if (target.startsWith("/")) + { + if (servlet_holder!=null && _filterMappings!=null && _filterMappings.length>0) + chain=getFilterChain(baseRequest, target, servlet_holder); + + } + else + { if (servlet_holder!=null) { if (_filterMappings!=null && _filterMappings.length>0) { - chain=getFilterChain(base_request, null,servlet_holder); + chain=getFilterChain(baseRequest, null,servlet_holder); } } } - if (Log.isDebugEnabled()) - { - Log.debug("chain="+chain); - Log.debug("servlet holder="+servlet_holder); - } - + Log.debug("chain=",chain); + try { // Do the filter/handling thang @@ -380,28 +415,12 @@ public class ServletHandler extends AbstractHandler } else { - old_scope=base_request.getUserIdentityScope(); - base_request.setUserIdentityScope(servlet_holder); - - // Handle context listeners - request_listeners = base_request.takeRequestListeners(); - if (request_listeners!=null) - { - request_event = new ServletRequestEvent(getServletContext(),request); - final int s=LazyList.size(request_listeners); - for(int i=0;i0;) - { - final ServletRequestListener listener = (ServletRequestListener)LazyList.get(request_listeners,i); - listener.requestDestroyed(request_event); - } - } - - if (old_scope!=null) - base_request.setUserIdentityScope(old_scope); - - if (!(DispatcherType.INCLUDE.equals(type))) - { - base_request.setServletPath(old_servlet_path); - base_request.setPathInfo(old_path_info); - } - } } /* ------------------------------------------------------------ */ @@ -1173,18 +1171,18 @@ public class ServletHandler extends AbstractHandler filter.doFilter(request, response, _next); else { - final Request base_request=(request instanceof Request)?((Request)request):HttpConnection.getCurrentConnection().getRequest(); - final boolean suspendable=base_request.isAsyncSupported(); + final Request baseRequest=(request instanceof Request)?((Request)request):HttpConnection.getCurrentConnection().getRequest(); + final boolean suspendable=baseRequest.isAsyncSupported(); if (suspendable) { try { - base_request.setAsyncSupported(false); + baseRequest.setAsyncSupported(false); filter.doFilter(request, response, _next); } finally { - base_request.setAsyncSupported(true); + baseRequest.setAsyncSupported(true); } } else @@ -1198,8 +1196,8 @@ public class ServletHandler extends AbstractHandler { if (Log.isDebugEnabled()) Log.debug("call servlet " + _servletHolder); - final Request base_request=(request instanceof Request)?((Request)request):HttpConnection.getCurrentConnection().getRequest(); - _servletHolder.handle(base_request,request, response); + final Request baseRequest=(request instanceof Request)?((Request)request):HttpConnection.getCurrentConnection().getRequest(); + _servletHolder.handle(baseRequest,request, response); } else // Not found notFound((HttpServletRequest)request, (HttpServletResponse)response); @@ -1404,5 +1402,6 @@ public class ServletHandler extends AbstractHandler } } + } diff --git a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/QoSFilter.java b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/QoSFilter.java index 861b81247c8..70a3df82d12 100644 --- a/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/QoSFilter.java +++ b/jetty-servlets/src/main/java/org/eclipse/jetty/servlets/QoSFilter.java @@ -211,12 +211,12 @@ public class QoSFilter implements Filter */ protected int getPriority(ServletRequest request) { - HttpServletRequest base_request = (HttpServletRequest)request; - if (base_request.getUserPrincipal() != null ) + HttpServletRequest baseRequest = (HttpServletRequest)request; + if (baseRequest.getUserPrincipal() != null ) return 2; else { - HttpSession session = base_request.getSession(false); + HttpSession session = baseRequest.getSession(false); if (session!=null && !session.isNew()) return 1; else diff --git a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java index cade160ea7a..64fbe41b315 100644 --- a/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java +++ b/jetty-webapp/src/main/java/org/eclipse/jetty/webapp/WebAppContext.java @@ -112,9 +112,8 @@ public class WebAppContext extends ServletContextHandler private String _extraClasspath; private Throwable _unavailableException; - private transient Map _resourceAliases; - private transient boolean _ownClassLoader=false; - private transient boolean _unavailable; + private Map _resourceAliases; + private boolean _ownClassLoader=false; public static ContextHandler getCurrentWebAppContext() { @@ -286,21 +285,6 @@ public class WebAppContext extends ServletContextHandler } - /* ------------------------------------------------------------ */ - /** - * @see org.eclipse.jetty.server.server.handler.ContextHandler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) - */ - public void handle(String target, HttpServletRequest request, HttpServletResponse response) - throws IOException, ServletException - { - if (_unavailable) - { - response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); - } - else - super.handle(target, request, response); - } - /* ------------------------------------------------------------ */ /* * @see org.eclipse.thread.AbstractLifeCycle#doStart() @@ -357,7 +341,7 @@ public class WebAppContext extends ServletContextHandler //start up of the webapp context failed, make sure it is not started Log.warn("Failed startup of context "+this, e); _unavailableException=e; - _unavailable = true; + setAvailable(false); } } @@ -412,8 +396,8 @@ public class WebAppContext extends ServletContextHandler { if (_ownClassLoader) setClassLoader(null); - - _unavailable = false; + + setAvailable(true); _unavailableException=null; } }