use of org.eclipse.jetty.server.Handler in project jetty.project by eclipse.
the class HandlerWrapper method setHandler.
/* ------------------------------------------------------------ */
/**
* @param handler Set the {@link Handler} which should be wrapped.
*/
public void setHandler(Handler handler) {
if (isStarted())
throw new IllegalStateException(STARTED);
// check for loops
if (handler == this || (handler instanceof HandlerContainer && Arrays.asList(((HandlerContainer) handler).getChildHandlers()).contains(this)))
throw new IllegalStateException("setHandler loop");
if (handler != null)
handler.setServer(getServer());
Handler old = _handler;
_handler = handler;
updateBean(old, _handler, true);
}
use of org.eclipse.jetty.server.Handler in project jetty.project by eclipse.
the class AbstractHandlerContainer method findContainerOf.
/* ------------------------------------------------------------ */
public static <T extends HandlerContainer> T findContainerOf(HandlerContainer root, Class<T> type, Handler handler) {
if (root == null || handler == null)
return null;
Handler[] branches = root.getChildHandlersByClass(type);
if (branches != null) {
for (Handler h : branches) {
@SuppressWarnings("unchecked") T container = (T) h;
Handler[] candidates = container.getChildHandlersByClass(handler.getClass());
if (candidates != null) {
for (Handler c : candidates) if (c == handler)
return container;
}
}
}
return null;
}
use of org.eclipse.jetty.server.Handler in project jetty.project by eclipse.
the class ShutdownHandlerTest method start.
public void start(HandlerWrapper wrapper) throws Exception {
server = new Server();
connector = new ServerConnector(server);
server.addConnector(connector);
Handler shutdown = new ShutdownHandler(shutdownToken);
Handler handler = shutdown;
if (wrapper != null) {
wrapper.setHandler(shutdown);
handler = wrapper;
}
server.setHandler(handler);
server.start();
}
use of org.eclipse.jetty.server.Handler in project jetty.project by eclipse.
the class ServletRequestLogTest method testLogHandlerWrapped.
/**
* Test an alternate (proposed) setup for using RequestLogHandler in a wrapped style
* @throws Exception on test failure
*/
@Test(timeout = 4000)
public void testLogHandlerWrapped() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
server.setConnectors(new Connector[] { connector });
// First the behavior as defined in etc/jetty.xml (as is)
// id="Handlers"
HandlerCollection handlers = new HandlerCollection();
// id="Contexts"
ContextHandlerCollection contexts = new ContextHandlerCollection();
// id="DefaultHandler"
DefaultHandler defaultHandler = new DefaultHandler();
handlers.setHandlers(new Handler[] { contexts, defaultHandler });
server.setHandler(handlers);
// Next the proposed behavioral change to etc/jetty-requestlog.xml
// the id="RequestLog"
RequestLogHandler requestLog = new RequestLogHandler();
CaptureLog captureLog = new CaptureLog();
requestLog.setRequestLog(captureLog);
Handler origServerHandler = server.getHandler();
requestLog.setHandler(origServerHandler);
server.setHandler(requestLog);
// Lastly, the behavior as defined by deployment of a webapp
// Add the Servlet Context
ServletContextHandler app = new ServletContextHandler(ServletContextHandler.SESSIONS);
app.setContextPath("/");
contexts.addHandler(app);
// Add the test servlet
ServletHolder testHolder = new ServletHolder(testServlet);
app.addServlet(testHolder, "/test");
app.addServlet(CustomErrorServlet.class, "/errorpage");
// Add error page mapping
ErrorPageErrorHandler errorMapper = new ErrorPageErrorHandler();
errorMapper.addErrorPage(500, "/errorpage");
app.setErrorHandler(errorMapper);
try {
server.start();
String host = connector.getHost();
if (host == null) {
host = "localhost";
}
int port = connector.getLocalPort();
URI serverUri = new URI("http", null, host, port, "/test", null, null);
// Make call to test handler
HttpURLConnection connection = (HttpURLConnection) serverUri.toURL().openConnection();
try {
connection.setAllowUserInteraction(false);
// log response status code
int statusCode = connection.getResponseCode();
LOG.info("Response Status Code: {}", statusCode);
if (statusCode == 200) {
// collect response message and log it
String content = getResponseContent(connection);
LOG.info("Response Content: {}", content);
}
} finally {
connection.disconnect();
}
assertRequestLog(captureLog);
} finally {
server.stop();
}
}
use of org.eclipse.jetty.server.Handler in project jetty.project by eclipse.
the class XmlConfiguredJetty method getWebAppContexts.
public List<WebAppContext> getWebAppContexts() {
List<WebAppContext> contexts = new ArrayList<>();
HandlerCollection handlers = (HandlerCollection) _server.getHandler();
Handler[] children = handlers.getChildHandlers();
for (Handler handler : children) {
if (handler instanceof WebAppContext) {
WebAppContext context = (WebAppContext) handler;
contexts.add(context);
}
}
return contexts;
}
Aggregations