use of org.glassfish.sse.api.ServerSentEventHandler in project Payara by payara.
the class ServerSentEventServlet method doGet.
@Override
@SuppressWarnings("unchecked")
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO needs to be injected
if (extension == null) {
throw new RuntimeException("Enable CDI by including empty WEB-INF/beans.xml");
}
Map<String, ServerSentEventApplication> applicationMap = extension.getApplicationMap();
ServerSentEventApplication sseApp = applicationMap.get(req.getServletPath());
Class<?> clazz = sseApp.getHandlerClass();
ServerSentEventHandler sseh;
CreationalContext cc;
// Check if SSE handler can be instantiated via CDI
Iterator<Bean<?>> it = bm.getBeans(clazz).iterator();
if (it.hasNext()) {
Bean bean = it.next();
cc = bm.createCreationalContext(bean);
sseh = (ServerSentEventHandler) bean.create(cc);
} else {
throw new RuntimeException("Cannot create ServerSentEventHandler using CDI");
}
ServerSentEventHandler.Status status = sseh.onConnecting(req);
if (status == ServerSentEventHandler.Status.DONT_RECONNECT) {
resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
cc.release();
return;
}
if (status != ServerSentEventHandler.Status.OK) {
throw new RuntimeException("Internal Error: need to handle status " + status);
}
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType("text/event-stream");
// writes status code and headers
resp.flushBuffer();
AsyncContext ac = req.startAsync(req, resp);
// no timeout. need config ?
ac.setTimeout(0);
ServerSentEventConnectionImpl con = sseApp.createConnection(req, sseh, cc, ac);
ac.addListener(con);
con.init();
}
Aggregations