OfferRevokeProvider.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.workgroup.packet;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.packet.IQ;
  20. import org.jivesoftware.smack.packet.IqData;
  21. import org.jivesoftware.smack.packet.XmlEnvironment;
  22. import org.jivesoftware.smack.provider.IqProvider;
  23. import org.jivesoftware.smack.util.ParserUtils;
  24. import org.jivesoftware.smack.xml.XmlPullParser;
  25. import org.jivesoftware.smack.xml.XmlPullParserException;

  26. import org.jxmpp.jid.Jid;

  27. /**
  28.  * An IQProvider class which has savvy about the offer-revoke tag.<br>
  29.  *
  30.  * @author loki der quaeler
  31.  */
  32. public class OfferRevokeProvider extends IqProvider<IQ> {

  33.     @Override
  34.     public OfferRevokePacket parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
  35.         // The parser will be positioned on the opening IQ tag, so get the JID attribute.
  36.         Jid userJID = ParserUtils.getJidAttribute(parser);
  37.         // Default the userID to the JID.
  38.         Jid userID = userJID;
  39.         String reason = null;
  40.         String sessionID = null;
  41.         boolean done = false;

  42.         while (!done) {
  43.             XmlPullParser.Event eventType = parser.next();

  44.             if ((eventType == XmlPullParser.Event.START_ELEMENT) && parser.getName().equals("reason")) {
  45.                 reason = parser.nextText();
  46.             }
  47.             else if ((eventType == XmlPullParser.Event.START_ELEMENT)
  48.                          && parser.getName().equals(SessionID.ELEMENT_NAME)) {
  49.                 sessionID = parser.getAttributeValue("", "id");
  50.             }
  51.             else if ((eventType == XmlPullParser.Event.START_ELEMENT)
  52.                          && parser.getName().equals(UserID.ELEMENT_NAME)) {
  53.                 userID = ParserUtils.getJidAttribute(parser, "id");
  54.             }
  55.             else if ((eventType == XmlPullParser.Event.END_ELEMENT) && parser.getName().equals(
  56.                     "offer-revoke")) {
  57.                 done = true;
  58.             }
  59.         }

  60.         return new OfferRevokePacket(userJID, userID, reason, sessionID);
  61.     }

  62.     public static class OfferRevokePacket extends IQ {

  63.         public static final String ELEMENT = "offer-revoke";
  64.         public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";
  65.         private final Jid userJID;
  66.         private final Jid userID;
  67.         private final String sessionID;
  68.         private final String reason;

  69.         public OfferRevokePacket (Jid userJID, Jid userID, String cause, String sessionID) {
  70.             super(ELEMENT, NAMESPACE);
  71.             this.userJID = userJID;
  72.             this.userID = userID;
  73.             this.reason = cause;
  74.             this.sessionID = sessionID;
  75.         }

  76.         public Jid getUserJID() {
  77.             return userJID;
  78.         }

  79.         public Jid getUserID() {
  80.             return this.userID;
  81.         }

  82.         public String getReason() {
  83.             return this.reason;
  84.         }

  85.         public String getSessionID() {
  86.             return this.sessionID;
  87.         }

  88.         @Override
  89.         protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  90.             buf.append(" jid=\"").append(userID).append("\">");
  91.             if (reason != null) {
  92.                 buf.append("<reason>").append(reason).append("</reason>");
  93.             }
  94.             if (sessionID != null) {
  95.                 buf.append(new SessionID(sessionID).toXML());
  96.             }
  97.             if (userID != null) {
  98.                 buf.append(new UserID(userID).toXML());
  99.             }
  100.             return buf;
  101.         }
  102.     }
  103. }