コード例 #1
0
ファイル: VCardTemp.java プロジェクト: hbaaron/ikan
  /**
   * Method description
   *
   * @param connectionId
   * @param packet
   * @param session
   * @param repo
   * @param results
   * @param settings
   * @throws PacketErrorTypeException
   */
  @Override
  public void processFromUserToServerPacket(
      JID connectionId,
      Packet packet,
      XMPPResourceConnection session,
      NonAuthUserRepository repo,
      Queue<Packet> results,
      Map<String, Object> settings)
      throws PacketErrorTypeException {
    if (packet.getType() != null) {
      try {
        Packet result = null;

        switch (packet.getType()) {
          case get:
            String strvCard = session.getPublicData(ID, VCARD_KEY, null);

            if (strvCard != null) {
              result = parseXMLData(strvCard, packet);
            } else {
              result = packet.okResult((String) null, 1);
            } // end of if (vcard != null) else

            break;

          case set:
            Element elvCard = packet.getElement().getChild(vCard);

            // This is added to support old vCard protocol where element
            // name was all upper cases. So here I am checking both
            // possibilities
            if (elvCard == null) {
              elvCard = packet.getElement().getChild(VCARD);
            }
            if (elvCard != null) {
              if (log.isLoggable(Level.FINER)) {
                log.finer("Adding vCard: " + elvCard);
              }
              session.setPublicData(ID, VCARD_KEY, elvCard.toString());
            } else {
              if (log.isLoggable(Level.FINER)) {
                log.finer("Removing vCard");
              }
              session.removePublicData(ID, VCARD_KEY);
            } // end of else
            result = packet.okResult((String) null, 0);

            break;

          default:

            // Ignore all others...
        }
        if (result != null) {
          result.setPacketTo(session.getConnectionId());
          results.offer(result);
        }
      } catch (NoConnectionIdException ex) {

        // This should not happen unless somebody sends a result vcard packet
        // to the server itself
        log.warning(
            "This should not happen, unless this is a vcard result packet "
                + "sent to the server, which should not happen: "
                + packet);
      } catch (NotAuthorizedException ex) {
        log.warning("Received vCard request but user session is not authorized yet: " + packet);
        results.offer(
            Authorization.NOT_AUTHORIZED.getResponseMessage(
                packet, "You must authorize session first.", true));
      } catch (TigaseDBException ex) {
        log.warning("Database problem, please contact admin: " + ex);
        results.offer(
            Authorization.INTERNAL_SERVER_ERROR.getResponseMessage(
                packet, "Database access problem, please contact administrator.", true));
      }
    } else {

      // TODO: if this really happen that this is clearly protocol error, as
      // that would be
      // vCard packet with no type set, do we really need to handle such an
      // erro? Let's
      // ignore it for now.
    }
  }