457788 Powered By in o.e.j.util.Jetty conditional on sendServerVersion

moved conditional to HttpConfiguration
This commit is contained in:
Greg Wilkins 2015-05-08 09:44:19 +10:00
parent df61317f84
commit 6428718962
8 changed files with 77 additions and 49 deletions

View File

@ -129,10 +129,10 @@ public class LikeJettyXml
// === jetty-https.xml ===
// SSL Context Factory
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath(jetty_home + "/etc/keystore");
sslContextFactory.setKeyStorePath(jetty_home + "/../../../jetty-server/src/test/config/etc/keystore");
sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
sslContextFactory.setTrustStorePath(jetty_home + "/etc/keystore");
sslContextFactory.setTrustStorePath(jetty_home + "/../../../jetty-server/src/test/config/etc/keystore");
sslContextFactory.setTrustStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
sslContextFactory.setExcludeCipherSuites("SSL_RSA_WITH_DES_CBC_SHA",
"SSL_DHE_RSA_WITH_DES_CBC_SHA", "SSL_DHE_DSS_WITH_DES_CBC_SHA",

View File

@ -99,7 +99,7 @@ public class HttpSpiContextHandler extends ContextHandler
ex.printStackTrace(writer);
writer.println("</pre>");
writer.println("<p>"+Jetty.POWERED_BY_HTML+"</p>");
baseRequest.getHttpChannel().getHttpConfiguration().writePoweredBy(writer,"<p>","</p>");
writer.close();
}

View File

