OfferRequestProvider.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 org.jivesoftware.smackx.workgroup.MetaData;
  19. import org.jivesoftware.smackx.workgroup.agent.InvitationRequest;
  20. import org.jivesoftware.smackx.workgroup.agent.OfferContent;
  21. import org.jivesoftware.smackx.workgroup.agent.TransferRequest;
  22. import org.jivesoftware.smackx.workgroup.agent.UserRequest;
  23. import org.jivesoftware.smackx.workgroup.util.MetaDataUtils;
  24. import org.jivesoftware.smack.packet.IQ;
  25. import org.jivesoftware.smack.provider.IQProvider;
  26. import org.jivesoftware.smack.util.PacketParserUtils;
  27. import org.jivesoftware.smack.util.ParserUtils;
  28. import org.jxmpp.jid.Jid;
  29. import org.xmlpull.v1.XmlPullParser;

  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.Map;

  33. /**
  34.  * An IQProvider for agent offer requests.
  35.  *
  36.  * @author loki der quaeler
  37.  */
  38. public class OfferRequestProvider extends IQProvider<IQ> {
  39.     // FIXME It seems because OfferRequestPacket is also defined here, we can
  40.     // not add it as generic to the provider, the provider and the packet should
  41.     // be split, but since this is legacy code, I don't think that this will
  42.     // happen anytime soon.

  43.     @Override
  44.     public OfferRequestPacket parse(XmlPullParser parser, int initialDepth) throws Exception {
  45.         int eventType = parser.getEventType();
  46.         String sessionID = null;
  47.         int timeout = -1;
  48.         OfferContent content = null;
  49.         boolean done = false;
  50.         Map<String, List<String>> metaData = new HashMap<String, List<String>>();

  51.         if (eventType != XmlPullParser.START_TAG) {
  52.             // throw exception
  53.         }

  54.         Jid userJID = ParserUtils.getJidAttribute(parser);
  55.         // Default userID to the JID.
  56.         Jid userID = userJID;

  57.         while (!done) {
  58.             eventType = parser.next();

  59.             if (eventType == XmlPullParser.START_TAG) {
  60.                 String elemName = parser.getName();

  61.                 if ("timeout".equals(elemName)) {
  62.                     timeout = Integer.parseInt(parser.nextText());
  63.                 }
  64.                 else if (MetaData.ELEMENT_NAME.equals(elemName)) {
  65.                     metaData = MetaDataUtils.parseMetaData(parser);
  66.                 }
  67.                 else if (SessionID.ELEMENT_NAME.equals(elemName)) {
  68.                    sessionID = parser.getAttributeValue("", "id");
  69.                 }
  70.                 else if (UserID.ELEMENT_NAME.equals(elemName)) {
  71.                     userID = ParserUtils.getJidAttribute(parser, "id");
  72.                 }
  73.                 else if ("user-request".equals(elemName)) {
  74.                     content = UserRequest.getInstance();
  75.                 }
  76.                 else if (RoomInvitation.ELEMENT_NAME.equals(elemName)) {
  77.                     RoomInvitation invitation = (RoomInvitation) PacketParserUtils
  78.                             .parsePacketExtension(RoomInvitation.ELEMENT_NAME, RoomInvitation.NAMESPACE, parser);
  79.                     content = new InvitationRequest(invitation.getInviter(), invitation.getRoom(),
  80.                             invitation.getReason());
  81.                 }
  82.                 else if (RoomTransfer.ELEMENT_NAME.equals(elemName)) {
  83.                     RoomTransfer transfer = (RoomTransfer) PacketParserUtils
  84.                             .parsePacketExtension(RoomTransfer.ELEMENT_NAME, RoomTransfer.NAMESPACE, parser);
  85.                     content = new TransferRequest(transfer.getInviter(), transfer.getRoom(), transfer.getReason());
  86.                 }
  87.             }
  88.             else if (eventType == XmlPullParser.END_TAG) {
  89.                 if ("offer".equals(parser.getName())) {
  90.                     done = true;
  91.                 }
  92.             }
  93.         }

  94.         OfferRequestPacket offerRequest =
  95.                 new OfferRequestPacket(userJID, userID, timeout, metaData, sessionID, content);
  96.         offerRequest.setType(IQ.Type.set);

  97.         return offerRequest;
  98.     }

  99.     public static class OfferRequestPacket extends IQ {

  100.         private int timeout;
  101.         private Jid userID;
  102.         private Jid userJID;
  103.         private Map<String, List<String>> metaData;
  104.         private String sessionID;
  105.         private OfferContent content;

  106.         public OfferRequestPacket(Jid userJID, Jid userID, int timeout, Map<String, List<String>> metaData,
  107.                 String sessionID, OfferContent content)
  108.         {
  109.             super("offer", "http://jabber.org/protocol/workgroup");
  110.             this.userJID = userJID;
  111.             this.userID = userID;
  112.             this.timeout = timeout;
  113.             this.metaData = metaData;
  114.             this.sessionID = sessionID;
  115.             this.content = content;
  116.         }

  117.         /**
  118.          * Returns the userID, which is either the same as the userJID or a special
  119.          * value that the user provided as part of their "join queue" request.
  120.          *
  121.          * @return the user ID.
  122.          */
  123.         public Jid getUserID() {
  124.             return userID;
  125.         }

  126.         /**
  127.          * The JID of the user that made the "join queue" request.
  128.          *
  129.          * @return the user JID.
  130.          */
  131.         public Jid getUserJID() {
  132.             return userJID;
  133.         }

  134.         /**
  135.          * Returns the session ID associated with the request and ensuing chat. If the offer
  136.          * does not contain a session ID, <tt>null</tt> will be returned.
  137.          *
  138.          * @return the session id associated with the request.
  139.          */
  140.         public String getSessionID() {
  141.             return sessionID;
  142.         }

  143.         /**
  144.          * Returns the number of seconds the agent has to accept the offer before
  145.          * it times out.
  146.          *
  147.          * @return the offer timeout (in seconds).
  148.          */
  149.         public int getTimeout() {
  150.             return this.timeout;
  151.         }

  152.         public OfferContent getContent() {
  153.             return content;
  154.         }

  155.         /**
  156.          * Returns any meta-data associated with the offer.
  157.          *
  158.          * @return meta-data associated with the offer.
  159.          */
  160.         public Map<String, List<String>> getMetaData() {
  161.             return this.metaData;
  162.         }

  163.         @Override
  164.         protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  165.             buf.append(" jid=\"").append(userJID).append("\">");
  166.             buf.append("<timeout>").append(Integer.toString(timeout)).append("</timeout>");

  167.             if (sessionID != null) {
  168.                 buf.append('<').append(SessionID.ELEMENT_NAME);
  169.                 buf.append(" session=\"");
  170.                 buf.append(getSessionID()).append("\" xmlns=\"");
  171.                 buf.append(SessionID.NAMESPACE).append("\"/>");
  172.             }

  173.             if (metaData != null) {
  174.                 buf.append(MetaDataUtils.serializeMetaData(metaData));
  175.             }

  176.             if (userID != null) {
  177.                 buf.append('<').append(UserID.ELEMENT_NAME);
  178.                 buf.append(" id=\"");
  179.                 buf.append(userID).append("\" xmlns=\"");
  180.                 buf.append(UserID.NAMESPACE).append("\"/>");
  181.             }

  182.             return buf;
  183.         }
  184.     }
  185. }