use of org.eclipse.jetty.server.handler.ContextHandler in project cxf by apache.
the class JettyHTTPServerEngine method addServant.
/**
* Register a servant.
*
* @param url the URL associated with the servant
* @param handler notified on incoming HTTP requests
*/
public synchronized void addServant(URL url, JettyHTTPHandler handler) {
if (shouldCheckUrl(handler.getBus())) {
checkRegistedContext(url);
}
initializeContexts();
SecurityHandler securityHandler = null;
if (server == null) {
DefaultHandler defaultHandler = null;
// create a new jetty server instance if there is no server there
server = createServer();
addServerMBean();
if (connector == null) {
connector = createConnector(getHost(), getPort(), handler.getBus());
if (LOG.isLoggable(Level.FINER)) {
logConnector((ServerConnector) connector);
}
}
server.addConnector(connector);
setupThreadPool();
/*
* The server may have no handler, it might have a collection handler,
* it might have a one-shot. We need to add one or more of ours.
*
*/
int numberOfHandlers = 1;
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.isEmpty()) {
for (Handler h : handlers) {
// 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);
}
}
}
}
/*
* 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);
}
try {
server.start();
} catch (Exception e) {
LOG.log(Level.SEVERE, "START_UP_SERVER_FAILED_MSG", new Object[] { e.getMessage(), port });
// problem starting server
try {
server.stop();
server.destroy();
} catch (Exception ex) {
// ignore - probably wasn't fully started anyway
}
server = null;
throw new Fault(new Message("START_UP_SERVER_FAILED_MSG", LOG, e.getMessage(), port), e);
}
}
String contextName = HttpUriMapper.getContextName(url.getPath());
ContextHandler context = new ContextHandler();
context.setContextPath(contextName);
// bind the jetty http handler with the context handler
if (isSessionSupport) {
SessionHandler sh = configureSession();
if (securityHandler != null) {
// use the securityHander which already wrap the jetty http handler
sh.setHandler(securityHandler);
} else {
sh.setHandler(handler);
}
context.setHandler(sh);
} else {
// otherwise, just the one.
if (securityHandler != null) {
// use the securityHander which already wrap the jetty http handler
context.setHandler(securityHandler);
} else {
context.setHandler(handler);
}
}
contexts.addHandler(context);
ServletContext sc = context.getServletContext();
handler.setServletContext(sc);
final String smap = getHandlerName(url, context);
handler.setName(smap);
if (contexts.isStarted()) {
try {
context.start();
} catch (Exception ex) {
LOG.log(Level.WARNING, "ADD_HANDLER_FAILED_MSG", new Object[] { ex.getMessage() });
}
}
registedPaths.add(url.getPath());
++servantCount;
}
use of org.eclipse.jetty.server.handler.ContextHandler in project cxf by apache.
the class JettyHTTPServerEngineTest method testGetContextHandler.
@Test
public void testGetContextHandler() throws Exception {
String urlStr = "http://localhost:" + PORT1 + "/hello/test";
JettyHTTPServerEngine engine = factory.createJettyHTTPServerEngine(PORT1, "http");
ContextHandler contextHandler = engine.getContextHandler(new URL(urlStr));
// can't find the context handler here
assertNull(contextHandler);
JettyHTTPTestHandler handler1 = new JettyHTTPTestHandler("string1", true);
JettyHTTPTestHandler handler2 = new JettyHTTPTestHandler("string2", true);
engine.addServant(new URL(urlStr), handler1);
// Note: There appears to be an internal issue in Jetty that does not
// unregister the MBean for handler1 during this setHandler operation.
// This scenario may create a warning message in the logs
// (javax.management.InstanceAlreadyExistsException: org.apache.cxf.
// transport.http_jetty:type=jettyhttptesthandler,id=0)
// when running subsequent tests.
contextHandler = engine.getContextHandler(new URL(urlStr));
contextHandler.stop();
contextHandler.setHandler(handler2);
contextHandler.start();
String response = getResponse(urlStr);
assertEquals("the jetty http handler did not take effect", response, "string2");
JettyHTTPServerEngineFactory.destroyForPort(PORT1);
}
use of org.eclipse.jetty.server.handler.ContextHandler in project cxf by apache.
the class JettyHTTPServerEngineTest method testJettyHTTPHandler.
@Test
public void testJettyHTTPHandler() throws Exception {
String urlStr1 = "http://localhost:" + PORT3 + "/hello/test1";
String urlStr2 = "http://localhost:" + PORT3 + "/hello/test2";
JettyHTTPServerEngine engine = factory.createJettyHTTPServerEngine(PORT3, "http");
ContextHandler contextHandler = engine.getContextHandler(new URL(urlStr1));
// can't find the context handler here
assertNull(contextHandler);
JettyHTTPHandler handler1 = new JettyHTTPTestHandler("test", false);
JettyHTTPHandler handler2 = new JettyHTTPTestHandler("test2", false);
engine.addServant(new URL(urlStr1), handler1);
contextHandler = engine.getContextHandler(new URL(urlStr1));
assertNotNull(contextHandler);
engine.addServant(new URL(urlStr2), handler2);
contextHandler = engine.getContextHandler(new URL(urlStr2));
assertNotNull(contextHandler);
String response = getResponse(urlStr1 + "/test");
assertEquals("the jetty http handler did not take effect", response, "test");
response = getResponse(urlStr2 + "/test");
assertEquals("the jetty http handler did not take effect", response, "test2");
JettyHTTPServerEngineFactory.destroyForPort(PORT3);
}
use of org.eclipse.jetty.server.handler.ContextHandler in project jetty.project by eclipse.
the class ManyContexts method main.
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
ContextHandler context = new ContextHandler("/");
context.setContextPath("/");
context.setHandler(new HelloHandler("Root Hello"));
ContextHandler contextFR = new ContextHandler("/fr");
contextFR.setHandler(new HelloHandler("Bonjoir"));
ContextHandler contextIT = new ContextHandler("/it");
contextIT.setHandler(new HelloHandler("Bongiorno"));
ContextHandler contextV = new ContextHandler("/");
contextV.setVirtualHosts(new String[] { "127.0.0.2" });
contextV.setHandler(new HelloHandler("Virtual Hello"));
ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.setHandlers(new Handler[] { context, contextFR, contextIT, contextV });
server.setHandler(contexts);
server.start();
server.join();
}
use of org.eclipse.jetty.server.handler.ContextHandler in project jetty.project by eclipse.
the class ServerProxyImpl method configureHandlers.
/**
*
*/
private void configureHandlers() {
RequestLogHandler requestLogHandler = new RequestLogHandler();
if (requestLog != null)
requestLogHandler.setRequestLog(requestLog);
contexts = (ContextHandlerCollection) server.getChildHandlerByClass(ContextHandlerCollection.class);
if (contexts == null) {
contexts = new ContextHandlerCollection();
HandlerCollection handlers = (HandlerCollection) server.getChildHandlerByClass(HandlerCollection.class);
if (handlers == null) {
handlers = new HandlerCollection();
server.setHandler(handlers);
handlers.setHandlers(new Handler[] { contexts, new DefaultHandler(), requestLogHandler });
} else {
handlers.addHandler(contexts);
}
}
//if there are any extra contexts to deploy
if (contextHandlers != null && contextHandlers.getContextHandlers() != null) {
for (ContextHandler c : contextHandlers.getContextHandlers()) contexts.addHandler(c);
}
}
Aggregations