Merge remote-tracking branch 'origin/master' into servlet-3.1-api

This commit is contained in:
Jan Bartel 2013-06-17 20:39:03 +10:00
commit d87df5d89e
2 changed files with 109 additions and 5 deletions

View File

@ -177,19 +177,28 @@ public class ServletContextHandler extends ContextHandler
if (getSessionHandler()!=null)
{
handler.setHandler(_sessionHandler);
if (handler==this)
super.setHandler(_sessionHandler);
else
handler.setHandler(_sessionHandler);
handler=_sessionHandler;
}
if (getSecurityHandler()!=null)
{
handler.setHandler(_securityHandler);
if (handler==this)
super.setHandler(_securityHandler);
else
handler.setHandler(_securityHandler);
handler=_securityHandler;
}
if (getServletHandler()!=null)
{
handler.setHandler(_servletHandler);
if (handler==this)
super.setHandler(_servletHandler);
else
handler.setHandler(_servletHandler);
handler=_servletHandler;
}
}
@ -489,6 +498,50 @@ public class ServletContextHandler extends ContextHandler
relinkHandlers();
_servletHandler.setHandler(next);
}
/* ------------------------------------------------------------ */
@Override
public void setHandler(Handler handler)
{
if (handler instanceof ServletHandler)
setServletHandler((ServletHandler) handler);
else if (handler instanceof SessionHandler)
setSessionHandler((SessionHandler) handler);
else if (handler instanceof SecurityHandler)
setSecurityHandler((SecurityHandler)handler);
else if (handler instanceof HandlerWrapper)
{
super.setHandler(handler);
relinkHandlers();
}
else
throw new IllegalArgumentException();
}
/* ------------------------------------------------------------ */
/**
* Insert a HandlerWrapper before the first Session,Security or ServletHandler
* but after any other HandlerWrappers.
*/
public void insertHandler(HandlerWrapper handler)
{
HandlerWrapper h=this;
// Skip any injected handlers
while (h.getHandler() instanceof HandlerWrapper)
{
HandlerWrapper wrapper = (HandlerWrapper)h.getHandler();
if (wrapper instanceof SessionHandler ||
wrapper instanceof SecurityHandler ||
wrapper instanceof ServletHandler)
break;
h=wrapper;
}
h.setHandler(handler);
relinkHandlers();
}
/* ------------------------------------------------------------ */
/**

View File

@ -23,12 +23,17 @@ import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.Servlet;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
@ -41,8 +46,11 @@ import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandlerContainer;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.util.resource.Resource;
import org.hamcrest.Matchers;
import org.hamcrest.core.IsEqual;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@ -229,6 +237,49 @@ public class ServletContextHandlerTest
response = _connector.getResponses(request.toString());
assertResponseContains("Hello World", response);
}
@Test
public void testReplaceHandler () throws Exception
{
ServletContextHandler servletContextHandler = new ServletContextHandler();
ServletHolder sh = new ServletHolder(new TestServlet());
servletContextHandler.addServlet(sh, "/foo");
final AtomicBoolean contextInit = new AtomicBoolean(false);
final AtomicBoolean contextDestroy = new AtomicBoolean(false);
servletContextHandler.addEventListener(new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent sce)
{
if (sce.getServletContext() != null)
contextInit.set(true);
}
@Override
public void contextDestroyed(ServletContextEvent sce)
{
if (sce.getServletContext() != null)
contextDestroy.set(true);
}
});
ServletHandler shandler = servletContextHandler.getServletHandler();
ResourceHandler rh = new ResourceHandler();
servletContextHandler.setHandler(rh);
assertEquals(shandler, servletContextHandler.getServletHandler());
assertEquals(rh, servletContextHandler.getHandler());
assertEquals(rh.getHandler(), shandler);
_server.setHandler(servletContextHandler);
_server.start();
assertTrue(contextInit.get());
_server.stop();
assertTrue(contextDestroy.get());
}
private int assertResponseContains(String expected, String response)
{