use of org.eclipse.jetty.server.Handler in project jetty.project by eclipse.
the class RequestLogHandlerTest method testMultipleLogHandlers.
@Test(timeout = 4000)
public void testMultipleLogHandlers() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
server.setConnectors(new Connector[] { connector });
List<CaptureLog> captureLogs = new ArrayList<>();
List<Handler> handlerList = new ArrayList<>();
handlerList.add(testHandler);
for (int i = 0; i < 4; ++i) {
CaptureLog captureLog = new CaptureLog();
captureLogs.add(captureLog);
RequestLogHandler requestLog = new RequestLogHandler();
requestLog.setRequestLog(captureLog);
handlerList.add(requestLog);
}
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(handlerList.toArray(new Handler[0]));
server.setHandler(handlers);
try {
server.start();
String host = connector.getHost();
if (host == null) {
host = "localhost";
}
int port = connector.getLocalPort();
URI serverUri = new URI("http", null, host, port, requestPath, 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.debug("Response Status Code: {}", statusCode);
if (statusCode == 200) {
// collect response message and log it
String content = getResponseContent(connection);
LOG.debug("Response Content: {}", content);
}
} finally {
connection.disconnect();
}
for (CaptureLog captureLog : captureLogs) assertRequestLog(captureLog);
} finally {
server.stop();
}
}
use of org.eclipse.jetty.server.Handler in project jetty.project by eclipse.
the class JettyHttpServer method checkIfContextIsFree.
private void checkIfContextIsFree(String path) {
Handler serverHandler = _server.getHandler();
if (serverHandler instanceof ContextHandler) {
ContextHandler ctx = (ContextHandler) serverHandler;
if (ctx.getContextPath().equals(path))
throw new RuntimeException("another context already bound to path " + path);
}
Handler[] handlers = _server.getHandlers();
if (handlers == null)
return;
for (Handler handler : handlers) {
if (handler instanceof ContextHandler) {
ContextHandler ctx = (ContextHandler) handler;
if (ctx.getContextPath().equals(path))
throw new RuntimeException("another context already bound to path " + path);
}
}
}
use of org.eclipse.jetty.server.Handler in project jetty.project by eclipse.
the class JettyHttpServer method findContextHandlerCollection.
private ContextHandlerCollection findContextHandlerCollection(Handler[] handlers) {
if (handlers == null)
return null;
for (Handler handler : handlers) {
if (handler instanceof ContextHandlerCollection) {
return (ContextHandlerCollection) handler;
}
if (handler instanceof HandlerCollection) {
HandlerCollection hc = (HandlerCollection) handler;
ContextHandlerCollection chc = findContextHandlerCollection(hc.getHandlers());
if (chc != null)
return chc;
}
}
return null;
}
use of org.eclipse.jetty.server.Handler in project jetty.project by eclipse.
the class Invoker method init.
/* ------------------------------------------------------------ */
public void init() {
ServletContext config = getServletContext();
_contextHandler = ((ContextHandler.Context) config).getContextHandler();
Handler handler = _contextHandler.getHandler();
while (handler != null && !(handler instanceof ServletHandler) && (handler instanceof HandlerWrapper)) handler = ((HandlerWrapper) handler).getHandler();
_servletHandler = (ServletHandler) handler;
Enumeration<String> e = getInitParameterNames();
while (e.hasMoreElements()) {
String param = e.nextElement();
String value = getInitParameter(param);
String lvalue = value.toLowerCase(Locale.ENGLISH);
if ("nonContextServlets".equals(param)) {
_nonContextServlets = value.length() > 0 && lvalue.startsWith("t");
}
if ("verbose".equals(param)) {
_verbose = value.length() > 0 && lvalue.startsWith("t");
} else {
if (_parameters == null)
_parameters = new HashMap<String, String>();
_parameters.put(param, value);
}
}
}
use of org.eclipse.jetty.server.Handler in project jetty.project by eclipse.
the class ServletContextHandler method setSessionHandler.
/* ------------------------------------------------------------ */
/**
* @param sessionHandler The sessionHandler to set.
*/
public void setSessionHandler(SessionHandler sessionHandler) {
if (isStarted())
throw new IllegalStateException("STARTED");
Handler next = null;
if (_sessionHandler != null) {
next = _sessionHandler.getHandler();
_sessionHandler.setHandler(null);
replaceHandler(_sessionHandler, sessionHandler);
}
_sessionHandler = sessionHandler;
if (next != null && _sessionHandler.getHandler() == null)
_sessionHandler.setHandler(next);
relinkHandlers();
}
Aggregations