289958 - StatisticsServlet incorrectly adds StatisticsHandler

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@938 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Jan Bartel 2009-09-21 02:50:24 +00:00
parent ac6c1b98e1
commit 33f42f70f0
3 changed files with 116 additions and 9 deletions

View File

@ -6,7 +6,10 @@ jetty-7.0.1-SNAPSHOT
+ 289265 Test harness for async input
+ 289027 deobfuscate HttpClient SSL passwords
jetty-7.0.0.RC6-SNAPSHOT
jetty-7.0.0
+ 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
+ JETTY-936 274251 Improved servlet matching and optimized'

View File

@ -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()))

View File

@ -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());
}
}
}