@SuppressWarnings("unused")
 @Override
 public List<User> getUsers() {
   Collection<ClientSession> sessions = sessionManager.getSessions();
   List<User> list = new ArrayList<User>();
   /**/
   return list;
 }
 /**
  * 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;
 }
  public void sendNotifcationToTags(
      String apiKey, String tag, String title, String message, String uri, boolean save) {

    Set<String> usernameSet = sessionManager.getUsernameSet(tag);
    if (usernameSet == null) {
      return;
    }
    Iterator<String> iterator = usernameSet.iterator();
    while (iterator.hasNext()) {
      String username = (String) iterator.next();
      System.out.println("sendNotifcationToTags--username:" + username);
      sendNotifcationToUser(apiKey, username, title, message, uri, save);
    }
  }
Example #4
0
  /**
   * Delivers the packet to the packet recipient.
   *
   * @param packet the packet to deliver
   * @throws PacketException if the packet is null or the recipient was not found.
   */
  public static void deliver(Packet packet) throws PacketException {
    if (packet == null) {
      throw new PacketException("Packet was null");
    }

    try {
      JID recipient = packet.getTo();
      if (recipient != null) {
        ClientSession clientSession = SessionManager.getInstance().getSession(recipient);
        if (clientSession != null) {
          clientSession.deliver(packet);
        }
      }
    } catch (Exception e) {
      log.error("Could not deliver packet: " + packet.toString(), e);
    }
  }
  /**
   * 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);
    }
  }
Example #6
0
  /**
   * 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);
    }
  }
Example #7
0
  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();
    }
  }
 public List<Map<String, String>> getUserInfo() {
   Collection<ClientSession> sessions = SessionManager.getInstance().getSessions();
   List<Map<String, String>> list = new ArrayList<Map<String, String>>();
   Map<String, String> u = null;
   for (ClientSession s : sessions) {
     if (s.getAuthToken() == null) continue;
     u = new HashMap<String, String>();
     try {
       @SuppressWarnings("unchecked")
       List<UserGroup> groups = ((List<UserGroup>) s.getSessionData("groups"));
       if (groups != null && groups.size() > 0) {
         String gname = "";
         for (UserGroup ug : groups) {
           gname += ug.getSGroupName() + ",";
         }
         if (gname.length() > 0) {
           gname = gname.substring(0, gname.length() - 1);
         }
         u.put("ugroup", gname);
       } else {
         u.put("ugroup", "");
       }
       u.put("username", s.getUsername());
       u.put("ip", s.getHostName());
       u.put("id", s.getStreamID());
       list.add(u);
     } catch (UserNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     } catch (UnknownHostException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
   }
   return list;
 }
 /** Constructor. */
 public NotificationManager() {
   sessionManager = SessionManager.getInstance();
   notificationService = ServiceLocator.getNoticationService();
   userService = ServiceLocator.getUserService();
 }
 public void sendNotifcationToAlias(
     String apiKey, String alias, String title, String message, String uri, boolean save) {
   String username = sessionManager.getUsernameByAlias(alias);
   System.out.println("sendNotifcationToAlias--username:" + username);
   sendNotifcationToUser(apiKey, username, title, message, uri, save);
 }
Example #12
0
 /** Constucts a packet router. */
 public PresenceRouter() {
   sessionManager = SessionManager.getInstance();
   presenceUpdateHandler = new PresenceUpdateHandler();
 }
 private void shutdownServer() {
   shuttingDown = true;
   // Close all connections
   SessionManager.getInstance().closeAllSessions();
   log.info("XmppServer stopped");
 }
 /**
  * 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;
 }
 /** Constructor. */
 public PresenceManager() {
   sessionManager = SessionManager.getInstance();
 }
 public AndroidUserServiceImpl() {
   sessionManager = SessionManager.getInstance();
 }