Package tigase.server

Examples of tigase.server.Packet


      String id = JIDUtils.getNodeID(packet.getElemTo());

      if (id.equals(session.getUserId())) {
        // Yes this is message to 'this' client
        Element elem = packet.getElement().clone();
        Packet result = new Packet(elem);
        result.setTo(session.getConnectionId());
        result.setFrom(packet.getTo());
        results.offer(result);
      } else {
        // This is message to some other client
        Element result = packet.getElement().clone();
        results.offer(new Packet(result));
      } // end of else
    } catch (NotAuthorizedException e) {
      log.warning("NotAuthorizedException for packet: "  + packet.getStringData());
      results.offer(Authorization.NOT_AUTHORIZED.getResponseMessage(packet,
          "You must authorize session first.", true));
View Full Code Here


            ElementUtils.createIqQuery(session.getDomain(), session.getJID(),
              StanzaType.result, packet.getElemId(), XMLNS);
          Element query = iq.getChild("query");
          Element stats = Command.getData(packet, "statistics", null);
          query.addChildren(stats.getChildren());
          Packet result = new Packet(iq);
          result.setTo(session.getConnectionId(packet.getElemTo()));
          results.offer(result);
          log.finest("Sending result: " + result.getStringData());
          return;
        } else {
          return;
        }
      } // end of if (packet.isCommand()


      // Maybe it is message to admininstrator:
      String id = packet.getElemTo() != null ?
        JIDUtils.getNodeID(packet.getElemTo()) : null;

      // If ID part of user account contains only host name
      // and this is local domain it is message to admin
      if (id == null || id.equals("")
        || id.equalsIgnoreCase(session.getDomain())) {
        Packet result =
          Command.GETSTATS.getPacket(packet.getElemFrom(),
          session.getDomain(), StanzaType.get, packet.getElemId());
        results.offer(result);
        log.finest("Sending result: " + result.getStringData());
        return;
      }

      if (id.equals(session.getUserId())) {
        // Yes this is message to 'this' client
        Element elem = packet.getElement().clone();
        Packet result = new Packet(elem);
        result.setTo(session.getConnectionId(packet.getElemTo()));
        result.setFrom(packet.getTo());
        results.offer(result);
        log.finest("Sending result: " + result.getStringData());
      } else {
        // This is message to some other client so I need to
        // set proper 'from' attribute whatever it is set to now.
        // Actually processor should not modify request but in this
        // case it is absolutely safe and recommended to set 'from'
        // attribute
        Element el_res = packet.getElement().clone();
        // Not needed anymore. Packet filter does it for all stanzas.
//         // According to spec we must set proper FROM attribute
//         el_res.setAttribute("from", session.getJID());
        Packet result = new Packet(el_res);
        results.offer(result);
        log.finest("Sending result: " + result.getStringData());
      } // end of else
    } catch (NotAuthorizedException e) {
      log.warning(
        "Received stats request but user session is not authorized yet: " +
        packet.getStringData());
View Full Code Here

      String id = JIDUtils.getNodeID(packet.getElemTo());

      if (id.equals(session.getUserId())) {
        // Yes this is message to 'this' client
        Element elem = packet.getElement().clone();
        Packet result = new Packet(elem);
        result.setTo(session.getConnectionId(packet.getElemTo()));
        result.setFrom(packet.getTo());
        results.offer(result);
      } else {
        // This is message to some other client
        Element result = packet.getElement().clone();
        results.offer(new Packet(result));
      } // end of else
    } catch (NotAuthorizedException e) {
      log.warning("NotAuthorizedException for packet: "  + packet.getStringData());
      results.offer(Authorization.NOT_AUTHORIZED.getResponseMessage(packet,
          "You must authorize session first.", true));
View Full Code Here

          (Set<String>)session.getSessionData(DIRECT_PRESENCE);
        if (direct_presences != null) {
          try {
            for (String buddy: direct_presences) {
              String peer = JIDUtils.getNodeID(buddy);
              Packet roster_update =
                new Packet(JabberIqRoster.createRosterPacket("set",
                  session.nextStanzaId(), peer, peer, session.getUserId(), null,
                  null, "remove", JabberIqRoster.ANON));
              results.offer(roster_update);
            } // end of for (String buddy: buddies)
          } catch (NotAuthorizedException e) {
View Full Code Here

        Element pres_update = new Element(PRESENCE_ELEMENT_NAME);
        pres_update.setAttribute("from", session.getJID());
        pres_update.setAttribute("to", conn.getJID());
        pres_update.setAttribute("type", StanzaType.unavailable.toString());
        pres_update.setXMLNS(XMLNS);
        Packet pack_update = new Packet(pres_update);
        pack_update.setTo(conn.getConnectionId());
        results.offer(pack_update);
      } else {
        log.finer("Skipping presence update to: " + conn.getJID());
      } // end of else
    } // end of for (XMPPResourceConnection conn: sessions)
View Full Code Here

      if (conn != session && conn.isResourceSet()) {
        // Send to new resource presence about old resource
        Element pres_update = presence.clone();
        pres_update.setAttribute("from", session.getJID());
        pres_update.setAttribute("to", conn.getJID());
        Packet pack_update = new Packet(pres_update);
        pack_update.setTo(conn.getConnectionId());
        results.offer(pack_update);
        Element presence_el = (Element)conn.getSessionData(PRESENCE_KEY);
        if (presence_el != null) {
          pres_update = presence_el.clone();
          pres_update.setAttribute("to", session.getJID());
          pres_update.setAttribute("from", conn.getJID());
          pack_update = new Packet(pres_update);
          pack_update.setTo(session.getConnectionId());
          results.offer(pack_update);
        }
      } else {
        log.finer("Skipping presence update to: " + conn.getJID());
      } // end of else
View Full Code Here

        // resources which already sent initial presence.
        log.finer("Update presence change to: " + conn.getJID());
        // Send to old resource presence about new resource
        Element pres_update = presence.clone();
        pres_update.setAttribute("to", conn.getJID());
        Packet pack_update = new Packet(pres_update);
        pack_update.setTo(conn.getConnectionId());
        results.offer(pack_update);
      } else {
        // Ignore....
        log.finest("Skipping update presence change for a resource which hasn't sent initial presence yet.");
      }
