/** * Returns the current presence of the user. * * @param user the user * @return the current presence of the user. */ public Presence getPresence(ApnUser user) { if (user == null) { return null; } Presence presence = null; ClientSession session = sessionManager.getSession(user.getUsername()); if (session != null) { presence = session.getPresence(); } return presence; }
/** * Broadcasts a newly created notification message to all connected users. * * @param apiKey the API key * @param title the title * @param message the message details * @param uri the uri */ public void sendBroadcast(String apiKey, String title, String message, String uri) { log.debug("sendBroadcast()..."); List<User> users = userService.getUsers(); for (User user : users) { ClientSession session = sessionManager.getSession(user.getUsername()); Random random = new Random(); String id = Integer.toHexString(random.nextInt()); IQ notificationIQ = createNotificationIQ(id, apiKey, title, message, uri); if (session != null && session.getPresence().isAvailable()) { notificationIQ.setTo(session.getAddress()); session.deliver(notificationIQ); } saveNotification(id, apiKey, user.getUsername(), title, message, uri); } }
/** * Routes the Presence packet. * * @param packet the packet to route */ public void route(Presence packet) { if (packet == null) { throw new NullPointerException(); } // 根据发送包的jid查询建立连接的会话(session) ClientSession session = sessionManager.getSession(packet.getFrom()); // 当没有建立连接且状态为非连接状态,进行Presence包处理 if (session == null || session.getStatus() != Session.STATUS_CONNECTED) { handle(packet); } else { // 返回未授权错误 packet.setTo(session.getAddress()); packet.setFrom((JID) null); packet.setError(PacketError.Condition.not_authorized); session.process(packet); } }
private void handle(Presence packet) { try { // 取得信息包类型 Presence.Type type = packet.getType(); // Presence updates (null == 'available') if (type == null || Presence.Type.unavailable == type) { presenceUpdateHandler.process(packet); } else { log.warn("Unknown presence type"); } } catch (Exception e) { log.error("Could not route packet", e); // 出现异常关闭会话 Session session = sessionManager.getSession(packet.getFrom()); if (session != null) { session.close(); } } }
/** * Sends a newly created notification message to the specific user. * * @param apiKey the API key * @param title the title * @param message the message details * @param uri the uri */ public void sendNotifcationToUser( String apiKey, String username, String title, String message, String uri, boolean save) { log.debug("sendNotifcationToUser()..."); Random random = new Random(); String id = Integer.toHexString(random.nextInt()); IQ notificationIQ = createNotificationIQ(id, apiKey, title, message, uri); ClientSession session = sessionManager.getSession(username); if (session != null) { if (session.getPresence().isAvailable()) { notificationIQ.setTo(session.getAddress()); session.deliver(notificationIQ); } } try { User user = userService.getUserByUsername(username); if (user != null && save) { saveNotification(id, apiKey, username, title, message, uri); } } catch (UserNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
/** * Returns the availability of the user. * * @param user the user * @return true if the user is available */ public boolean isAvailable(ApnUser user) { return sessionManager.getSession(user.getUsername()) != null; }