Ejemplo n.º 1
0
  /**
   * Method description
   *
   * @param connectionId
   * @param packet
   * @param session
   * @param repo
   * @param results
   * @param settings
   * @throws PacketErrorTypeException
   */
  @Override
  public void processFromUserOutPacket(
      JID connectionId,
      Packet packet,
      XMPPResourceConnection session,
      NonAuthUserRepository repo,
      Queue<Packet> results,
      Map<String, Object> settings)
      throws PacketErrorTypeException {
    if (session.isLocalDomain(packet.getStanzaTo().getDomain(), false)) {

      // This is a local user so we can quickly get his vCard from the database
      try {
        String strvCard =
            repo.getPublicData(packet.getStanzaTo().getBareJID(), ID, VCARD_KEY, null);
        Packet result = null;

        if (strvCard != null) {
          result = parseXMLData(strvCard, packet);
        } else {
          result = packet.okResult((String) null, 1);
        } // end of if (vcard != null)
        result.setPacketTo(connectionId);
        results.offer(result);
      } catch (UserNotFoundException e) {
        results.offer(
            Authorization.ITEM_NOT_FOUND.getResponseMessage(packet, "User not found", true));
      } // end of try-catch
    } else {

      // Else forward the packet to a remote server
      results.offer(packet.copyElementOnly());
    }
  }
Ejemplo n.º 2
0
  /**
   * Method description
   *
   * @param packet
   * @param repo
   * @param results
   * @param settings
   * @throws PacketErrorTypeException
   */
  @Override
  public void processNullSessionPacket(
      Packet packet,
      NonAuthUserRepository repo,
      Queue<Packet> results,
      Map<String, Object> settings)
      throws PacketErrorTypeException {
    if (packet.getType() == StanzaType.get) {
      try {
        String strvCard =
            repo.getPublicData(packet.getStanzaTo().getBareJID(), ID, VCARD_KEY, null);

        if (strvCard != null) {
          results.offer(parseXMLData(strvCard, packet));
        } else {
          results.offer(packet.okResult((String) null, 1));
        } // end of if (vcard != null)
      } catch (UserNotFoundException e) {
        results.offer(
            Authorization.ITEM_NOT_FOUND.getResponseMessage(packet, "User not found", true));
      } // end of try-catch
    } else {

      // This is most likely a response to the user from the remote
      // entity with vCard request results.
      // Processed in processToUserPacket() method.
    }
  }
  @Override
  protected void processCommand(Packet packet) {
    BoshSession session = getBoshSession(packet.getTo());

    switch (packet.getCommand()) {
      case USER_LOGIN:
        String jid = Command.getFieldValue(packet, "user-jid");

        if (jid != null) {
          if (session != null) {
            try {
              BareJID fromJID = BareJID.bareJIDInstance(jid);
              BareJID hostJid = getSeeOtherHostForJID(fromJID, Phase.LOGIN);

              if (hostJid != null) {
                Element streamErrorElement =
                    see_other_host_strategy.getStreamError(
                        "urn:ietf:params:xml:ns:xmpp-streams", hostJid);
                Packet redirectPacket = Packet.packetInstance(streamErrorElement);

                redirectPacket.setPacketTo(packet.getTo());
                writePacketToSocket(redirectPacket);
                session.sendWaitingPackets();
                session.close();
                if (log.isLoggable(Level.FINE)) {
                  log.log(
                      Level.FINE,
                      "{0} : {1} ({2})",
                      new Object[] {
                        BOSH_OPERATION_TYPE.REMOVE, session.getSid(), "See other host"
                      });
                }
                sessions.remove(session.getSid());
              } else {
                session.setUserJid(jid);
              }
            } catch (TigaseStringprepException ex) {
              log.log(Level.SEVERE, "user JID violates RFC6122 (XMPP:Address Format): ", ex);
            }
          } else {
            if (log.isLoggable(Level.FINE)) {
              log.log(Level.FINE, "Missing XMPPIOService for USER_LOGIN command: {0}", packet);
            }
          }
        } else {
          log.log(Level.WARNING, "Missing user-jid for USER_LOGIN command: {0}", packet);
        }

        break;

      case CLOSE:
        if (session != null) {
          if (log.isLoggable(Level.FINER)) {
            log.log(Level.FINER, "Closing session for command CLOSE: {0}", session.getSid());
          }
          try {
            List<Element> err_el = packet.getElement().getChildrenStaticStr(Iq.IQ_COMMAND_PATH);

            if ((err_el != null) && (err_el.size() > 0)) {
              Element error = new Element("stream:error");

              error.addChild(err_el.get(0));

              Packet condition = Packet.packetInstance(error);

              condition.setPacketTo(packet.getTo());
              writePacketToSocket(condition);
              session.sendWaitingPackets();
              bosh_session_close_delay = 100;
            }
          } catch (TigaseStringprepException ex) {
            Logger.getLogger(BoshConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
          }
          if (bosh_session_close_delay > 0) {
            try {
              Thread.sleep(bosh_session_close_delay);
            } catch (InterruptedException ex) {

              // Intentionally left blank
            }
          }
          session.close();
          if (log.isLoggable(Level.FINE)) {
            log.log(
                Level.FINE,
                "{0} : {1} ({2})",
                new Object[] {
                  BOSH_OPERATION_TYPE.REMOVE, session.getSid(), "Closing session for command CLOSE"
                });
          }
          sessions.remove(session.getSid());
        } else {
          if (log.isLoggable(Level.FINE)) {
            log.log(Level.FINE, "Session does not exist for packet: {0}", packet);
          }
        }

        break;

      case CHECK_USER_CONNECTION:
        if (session != null) {

          // It's ok, the session has been found, respond with OK.
          addOutPacket(packet.okResult((String) null, 0));
        } else {

          // Session is no longer active, respond with an error.
          try {
            addOutPacket(
                Authorization.ITEM_NOT_FOUND.getResponseMessage(packet, "Connection gone.", false));
          } catch (PacketErrorTypeException e) {

            // Hm, error already, ignoring...
            log.log(Level.INFO, "Error packet is not really expected here: {0}", packet);
          }
        }

        break;

      default:
        super.processCommand(packet);

        break;
    } // end of switch (pc.getCommand())
  }