jetty-9 passing jetty-servlet tests!!!!

This commit is contained in:
Greg Wilkins 2012-06-05 18:11:20 +02:00
parent 5415517cc0
commit c1f3545d7c
11 changed files with 81 additions and 391 deletions

View File

@ -732,6 +732,7 @@ public abstract class HttpChannel
/* ------------------------------------------------------------ */
@Override
public void sendContent(Object content) throws IOException
{
Resource resource=null;

View File

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

View File

@ -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<String> 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<ranges.size();i++)
{
InclusiveByteRange ibr = (InclusiveByteRange) ranges.get(i);
multi.startPart(mimetype,new String[]{HttpHeaders.CONTENT_RANGE+": "+header[i]});
multi.startPart(mimetype,new String[]{HttpHeader.CONTENT_RANGE+": "+header[i]});
long start=ibr.getFirst(content_length);
long size=ibr.getSize(content_length);
@ -962,12 +962,12 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory
HttpFields fields = r.getHttpFields();
if (content.getLastModified()!=null)
fields.put(HttpHeaders.LAST_MODIFIED_BUFFER,content.getLastModified());
fields.put(HttpHeader.LAST_MODIFIED,content.getLastModified());
else if (content.getResource()!=null)
{
long lml=content.getResource().lastModified();
if (lml!=-1)
fields.putDateField(HttpHeaders.LAST_MODIFIED_BUFFER,lml);
fields.putDateField(HttpHeader.LAST_MODIFIED,lml);
}
if (count != -1)
@ -979,14 +979,14 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory
{
long lml=content.getResource().lastModified();
if (lml>=0)
response.setDateHeader(HttpHeaders.LAST_MODIFIED,lml);
response.setDateHeader(HttpHeader.LAST_MODIFIED.asString(),lml);
if (count != -1)
{
if (count<Integer.MAX_VALUE)
response.setContentLength((int)count);
else
response.setHeader(HttpHeaders.CONTENT_LENGTH,Long.toString(count));
response.setHeader(HttpHeader.CONTENT_LENGTH.asString(),Long.toString(count));
}
writeOptionHeaders(response);
@ -997,20 +997,20 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory
protected void writeOptionHeaders(HttpFields fields) throws IOException
{
if (_acceptRanges)
fields.put(HttpHeaders.ACCEPT_RANGES_BUFFER,HttpHeaderValues.BYTES_BUFFER);
fields.put(HttpHeader.ACCEPT_RANGES,"bytes");
if (_cacheControl!=null)
fields.put(HttpHeaders.CACHE_CONTROL_BUFFER,_cacheControl);
fields.put(HttpHeader.CACHE_CONTROL,_cacheControl);
}
/* ------------------------------------------------------------ */
protected void writeOptionHeaders(HttpServletResponse response) throws IOException
{
if (_acceptRanges)
response.setHeader(HttpHeaders.ACCEPT_RANGES,"bytes");
response.setHeader(HttpHeader.ACCEPT_RANGES.asString(),"bytes");
if (_cacheControl!=null)
response.setHeader(HttpHeaders.CACHE_CONTROL,_cacheControl.toString());
response.setHeader(HttpHeader.CACHE_CONTROL.asString(),_cacheControl);
}
/* ------------------------------------------------------------ */

View File

@ -24,8 +24,7 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpMethods;
import org.eclipse.jetty.server.AbstractHttpConnection;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.server.Dispatcher;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.ContextHandler;
@ -62,9 +61,9 @@ public class ErrorPageErrorHandler extends ErrorHandler
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
{
String method = request.getMethod();
if (!HttpMethod.GET.is(method) && !HttpMethod.POST.is(method) && !HttpMethod.HEAD.isMethod())
if (!HttpMethod.GET.is(method) && !HttpMethod.POST.is(method) && !HttpMethod.HEAD.is(method))
{
AbstractHttpConnection.getCurrentConnection().getRequest().setHandled(true);
baseRequest.setHandled(true);
return;
}
if (_errorPages!=null)

View File

@ -26,9 +26,9 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.AbstractHttpConnection;
import org.eclipse.jetty.server.Dispatcher;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpChannel;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.HandlerWrapper;
@ -221,7 +221,7 @@ public class Invoker extends HttpServlet
if (holder!=null)
{
final Request baseRequest=(request instanceof Request)?((Request)request):AbstractHttpConnection.getCurrentConnection().getRequest();
final Request baseRequest=(request instanceof Request)?((Request)request):HttpChannel.getCurrentHttpChannel().getRequest();
holder.handle(baseRequest,
new InvokedRequest(request,included,servlet,servlet_path,path_info),
response);

View File

@ -44,15 +44,13 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.continuation.ContinuationThrowable;
import org.eclipse.jetty.http.HttpException;
import org.eclipse.jetty.http.PathMap;
import org.eclipse.jetty.io.EofException;
import org.eclipse.jetty.io.RuntimeIOException;
import org.eclipse.jetty.security.IdentityService;
import org.eclipse.jetty.security.SecurityHandler;
import org.eclipse.jetty.server.AbstractHttpConnection;
import org.eclipse.jetty.server.Dispatcher;
import org.eclipse.jetty.server.AbstractHttpConnection;
import org.eclipse.jetty.server.HttpChannel;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServletRequestHttpWrapper;
@ -252,7 +250,7 @@ public class ServletHandler extends ScopedHandler
* @param pathInContext Path within _context.
* @return PathMap Entries pathspec to ServletHolder
*/
public PathMap.Entry getHolderEntry(String pathInContext)
public PathMap.MappedEntry getHolderEntry(String pathInContext)
{
if (_servletPathMap==null)
return null;
@ -367,7 +365,7 @@ public class ServletHandler extends ScopedHandler
if (target.startsWith("/"))
{
// Look for the servlet by path
PathMap.Entry entry=getHolderEntry(target);
PathMap.MappedEntry entry=getHolderEntry(target);
if (entry!=null)
{
servlet_holder=(ServletHolder)entry.getValue();
@ -531,8 +529,6 @@ public class ServletHandler extends ScopedHandler
}
// handle or log exception
if (th instanceof HttpException)
throw (HttpException)th;
else if (th instanceof RuntimeIOException)
throw (RuntimeIOException)th;
else if (th instanceof EofException)
@ -1324,7 +1320,7 @@ public class ServletHandler extends ScopedHandler
public void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException
{
final Request baseRequest=(request instanceof Request)?((Request)request):AbstractHttpConnection.getCurrentConnection().getRequest();
final Request baseRequest=(request instanceof Request)?((Request)request):HttpChannel.getCurrentHttpChannel().getRequest();
// pass to next filter
if (_filterHolder!=null)
@ -1446,7 +1442,7 @@ public class ServletHandler extends ScopedHandler
notFound(srequest, (HttpServletResponse)response);
else
{
Request baseRequest=(request instanceof Request)?((Request)request):AbstractHttpConnection.getCurrentConnection().getRequest();
Request baseRequest=(request instanceof Request)?((Request)request):HttpChannel.getCurrentHttpChannel().getRequest();
nextHandle(URIUtil.addPaths(srequest.getServletPath(),srequest.getPathInfo()),
baseRequest,srequest,(HttpServletResponse)response);
}

View File

@ -1,226 +0,0 @@
// ========================================================================
// Copyright (c) 2008-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 java.io.IOException;
import java.io.PrintWriter;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.StatisticsHandler;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
public class StatisticsServlet extends HttpServlet
{
private static final Logger LOG = Log.getLogger(StatisticsServlet.class);
boolean _restrictToLocalhost = true; // defaults to true
private StatisticsHandler _statsHandler;
private MemoryMXBean _memoryBean;
private Connector[] _connectors;
public void init() throws ServletException
{
ServletContext context = getServletContext();
ContextHandler.Context scontext = (ContextHandler.Context) context;
Server _server = scontext.getContextHandler().getServer();
Handler handler = _server.getChildHandlerByClass(StatisticsHandler.class);
if (handler != null)
{
_statsHandler = (StatisticsHandler) handler;
}
else
{
LOG.warn("Statistics Handler not installed!");
return;
}
_memoryBean = ManagementFactory.getMemoryMXBean();
_connectors = _server.getConnectors();
if (getInitParameter("restrictToLocalhost") != null)
{
_restrictToLocalhost = "true".equals(getInitParameter("restrictToLocalhost"));
}
}
public void doPost(HttpServletRequest sreq, HttpServletResponse sres) throws ServletException, IOException
{
doGet(sreq, sres);
}
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()))
{
resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
return;
}
}
String wantXml = req.getParameter("xml");
if (wantXml == null)
wantXml = req.getParameter("XML");
if (wantXml != null && "true".equalsIgnoreCase(wantXml))
{
sendXmlResponse(resp);
}
else
{
sendTextResponse(resp);
}
}
private void sendXmlResponse(HttpServletResponse response) throws IOException
{
StringBuilder sb = new StringBuilder();
sb.append("<statistics>\n");
sb.append(" <requests>\n");
sb.append(" <statsOnMs>").append(_statsHandler.getStatsOnMs()).append("</statsOnMs>\n");
sb.append(" <requests>").append(_statsHandler.getRequests()).append("</requests>\n");
sb.append(" <requestsActive>").append(_statsHandler.getRequestsActive()).append("</requestsActive>\n");
sb.append(" <requestsActiveMax>").append(_statsHandler.getRequestsActiveMax()).append("</requestsActiveMax>\n");
sb.append(" <requestsTimeTotal>").append(_statsHandler.getRequestTimeTotal()).append("</requestsTimeTotal>\n");
sb.append(" <requestsTimeMean>").append(_statsHandler.getRequestTimeMean()).append("</requestsTimeMean>\n");
sb.append(" <requestsTimeMax>").append(_statsHandler.getRequestTimeMax()).append("</requestsTimeMax>\n");
sb.append(" <requestsTimeStdDev>").append(_statsHandler.getRequestTimeStdDev()).append("</requestsTimeStdDev>\n");
sb.append(" <dispatched>").append(_statsHandler.getDispatched()).append("</dispatched>\n");
sb.append(" <dispatchedActive>").append(_statsHandler.getDispatchedActive()).append("</dispatchedActive>\n");
sb.append(" <dispatchedActiveMax>").append(_statsHandler.getDispatchedActiveMax()).append("</dispatchedActiveMax>\n");
sb.append(" <dispatchedTimeTotal>").append(_statsHandler.getDispatchedTimeTotal()).append("</dispatchedTimeTotal>\n");
sb.append(" <dispatchedTimeMean").append(_statsHandler.getDispatchedTimeMean()).append("</dispatchedTimeMean>\n");
sb.append(" <dispatchedTimeMax>").append(_statsHandler.getDispatchedTimeMax()).append("</dispatchedTimeMax>\n");
sb.append(" <dispatchedTimeStdDev").append(_statsHandler.getDispatchedTimeStdDev()).append("</dispatchedTimeStdDev>\n");
sb.append(" <requestsSuspended>").append(_statsHandler.getSuspends()).append("</requestsSuspended>\n");
sb.append(" <requestsExpired>").append(_statsHandler.getExpires()).append("</requestsExpired>\n");
sb.append(" <requestsResumed>").append(_statsHandler.getResumes()).append("</requestsResumed>\n");
sb.append(" </requests>\n");
sb.append(" <responses>\n");
sb.append(" <responses1xx>").append(_statsHandler.getResponses1xx()).append("</responses1xx>\n");
sb.append(" <responses2xx>").append(_statsHandler.getResponses2xx()).append("</responses2xx>\n");
sb.append(" <responses3xx>").append(_statsHandler.getResponses3xx()).append("</responses3xx>\n");
sb.append(" <responses4xx>").append(_statsHandler.getResponses4xx()).append("</responses4xx>\n");
sb.append(" <responses5xx>").append(_statsHandler.getResponses5xx()).append("</responses5xx>\n");
sb.append(" <responsesBytesTotal>").append(_statsHandler.getResponsesBytesTotal()).append("</responsesBytesTotal>\n");
sb.append(" </responses>\n");
sb.append(" <connections>\n");
for (Connector connector : _connectors)
{
sb.append(" <connector>\n");
sb.append(" <name>").append(connector.getName()).append("</name>\n");
sb.append(" <statsOn>").append(connector.getStatsOn()).append("</statsOn>\n");
if (connector.getStatsOn())
{
sb.append(" <statsOnMs>").append(connector.getStatsOnMs()).append("</statsOnMs>\n");
sb.append(" <connections>").append(connector.getConnections()).append("</connections>\n");
sb.append(" <connectionsOpen>").append(connector.getConnectionsOpen()).append("</connectionsOpen>\n");
sb.append(" <connectionsOpenMax>").append(connector.getConnectionsOpenMax()).append("</connectionsOpenMax>\n");
sb.append(" <connectionsDurationTotal>").append(connector.getConnectionsDurationTotal()).append("</connectionsDurationTotal>\n");
sb.append(" <connectionsDurationMean>").append(connector.getConnectionsDurationMean()).append("</connectionsDurationMean>\n");
sb.append(" <connectionsDurationMax>").append(connector.getConnectionsDurationMax()).append("</connectionsDurationMax>\n");
sb.append(" <connectionsDurationStdDev>").append(connector.getConnectionsDurationStdDev()).append("</connectionsDurationStdDev>\n");
sb.append(" <requests>").append(connector.getRequests()).append("</requests>\n");
sb.append(" <connectionsRequestsMean>").append(connector.getConnectionsRequestsMean()).append("</connectionsRequestsMean>\n");
sb.append(" <connectionsRequestsMax>").append(connector.getConnectionsRequestsMax()).append("</connectionsRequestsMax>\n");
sb.append(" <connectionsRequestsStdDev>").append(connector.getConnectionsRequestsStdDev()).append("</connectionsRequestsStdDev>\n");
}
sb.append(" </connector>\n");
}
sb.append(" </connections>\n");
sb.append(" <memory>\n");
sb.append(" <heapMemoryUsage>").append(_memoryBean.getHeapMemoryUsage().getUsed()).append("</heapMemoryUsage>\n");
sb.append(" <nonHeapMemoryUsage>").append(_memoryBean.getNonHeapMemoryUsage().getUsed()).append("</nonHeapMemoryUsage>\n");
sb.append(" </memory>\n");
sb.append("</statistics>\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("<h2>Connections:</h2>\n");
for (Connector connector : _connectors)
{
sb.append("<h3>").append(connector.getName()).append("</h3>");
if (connector.getStatsOn())
{
sb.append("Statistics gathering started ").append(connector.getStatsOnMs()).append("ms ago").append("<br />\n");
sb.append("Total connections: ").append(connector.getConnections()).append("<br />\n");
sb.append("Current connections open: ").append(connector.getConnectionsOpen());
sb.append("Max concurrent connections open: ").append(connector.getConnectionsOpenMax()).append("<br />\n");
sb.append("Total connections duration: ").append(connector.getConnectionsDurationTotal()).append("<br />\n");
sb.append("Mean connection duration: ").append(connector.getConnectionsDurationMean()).append("<br />\n");
sb.append("Max connection duration: ").append(connector.getConnectionsDurationMax()).append("<br />\n");
sb.append("Connection duration standard deviation: ").append(connector.getConnectionsDurationStdDev()).append("<br />\n");
sb.append("Total requests: ").append(connector.getRequests()).append("<br />\n");
sb.append("Mean requests per connection: ").append(connector.getConnectionsRequestsMean()).append("<br />\n");
sb.append("Max requests per connection: ").append(connector.getConnectionsRequestsMax()).append("<br />\n");
sb.append("Requests per connection standard deviation: ").append(connector.getConnectionsRequestsStdDev()).append("<br />\n");
}
else
{
sb.append("Statistics gathering off.\n");
}
}
sb.append("<h2>Memory:</h2>\n");
sb.append("Heap memory usage: ").append(_memoryBean.getHeapMemoryUsage().getUsed()).append(" bytes").append("<br />\n");
sb.append("Non-heap memory usage: ").append(_memoryBean.getNonHeapMemoryUsage().getUsed()).append(" bytes").append("<br />\n");
response.setContentType("text/html");
PrintWriter pout = response.getWriter();
pout.write(sb.toString());
}
}

View File

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

View File

@ -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");

View File

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

View File

@ -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;
@ -339,6 +340,19 @@ public class BufferUtil
}
}
/* ------------------------------------------------------------ */
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<buffer.limit();i++)
out.write(buffer.get(i));
}
}
/* ------------------------------------------------------------ */
/** Convert the buffer to an ISO-8859-1 String
* @param buffer The buffer to convert in flush mode. The buffer is unchanged