Examples of XMPPResourceConnection


Examples of tigase.xmpp.XMPPResourceConnection

        // Make sure we send it to right client connection - the connection
        // which supports jingle - this Yate specific code....
        List<XMPPResourceConnection> res =
          conn.getParentSession().getActiveResources();
        XMPPResourceConnection session = conn;
        if (res != null && res.size() > 1) {
          // If there are more than 1 connection for this user
          // let's look for a connection with jingle flag set...
          for (XMPPResourceConnection sess: res) {
            if (sess.getSessionData("jingle") != null) {
              session = sess;
              break;
            }
          }
        }

        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();
View Full Code Here

Examples of tigase.xmpp.XMPPResourceConnection

    if (packet.isCommand() && processCommand(packet)) {
      packet.processedBy("SessionManager");
      // No more processing is needed for command packet
      return;
    } // end of if (pc.isCommand())
    XMPPResourceConnection conn = getXMPPResourceConnection(packet);
    if (conn == null
      && (isBrokenPacket(packet) || processAdminsOrDomains(packet)
        || sentToNextNode(packet))) {
      return;
    }
    if (conn != null) {
      switch (conn.getConnectionStatus()) {
      case ON_HOLD:
        LinkedList<Packet> packets =
          (LinkedList<Packet>)conn.getSessionData(SESSION_PACKETS);
        if (packets == null) {
          packets = new LinkedList<Packet>();
          conn.putSessionData(SESSION_PACKETS, packets);
        }
        packets.offer(packet);
        log.finest("Packet put on hold: " + packet.toString());
        return;
      case REDIRECT:
        packet.setTo((String)conn.getSessionData("redirect-to"));
        fastAddOutPacket(packet);
        log.finest("Packet redirected: " + packet.toString());
        return;
      }
    }
View Full Code Here

