diff options
author | Jan Bartel | 2009-09-21 02:45:01 +0000 |
---|---|---|
committer | Jan Bartel | 2009-09-21 02:45:01 +0000 |
commit | 35f6e679cfbdcf69887b0ef517f121fc7e3640e8 (patch) | |
tree | 9106ffd93122082ef76c871b66f206725c2355ed | |
parent | d93c13da58520d67ecc166f0b962d5cc558a4dde (diff) | |
download | org.eclipse.jetty.project-35f6e679cfbdcf69887b0ef517f121fc7e3640e8.tar.gz org.eclipse.jetty.project-35f6e679cfbdcf69887b0ef517f121fc7e3640e8.tar.xz org.eclipse.jetty.project-35f6e679cfbdcf69887b0ef517f121fc7e3640e8.zip |
289958 StatisticsServlet incorrectly adds StatisticsHandler
git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/branches/jetty-7.0.0.x@937 7e9141cc-0065-0410-87d8-b60c137991c4
3 files changed, 115 insertions, 8 deletions
diff --git a/VERSION.txt b/VERSION.txt index beb8bee505..b81c9ca7c9 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1,3 +1,6 @@ +jetty-7.0.0.RC7-SNAPSHOT + + 289958 StatisticsServlet incorrectly adds StatisticsHandler + jetty-7.0.0.RC6 September 18th 2009 + JETTY-719 Document state machine of jetty http client + JETTY-780 CNFE during startup of webapp with spring-context >= 2.5.1 diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/StatisticsServlet.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/StatisticsServlet.java index 94d649107c..5fe3f98455 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/StatisticsServlet.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/StatisticsServlet.java @@ -39,8 +39,6 @@ public class StatisticsServlet extends HttpServlet public void init() throws ServletException { - _memoryBean = ManagementFactory.getMemoryMXBean(); - ServletContext context = getServletContext(); ContextHandler.Context scontext = (ContextHandler.Context) context; Server _server = scontext.getContextHandler().getServer(); @@ -53,12 +51,11 @@ public class StatisticsServlet extends HttpServlet } else { - Log.info("Installing Statistics Handler"); - _statsHandler = new StatisticsHandler(); - _server.setHandler(_statsHandler); + Log.warn("Statistics Handler not installed!"); + return; } - - + + _memoryBean = ManagementFactory.getMemoryMXBean(); _connectors = _server.getConnectors(); if (getInitParameter("restrictToLocalhost") != null) @@ -75,7 +72,12 @@ public class StatisticsServlet extends HttpServlet protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - + if (_statsHandler == null) + { + Log.warn("Statistics Handler not installed!"); + resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); + return; + } if (_restrictToLocalhost) { if (!"127.0.0.1".equals(req.getRemoteAddr())) diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/StatisticsServletTest.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/StatisticsServletTest.java new file mode 100644 index 0000000000..0c4fae7fa7 --- /dev/null +++ b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/StatisticsServletTest.java @@ -0,0 +1,102 @@ +// ======================================================================== +// Copyright (c) 2009 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +package org.eclipse.jetty.servlet; + +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.LocalConnector; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.StatisticsHandler; + +import junit.framework.AssertionFailedError; +import junit.framework.TestCase; + +public class StatisticsServletTest extends TestCase +{ + Server server; + LocalConnector connector; + ServletContextHandler context; + + protected void setUp() throws Exception + { + super.setUp(); + + server = new Server(); + server.setSendServerVersion(false); + context = new ServletContextHandler(); + context.setContextPath("/"); + ServletHolder holder = new ServletHolder(); + holder.setServlet(new org.eclipse.jetty.servlet.StatisticsServlet()); + holder.setInitParameter("restrictToLocalhost", "false"); + context.addServlet(holder, "/stats"); + + server.setHandler(context); + connector = new LocalConnector(); + server.addConnector(connector); + } + + protected void tearDown() throws Exception + { + super.tearDown(); + + if (server != null) + { + server.stop(); + } + } + + + public void testNoHandler () throws Exception + { + server.start(); + + StringBuffer req1 = new StringBuffer(); + req1.append("GET /stats HTTP/1.1\n"); + req1.append("Host: localhost\n"); + req1.append("\n"); + + String response = connector.getResponses(req1.toString()); + assertResponseContains("503", response); + } + + public void testWithHandler () throws Exception + { + StatisticsHandler statsHandler = new StatisticsHandler(); + statsHandler.setHandler(context); + server.setHandler(statsHandler); + server.start(); + + StringBuffer req1 = new StringBuffer(); + req1.append("GET /stats HTTP/1.1\n"); + req1.append("Host: localhost\n"); + req1.append("\n"); + + String response = connector.getResponses(req1.toString()); + assertResponseContains("Statistics gathering started ", response); + } + + + private void assertResponseContains(String expected, String response) + { + int idx = response.indexOf(expected); + if (idx == (-1)) + { + // Not found + StringBuffer err = new StringBuffer(); + err.append("Response does not contain expected string \"").append(expected).append("\""); + err.append("\n").append(response); + + System.err.println(err); + throw new AssertionFailedError(err.toString()); + } + } +} |