if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
// Check that context does not exist yet
HandlerCollection handlerCollection = (HandlerCollection) server.getHandler();
ContextHandlerCollection contexts = (ContextHandlerCollection) handlerCollection.getHandlers()[0];
Handler[] handlers = contexts.getHandlers();
if (handlers != null) {
for (int i = 0; i < handlers.length; i++) {
if (handlers[i] instanceof ContextHandler) {
ContextHandler h = (ContextHandler) handlers[i];
if (h.getContextPath().startsWith(path) ||
path.startsWith(h.getContextPath())) {
throw new Exception("The requested context for path '" + path + "' overlaps with an existing context for path: '" + h.getContextPath() + "'");
}
}
}
}
// Create context
ContextHandler context = new ContextHandler();
context.setContextPath(path);
ServletHolder holder = new ServletHolder();
holder.setName("jbiServlet");
holder.setClassName(HttpBridgeServlet.class.getName());
ServletHandler handler = new ServletHandler();
handler.setServlets(new ServletHolder[] { holder });
ServletMapping mapping = new ServletMapping();
mapping.setServletName("jbiServlet");
mapping.setPathSpec("/*");
handler.setServletMappings(new ServletMapping[] { mapping });
if (processor.getAuthMethod() != null) {
SecurityHandler secHandler = new SecurityHandler();
ConstraintMapping constraintMapping = new ConstraintMapping();
Constraint constraint = new Constraint();
constraint.setAuthenticate(true);
constraint.setRoles(new String[] { "*" });
constraintMapping.setConstraint(constraint);
constraintMapping.setPathSpec("/");
secHandler.setConstraintMappings(new ConstraintMapping[] { constraintMapping });
secHandler.setHandler(handler);
secHandler.setAuthMethod(processor.getAuthMethod());
secHandler.setUserRealm(new JaasUserRealm());
context.setHandler(secHandler);
} else {
context.setHandler(handler);
}
context.setAttribute("processor", processor);
// add context
contexts.addHandler(context);
context.start();
return context;
}