/**
  * 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;
 }
  /**
   * 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);
    }
  }
Exemple #4
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);
    }
  }
  /**
   * 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;
 }