@ -28,6 +28,8 @@ import java.util.concurrent.ThreadPoolExecutor;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.NetworkConnector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
@ -48,24 +50,35 @@ public class JettyHttpServer extends com.sun.net.httpserver.HttpServer
{
private static final Logger LOG = Log.getLogger(JettyHttpServer.class);
private final HttpConfiguration _httpConfiguration;
private final Server _server;
private Server _server;
private boolean _serverShared;
private InetSocketAddress _addr;
private ThreadPoolExecutor _executor;
private Map<String, JettyHttpContext> _contexts = new HashMap<String, JettyHttpContext>();
private Map<String, Connector> _connectors = new HashMap<String, Connector>();
public JettyHttpServer(Server server, boolean shared)
{
this(server,shared,new HttpConfiguration());
}
public JettyHttpServer(Server server, boolean shared, HttpConfiguration configuration)
{
this._server = server;
this._serverShared = shared;
this._httpConfiguration = configuration;
}
public HttpConfiguration getHttpConfiguration()
{
return _httpConfiguration;
}
@Override
@ -83,10 +96,10 @@ public class JettyHttpServer extends com.sun.net.httpserver.HttpServer
}
}
}
if (_serverShared)
throw new IOException("jetty server is not bound to port " + addr.getPort());
throw new IOException("jetty server is not bound to port " + addr.getPort());
this._addr = addr;
if (LOG.isDebugEnabled()) LOG.debug("binding server to port " + addr.getPort());
@ -94,10 +107,18 @@ public class JettyHttpServer extends com.sun.net.httpserver.HttpServer
connector.setPort(addr.getPort());
connector.setHost(addr.getHostName());
_server.addConnector(connector);
_connectors.put(addr.getHostName() + addr.getPort(), connector);
}
protected ServerConnector newServerConnector(InetSocketAddress addr,int backlog)
{
ServerConnector connector = new ServerConnector(_server,new HttpConnectionFactory(_httpConfiguration));
connector.setPort(addr.getPort());
connector.setHost(addr.getHostName());
return connector;
}
@Override
public InetSocketAddress getAddress()
{
@ -108,7 +129,7 @@ public class JettyHttpServer extends com.sun.net.httpserver.HttpServer
public void start()
{
if (_serverShared) return;
try
{
_server.start();
@ -145,7 +166,7 @@ public class JettyHttpServer extends com.sun.net.httpserver.HttpServer
{
cleanUpContexts();
cleanUpConnectors();
if (_serverShared) return;
try
@ -158,15 +179,15 @@ public class JettyHttpServer extends com.sun.net.httpserver.HttpServer
}
}
private void cleanUpContexts()
{
private void cleanUpContexts()
{
for (Map.Entry<String, JettyHttpContext> stringJettyHttpContextEntry : _contexts.entrySet())
{
JettyHttpContext context = stringJettyHttpContextEntry.getValue();
_server.removeBean(context.getJettyContextHandler());
}
_contexts.clear();
}
_contexts.clear();
}
private void cleanUpConnectors()
{
@ -176,15 +197,17 @@ public class JettyHttpServer extends com.sun.net.httpserver.HttpServer
try
{
connector.stop();
} catch (Exception ex) {
}
catch (Exception ex)
{
LOG.warn(ex);
}
_server.removeConnector(connector);
}
_connectors.clear();
}
_connectors.clear();
}
@Override
@Override
public HttpContext createContext(String path, HttpHandler httpHandler)
{
checkIfContextIsFree(path);
@ -194,7 +217,7 @@ public class JettyHttpServer extends com.sun.net.httpserver.HttpServer
ContextHandlerCollection chc = findContextHandlerCollection(_server.getHandlers());
if (chc == null)
throw new RuntimeException("could not find ContextHandlerCollection, you must configure one");
throw new RuntimeException("could not find ContextHandlerCollection, you must configure one");
chc.addHandler(jettyContextHandler);
_contexts.put(path, context);
@ -228,13 +251,13 @@ public class JettyHttpServer extends com.sun.net.httpserver.HttpServer
private void checkIfContextIsFree(String path)
{
Handler serverHandler = _server.getHandler();
if (serverHandler instanceof ContextHandler)
{
ContextHandler ctx = (ContextHandler) serverHandler;
if (ctx.getContextPath().equals(path))
throw new RuntimeException("another context already bound to path " + path);
}
if (serverHandler instanceof ContextHandler)
{
ContextHandler ctx = (ContextHandler) serverHandler;
if (ctx.getContextPath().equals(path))
throw new RuntimeException("another context already bound to path " + path);
}
Handler[] handlers = _server.getHandlers();
if (handlers == null) return;
@ -246,9 +269,9 @@ public class JettyHttpServer extends com.sun.net.httpserver.HttpServer
throw new RuntimeException("another context already bound to path " + path);
}
}
}
}
@Override
@Override
public HttpContext createContext(String path)
{
return createContext(path, null);

View File

@ -18,6 +18,7 @@
package org.eclipse.jetty.server;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
@ -208,6 +209,19 @@ public class HttpConfiguration
return _sendServerVersion;
}
/* ------------------------------------------------------------ */
public void writePoweredBy(Appendable out,String preamble,String postamble) throws IOException
{
if (getSendServerVersion())
{
if (preamble!=null)
out.append(preamble);
out.append(Jetty.POWERED_BY);
if (postamble!=null)
out.append(postamble);
}
}
/* ------------------------------------------------------------ */
public void setSendXPoweredBy (boolean sendXPoweredBy)
{

View File

@ -605,11 +605,8 @@ public class Response implements HttpServletResponse
writer.write(message);
writer.write("</pre>");
writer.write("</p>\n<hr />");
if (getHttpChannel().getHttpConfiguration().getSendServerVersion())
{
writer.write(Jetty.POWERED_BY_HTML);
writer.write("<hr />");
}
getHttpChannel().getHttpConfiguration().writePoweredBy(writer,null,"<hr/>");
writer.write("\n</body>\n</html>\n");
writer.flush();

View File

@ -165,12 +165,8 @@ public class DefaultHandler extends AbstractHandler
writer.write("</ul><hr>");
if (baseRequest.getHttpChannel().getHttpConfiguration().getSendServerVersion())
{
writer.write("<a href=\"http://eclipse.org/jetty\"><img border=0 src=\"/favicon.ico\"/></a>&nbsp;");
writer.write(Jetty.POWERED_BY_HTML+"<hr/>\n");
}
baseRequest.getHttpChannel().getHttpConfiguration()
.writePoweredBy(writer,"<a href=\"http://eclipse.org/jetty\"><img border=0 src=\"/favicon.ico\"/></a>&nbsp;","<hr/>\n");
writer.write("\n</BODY>\n</HTML>\n");
writer.flush();

View File

@ -162,9 +162,9 @@ public class ErrorHandler extends AbstractHandler
writeErrorPageMessage(request,writer,code,message,uri);
if (showStacks)
writeErrorPageStacks(request,writer);
if (Request.getBaseRequest(request).getHttpChannel().getHttpConfiguration().getSendServerVersion())
writer.write("<hr>"+Jetty.POWERED_BY_HTML+"<hr/>\n");
Request.getBaseRequest(request).getHttpChannel().getHttpConfiguration()
.writePoweredBy(writer,"<hr>","<hr/>\n");
}
/* ------------------------------------------------------------ */

View File

@ -22,7 +22,6 @@ public class Jetty
{
public static final String VERSION;
public static final String POWERED_BY;
public static final String POWERED_BY_HTML;
static
{
@ -34,8 +33,7 @@ public class Jetty
else
VERSION = System.getProperty("jetty.version", "9.3.z-SNAPSHOT");
POWERED_BY="Powered by Jetty://"+VERSION;
POWERED_BY_HTML="<a href=\"http://eclipse.org/jetty\">"+POWERED_BY+"</a>";
POWERED_BY="<a href=\"http://eclipse.org/jetty\">Powered by Jetty:// "+VERSION+"</a>";
}
private Jetty()