@RequestMapping(value = "/user-notifications/{userId}") @ResponseStatus(HttpStatus.OK) @ResponseBody public void watch(@PathVariable("userId") String userId, HttpServletRequest request) throws Exception { //Atmosphere framework puts filter/servlet that adds ATMOSPHERE_RESOURCE to all requests AtmosphereResource resource = (AtmosphereResource)request.getAttribute(ApplicationConfig.ATMOSPHERE_RESOURCE); //suspending resource to keep connection resource.suspend(); //find broadcaster, second parameter says to create broadcaster if it doesn't exist Broadcaster broadcaster = BroadcasterFactory.getDefault().lookup(userId,true); //saving resource for notifications broadcaster.addAtmosphereResource(resource); }
public void notify(User user, Event event){ Broadcaster b = BroadcasterFactory.getDefault().lookup(user.getId()); if (b!=null){ b.broadcast(event); } }
public void destroy() { atmosphere.destroy(); }
AtmosphereResource resource = event.getResource(); resource.getRequest(), service); VaadinSession session = null; AtmospherePushConnection pushConnection = getConnectionForUI(ui); String id = resource.uuid(); new Object[] { id, resource.transport() }); } else { if (!pushMode.isEnabled()) { new Object[] { id, resource.transport() });
/** * Tries to send a critical notification to the client and close the * connection. Does nothing if the connection is already closed. */ private static void sendNotificationAndDisconnect( AtmosphereResource resource, String notificationJson) { // TODO Implemented differently from sendRefreshAndDisconnect try { if (resource instanceof AtmosphereResourceImpl && !((AtmosphereResourceImpl) resource).isInScope()) { // The resource is no longer valid so we should not write // anything to it getLogger().fine( "sendNotificationAndDisconnect called for resource no longer in scope"); return; } resource.getResponse() .setContentType(JsonConstants.JSON_CONTENT_TYPE); resource.getResponse().getWriter().write(notificationJson); resource.resume(); } catch (Exception e) { getLogger().log(Level.FINEST, "Failed to send critical notification to client", e); } }
atmosphere.addAtmosphereHandler("/*", new PushAtmosphereHandler()); atmosphere.addInitParameter(ApplicationConfig.BROADCASTER_CACHE, UUIDBroadcasterCache.class.getName()); atmosphere.addInitParameter(ApplicationConfig.ANNOTATION_PROCESSOR, VoidAnnotationProcessor.class.getName()); atmosphere.addInitParameter(ApplicationConfig.PROPERTY_SESSION_SUPPORT, "true"); atmosphere.addInitParameter(ApplicationConfig.MESSAGE_DELIMITER, String.valueOf(PushConstants.MESSAGE_DELIMITER)); atmosphere.addInitParameter( ApplicationConfig.DROP_ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "false"); atmosphere.addInitParameter(ApplicationConfig.WEBSOCKET_BUFFER_SIZE, bufferSize); atmosphere.addInitParameter(ApplicationConfig.WEBSOCKET_MAXTEXTSIZE, bufferSize); atmosphere.addInitParameter(ApplicationConfig.WEBSOCKET_MAXBINARYSIZE, bufferSize); atmosphere.addInitParameter( ApplicationConfig.PROPERTY_ALLOW_SESSION_TIMEOUT_REMOVAL, "false"); atmosphere.addInitParameter(ApplicationConfig.RECOVER_DEAD_BROADCASTER, "false"); atmosphere.addInitParameter("org.atmosphere.cpr.showSupportMessage", "false");
@Override public void onRequest(AtmosphereResource resource) { if (pushHandler == null) { getLogger().warning( "AtmosphereHandler.onRequest called before PushHandler has been set. This should really not happen"); return; } AtmosphereRequest req = resource.getRequest(); if (req.getMethod().equalsIgnoreCase("GET")) { onConnect(resource); } else if (req.getMethod().equalsIgnoreCase("POST")) { onMessage(resource); } }
/** * Sends the given message to the current client. Cannot be called if * {@isConnected()} is false. * * @param message * The message to send */ protected void sendMessage(String message) { assert (isConnected()); // "Broadcast" the changes to the single client only outgoingMessage = getResource().getBroadcaster().broadcast(message, getResource()); }
atmosphere.getAtmosphereConfig().getInitParameter( com.vaadin.server.Constants.SERVLET_PARAMETER_PUSH_SUSPEND_TIMEOUT_LONGPOLLING, -1)); for (AtmosphereHandlerWrapper handlerWrapper : atmosphere .getAtmosphereHandlers().values()) { AtmosphereHandler handler = handlerWrapper.atmosphereHandler; if (handler instanceof PushAtmosphereHandler) {
@Override public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException { if (!ServletPortletHelper.isPushRequest(request)) { return false; } if (request instanceof VaadinServletRequest) { if (atmosphere == null) { response.sendError(500, "Atmosphere initialization failed. No push available."); return true; } try { atmosphere.doCometSupport( AtmosphereRequestImpl .wrap((VaadinServletRequest) request), AtmosphereResponseImpl .wrap((VaadinServletResponse) response)); } catch (ServletException e) { // TODO PUSH decide how to handle throw new RuntimeException(e); } } else { throw new IllegalArgumentException( "Portlets not currently supported"); } return true; }
/** * Called when a message is received through the push connection * * @since 7.5.0 * @param resource * The related atmosphere resources */ void onMessage(AtmosphereResource resource) { callWithUi(resource, receiveCallback, resource.transport() == TRANSPORT.WEBSOCKET); }
@Override public void onStateChange(AtmosphereResourceEvent event) throws IOException { super.onStateChange(event); if (pushHandler == null) { getLogger().warning( "AtmosphereHandler.onStateChange called before PushHandler has been set. This should really not happen"); return; } if (event.isCancelled() || event.isResumedOnTimeout()) { pushHandler.connectionLost(event); } }
/** * Called when the client sends the first request (to establish a push * connection) * * @param resource */ private void onConnect(AtmosphereResource resource) { resource.addEventListener(new AtmosphereResourceListener()); pushHandler.onConnect(resource); }
@Override public AtmosphereFramework addInitParameter(String name, String value) { if (vaadinServletConfig.getInitParameter(name) == null) { super.addInitParameter(name, value); } return this; } };
@Override public void onThrowable(AtmosphereResourceEvent event) { getLogger().log(Level.SEVERE, "Exception in push connection", event.throwable()); pushHandler.connectionLost(event); } }
@Override public void onDisconnect(AtmosphereResourceEvent event) { // Log event on trace level super.onDisconnect(event); pushHandler.connectionLost(event); }
/** * Reads and buffers a (possibly partial) message. If a complete message was * received, or if the call resulted in the completion of a partially * received message, returns a {@link Reader} yielding the complete message. * Otherwise, returns null. * * @param reader * A Reader from which to read the (partial) message * @return A Reader yielding a complete message or null if the message is * not yet complete. * @throws IOException */ protected Reader receiveMessage(Reader reader) throws IOException { if (resource == null || resource.transport() != TRANSPORT.WEBSOCKET) { return reader; } if (incomingMessage == null) { // No existing partially received message incomingMessage = new FragmentedMessage(reader); } if (incomingMessage.append(reader)) { // Message is complete Reader completeReader = incomingMessage.getReader(); incomingMessage = null; return completeReader; } else { // Only received a partial message return null; } }
@Override public void contextDestroyed(ServletContextEvent sce) { // Destroy any AtmosphereFramework instance we have initialized. // This must be done here to ensure that we cleanup Atmosphere instances // related to servlets which are never initialized ServletContext servletContext = sce.getServletContext(); Enumeration<String> attributeNames = servletContext.getAttributeNames(); while (attributeNames.hasMoreElements()) { String attributeName = attributeNames.nextElement(); if (isAtmosphereFrameworkAttribute(attributeName)) { Object value = servletContext.getAttribute(attributeName); if (value instanceof AtmosphereFramework) { // This might result in calling destroy() twice, once from // here and once from PushRequestHandler but // AtmosphereFramework.destroy() deals with that ((AtmosphereFramework) value).destroy(); } } } }