From the Connection, I can search Jetty Server for the right servlet context to find state set by the servlet (see code below). But this seems a bit fragile and would appreciate it if anybody knows a cleaner solution.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext s = this.getServletContext();
s.setAttribute("foo", "foo value");
...
}
@Override
public Connection newConnection(Connector connector, EndPoint ep) {
ServerConnector sc = (ServerConnector)connector;
System.out.println(findAttribute(sc.getServer(), "foo"));
....
private Object findAttribute(Server server, String name) {
Deque<Handler> handlers = new ArrayDeque<Handler>();
addHandlers(handlers, server.getHandlers());
while (!handlers.isEmpty()) {
Handler h = handlers.pop();
if (h instanceof HandlerContainer) {
addHandlers(handlers, ((HandlerContainer)h).getChildHandlers());
}
if (!(h instanceof WebAppContext)) {
continue;
}
WebAppContext ctx = (WebAppContext)h;
Object o = ctx.getServletContext().getAttribute(name);
if (o != null) {
return o;
}
}
return null;
}
private void addHandlers(Deque<Handler> deque, Handler[] handlers) {
for (Handler h : handlers) {
deque.add(h);
}
}