if (handlers != null) {
numberOfHandlers += handlers.size();
}
Handler existingHandler = server.getHandler();
HandlerCollection handlerCollection = null;
boolean existingHandlerCollection = existingHandler instanceof HandlerCollection;
if (existingHandlerCollection) {
handlerCollection = (HandlerCollection) existingHandler;
}
if (!existingHandlerCollection
&&
(existingHandler != null || numberOfHandlers > 1)) {
handlerCollection = new HandlerCollection();
if (existingHandler != null) {
handlerCollection.addHandler(existingHandler);
}
server.setHandler(handlerCollection);
}
/*
* At this point, the server's handler is a collection. It was either
* one to start, or it is now one containing only the single handler
* that was there to begin with.
*/
if (handlers != null && handlers.size() > 0) {
for (Handler h : handlers) {
// Filtering out the jetty default handler
// which should not be added at this point.
if (h instanceof DefaultHandler) {
defaultHandler = (DefaultHandler) h;
} else {
if ((h instanceof SecurityHandler)
&& ((SecurityHandler)h).getHandler() == null) {
//if h is SecurityHandler(such as ConstraintSecurityHandler)
//then it need be on top of JettyHTTPHandler
//set JettyHTTPHandler as inner handler if
//inner handler is null
((SecurityHandler)h).setHandler(handler);
securityHandler = (SecurityHandler)h;
} else {
handlerCollection.addHandler(h);
}
}
}
}
contexts = new ContextHandlerCollection();
/*
* handlerCollection may be null here if is only one handler to deal with.
* Which in turn implies that there can't be a 'defaultHander' to deal with.
*/
if (handlerCollection != null) {
handlerCollection.addHandler(contexts);
if (defaultHandler != null) {
handlerCollection.addHandler(defaultHandler);
}
} else {
server.setHandler(contexts);
}