@Override public void onStateChange(AtmosphereResourceEvent event) throws IOException { AtmosphereResponse response = event.getResource().getResponse(); AtmosphereResource resource = event.getResource(); if (event.isSuspended()) { // Set content type before do response.getWriter() // http://docs.oracle.com/javaee/5/api/javax/servlet/ServletResponse.html#setContentType(java.lang.String) response.setContentType("text/plain; charset=UTF-8"); response.setCharacterEncoding("UTF-8"); if (event.getMessage().getClass().isArray()) { LOG.fine("SEND MESSAGE ARRAY " + event.getMessage().toString()); List<Object> list = Arrays.asList(event.getMessage()); response.getOutputStream().write(MSG_SEPARATOR.getBytes(MSG_CHARSET)); for (Object object : list) { String message = (String) object; message += MSG_SEPARATOR; response.getOutputStream().write(message.getBytes(MSG_CHARSET)); } } else if (event.getMessage() instanceof List) { LOG.fine("SEND MESSAGE LIST " + event.getMessage().toString()); @SuppressWarnings("unchecked") List<Object> list = List.class.cast(event.getMessage()); response.getOutputStream().write(MSG_SEPARATOR.getBytes(MSG_CHARSET)); for (Object object : list) { String message = (String) object; message += MSG_SEPARATOR; response.getOutputStream().write(message.getBytes(MSG_CHARSET)); } } else if (event.getMessage() instanceof String) { LOG.fine("SEND MESSAGE " + event.getMessage().toString()); String message = (String) event.getMessage(); response.getOutputStream().write(message.getBytes(MSG_CHARSET)); } try { response.flushBuffer(); switch (resource.transport()) { case JSONP: case LONG_POLLING: event.getResource().resume(); break; case WEBSOCKET: case STREAMING: case SSE: response.getOutputStream().flush(); break; default: LOG.info("Unknown transport"); break; } } catch (IOException e) { LOG.info("Error resuming resource response", e); } } else if (event.isResuming()) { LOG.fine("RESUMING"); } else if (event.isResumedOnTimeout()) { LOG.fine("RESUMED ON TIMEOUT"); } else if (event.isClosedByApplication() || event.isClosedByClient()) { LOG.fine("CONNECTION CLOSED"); AtmosphereResourceSession resourceSession = AtmosphereResourceSessionFactory.getDefault().getSession(resource); AtmosphereChannel resourceChannel = resourceSession.getAttribute(WAVE_CHANNEL_ATTRIBUTE, AtmosphereChannel.class); if (resourceChannel != null) { resourceChannel.onDisconnect(); } } }
/** * Invoke the {@link AtmosphereHandler#onRequest} method. * * @param req the {@link AtmosphereRequest} * @param res the {@link AtmosphereResponse} * @return action the Action operation. * @throws java.io.IOException * @throws javax.servlet.ServletException */ Action action(AtmosphereRequest req, AtmosphereResponse res) throws IOException, ServletException { boolean webSocketEnabled = false; if (req.getHeaders("Connection") != null && req.getHeaders("Connection").hasMoreElements()) { String[] e = req.getHeaders("Connection").nextElement().toString().split(","); for (String upgrade : e) { if (upgrade.equalsIgnoreCase("Upgrade")) { webSocketEnabled = true; break; } } } if (webSocketEnabled && !supportWebSocket()) { res.setStatus(501); res.addHeader(X_ATMOSPHERE_ERROR, "Websocket protocol not supported"); res.flushBuffer(); return new Action(); } if (config.handlers().isEmpty()) { logger.error( "No AtmosphereHandler found. Make sure you define it inside META-INF/atmosphere.xml"); throw new AtmosphereMappingException( "No AtmosphereHandler found. Make sure you define it insides META-INF/atmosphere.xml"); } if (supportSession()) { // Create the session needed to support the Resume // operation from disparate requests. HttpSession session = req.getSession(true); // Do not allow times out. if (session.getMaxInactiveInterval() == DEFAULT_SESSION_TIMEOUT) { session.setMaxInactiveInterval(-1); } } req.setAttribute(FrameworkConfig.SUPPORT_SESSION, supportSession()); AtmosphereHandlerWrapper handlerWrapper = map(req); // Check Broadcaster state. If destroyed, replace it. Broadcaster b = handlerWrapper.broadcaster; if (b.isDestroyed()) { synchronized (handlerWrapper) { config.getBroadcasterFactory().remove(b, b.getID()); handlerWrapper.broadcaster = config.getBroadcasterFactory().get(b.getID()); } } AtmosphereResourceImpl resource = new AtmosphereResourceImpl( config, handlerWrapper.broadcaster, req, res, this, handlerWrapper.atmosphereHandler); req.setAttribute(FrameworkConfig.ATMOSPHERE_RESOURCE, resource); req.setAttribute(FrameworkConfig.ATMOSPHERE_HANDLER, handlerWrapper.atmosphereHandler); try { handlerWrapper.atmosphereHandler.onRequest(resource); } catch (IOException t) { resource.onThrowable(t); throw t; } if (trackActiveRequest && resource.getAtmosphereResourceEvent().isSuspended() && req.getAttribute(FrameworkConfig.CANCEL_SUSPEND_OPERATION) == null) { req.setAttribute(MAX_INACTIVE, System.currentTimeMillis()); aliveRequests.put(req, resource); } return resource.action(); }