Merge branch 'master' into jetty-9.4.x-Feature

This commit is contained in:
Greg Wilkins 2016-02-16 16:09:38 +01:00
commit 89803aec95
13 changed files with 162 additions and 145 deletions

View File

@ -20,6 +20,7 @@ package org.eclipse.jetty.jsp;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -59,10 +60,10 @@ public class JettyJspServlet extends JspServlet
String servletPath=null;
String pathInfo=null;
if (request.getAttribute("javax.servlet.include.request_uri")!=null)
if (request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI)!=null)
{
servletPath=(String)request.getAttribute("javax.servlet.include.servlet_path");
pathInfo=(String)request.getAttribute("javax.servlet.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();

View File

@ -13,12 +13,15 @@
<Ref refid="Server"/>
</Arg>
<Set name="workerName"><Property name="jetty.jdbcSession.workerName" default="node1"/></Set>
<Set name="scavengeInterval"><Property name="jetty.jdbcSession.scavenge" default="1800"/></Set>
<Set name="sessionScavenger">
<New class="org.eclipse.jetty.server.session.SessionScavenger">
<Set name="scavengeIntervalSec"><Property name="jetty.jdbcSession.scavenge" default="1800"/></Set>
</New>
</Set>
<!-- ===================================================================== -->
<!-- Uncomment either the datasource or driver setup and configure -->
<!-- ===================================================================== -->
<Get name="databaseAdaptor">
<!--
<Set name="DatasourceName"><Property name="jetty.jdbcSession.datasource" default="javax.sql.DataSource/default"/></Set>
-->
@ -28,6 +31,7 @@
<Arg><Property name="jetty.jdbcSession.connectionURL"/></Arg>
</Call>
-->
</Get>
</New>
</Set>

View File

@ -25,6 +25,7 @@ import java.io.Writer;
import java.nio.ByteBuffer;
import javax.servlet.ServletContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -34,9 +35,7 @@ import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.server.AsyncContextEvent;
import org.eclipse.jetty.server.Dispatcher;
import org.eclipse.jetty.server.HttpOutput;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.server.Server;
@ -46,23 +45,27 @@ import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/**
* <p>Component that handles Error Pages.</p>
* <p>An ErrorHandler is registered with {@link ContextHandler#setErrorHandler(ErrorHandler)} or
* {@link org.eclipse.jetty.server.Server#addBean(Object)}.</p>
* <p>It is called by {@link HttpServletResponse#sendError(int)} to write an error page via
* {@link #handle(String, Request, HttpServletRequest, HttpServletResponse)}
* or via {@link #badMessageError(int, String, HttpFields)} for bad requests for which a
* dispatch cannot be done.</p>
/* ------------------------------------------------------------ */
/** Handler for Error pages
* An ErrorHandler is registered with {@link ContextHandler#setErrorHandler(ErrorHandler)} or
* {@link org.eclipse.jetty.server.Server#addBean(Object)}.
* It is called by the HttpResponse.sendError method to write a error page via {@link #handle(String, Request, HttpServletRequest, HttpServletResponse)}
* or via {@link #badMessageError(int, String, HttpFields)} for bad requests for which a dispatch cannot be done.
*
*/
public class ErrorHandler extends AbstractHandler
{
{
private static final Logger LOG = Log.getLogger(ErrorHandler.class);
public final static String ERROR_PAGE="org.eclipse.jetty.server.error_page";
boolean _showStacks=true;
boolean _showMessageInTitle=true;
String _cacheControl="must-revalidate,no-cache,no-store";
private boolean _showStacks = true;
private boolean _showMessageInTitle = true;
private String _cacheControl = "must-revalidate,no-cache,no-store";
/* ------------------------------------------------------------ */
/*
* @see org.eclipse.jetty.server.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
*/
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
{
@ -72,31 +75,31 @@ public class ErrorHandler extends AbstractHandler
baseRequest.setHandled(true);
return;
}
if (this instanceof ErrorPageMapper)
{
String error_page = ((ErrorPageMapper)this).getErrorPage(request);
ServletContext context = request.getServletContext();
if (context == null)
String error_page=((ErrorPageMapper)this).getErrorPage(request);
if (error_page!=null && request.getServletContext()!=null)
{
AsyncContextEvent event = baseRequest.getHttpChannelState().getAsyncContextEvent();
context = event == null ? null : event.getServletContext();
}
if (error_page != null && context != null)
{
Dispatcher dispatcher = (Dispatcher)context.getRequestDispatcher(error_page);
if (dispatcher != null)
String old_error_page=(String)request.getAttribute(ERROR_PAGE);
if (old_error_page==null || !old_error_page.equals(error_page))
{
request.setAttribute(ERROR_PAGE, error_page);
Dispatcher dispatcher = (Dispatcher) request.getServletContext().getRequestDispatcher(error_page);
try
{
dispatcher.error(request, response);
return;
if(dispatcher!=null)
{
dispatcher.error(request, response);
return;
}
LOG.warn("No error page "+error_page);
}
catch (ServletException x)
catch (ServletException e)
{
throw new IOException(x);
LOG.warn(Log.EXCEPTION, e);
return;
}
}
} else {
@ -104,124 +107,112 @@ public class ErrorHandler extends AbstractHandler
{
LOG.debug("No Error Page mapping for request({} {}) (using default)",request.getMethod(),request.getRequestURI());
}
else
{
LOG.warn("Could not dispatch to error page: {}", error_page);
// Fall through to provide the default error page.
}
}
}
baseRequest.setHandled(true);
HttpOutput out = baseRequest.getResponse().getHttpOutput();
if (!out.isAsync())
{
response.setContentType(MimeTypes.Type.TEXT_HTML_8859_1.asString());
String cacheHeader = getCacheControl();
if (cacheHeader != null)
response.setHeader(HttpHeader.CACHE_CONTROL.asString(), cacheHeader);
ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(4096);
String reason = (response instanceof Response) ? ((Response)response).getReason() : null;
handleErrorPage(request, writer, response.getStatus(), reason);
writer.flush();
response.setContentLength(writer.size());
writer.writeTo(response.getOutputStream());
writer.destroy();
}
baseRequest.setHandled(true);
response.setContentType(MimeTypes.Type.TEXT_HTML_8859_1.asString());
if (_cacheControl!=null)
response.setHeader(HttpHeader.CACHE_CONTROL.asString(), _cacheControl);
ByteArrayISO8859Writer writer= new ByteArrayISO8859Writer(4096);
String reason=(response instanceof Response)?((Response)response).getReason():null;
handleErrorPage(request, writer, response.getStatus(), reason);
writer.flush();
response.setContentLength(writer.size());
writer.writeTo(response.getOutputStream());
writer.destroy();
}
/* ------------------------------------------------------------ */
protected void handleErrorPage(HttpServletRequest request, Writer writer, int code, String message)
throws IOException
throws IOException
{
writeErrorPage(request, writer, code, message, isShowStacks());
writeErrorPage(request, writer, code, message, _showStacks);
}
/* ------------------------------------------------------------ */
protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
throws IOException
throws IOException
{
if (message == null)
message = HttpStatus.getMessage(code);
message=HttpStatus.getMessage(code);
writer.write("<html>\n<head>\n");
writeErrorPageHead(request, writer, code, message);
writeErrorPageHead(request,writer,code,message);
writer.write("</head>\n<body>");
writeErrorPageBody(request, writer, code, message, showStacks);
writeErrorPageBody(request,writer,code,message,showStacks);
writer.write("\n</body>\n</html>\n");
}
/* ------------------------------------------------------------ */
protected void writeErrorPageHead(HttpServletRequest request, Writer writer, int code, String message)
throws IOException
{
throws IOException
{
writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/>\n");
writer.write("<title>Error ");
writer.write(Integer.toString(code));
if (getShowMessageInTitle())
if (_showMessageInTitle)
{
writer.write(' ');
write(writer, message);
write(writer,message);
}
writer.write("</title>\n");
}
/* ------------------------------------------------------------ */
protected void writeErrorPageBody(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks)
throws IOException
throws IOException
{
String uri = request.getRequestURI();
String uri= request.getRequestURI();
writeErrorPageMessage(request, writer, code, message, uri);
writeErrorPageMessage(request,writer,code,message,uri);
if (showStacks)
writeErrorPageStacks(request, writer);
writeErrorPageStacks(request,writer);
Request.getBaseRequest(request).getHttpChannel().getHttpConfiguration()
.writePoweredBy(writer, "<hr>", "<hr/>\n");
.writePoweredBy(writer,"<hr>","<hr/>\n");
}
/* ------------------------------------------------------------ */
protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message, String uri)
throws IOException
protected void writeErrorPageMessage(HttpServletRequest request, Writer writer, int code, String message,String uri)
throws IOException
{
writer.write("<h2>HTTP ERROR ");
writer.write(Integer.toString(code));
writer.write("</h2>\n<p>Problem accessing ");
write(writer, uri);
write(writer,uri);
writer.write(". Reason:\n<pre> ");
write(writer, message);
write(writer,message);
writer.write("</pre></p>");
}
/* ------------------------------------------------------------ */
protected void writeErrorPageStacks(HttpServletRequest request, Writer writer)
throws IOException
throws IOException
{
Throwable th = (Throwable)request.getAttribute("javax.servlet.error.exception");
while (th != null)
Throwable th = (Throwable)request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
while(th!=null)
{
writer.write("<h3>Caused by:</h3><pre>");
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
th.printStackTrace(pw);
pw.flush();
write(writer, sw.getBuffer().toString());
write(writer,sw.getBuffer().toString());
writer.write("</pre>\n");
th = th.getCause();
th =th.getCause();
}
}
/* ------------------------------------------------------------ */
/**
* <p>Generate a error response body to be sent for a bad message.</p>
* <p>In this case there is something wrong with the request, so either
/** Bad Message Error body
* <p>Generate a error response body to be sent for a bad message.
* In this case there is something wrong with the request, so either
* a request cannot be built, or it is not safe to build a request.
* This method allows for a simple error page body to be returned
* and some response headers to be set.</p>
*
* This method allows for a simple error page body to be returned
* and some response headers to be set.
* @param status The error code that will be sent
* @param reason The reason for the error code (may be null)
* @param fields The header fields that will be sent with the response.
@ -229,14 +220,14 @@ public class ErrorHandler extends AbstractHandler
*/
public ByteBuffer badMessageError(int status, String reason, HttpFields fields)
{
if (reason == null)
reason = HttpStatus.getMessage(status);
fields.put(HttpHeader.CONTENT_TYPE, MimeTypes.Type.TEXT_HTML_8859_1.asString());
if (reason==null)
reason=HttpStatus.getMessage(status);
fields.put(HttpHeader.CONTENT_TYPE,MimeTypes.Type.TEXT_HTML_8859_1.asString());
return BufferUtil.toBuffer("<h1>Bad Message " + status + "</h1><pre>reason: " + reason + "</pre>");
}
}
/* ------------------------------------------------------------ */
/**
/** Get the cacheControl.
* @return the cacheControl header to set on error responses.
*/
public String getCacheControl()
@ -245,7 +236,7 @@ public class ErrorHandler extends AbstractHandler
}
/* ------------------------------------------------------------ */
/**
/** Set the cacheControl.
* @param cacheControl the cacheControl header to set on error responses.
*/
public void setCacheControl(String cacheControl)
@ -255,7 +246,7 @@ public class ErrorHandler extends AbstractHandler
/* ------------------------------------------------------------ */
/**
* @return whether stack traces are shown in the error pages
* @return True if stack traces are shown in the error pages
*/
public boolean isShowStacks()
{
@ -264,7 +255,7 @@ public class ErrorHandler extends AbstractHandler
/* ------------------------------------------------------------ */
/**
* @param showStacks whether stack traces are shown in the error pages
* @param showStacks True if stack traces are shown in the error pages
*/
public void setShowStacks(boolean showStacks)
{
@ -273,27 +264,25 @@ public class ErrorHandler extends AbstractHandler
/* ------------------------------------------------------------ */
/**
* @return whether the error message appears in page title
*/
public boolean getShowMessageInTitle()
{
return _showMessageInTitle;
}
/* ------------------------------------------------------------ */
/**
* @param showMessageInTitle whether the error message appears in page title
* @param showMessageInTitle if true, the error message appears in page title
*/
public void setShowMessageInTitle(boolean showMessageInTitle)
{
_showMessageInTitle = showMessageInTitle;
}
/* ------------------------------------------------------------ */
protected void write(Writer writer, String string)
throws IOException
public boolean getShowMessageInTitle()
{
if (string == null)
return _showMessageInTitle;
}
/* ------------------------------------------------------------ */
protected void write(Writer writer,String string)
throws IOException
{
if (string==null)
return;
writer.write(StringUtil.sanitizeXmlString(string));
@ -308,22 +297,11 @@ public class ErrorHandler extends AbstractHandler
/* ------------------------------------------------------------ */
public static ErrorHandler getErrorHandler(Server server, ContextHandler context)
{
ErrorHandler error_handler = null;
if (context != null)
error_handler = context.getErrorHandler();
if (error_handler == null)
{
synchronized (ErrorHandler.class)
{
error_handler = server.getBean(ErrorHandler.class);
if (error_handler == null)
{
error_handler = new ErrorHandler();
error_handler.setServer(server);
server.addManaged(error_handler);
}
}
}
ErrorHandler error_handler=null;
if (context!=null)
error_handler=context.getErrorHandler();
if (error_handler==null && server!=null)
error_handler = server.getBean(ErrorHandler.class);
return error_handler;
}
}

View File

@ -61,6 +61,9 @@ public class DatabaseAdaptor
private String _connectionUrl;
private Driver _driver;
private DataSource _datasource;
private String _jndiName;
@ -217,6 +220,37 @@ public class DatabaseAdaptor
_jndiName=jndi;
}
public String getDatasourceName ()
{
return _jndiName;
}
public DataSource getDatasource()
{
return _datasource;
}
public String getDriverClassName()
{
return _driverClassName;
}
public Driver getDriver()
{
return _driver;
}
public String getConnectionUrl()
{
return _connectionUrl;
}
public void initialize ()
throws Exception
{
@ -249,7 +283,6 @@ public class DatabaseAdaptor
}
}
}
/**

View File

@ -27,6 +27,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
@ -76,7 +77,7 @@ public class ConcatServletTest
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String includedURI = (String)request.getAttribute("javax.servlet.include.request_uri");
String includedURI = (String)request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI);
response.getOutputStream().println(includedURI);
}
});

View File

@ -43,6 +43,7 @@ import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainer
import org.eclipse.jetty.websocket.server.WebSocketServerFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class SessionTrackingTest
@ -129,6 +130,7 @@ public class SessionTrackingTest
}
@Test
@Ignore
public void testAddRemoveSessions() throws Exception
{
// Create Client

View File

@ -118,13 +118,9 @@ public class JdbcTestServer extends AbstractTestServer
JDBCSessionIdManager idManager = new JDBCSessionIdManager(_server);
idManager.setWorkerName("w"+(__workers++));
idManager.getDatabaseAdaptor().setDriverInfo(DRIVER_CLASS, (config==null?DEFAULT_CONNECTION_URL:(String)config));
JDBCSessionIdManager.SessionIdTableSchema idTableSchema = new JDBCSessionIdManager.SessionIdTableSchema();
JDBCSessionIdManager.SessionIdTableSchema idTableSchema = idManager.getSessionIdTableSchema();
idTableSchema.setTableName("mysessionids");
idTableSchema.setIdColumn("myid");
return idManager;
}
}

View File

@ -24,6 +24,7 @@ import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@ -178,7 +179,7 @@ public class SessionDump extends HttpServlet
/* ------------------------------------------------------------ */
private String getURI(HttpServletRequest request)
{
String uri=(String)request.getAttribute("javax.servlet.forward.request_uri");
String uri=(String)request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
if (uri==null)
uri=request.getRequestURI();
return uri;

View File

@ -118,9 +118,7 @@
<contextPath>/test</contextPath>
<tempDirectory>${project.build.directory}/work</tempDirectory>
<sessionHandler implementation="org.eclipse.jetty.server.session.SessionHandler">
<sessionManager implementation="org.eclipse.jetty.server.session.HashSessionManager">
<storeDirectory>${basedir}/target/sessions</storeDirectory>
</sessionManager>
<sessionManager implementation="org.eclipse.jetty.server.session.HashSessionManager"/>
</sessionHandler>
</webApp>
<loginServices>

View File

@ -22,6 +22,7 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.TimeUnit;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
@ -105,7 +106,7 @@ public class CookieDump extends HttpServlet
/* ------------------------------------------------------------ */
private String getURI(HttpServletRequest request)
{
String uri=(String)request.getAttribute("javax.servlet.forward.request_uri");
String uri=(String)request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
if (uri==null)
uri=request.getRequestURI();
return uri;

View File

@ -68,8 +68,8 @@ public class DispatchServlet extends HttpServlet
String info;
if (sreq.getAttribute("javax.servlet.include.servlet_path") != null)
info= (String)sreq.getAttribute("javax.servlet.include.path_info");
if (sreq.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH) != null)
info= (String)sreq.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO);
else
info= sreq.getPathInfo();

View File

@ -39,6 +39,7 @@ import java.util.TimerTask;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
@ -329,7 +330,7 @@ public class Dump extends HttpServlet
// handle an error
String error= request.getParameter("error");
if (error != null && error.length() > 0 && request.getAttribute("javax.servlet.error.status_code")==null)
if (error != null && error.length() > 0 && request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)==null)
{
response.getOutputStream().println("THIS SHOULD NOT BE SEEN!");
response.sendError(Integer.parseInt(error));
@ -874,7 +875,7 @@ public class Dump extends HttpServlet
/* ------------------------------------------------------------ */
private String getURI(HttpServletRequest request)
{
String uri= (String)request.getAttribute("javax.servlet.forward.request_uri");
String uri= (String)request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
if (uri == null)
uri= request.getRequestURI();
return uri;

View File

@ -23,6 +23,7 @@ import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@ -193,7 +194,7 @@ public class SessionDump extends HttpServlet
/* ------------------------------------------------------------ */
private String getURI(HttpServletRequest request)
{
String uri=(String)request.getAttribute("javax.servlet.forward.request_uri");
String uri=(String)request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
if (uri==null)
uri=request.getRequestURI();
return uri;