Examples of tigase.xmpp.XMPPResourceConnection

    if (elems != null && elems.size() > 0) {
      for (Element elem: elems) {
        Packet el_packet = new Packet(elem);
        //processPacket(el_packet);
        //el_packet.setFrom(packet_from);
        XMPPResourceConnection conn = getXMPPResourceConnection(el_packet);
        if (conn != null || !sentToNextNode(packet)) {
          if (conn != null) {
            switch (conn.getConnectionStatus()) {
            case ON_HOLD:
              LinkedList<Packet> packets =
              (LinkedList<Packet>)conn.getSessionData(SESSION_PACKETS);
              if (packets == null) {
                packets = new LinkedList<Packet>();
                conn.putSessionData(SESSION_PACKETS, packets);
              }
              packets.offer(el_packet);
              log.finest("Packet put on hold: " + el_packet.toString());
              return;
            case REDIRECT:
              el_packet.setTo((String)conn.getSessionData("redirect-to"));
              fastAddOutPacket(el_packet);
              log.finest("Packet redirected: " + el_packet.toString());
              return;
            }
          }
View Full Code Here

Examples of tigase.xmpp.XMPPResourceConnection

        Set<String> cancel_nodes = new LinkedHashSet<String>();
        String node_found = null;
        String userId = clel.getMethodParam(USER_ID);
        XMPPSession session = getSession(userId);
        String connectionId = clel.getMethodParam(CONNECTION_ID);
        XMPPResourceConnection conn = null;
        if (session != null) {
          conn = session.getResourceForConnectionId(connectionId);
        }
              //ClusterElement result = null;
        if (getComponentId().equals(clel.getFirstNode())) {
          log.finest("Session transfer request came back to me....");
          // Ok, the request has came back to me, let's look what to do now....
          // First let's check whether any of the nodes has decided to accept
          // the transfer:
          // This is a set of nodes we will send a transfer cancell later on
          long time = 0;
          long hash = 0;
          for (String node: clel.getVisitedNodes()) {
            if (clel.getMethodResultVal(node + "-CREATED") != null) {
              long tmp_time = clel.getMethodResultVal(node + "-" + AUTH_TIME, 0);
              long tmp_hash = clel.getMethodResultVal(
                node + "-HASH-" + XMPP_SESSION_ID, 0);
              log.finest("Node: " + node + " responded with: "
                + clel.getMethodResultVal(node + "-CREATED")
                + ", tmp_time: " + tmp_time
                + ", tmp_hash: " + tmp_hash);
              boolean replace_node = false;
              if (tmp_time == time) {
                if (tmp_hash > hash) {
                  replace_node = true;
                }
              } else {
                if (tmp_time > time) {
                  replace_node = true;
                }
              }
              if (replace_node) {
                if (node_found != null) {
                  log.finest("Addeding node to cancel_nodes: " + node_found);
                  cancel_nodes.add(node_found);
                }
                node_found = node;
                hash = tmp_hash;
                time = tmp_time;
              } else {
                log.finest("Addeding node to cancel_nodes: " + node);
                cancel_nodes.add(node);
              }
            }
          }
          if (node_found != null) {
            // This is where we want to do the user session transfer then
            if (session != null) {
              if (conn != null) {
                Map<String, String> res_vals = new LinkedHashMap<String, String>();
                res_vals.put(TRANSFER, "accept");
                ClusterElement result = clel.createMethodResponse(getComponentId(),
                  node_found, "result", res_vals);
                fastAddOutPacket(new Packet(result.getClusterElement()));
                conn.putSessionData("redirect-to", node_found);
                sendAllOnHold(conn);

                String xmpp_sessionId = clel.getMethodParam(XMPP_SESSION_ID);
                Packet redirect = Command.REDIRECT.getPacket(node_found,
                  connectionId, StanzaType.set, "1", "submit");
                Command.addFieldValue(redirect, "session-id", xmpp_sessionId);
                fastAddOutPacket(redirect);
              } else {
                // Ups, the user has disconnected, send session transfer to all
                log.finest("Addeding node to cancel_nodes: " + node_found);
                cancel_nodes.add(node_found);
                log.fine("The user connection doesn't exist: " + userId
                  + ", connectionId: " + connectionId);
              }
            } else {
              // Ups, the user has disconnected, send session transfer to all
              log.finest("Addeding node to cancel_nodes: " + node_found);
              cancel_nodes.add(node_found);
              log.fine("The user session doesn't exist: " + userId);
            }
          } else {
            // Set status to NORMALL, user is not logged in on any other node.
            if (conn != null) {
              log.finest("Set status to NORMAL and send all ON_HOLD");
              conn.setConnectionStatus(ConnectionStatus.NORMAL);
              sendAllOnHold(conn);
            } else {
              log.fine("The user connection doesn't exist: " + userId
                + ", connectionId: " + connectionId);
            }
          }
          if (cancel_nodes.size() > 0) {
            // Send session transfer to all.
            Map<String, String> res_vals = new LinkedHashMap<String, String>();
            res_vals.put(TRANSFER, "cancel");
            for (String node: cancel_nodes) {
              ClusterElement result = clel.createMethodResponse(getComponentId(),
                node, "result", res_vals);
              log.finest("Sending sesstion transfer CANCEL to node: " + node);
              fastAddOutPacket(new Packet(result.getClusterElement()));
            }
          }
        } else {
          // A request from some other node, maybe the user session should be
          // transfered here...
          ClusterElement result = ClusterElement.createForNextNode(clel,
            cluster_nodes,  getComponentId());
          if (session != null) {
            conn = session.getOldestConnection();
            boolean transfer_in = false;
            switch (conn.getConnectionStatus()) {
            case ON_HOLD:
              long local_auth_time = conn.getAuthTime();
              long remote_auth_time = clel.getMethodParam(AUTH_TIME, 0L);
              if (local_auth_time == remote_auth_time) {
                transfer_in = (conn.getSessionId().hashCode() >
                  clel.getMethodParam(XMPP_SESSION_ID).hashCode());
              } else {
                transfer_in = (local_auth_time > remote_auth_time);
              }
              break;
            case REDIRECT:
              transfer_in = false;
              break;
            case NORMAL:
              transfer_in = true;
              break;
            default:
              break;
            }
            if (transfer_in) {
              addTempSession(clel);
              result.addMethodResult(getComponentId() + "-" + AUTH_TIME,
                "" + conn.getAuthTime());
              result.addMethodResult(getComponentId() + "-HASH-" + XMPP_SESSION_ID,
                "" + conn.getSessionId().hashCode());
              result.addMethodResult(getComponentId() + "-STATUS",
                "" + conn.getConnectionStatus());
              result.addMethodResult(getComponentId() + "-CREATED",
                "" + true);
            }
          } else {
            // Do nothing really, just forward the request to a next node
          }
          fastAddOutPacket(new Packet(result.getClusterElement()));
        }
      }
      break;
    case result:
      if (ClusterMethods.SESSION_TRANSFER.toString().equals(clel.getMethodName())) {
        String transfer = clel.getMethodResultVal(TRANSFER);
        if (transfer == null) {
          log.warning("Incorrect response for the session transfer: "
            + packet.toString());
          return;
        }
        if (transfer.equals("accept")) {
          String userId = clel.getMethodParam(USER_ID);
          XMPPSession session = getSession(userId);
          if (session == null) {
            // Ups, something wrong happened, let's record this in the log
            // file for now...
            log.warning("User session does not exist for the request to complete the user transfer: " + packet.toString());
            return;
          }
          String connectionId = clel.getMethodParam(CONNECTION_ID);
          XMPPResourceConnection conn =
            session.getResourceForConnectionId(connectionId);
          if (conn == null) {
            // Ups, something wrong happened, let's record this in the log
            // file for now...
            log.warning("User connection does not exist for the request to complete the user transfer: " + packet.toString());
            return;
          }
          String token = (String)conn.getSessionData(TOKEN);
          String xmpp_sessionId = conn.getSessionId();
          Authorization auth_res = null;
          try {
            auth_res = conn.loginToken(xmpp_sessionId, token);
          } catch (Exception e) {
            log.log(Level.WARNING, "Token authentication unsuccessful.", e);
            auth_res = Authorization.NOT_AUTHORIZED;
          }
          if (auth_res == Authorization.AUTHORIZED) {
            log.finest("SESSION_TRANSFER received SET request, userId: " + userId
              + ", xmpp_sessionId: " + xmpp_sessionId
              + ", connectionId: " + connectionId + ", auth_res: " + auth_res);
          } else {
            log.finest("SESSION_TRANSFER authorization failed: " + auth_res
              + ", userId: " + userId);
            closeConnection(conn.getConnectionId(), true);
            Packet close = Command.CLOSE.getPacket(getComponentId(),
              connectionId, StanzaType.set, "1");
            fastAddOutPacket(close);
          }
          conn.setConnectionStatus(ConnectionStatus.NORMAL);
//           Packet redirect = Command.REDIRECT.getPacket(getComponentId(),
//             connectionId, StanzaType.set, "1", "submit");
//           Command.addFieldValue(redirect, "session-id", xmpp_sessionId);
//           fastAddOutPacket(redirect);
View Full Code Here

Examples of tigase.xmpp.XMPPResourceConnection

  private void addTempSession(ClusterElement clel) {
    String connectionId = clel.getMethodParam(CONNECTION_ID);
    String userId = clel.getMethodParam(USER_ID);
    String domain = JIDUtils.getNodeHost(userId);
    XMPPResourceConnection res_con = createUserSession(connectionId, domain, userId);
    res_con.setConnectionStatus(ConnectionStatus.TEMP);
    String xmpp_sessionId = clel.getMethodParam(XMPP_SESSION_ID);
    res_con.setSessionId(xmpp_sessionId);
    String token = clel.getMethodParam(TOKEN);
    res_con.putSessionData(TOKEN, token);
  }
View Full Code Here

Examples of tigase.xmpp.XMPPResourceConnection

  protected XMPPResourceConnection getXMPPResourceConnection(String connId) {
    return connectionsByFrom.get(connId);
  }

  protected XMPPResourceConnection getXMPPResourceConnection(Packet p) {
    XMPPResourceConnection conn = null;
    if (p.getFrom() != null) {
      conn = connectionsByFrom.get(p.getFrom());
      if (conn != null) {
        return conn.getConnectionStatus() == ConnectionStatus.TEMP ? null : conn;
      }
    }
    // It might be a message _to_ some user on this server
    // so let's look for established session for this user...
    final String to = p.getElemTo();
    if (to != null) {
      log.finest("Searching for resource connection for: " + to);
      conn = getResourceConnection(to);
      if (conn != null && conn.getConnectionStatus() == ConnectionStatus.TEMP) {
        conn = null;
      }
    } else {
      // Hm, not sure what should I do now....
      // Maybe I should treat it as message to admin....
View Full Code Here

Examples of tigase.xmpp.XMPPResourceConnection

    if (packet.isCommand() && processCommand(packet)) {
      packet.processedBy("SessionManager");
      // No more processing is needed for command packet
      return;
    } // end of if (pc.isCommand())
    XMPPResourceConnection conn = getXMPPResourceConnection(packet);
    if (conn == null && (isBrokenPacket(packet) ||
            processAdminsOrDomains(packet))) {
      return;
    }
    processPacket(packet, conn);
View Full Code Here

Examples of tigase.xmpp.XMPPResourceConnection

    } // end of if (session != null)
    return null;
  }

  private String getConnectionId(String jid) {
    XMPPResourceConnection res = getResourceConnection(jid);
    if (res != null) {
      return res.getConnectionId();
    } // end of if (res != null)
    return null;
  }
View Full Code Here

Examples of tigase.xmpp.XMPPResourceConnection

    } // end of if (children != null)
  }

  protected XMPPResourceConnection createUserSession(String conn_id,
    String domain, String user_jid) {
    XMPPResourceConnection connection = new XMPPResourceConnection(conn_id,
      user_repository, auth_repository, this, false);
    connection.setDomain(domain);
    // Dummy session ID, we might decide later to set real thing here
    connection.setSessionId("session-id");
    //connection.setAnonymousPeers(anon_peers);
    connectionsByFrom.put(conn_id, connection);
    registerNewSession(JIDUtils.getNodeID(user_jid), connection);
    try {
      connection.setResource(JIDUtils.getNodeResource(user_jid));
    } catch (NotAuthorizedException e) {
      log.warning("Something wrong with authorization: " + e
        + ", for user: " + user_jid);
    }
    return connection;
View Full Code Here

Examples of tigase.xmpp.XMPPResourceConnection

  protected boolean processCommand(Packet pc) {
    boolean processing_result = false;
    log.finer(pc.getCommand().toString() + " command from: " + pc.getFrom());
    //Element command = pc.getElement();
    XMPPResourceConnection connection =  connectionsByFrom.get(pc.getFrom());
    switch (pc.getCommand()) {
    case STREAM_OPENED:
      // It might be existing opened stream after TLS/SASL authorization
      // If not, it means this is new stream
      if (connection == null) {
        log.finer("Adding resource connection for: " + pc.getFrom());
        final String hostname = Command.getFieldValue(pc, "hostname");
        connection = new XMPPResourceConnection(pc.getFrom(),
          user_repository, auth_repository, this,
          isAnonymousEnabled(hostname));
        if (hostname != null) {
          log.finest("Setting hostname " + hostname
            + " for connection: " + connection.getConnectionId());
          connection.setDomain(hostname);
        } // end of if (hostname != null)
        else {
          connection.setDomain(getDefHostName());
        } // end of if (hostname != null) else
        //connection.setAnonymousPeers(anon_peers);
        connectionsByFrom.put(pc.getFrom(), connection);
      } else {
        log.finest("Stream opened for existing session, authorized: "
          + connection.isAuthorized());
      } // end of else
      connection.setSessionId(Command.getFieldValue(pc, "session-id"));
      connection.setDefLang(Command.getFieldValue(pc, "xml:lang"));
      log.finest("Setting session-id " + connection.getSessionId()
        + " for connection: " + connection.getConnectionId());
      processing_result = true;
      break;
    case GETFEATURES:
      if (pc.getType() == StanzaType.get) {
        List<Element> features =
          getFeatures(connectionsByFrom.get(pc.getFrom()));
        Packet result = pc.commandResult(null);
        Command.setData(result, features);
        addOutPacket(result);
      } // end of if (pc.getType() == StanzaType.get)
      processing_result = true;
      break;
    case STREAM_CLOSED:
      closeConnection(pc.getFrom(), false);
      processing_result = true;
      break;
    case BROADCAST_TO_ONLINE:
      String from = pc.getFrom();
      boolean trusted = false;
      try {
        trusted = (from != null && isTrusted(from))
          || (connection != null && isTrusted(connection.getUserId()));
      } catch (NotAuthorizedException e) {
        trusted = false;
      }
      try {
        if (trusted) {
          List<Element> packets = Command.getData(pc);
          if (packets != null) {
            for (XMPPResourceConnection conn: connectionsByFrom.values()) {
              if (conn.isAuthorized()) {
                try {
                  for (Element el_pack: packets) {
                    Element el_copy = el_pack.clone();
                    el_copy.setAttribute("to", conn.getJID());
                    Packet out_packet = new Packet(el_copy);
                    out_packet.setTo(conn.getConnectionId());
                    addOutPacket(out_packet);
                  }
                } catch (NotAuthorizedException e) {
                  log.warning("Something wrong, connection is authenticated but "
                    + "NoAuthorizedException is thrown.");
                }
              }
            }
          } else {
            addOutPacket(Authorization.BAD_REQUEST.getResponseMessage(pc,
                "Missing packets for broadcast.", true));
          }
        } else {
          addOutPacket(Authorization.FORBIDDEN.getResponseMessage(pc,
              "You don't have enough permission to brodcast packet.", true));
        }
      } catch (PacketErrorTypeException e) {
        log.warning("Packet processing exception: " + e
          + ", packet: " + pc.toString());
      }
      processing_result = true;
      break;
    case USER_STATUS:
      try {
        if (isTrusted(pc.getElemFrom())
          || isTrusted(JIDUtils.getNodeHost(pc.getElemFrom()))) {
          String av = Command.getFieldValue(pc, "available");
          boolean available = !(av != null && av.equalsIgnoreCase("false"));
          if (available) {
            Packet presence = null;
            Element p = pc.getElement().getChild("command").getChild("presence");
            if (p != null) {
// +              // use this hack to break XMLNS
// +              Element el = new Element("presence");
// +              el.setChildren(p.getChildren());
              Element elem = p.clone();
              elem.setXMLNS("jabber:client");
              presence = new Packet(elem);
            }
            connection = connectionsByFrom.get(pc.getElemFrom());
            if (connection == null) {
              String user_jid = Command.getFieldValue(pc, "jid");
              String hostname = JIDUtils.getNodeHost(user_jid);
              connection = createUserSession(pc.getElemFrom(), hostname, user_jid);
              connection.setSessionId("USER_STATUS");
              user_repository.setData(JIDUtils.getNodeID(user_jid), "tokens",
                "USER_STATUS", "USER_STATUS");
              connection.loginToken("USER_STATUS", "USER_STATUS");
              handleLogin(user_jid, connection);
              connection.putSessionData("jingle", "active");
              addOutPacket(pc.okResult((String)null, 0));
              if (presence == null) {
                presence =
                  new Packet(new Element("presence",
                      new Element[] {
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.