use of org.eclipse.jetty.servlet.ServletContextHandler in project camel by apache.
the class CometdComponent method createServletForConnector.
protected CometDServlet createServletForConnector(Server server, Connector connector, CometdEndpoint endpoint) throws Exception {
CometDServlet servlet = new CometDServlet();
ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.NO_SECURITY | ServletContextHandler.NO_SESSIONS);
ServletHolder holder = new ServletHolder();
holder.setServlet(servlet);
holder.setAsyncSupported(true);
// pointing to by example classpath:webapp
if (endpoint.getBaseResource() != null) {
String[] resources = endpoint.getBaseResource().split(":");
if (LOG.isDebugEnabled()) {
LOG.debug(">>> Protocol found: " + resources[0] + ", and resource: " + resources[1]);
}
if (resources[0].equals("file")) {
context.setBaseResource(Resource.newResource(resources[1]));
} else if (resources[0].equals("classpath")) {
// Create a URL handler using classpath protocol
URL url = this.getCamelContext().getClassResolver().loadResourceAsURL(resources[1]);
context.setBaseResource(Resource.newResource(url));
}
}
applyCrossOriginFiltering(endpoint, context);
context.addServlet(holder, "/cometd/*");
context.addServlet("org.eclipse.jetty.servlet.DefaultServlet", "/");
context.setSessionHandler(new SessionHandler());
holder.setInitParameter("timeout", Integer.toString(endpoint.getTimeout()));
holder.setInitParameter("interval", Integer.toString(endpoint.getInterval()));
holder.setInitParameter("maxInterval", Integer.toString(endpoint.getMaxInterval()));
holder.setInitParameter("multiFrameInterval", Integer.toString(endpoint.getMultiFrameInterval()));
holder.setInitParameter("JSONCommented", Boolean.toString(endpoint.isJsonCommented()));
holder.setInitParameter("logLevel", Integer.toString(endpoint.getLogLevel()));
return servlet;
}
use of org.eclipse.jetty.servlet.ServletContextHandler in project camel by apache.
the class CometdCrossOriginConsumerTest method testCrossOriginFilterNotAddedWhenOff.
@Test
public void testCrossOriginFilterNotAddedWhenOff() throws Exception {
// setup
CometdComponent component = context.getComponent("cometd", CometdComponent.class);
Server server = new Server();
when(endpoint.isCrossOriginFilterOn()).thenReturn(false);
// act
component.createServletForConnector(server, connector, endpoint);
// assert
ServletContextHandler handler = (ServletContextHandler) server.getHandler();
assertEquals(0, handler.getServletHandler().getFilters().length);
}
use of org.eclipse.jetty.servlet.ServletContextHandler in project camel by apache.
the class CometdCrossOriginConsumerTest method testCrossOriginFilterAddedWhenOn.
@Test
public void testCrossOriginFilterAddedWhenOn() throws Exception {
// setup
CometdComponent component = context.getComponent("cometd", CometdComponent.class);
Server server = new Server();
when(endpoint.isCrossOriginFilterOn()).thenReturn(true);
when(endpoint.getFilterPath()).thenReturn(FILTER_PATH);
when(endpoint.getAllowedOrigins()).thenReturn(ALLOWED_ORIGINS);
// act
component.createServletForConnector(server, connector, endpoint);
// assert
ServletContextHandler handler = (ServletContextHandler) server.getHandler();
assertEquals(1, handler.getServletHandler().getFilters().length);
FilterHolder filterHolder = handler.getServletHandler().getFilters()[0];
Filter filter = filterHolder.getFilter();
assertTrue(filter instanceof CrossOriginFilter);
}
use of org.eclipse.jetty.servlet.ServletContextHandler in project coprhd-controller by CoprHD.
the class AuthenticationServerImpl method initServer.
@Override
protected void initServer() throws Exception {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
String authDocumentRoot = loader.getResource(AUTH_DOCUMENT_ROOT).toString();
_server = new Server();
initConnectors();
// Static Pages
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setWelcomeFiles(new String[] { "*" });
resourceHandler.setResourceBase(authDocumentRoot);
// AuthN servlet filters
ServletContextHandler rootHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
rootHandler.setContextPath("/");
HandlerCollection handlerCollection = new HandlerCollection();
handlerCollection.setHandlers(new Handler[] { resourceHandler, rootHandler });
_server.setHandler(handlerCollection);
((AbstractSessionManager) rootHandler.getSessionHandler().getSessionManager()).setUsingCookies(false);
final FilterHolder securityFilterHolder = new FilterHolder(new DelegatingFilterProxy(_secFilters));
rootHandler.addFilter(securityFilterHolder, "/*", FilterMapping.REQUEST);
// Add the REST resources
if (_app != null) {
ResourceConfig config = new DefaultResourceConfig();
config.add(_app);
Map<String, MediaType> type = config.getMediaTypeMappings();
type.put("json", MediaType.APPLICATION_JSON_TYPE);
type.put("xml", MediaType.APPLICATION_XML_TYPE);
rootHandler.addServlet(new ServletHolder(new ServletContainer(config)), "/*");
}
// load trust store from file to zk. must do it before authmgr started, who holds the connection with ad.
loadTrustStoreFromLocalFiles();
_dbClient.start();
_tokenManager.init();
_authManager.init();
}
use of org.eclipse.jetty.servlet.ServletContextHandler in project nifi by apache.
the class JettyWebSocketServer method startServer.
@OnEnabled
@Override
public void startServer(final ConfigurationContext context) throws Exception {
if (server != null && server.isRunning()) {
getLogger().info("A WebSocket server is already running. {}", new Object[] { server });
return;
}
configuredPolicy = WebSocketPolicy.newServerPolicy();
configurePolicy(context, configuredPolicy);
server = new Server();
final ContextHandlerCollection handlerCollection = new ContextHandlerCollection();
final ServletContextHandler contextHandler = new ServletContextHandler();
servletHandler = new ServletHandler();
contextHandler.insertHandler(servletHandler);
handlerCollection.setHandlers(new Handler[] { contextHandler });
server.setHandler(handlerCollection);
listenPort = context.getProperty(LISTEN_PORT).asInteger();
final SslContextFactory sslContextFactory = createSslFactory(context);
final ServerConnector serverConnector = createConnector(sslContextFactory, listenPort);
server.setConnectors(new Connector[] { serverConnector });
servletHandler.addServletWithMapping(JettyWebSocketServlet.class, "/*");
getLogger().info("Starting JettyWebSocketServer on port {}.", new Object[] { listenPort });
server.start();
portToControllerService.put(listenPort, this);
}
Aggregations