use of org.eclipse.jetty.server.handler.RequestLogHandler in project jetty.project by eclipse.
the class ServletRequestLogTest method testLogHandlerCollection.
/**
* Test a RequestLogHandler at the end of a HandlerCollection.
* This handler chain is setup to look like Jetty versions up to 9.2.
* Default configuration.
* @throws Exception on test failure
*/
@Test(timeout = 4000)
public void testLogHandlerCollection() 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
// 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 behavior as defined by etc/jetty-requestlog.xml
// the id="RequestLog"
RequestLogHandler requestLog = new RequestLogHandler();
CaptureLog captureLog = new CaptureLog();
requestLog.setRequestLog(captureLog);
handlers.addHandler(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");
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();
}
assertRequestLog(captureLog);
} finally {
server.stop();
}
}
use of org.eclipse.jetty.server.handler.RequestLogHandler in project jetty.project by eclipse.
the class ServletRequestLogTest method testLogHandlerCollection_ErrorHandler_ServerBean.
/**
* Test a RequestLogHandler at the end of a HandlerCollection.
* and also with the default ErrorHandler as server bean in place.
* @throws Exception on test failure
*/
@Test(timeout = 4000)
public void testLogHandlerCollection_ErrorHandler_ServerBean() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
server.setConnectors(new Connector[] { connector });
ErrorHandler errorHandler = new ErrorHandler();
server.addBean(errorHandler);
// First the behavior as defined in etc/jetty.xml
// 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 behavior as defined by etc/jetty-requestlog.xml
// the id="RequestLog"
RequestLogHandler requestLog = new RequestLogHandler();
CaptureLog captureLog = new CaptureLog();
requestLog.setRequestLog(captureLog);
handlers.addHandler(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");
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();
}
assertRequestLog(captureLog);
} finally {
server.stop();
}
}
use of org.eclipse.jetty.server.handler.RequestLogHandler in project jetty.project by eclipse.
the class ServletRequestLogTest method testLogHandlerCollection_SimpleErrorPageMapping.
/**
* Test a RequestLogHandler at the end of a HandlerCollection
* using servlet specific error page mapping.
* @throws Exception on test failure
*/
@Test(timeout = 4000)
public void testLogHandlerCollection_SimpleErrorPageMapping() 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
// 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 behavior as defined by etc/jetty-requestlog.xml
// the id="RequestLog"
RequestLogHandler requestLog = new RequestLogHandler();
CaptureLog captureLog = new CaptureLog();
requestLog.setRequestLog(captureLog);
handlers.addHandler(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, 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();
}
assertRequestLog(captureLog);
} finally {
server.stop();
}
}
use of org.eclipse.jetty.server.handler.RequestLogHandler in project druid by druid-io.
the class JettyServerInitUtils method getJettyRequestLogHandler.
public static Handler getJettyRequestLogHandler() {
// Ref: http://www.eclipse.org/jetty/documentation/9.2.6.v20141205/configuring-jetty-request-logs.html
RequestLogHandler requestLogHandler = new RequestLogHandler();
requestLogHandler.setRequestLog(new JettyRequestLog());
return requestLogHandler;
}
use of org.eclipse.jetty.server.handler.RequestLogHandler in project hbase by apache.
the class HttpServer method initializeWebServer.
private void initializeWebServer(String name, String hostName, Configuration conf, String[] pathSpecs) throws FileNotFoundException, IOException {
Preconditions.checkNotNull(webAppContext);
HandlerCollection handlerCollection = new HandlerCollection();
ContextHandlerCollection contexts = new ContextHandlerCollection();
RequestLog requestLog = HttpRequestLog.getRequestLog(name);
if (requestLog != null) {
RequestLogHandler requestLogHandler = new RequestLogHandler();
requestLogHandler.setRequestLog(requestLog);
handlerCollection.addHandler(requestLogHandler);
}
final String appDir = getWebAppsPath(name);
handlerCollection.addHandler(contexts);
handlerCollection.addHandler(webAppContext);
webServer.setHandler(handlerCollection);
addDefaultApps(contexts, appDir, conf);
addGlobalFilter("safety", QuotingInputFilter.class.getName(), null);
Map<String, String> params = new HashMap<>();
params.put("xframeoptions", conf.get("hbase.http.filter.xframeoptions.mode", "DENY"));
addGlobalFilter("clickjackingprevention", ClickjackingPreventionFilter.class.getName(), params);
final FilterInitializer[] initializers = getFilterInitializers(conf);
if (initializers != null) {
conf = new Configuration(conf);
conf.set(BIND_ADDRESS, hostName);
for (FilterInitializer c : initializers) {
c.initFilter(this, conf);
}
}
addDefaultServlets();
if (pathSpecs != null) {
for (String path : pathSpecs) {
LOG.info("adding path spec: " + path);
addFilterPathMapping(path, webAppContext);
}
}
}
Aggregations