path = path.substring(0, path.length() - 1);
}
String pathSlash = path + "/";
// 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];
String handlerPath = h.getContextPath() + "/";
if (handlerPath.startsWith(pathSlash) || pathSlash.startsWith(handlerPath)) {
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());
JaasUserRealm realm = new JaasUserRealm();
if (configuration.getAuthenticationService() != null) {
realm.setAuthenticationService(configuration.getAuthenticationService());
}
secHandler.setUserRealm(realm);
context.setHandler(secHandler);
} else {
context.setHandler(handler);
}
context.setAttribute("processor", processor);
// add context
contexts.addHandler(context);
handler.initialize();
context.start();
return context;
}