View Full Code Here

     // According to spec we must set proper FROM attribute
    // Yes, but packet filter put full JID and we need a subscription
    // presence without resource here.
    result.setAttribute("from", from);
    log.finest("\n\nFORWARD presence: " + result.toString());
    results.offer(new Packet(result));
  }
View Full Code Here

      presence = pres.clone();
    } // end of if (pres == null) else
    presence.setAttribute("to", to);
    presence.setAttribute("from", from);
    presence.setXMLNS(XMLNS);
    Packet packet = new Packet(presence);
    log.finest("Sending presence info: " + packet.getStringData());
    results.offer(packet);
  }
View Full Code Here

              String peer = JIDUtils.getNodeID(packet.getElemTo());
              String nick = packet.getElemCData("/presence/nick");
              if (nick == null) {
                nick = session.getUserName();
              }
              Packet rost_update =
                new Packet(JabberIqRoster.createRosterPacket("set",
                    session.nextStanzaId(), peer, peer, session.getUserId(),
                    nick, "Anonymous peers", null,
                    JabberIqRoster.ANON));
              results.offer(rost_update);
              log.finest("Sending roster update: " + rost_update.toString());
            }
            Element result = packet.getElement().clone();
            results.offer(new Packet(result));
            // If this is unavailable presence, remove jid from Set
            // otherwise add it to the Set
            if (packet.getType() != null &&
              packet.getType() == StanzaType.unavailable) {
              removeDirectPresenceJID(packet.getElemTo(), session);
            } else {
              addDirectPresenceJID(packet.getElemTo(), session);
            }
          } else {
            boolean first = false;
            if (session.getSessionData(PRESENCE_KEY) == null) {
              first = true;
            }

            // Store user presence for later time...
            // To send response to presence probes for example.
            session.putSessionData(PRESENCE_KEY, packet.getElement());

            // Parse resource priority:
            String priority = packet.getElemCData("/presence/priority");
            if (priority != null) {
              int pr = 1;
              try {
                pr = Integer.decode(priority);
              } catch (NumberFormatException e) {
                log.finer("Incorrect priority value: " + priority
                  + ", setting 1 as default.");
                pr = 1;
              }
              session.setPriority(pr);
            }

            // Special actions on the first availability presence
            if (first && type == StanzaType.available) {
              // Send presence probes to 'to' or 'both' contacts
              sendPresenceBroadcast(StanzaType.probe, session, TO_SUBSCRIBED,
                results, null, settings);
              // Resend pending in subscription requests
              resendPendingInRequests(session, results);
            } // end of if (type == StanzaType.available)

            // Broadcast initial presence to 'from' or 'both' contacts
            sendPresenceBroadcast(type, session, FROM_SUBSCRIBED,
              results, packet.getElement(), settings);

            // Broadcast initial presence to other available user resources
            //        Element presence = packet.getElement().clone();
            // Already done above, don't need to set it again here
            // presence.setAttribute("from", session.getJID());
            updateUserResources(packet.getElement(), session, results);
          }
          break;
        case out_subscribe:
        case out_unsubscribe:
          if (pres_type == PresenceType.out_subscribe) {
            SubscriptionType current_subscription =
              roster_util.getBuddySubscription(session, packet.getElemTo());
            if (current_subscription == null) {
              roster_util.addBuddy(session, packet.getElemTo(), null, null);
            } // end of if (current_subscription == null)
          }
          subscr_changed = roster_util.updateBuddySubscription(session, pres_type,
            packet.getElemTo());
          if (subscr_changed) {
            roster_util.updateBuddyChange(session, results,
              roster_util.getBuddyItem(session, packet.getElemTo()));
          } // end of if (subscr_changed)
          // According to RFC-3921 I must forward all these kind presence
          // requests, it allows to resynchronize
          // subscriptions in case of synchronization loss
          forwardPresence(results, packet, session.getUserId());
          break;
        case out_subscribed:
        case out_unsubscribed:
          forwardPresence(results, packet, session.getUserId());
          subscr_changed = roster_util.updateBuddySubscription(session, pres_type,
            packet.getElemTo());
          if (subscr_changed) {
            roster_util.updateBuddyChange(session, results,
              roster_util.getBuddyItem(session, packet.getElemTo()));
            if (pres_type == PresenceType.out_subscribed) {
              Element presence = (Element)session.getSessionData(PRESENCE_KEY);
              if (presence != null) {
                sendPresence(null, packet.getElemTo(), session.getJID(),
                  results, presence);
              } else {
                sendPresence(StanzaType.available, packet.getElemTo(),
                  session.getJID(), results, null);
              }
            } else {
              sendPresence(StanzaType.unavailable, packet.getElemTo(),
                session.getJID(), results, null);
            }
          } // end of if (subscr_changed)
          break;
        case in_initial:
          if (packet.getElemFrom() == null) {
            // That really happened already. It looks like a bug in tigase
            // let's try to catch it here....
            log.warning("Initial presence without from attribute set: "
              + packet.toString());
            return;
          }
          // If other users are in 'to' or 'both' contacts, broadcast
          // their preseces to all active resources
          if (roster_util.isSubscribedTo(session, packet.getElemFrom())
            || (DynamicRoster.getBuddyItem(session, settings,
                packet.getElemFrom()) != null)) {
            updatePresenceChange(packet.getElement(), session, results);
          } else {
            // The code below looks like a bug to me.
            // If the buddy is nt subscribed I should ignore all presences
            // states from him. Commenting this out for now....
            // Well, it is not a bug and it is intentional.
            // All presences received from MUC come from not subscribed buddies
            // therefore it seems presences from unknown buddy should be passed out
            Element elem = packet.getElement().clone();
            Packet result = new Packet(elem);
            result.setTo(session.getConnectionId());
            result.setFrom(packet.getTo());
            results.offer(result);
          }
          break;
        case in_subscribe:
          // If the buddy is already subscribed then auto-reply with sybscribed
          // presence stanza.
          if (roster_util.isSubscribedFrom(session, packet.getElemFrom())) {
            sendPresence(StanzaType.subscribed, packet.getElemFrom(),
              session.getJID(), results, null);
          } else {
            SubscriptionType curr_sub =
              roster_util.getBuddySubscription(session, packet.getElemFrom());
            if (curr_sub == null) {
              curr_sub = SubscriptionType.none;
              roster_util.addBuddy(session, packet.getElemFrom(), null, null);
            } // end of if (curr_sub == null)
            roster_util.updateBuddySubscription(session, pres_type,
              packet.getElemFrom());
            updatePresenceChange(packet.getElement(), session, results);
          } // end of else
          break;
        case in_unsubscribe:
          subscr_changed = roster_util.updateBuddySubscription(session, pres_type,
            packet.getElemFrom());
          if (subscr_changed) {
            // No longer needed according to RFC-3921-bis5
            //sendPresence(StanzaType.unsubscribed, packet.getElemFrom(),
            //  session.getJID(), results, null);
            //updatePresenceChange(packet.getElement(), session, results);
            roster_util.updateBuddyChange(session, results,
              roster_util.getBuddyItem(session, packet.getElemFrom()));
          }
          break;
        case in_subscribed: {
          SubscriptionType curr_sub =
            roster_util.getBuddySubscription(session, packet.getElemFrom());
          if (curr_sub == null) {
            curr_sub = SubscriptionType.none;
            roster_util.addBuddy(session, packet.getElemFrom(), null, null);
          } // end of if (curr_sub == null)
          subscr_changed = roster_util.updateBuddySubscription(session, pres_type,
            packet.getElemFrom());
          if (subscr_changed) {
            //updatePresenceChange(packet.getElement(), session, results);
            roster_util.updateBuddyChange(session, results,
              roster_util.getBuddyItem(session, packet.getElemFrom()));
          }
        }
          break;
        case in_unsubscribed: {
          SubscriptionType curr_sub =
            roster_util.getBuddySubscription(session, packet.getElemFrom());
          if (curr_sub != null) {
            subscr_changed = roster_util.updateBuddySubscription(session, pres_type,
              packet.getElemFrom());
            if (subscr_changed) {
              // No longer needed according to RFC-3921-bis5
              //updatePresenceChange(packet.getElement(), session, results);
              roster_util.updateBuddyChange(session, results,
                roster_util.getBuddyItem(session, packet.getElemFrom()));
            }
          }
        }
          break;
        case in_probe:
          SubscriptionType buddy_subscr = null;
          if (DynamicRoster.getBuddyItem(session, settings,
              packet.getElemFrom()) != null) {
            buddy_subscr = SubscriptionType.both;
          } else {
            buddy_subscr =
              roster_util.getBuddySubscription(session, packet.getElemFrom());
          }
          if (buddy_subscr == null) {
            buddy_subscr = SubscriptionType.none;
          } // end of if (buddy_subscr == null)
          switch (buddy_subscr) {
          case none:
          case none_pending_out:
          case to:
            results.offer(Authorization.FORBIDDEN.getResponseMessage(packet,
                "Presence information is forbidden.", false));
            break;
          case none_pending_in:
          case none_pending_out_in:
          case to_pending_in:
            results.offer(Authorization.NOT_AUTHORIZED.getResponseMessage(packet,
                "You are not authorized to get presence information.", false));
            break;
          default:
            break;
          } // end of switch (buddy_subscr)
          if (roster_util.isSubscribedFrom(buddy_subscr)) {
            for (XMPPResourceConnection conn: session.getActiveSessions()) {
              Element pres = (Element)conn.getSessionData(PRESENCE_KEY);
              sendPresence(null, packet.getElemFrom(), conn.getJID(),
                results, pres);
            }
          } // end of if (roster_util.isSubscribedFrom(session, packet.getElemFrom()))
          break;
        case error: {
          // This is message to 'this' client probably
          // Only error responses to DIRECT presence should be sent back
          // to the client, all other should be ignored for now.
          // Later on the Tigase should remember who responded with
          // an error and don't send presence updates to this entity
          Set<String> direct_presences =
                  (Set<String>) session.getSessionData(DIRECT_PRESENCE);
          if (direct_presences != null &&
                  direct_presences.contains(packet.getElemFrom())) {
            Element elem = packet.getElement().clone();
            Packet result = new Packet(elem);
            result.setTo(session.getConnectionId());
            result.setFrom(packet.getTo());
            results.offer(result);
          } else {
            // Ignore for now....
          }
        }
View Full Code Here

TOP

Related Classes of tigase.server.Packet

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.