From c1f3545d7c5a53ea44f2a470257fe4a23ceeec04 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Tue, 5 Jun 2012 18:11:20 +0200 Subject: [PATCH] jetty-9 passing jetty-servlet tests!!!! --- .../org/eclipse/jetty/server/HttpChannel.java | 1 + .../org/eclipse/jetty/server/HttpOutput.java | 7 + .../eclipse/jetty/servlet/DefaultServlet.java | 90 +++---- .../jetty/servlet/ErrorPageErrorHandler.java | 7 +- .../org/eclipse/jetty/servlet/Invoker.java | 4 +- .../eclipse/jetty/servlet/ServletHandler.java | 14 +- .../jetty/servlet/StatisticsServlet.java | 226 ------------------ .../AsyncContextDispatchWithQueryStrings.java | 6 +- .../eclipse/jetty/servlet/DispatcherTest.java | 4 +- .../jetty/servlet/StatisticsServletTest.java | 99 -------- .../org/eclipse/jetty/util/BufferUtil.java | 14 ++ 11 files changed, 81 insertions(+), 391 deletions(-) delete mode 100644 jetty-servlet/src/main/java/org/eclipse/jetty/servlet/StatisticsServlet.java delete mode 100644 jetty-servlet/src/test/java/org/eclipse/jetty/servlet/StatisticsServletTest.java diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java index dc7edea649b..24e0cba9ce3 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java @@ -732,6 +732,7 @@ public abstract class HttpChannel /* ------------------------------------------------------------ */ + @Override public void sendContent(Object content) throws IOException { Resource resource=null; diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpOutput.java b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpOutput.java index d774974360f..11c28f5489f 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpOutput.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpOutput.java @@ -19,6 +19,7 @@ import java.nio.ByteBuffer; import javax.servlet.ServletOutputStream; +import org.eclipse.jetty.http.HttpContent; import org.eclipse.jetty.io.EofException; import org.eclipse.jetty.util.ByteArrayOutputStream2; @@ -148,4 +149,10 @@ public class HttpOutput extends ServletOutputStream write(s.getBytes()); } + /* ------------------------------------------------------------ */ + public void sendContent(Object content) throws IOException + { + throw new IllegalStateException("Not implemented"); + } + } diff --git a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java index b95a291ee2d..61926a5a516 100644 --- a/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java +++ b/jetty-servlet/src/main/java/org/eclipse/jetty/servlet/DefaultServlet.java @@ -19,6 +19,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; +import java.nio.ByteBuffer; import java.util.Enumeration; import java.util.List; import java.util.Map; @@ -33,23 +34,23 @@ import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.http.HttpContent; import org.eclipse.jetty.http.HttpFields; -import org.eclipse.jetty.http.HttpHeaderValues; -import org.eclipse.jetty.http.HttpHeaders; -import org.eclipse.jetty.http.HttpMethods; +import org.eclipse.jetty.http.HttpHeaderValue; +import org.eclipse.jetty.http.HttpHeader; +import org.eclipse.jetty.http.HttpMethod; import org.eclipse.jetty.http.MimeTypes; -import org.eclipse.jetty.io.Buffer; -import org.eclipse.jetty.io.ByteArrayBuffer; import org.eclipse.jetty.io.WriterOutputStream; -import org.eclipse.jetty.server.AbstractHttpConnection; +import org.eclipse.jetty.server.HttpConnection; +import org.eclipse.jetty.server.HttpChannel; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Dispatcher; +import org.eclipse.jetty.server.HttpConnector; import org.eclipse.jetty.server.HttpOutput; import org.eclipse.jetty.server.InclusiveByteRange; import org.eclipse.jetty.server.ResourceCache; import org.eclipse.jetty.server.Response; import org.eclipse.jetty.server.handler.ContextHandler; -import org.eclipse.jetty.server.nio.NIOConnector; import org.eclipse.jetty.server.ssl.SslConnector; +import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.MultiPartOutputStream; import org.eclipse.jetty.util.URIUtil; @@ -154,7 +155,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory private String[] _welcomes; private Resource _stylesheet; private boolean _useFileMappedBuffer=false; - private ByteArrayBuffer _cacheControl; + private String _cacheControl; private String _relativeResourceBase; private ServletHandler _servletHandler; private ServletHolder _defaultHolder; @@ -237,9 +238,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory LOG.debug(e); } - String t=getInitParameter("cacheControl"); - if (t!=null) - _cacheControl=new ByteArrayBuffer(t); + _cacheControl=getInitParameter("cacheControl"); String resourceCache = getInitParameter("resourceCache"); int max_cache_size=getInitInt("maxCacheSize", -2); @@ -390,11 +389,11 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory String servletPath=null; String pathInfo=null; Enumeration reqRanges = null; - Boolean included =request.getAttribute(Dispatcher.INCLUDE_REQUEST_URI)!=null; + Boolean included =request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI)!=null; if (included!=null && included.booleanValue()) { - servletPath=(String)request.getAttribute(Dispatcher.INCLUDE_SERVLET_PATH); - pathInfo=(String)request.getAttribute(Dispatcher.INCLUDE_PATH_INFO); + servletPath=(String)request.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH); + pathInfo=(String)request.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO); if (servletPath==null) { servletPath=request.getServletPath(); @@ -408,7 +407,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory pathInfo = request.getPathInfo(); // Is this a Range request? - reqRanges = request.getHeaders(HttpHeaders.RANGE); + reqRanges = request.getHeaders(HttpHeader.RANGE.asString()); if (!hasDefinedRange(reqRanges)) reqRanges = null; } @@ -421,7 +420,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory boolean gzip=false; if (!included.booleanValue() && _gzip && reqRanges==null && !endsWithSlash ) { - String accept=request.getHeader(HttpHeaders.ACCEPT_ENCODING); + String accept=request.getHeader(HttpHeader.ACCEPT_ENCODING.asString()); if (accept!=null && accept.indexOf("gzip")>=0) gzip=true; } @@ -496,7 +495,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory { if (gzip) { - response.setHeader(HttpHeaders.CONTENT_ENCODING,"gzip"); + response.setHeader(HttpHeader.CONTENT_ENCODING.asString(),"gzip"); String mt=_servletContext.getMimeType(pathInContext); if (mt!=null) response.setContentType(mt); @@ -661,17 +660,17 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory { try { - if (!HttpMethods.HEAD.is(request.getMethod())) + if (!HttpMethod.HEAD.is(request.getMethod())) { - String ifms=request.getHeader(HttpHeaders.IF_MODIFIED_SINCE); + String ifms=request.getHeader(HttpHeader.IF_MODIFIED_SINCE.asString()); if (ifms!=null) { if (content!=null) { - Buffer mdlm=content.getLastModified(); + String mdlm=content.getLastModified(); if (mdlm!=null) { - if (ifms.equals(mdlm.toString())) + if (ifms.equals(mdlm)) { response.reset(); response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); @@ -681,7 +680,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory } } - long ifmsl=request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE); + long ifmsl=request.getDateHeader(HttpHeader.IF_MODIFIED_SINCE.asString()); if (ifmsl!=-1) { if (resource.lastModified()/1000 <= ifmsl/1000) @@ -695,7 +694,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory } // Parse the if[un]modified dates and compare to resource - long date=request.getDateHeader(HttpHeaders.IF_UNMODIFIED_SINCE); + long date=request.getDateHeader(HttpHeader.IF_UNMODIFIED_SINCE.asString()); if (date!=-1) { @@ -772,8 +771,9 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory } else { - Connector connector = AbstractHttpConnection.getCurrentConnection().getConnector(); - direct=connector instanceof NIOConnector && ((NIOConnector)connector).getUseDirectBuffers() && !(connector instanceof SslConnector); + HttpConnector connector = HttpChannel.getCurrentHttpChannel().getHttpConnector(); + // TODO either make this more targeted and/or configurable or just get rid of the choice + direct=!(connector instanceof SslConnector); content_length=content.getContentLength(); } @@ -788,7 +788,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory // has a filter already written to the response? written = out instanceof HttpOutput ? ((HttpOutput)out).isWritten() - : AbstractHttpConnection.getCurrentConnection().getGenerator().isWritten(); + : true; } catch(IllegalStateException e) { @@ -811,15 +811,15 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory if (response instanceof Response) { writeOptionHeaders(((Response)response).getHttpFields()); - ((AbstractHttpConnection.Output)out).sendContent(content); + ((HttpOutput)out).sendContent(content); } else { - Buffer buffer = direct?content.getDirectBuffer():content.getIndirectBuffer(); + ByteBuffer buffer = direct?content.getDirectBuffer():content.getIndirectBuffer(); if (buffer!=null) { writeHeaders(response,content,content_length); - ((AbstractHttpConnection.Output)out).sendContent(buffer); + ((HttpOutput)out).sendContent(buffer); } else { @@ -834,9 +834,9 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory writeHeaders(response,content,written?-1:content_length); // Write content normally - Buffer buffer = (content==null)?null:content.getIndirectBuffer(); + ByteBuffer buffer = (content==null)?null:content.getIndirectBuffer(); if (buffer!=null) - buffer.writeTo(out); + BufferUtil.writeTo(buffer,out); else resource.writeTo(out,0,content_length); } @@ -852,7 +852,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory { writeHeaders(response, content, content_length); response.setStatus(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE); - response.setHeader(HttpHeaders.CONTENT_RANGE, + response.setHeader(HttpHeader.CONTENT_RANGE.asString(), InclusiveByteRange.to416HeaderRangeString(content_length)); resource.writeTo(out,0,content_length); return; @@ -867,7 +867,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory long singleLength = singleSatisfiableRange.getSize(content_length); writeHeaders(response,content,singleLength ); response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); - response.setHeader(HttpHeaders.CONTENT_RANGE, + response.setHeader(HttpHeader.CONTENT_RANGE.asString(), singleSatisfiableRange.toHeaderRangeString(content_length)); resource.writeTo(out,singleSatisfiableRange.getFirst(content_length),singleLength); return; @@ -886,7 +886,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory // send an old style multipart/x-byteranges Content-Type. This // keeps Netscape and acrobat happy. This is what Apache does. String ctp; - if (request.getHeader(HttpHeaders.REQUEST_RANGE)!=null) + if (request.getHeader(HttpHeader.REQUEST_RANGE.asString())!=null) ctp = "multipart/x-byteranges; boundary="; else ctp = "multipart/byteranges; boundary="; @@ -905,8 +905,8 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory length+= ((i>0)?2:0)+ 2+multi.getBoundary().length()+2+ - HttpHeaders.CONTENT_TYPE.length()+2+mimetype.length()+2+ - HttpHeaders.CONTENT_RANGE.length()+2+header[i].length()+2+ + HttpHeader.CONTENT_TYPE.asString().length()+2+mimetype.length()+2+ + HttpHeader.CONTENT_RANGE.asString().length()+2+header[i].length()+2+ 2+ (ibr.getLast(content_length)-ibr.getFirst(content_length))+1; } @@ -916,7 +916,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory for (int i=0;i=0) - response.setDateHeader(HttpHeaders.LAST_MODIFIED,lml); + response.setDateHeader(HttpHeader.LAST_MODIFIED.asString(),lml); if (count != -1) { if (count\n"); - - sb.append(" \n"); - sb.append(" ").append(_statsHandler.getStatsOnMs()).append("\n"); - - sb.append(" ").append(_statsHandler.getRequests()).append("\n"); - sb.append(" ").append(_statsHandler.getRequestsActive()).append("\n"); - sb.append(" ").append(_statsHandler.getRequestsActiveMax()).append("\n"); - sb.append(" ").append(_statsHandler.getRequestTimeTotal()).append("\n"); - sb.append(" ").append(_statsHandler.getRequestTimeMean()).append("\n"); - sb.append(" ").append(_statsHandler.getRequestTimeMax()).append("\n"); - sb.append(" ").append(_statsHandler.getRequestTimeStdDev()).append("\n"); - - sb.append(" ").append(_statsHandler.getDispatched()).append("\n"); - sb.append(" ").append(_statsHandler.getDispatchedActive()).append("\n"); - sb.append(" ").append(_statsHandler.getDispatchedActiveMax()).append("\n"); - sb.append(" ").append(_statsHandler.getDispatchedTimeTotal()).append("\n"); - sb.append(" \n"); - sb.append(" ").append(_statsHandler.getDispatchedTimeMax()).append("\n"); - sb.append(" \n"); - - sb.append(" ").append(_statsHandler.getSuspends()).append("\n"); - sb.append(" ").append(_statsHandler.getExpires()).append("\n"); - sb.append(" ").append(_statsHandler.getResumes()).append("\n"); - sb.append(" \n"); - - sb.append(" \n"); - sb.append(" ").append(_statsHandler.getResponses1xx()).append("\n"); - sb.append(" ").append(_statsHandler.getResponses2xx()).append("\n"); - sb.append(" ").append(_statsHandler.getResponses3xx()).append("\n"); - sb.append(" ").append(_statsHandler.getResponses4xx()).append("\n"); - sb.append(" ").append(_statsHandler.getResponses5xx()).append("\n"); - sb.append(" ").append(_statsHandler.getResponsesBytesTotal()).append("\n"); - sb.append(" \n"); - - sb.append(" \n"); - for (Connector connector : _connectors) - { - sb.append(" \n"); - sb.append(" ").append(connector.getName()).append("\n"); - sb.append(" ").append(connector.getStatsOn()).append("\n"); - if (connector.getStatsOn()) - { - sb.append(" ").append(connector.getStatsOnMs()).append("\n"); - sb.append(" ").append(connector.getConnections()).append("\n"); - sb.append(" ").append(connector.getConnectionsOpen()).append("\n"); - sb.append(" ").append(connector.getConnectionsOpenMax()).append("\n"); - sb.append(" ").append(connector.getConnectionsDurationTotal()).append("\n"); - sb.append(" ").append(connector.getConnectionsDurationMean()).append("\n"); - sb.append(" ").append(connector.getConnectionsDurationMax()).append("\n"); - sb.append(" ").append(connector.getConnectionsDurationStdDev()).append("\n"); - sb.append(" ").append(connector.getRequests()).append("\n"); - sb.append(" ").append(connector.getConnectionsRequestsMean()).append("\n"); - sb.append(" ").append(connector.getConnectionsRequestsMax()).append("\n"); - sb.append(" ").append(connector.getConnectionsRequestsStdDev()).append("\n"); - } - sb.append(" \n"); - } - sb.append(" \n"); - - sb.append(" \n"); - sb.append(" ").append(_memoryBean.getHeapMemoryUsage().getUsed()).append("\n"); - sb.append(" ").append(_memoryBean.getNonHeapMemoryUsage().getUsed()).append("\n"); - sb.append(" \n"); - - sb.append("\n"); - - response.setContentType("text/xml"); - PrintWriter pout = response.getWriter(); - pout.write(sb.toString()); - } - - private void sendTextResponse(HttpServletResponse response) throws IOException - { - StringBuilder sb = new StringBuilder(); - sb.append(_statsHandler.toStatsHTML()); - - sb.append("

Connections:

\n"); - for (Connector connector : _connectors) - { - sb.append("

").append(connector.getName()).append("

"); - - if (connector.getStatsOn()) - { - sb.append("Statistics gathering started ").append(connector.getStatsOnMs()).append("ms ago").append("
\n"); - sb.append("Total connections: ").append(connector.getConnections()).append("
\n"); - sb.append("Current connections open: ").append(connector.getConnectionsOpen()); - sb.append("Max concurrent connections open: ").append(connector.getConnectionsOpenMax()).append("
\n"); - sb.append("Total connections duration: ").append(connector.getConnectionsDurationTotal()).append("
\n"); - sb.append("Mean connection duration: ").append(connector.getConnectionsDurationMean()).append("
\n"); - sb.append("Max connection duration: ").append(connector.getConnectionsDurationMax()).append("
\n"); - sb.append("Connection duration standard deviation: ").append(connector.getConnectionsDurationStdDev()).append("
\n"); - sb.append("Total requests: ").append(connector.getRequests()).append("
\n"); - sb.append("Mean requests per connection: ").append(connector.getConnectionsRequestsMean()).append("
\n"); - sb.append("Max requests per connection: ").append(connector.getConnectionsRequestsMax()).append("
\n"); - sb.append("Requests per connection standard deviation: ").append(connector.getConnectionsRequestsStdDev()).append("
\n"); - } - else - { - sb.append("Statistics gathering off.\n"); - } - - } - - sb.append("

Memory:

\n"); - sb.append("Heap memory usage: ").append(_memoryBean.getHeapMemoryUsage().getUsed()).append(" bytes").append("
\n"); - sb.append("Non-heap memory usage: ").append(_memoryBean.getNonHeapMemoryUsage().getUsed()).append(" bytes").append("
\n"); - - response.setContentType("text/html"); - PrintWriter pout = response.getWriter(); - pout.write(sb.toString()); - - } -} diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncContextDispatchWithQueryStrings.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncContextDispatchWithQueryStrings.java index 27a50931f1d..b9b2fa98ff6 100644 --- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncContextDispatchWithQueryStrings.java +++ b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/AsyncContextDispatchWithQueryStrings.java @@ -12,9 +12,9 @@ import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.continuation.Continuation; import org.eclipse.jetty.continuation.ContinuationSupport; -import org.eclipse.jetty.server.AsyncContinuation; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.HttpChannelState; import org.eclipse.jetty.server.LocalHttpConnector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.DefaultHandler; @@ -74,12 +74,12 @@ public class AsyncContextDispatchWithQueryStrings { String path = request.getRequestURI(); String queryString = request.getQueryString(); if ("/initialCall".equals(path)) { - AsyncContinuation continuation = (AsyncContinuation) ContinuationSupport.getContinuation(request); + HttpChannelState continuation = (HttpChannelState) ContinuationSupport.getContinuation(request); continuation.suspend(); continuation.dispatch("/firstDispatchWithNewQueryString?newQueryString=initialValue"); assertEquals("initialParam=right", queryString); } else if ("/firstDispatchWithNewQueryString".equals(path)) { - AsyncContinuation continuation = (AsyncContinuation) ContinuationSupport.getContinuation(request); + HttpChannelState continuation = (HttpChannelState) ContinuationSupport.getContinuation(request); continuation.suspend(); continuation.dispatch("/secondDispatchNewValueForExistingQueryString?newQueryString=newValue"); assertEquals("newQueryString=initialValue&initialParam=right", queryString); diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DispatcherTest.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DispatcherTest.java index bad6bca48d9..13653709a3b 100644 --- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DispatcherTest.java +++ b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/DispatcherTest.java @@ -152,9 +152,7 @@ public class DispatcherTest String expected= "HTTP/1.1 200 OK\r\n"+ - "Transfer-Encoding: chunked\r\n"+ - "\r\n"+ - "0\r\n"+ + "Content-Length: 0\r\n"+ "\r\n"; String responses = _connector.getResponses("GET /context/IncludeServlet/includepath?do=forward HTTP/1.1\n" + "Host: localhost\n\n"); 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 deleted file mode 100644 index 935fe998ae4..00000000000 --- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/StatisticsServletTest.java +++ /dev/null @@ -1,99 +0,0 @@ -// ======================================================================== -// 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 junit.framework.AssertionFailedError; - -import org.eclipse.jetty.server.LocalHttpConnector; -import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.server.handler.StatisticsHandler; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -public class StatisticsServletTest -{ - private Server server; - private LocalHttpConnector connector; - private ServletContextHandler context; - - @Before - public void init() throws Exception - { - 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 LocalHttpConnector(); - server.addConnector(connector); - } - - @After - public void destroy() throws Exception - { - server.stop(); - server.join(); - } - - @Test - 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); - } - - @Test - 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()); - } - } -} diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java b/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java index 466aa7f29be..d319b2bc04f 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java @@ -16,6 +16,7 @@ package org.eclipse.jetty.util; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.io.RandomAccessFile; import java.nio.Buffer; import java.nio.ByteBuffer; @@ -338,6 +339,19 @@ public class BufferUtil buffer.put(tmp); } } + + /* ------------------------------------------------------------ */ + public static void writeTo(ByteBuffer buffer, OutputStream out) throws IOException + { + if (buffer.hasArray()) + out.write(buffer.array(),buffer.arrayOffset()+buffer.position(),buffer.remaining()); + else + { + // TODO this is horribly inefficient + for (int i=buffer.position();i