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 java.io.IOException;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;

  22. import org.jivesoftware.smack.packet.IQ;
  23. import org.jivesoftware.smack.packet.IqData;
  24. import org.jivesoftware.smack.packet.XmlEnvironment;
  25. import org.jivesoftware.smack.parsing.SmackParsingException;
  26. import org.jivesoftware.smack.provider.IqProvider;
  27. import org.jivesoftware.smack.util.PacketParserUtils;
  28. import org.jivesoftware.smack.util.ParserUtils;
  29. import org.jivesoftware.smack.xml.XmlPullParser;
  30. import org.jivesoftware.smack.xml.XmlPullParserException;

  31. import org.jivesoftware.smackx.workgroup.MetaData;
  32. import org.jivesoftware.smackx.workgroup.agent.InvitationRequest;
  33. import org.jivesoftware.smackx.workgroup.agent.OfferContent;
  34. import org.jivesoftware.smackx.workgroup.agent.TransferRequest;
  35. import org.jivesoftware.smackx.workgroup.agent.UserRequest;
  36. import org.jivesoftware.smackx.workgroup.util.MetaDataUtils;

  37. import org.jxmpp.jid.Jid;

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

  48.     @Override
  49.     public OfferRequestPacket parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
  50.         XmlPullParser.Event eventType = parser.getEventType();
  51.         String sessionID = null;
  52.         int timeout = -1;
  53.         OfferContent content = null;
  54.         boolean done = false;
  55.         Map<String, List<String>> metaData = new HashMap<>();

  56.         if (eventType != XmlPullParser.Event.START_ELEMENT) {
  57.             // throw exception
  58.         }

  59.         Jid userJID = ParserUtils.getJidAttribute(parser);
  60.         // Default userID to the JID.
  61.         Jid userID = userJID;

  62.         while (!done) {
  63.             eventType = parser.next();

  64.             if (eventType == XmlPullParser.Event.START_ELEMENT) {
  65.                 String elemName = parser.getName();

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

  99.         OfferRequestPacket offerRequest =
  100.                 new OfferRequestPacket(userJID, userID, timeout, metaData, sessionID, content);
  101.         offerRequest.setType(IQ.Type.set);

  102.         return offerRequest;
  103.     }

  104.     public static class OfferRequestPacket extends IQ {

  105.         public static final String ELEMENT = "offer";
  106.         public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";

  107.         private final int timeout;
  108.         private final Jid userID;
  109.         private final Jid userJID;
  110.         private final Map<String, List<String>> metaData;
  111.         private final String sessionID;
  112.         private final OfferContent content;

  113.         public OfferRequestPacket(Jid userJID, Jid userID, int timeout, Map<String, List<String>> metaData,
  114.                 String sessionID, OfferContent content) {
  115.             super(ELEMENT, NAMESPACE);
  116.             this.userJID = userJID;
  117.             this.userID = userID;
  118.             this.timeout = timeout;
  119.             this.metaData = metaData;
  120.             this.sessionID = sessionID;
  121.             this.content = content;
  122.         }

  123.         /**
  124.          * Returns the userID, which is either the same as the userJID or a special
  125.          * value that the user provided as part of their "join queue" request.
  126.          *
  127.          * @return the user ID.
  128.          */
  129.         public Jid getUserID() {
  130.             return userID;
  131.         }

  132.         /**
  133.          * The JID of the user that made the "join queue" request.
  134.          *
  135.          * @return the user JID.
  136.          */
  137.         public Jid getUserJID() {
  138.             return userJID;
  139.         }

  140.         /**
  141.          * Returns the session ID associated with the request and ensuing chat. If the offer
  142.          * does not contain a session ID, <code>null</code> will be returned.
  143.          *
  144.          * @return the session id associated with the request.
  145.          */
  146.         public String getSessionID() {
  147.             return sessionID;
  148.         }

  149.         /**
  150.          * Returns the number of seconds the agent has to accept the offer before
  151.          * it times out.
  152.          *
  153.          * @return the offer timeout (in seconds).
  154.          */
  155.         public int getTimeout() {
  156.             return this.timeout;
  157.         }

  158.         public OfferContent getContent() {
  159.             return content;
  160.         }

  161.         /**
  162.          * Returns any meta-data associated with the offer.
  163.          *
  164.          * @return meta-data associated with the offer.
  165.          */
  166.         public Map<String, List<String>> getMetaData() {
  167.             return this.metaData;
  168.         }

  169.         @Override
  170.         protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
  171.             buf.append(" jid=\"").append(userJID).append("\">");
  172.             buf.append("<timeout>").append(Integer.toString(timeout)).append("</timeout>");

  173.             if (sessionID != null) {
  174.                 buf.append('<').append(SessionID.ELEMENT_NAME);
  175.                 buf.append(" session=\"");
  176.                 buf.append(getSessionID()).append("\" xmlns=\"");
  177.                 buf.append(SessionID.NAMESPACE).append("\"/>");
  178.             }

  179.             if (metaData != null) {
  180.                 buf.append(MetaDataUtils.serializeMetaData(metaData));
  181.             }

  182.             if (userID != null) {
  183.                 buf.append('<').append(UserID.ELEMENT_NAME);
  184.                 buf.append(" id=\"");
  185.                 buf.append(userID).append("\" xmlns=\"");
  186.                 buf.append(UserID.NAMESPACE).append("\"/>");
  187.             }

  188.             return buf;
  189.         }
  190.     }
  191. }