001/* 002 * 003 * Copyright 2003-2007 Jive Software. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.jivesoftware.smackx.workgroup.packet; 019 020import java.io.IOException; 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024 025import org.jivesoftware.smack.packet.IQ; 026import org.jivesoftware.smack.packet.IqData; 027import org.jivesoftware.smack.packet.XmlEnvironment; 028import org.jivesoftware.smack.parsing.SmackParsingException; 029import org.jivesoftware.smack.provider.IqProvider; 030import org.jivesoftware.smack.util.PacketParserUtils; 031import org.jivesoftware.smack.util.ParserUtils; 032import org.jivesoftware.smack.xml.XmlPullParser; 033import org.jivesoftware.smack.xml.XmlPullParserException; 034 035import org.jivesoftware.smackx.workgroup.MetaData; 036import org.jivesoftware.smackx.workgroup.agent.InvitationRequest; 037import org.jivesoftware.smackx.workgroup.agent.OfferContent; 038import org.jivesoftware.smackx.workgroup.agent.TransferRequest; 039import org.jivesoftware.smackx.workgroup.agent.UserRequest; 040import org.jivesoftware.smackx.workgroup.util.MetaDataUtils; 041 042import org.jxmpp.JxmppContext; 043import org.jxmpp.jid.Jid; 044 045/** 046 * An IQProvider for agent offer requests. 047 * 048 * @author loki der quaeler 049 */ 050public class OfferRequestProvider extends IqProvider<IQ> { 051 // FIXME It seems because OfferRequestPacket is also defined here, we can 052 // not add it as generic to the provider, the provider and the packet should 053 // be split, but since this is legacy code, I don't think that this will 054 // happen anytime soon. 055 056 @Override 057 public OfferRequestPacket parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment, JxmppContext jxmppContext) throws XmlPullParserException, IOException, SmackParsingException { 058 XmlPullParser.Event eventType = parser.getEventType(); 059 String sessionID = null; 060 int timeout = -1; 061 OfferContent content = null; 062 boolean done = false; 063 Map<String, List<String>> metaData = new HashMap<>(); 064 065 if (eventType != XmlPullParser.Event.START_ELEMENT) { 066 // throw exception 067 } 068 069 Jid userJID = ParserUtils.getJidAttribute(parser, jxmppContext); 070 // Default userID to the JID. 071 Jid userID = userJID; 072 073 while (!done) { 074 eventType = parser.next(); 075 076 if (eventType == XmlPullParser.Event.START_ELEMENT) { 077 String elemName = parser.getName(); 078 079 if ("timeout".equals(elemName)) { 080 timeout = Integer.parseInt(parser.nextText()); 081 } 082 else if (MetaData.ELEMENT_NAME.equals(elemName)) { 083 metaData = MetaDataUtils.parseMetaData(parser); 084 } 085 else if (SessionID.ELEMENT_NAME.equals(elemName)) { 086 sessionID = parser.getAttributeValue("", "id"); 087 } 088 else if (UserID.ELEMENT_NAME.equals(elemName)) { 089 userID = ParserUtils.getJidAttribute(parser, "id", jxmppContext); 090 } 091 else if ("user-request".equals(elemName)) { 092 content = UserRequest.getInstance(); 093 } 094 else if (RoomInvitation.ELEMENT_NAME.equals(elemName)) { 095 RoomInvitation invitation = (RoomInvitation) PacketParserUtils 096 .parseExtensionElement(RoomInvitation.ELEMENT_NAME, RoomInvitation.NAMESPACE, parser, xmlEnvironment, jxmppContext); 097 content = new InvitationRequest(invitation.getInviter(), invitation.getRoom(), 098 invitation.getReason()); 099 } 100 else if (RoomTransfer.ELEMENT_NAME.equals(elemName)) { 101 RoomTransfer transfer = (RoomTransfer) PacketParserUtils 102 .parseExtensionElement(RoomTransfer.ELEMENT_NAME, RoomTransfer.NAMESPACE, parser, xmlEnvironment, jxmppContext); 103 content = new TransferRequest(transfer.getInviter(), transfer.getRoom(), transfer.getReason()); 104 } 105 } 106 else if (eventType == XmlPullParser.Event.END_ELEMENT) { 107 if ("offer".equals(parser.getName())) { 108 done = true; 109 } 110 } 111 } 112 113 OfferRequestPacket offerRequest = 114 new OfferRequestPacket(userJID, userID, timeout, metaData, sessionID, content); 115 offerRequest.setType(IQ.Type.set); 116 117 return offerRequest; 118 } 119 120 public static class OfferRequestPacket extends IQ { 121 122 public static final String ELEMENT = "offer"; 123 public static final String NAMESPACE = "http://jabber.org/protocol/workgroup"; 124 125 private final int timeout; 126 private final Jid userID; 127 private final Jid userJID; 128 private final Map<String, List<String>> metaData; 129 private final String sessionID; 130 private final OfferContent content; 131 132 public OfferRequestPacket(Jid userJID, Jid userID, int timeout, Map<String, List<String>> metaData, 133 String sessionID, OfferContent content) { 134 super(ELEMENT, NAMESPACE); 135 this.userJID = userJID; 136 this.userID = userID; 137 this.timeout = timeout; 138 this.metaData = metaData; 139 this.sessionID = sessionID; 140 this.content = content; 141 } 142 143 /** 144 * Returns the userID, which is either the same as the userJID or a special 145 * value that the user provided as part of their "join queue" request. 146 * 147 * @return the user ID. 148 */ 149 public Jid getUserID() { 150 return userID; 151 } 152 153 /** 154 * The JID of the user that made the "join queue" request. 155 * 156 * @return the user JID. 157 */ 158 public Jid getUserJID() { 159 return userJID; 160 } 161 162 /** 163 * Returns the session ID associated with the request and ensuing chat. If the offer 164 * does not contain a session ID, <code>null</code> will be returned. 165 * 166 * @return the session id associated with the request. 167 */ 168 public String getSessionID() { 169 return sessionID; 170 } 171 172 /** 173 * Returns the number of seconds the agent has to accept the offer before 174 * it times out. 175 * 176 * @return the offer timeout (in seconds). 177 */ 178 public int getTimeout() { 179 return this.timeout; 180 } 181 182 public OfferContent getContent() { 183 return content; 184 } 185 186 /** 187 * Returns any meta-data associated with the offer. 188 * 189 * @return meta-data associated with the offer. 190 */ 191 public Map<String, List<String>> getMetaData() { 192 return this.metaData; 193 } 194 195 @Override 196 protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) { 197 buf.append(" jid=\"").append(userJID).append("\">"); 198 buf.append("<timeout>").append(Integer.toString(timeout)).append("</timeout>"); 199 200 if (sessionID != null) { 201 buf.append('<').append(SessionID.ELEMENT_NAME); 202 buf.append(" session=\""); 203 buf.append(getSessionID()).append("\" xmlns=\""); 204 buf.append(SessionID.NAMESPACE).append("\"/>"); 205 } 206 207 if (metaData != null) { 208 buf.append(MetaDataUtils.serializeMetaData(metaData)); 209 } 210 211 if (userID != null) { 212 buf.append('<').append(UserID.ELEMENT_NAME); 213 buf.append(" id=\""); 214 buf.append(userID).append("\" xmlns=\""); 215 buf.append(UserID.NAMESPACE).append("\"/>"); 216 } 217 218 return buf; 219 } 220 } 221}