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.util.HashMap; 021import java.util.List; 022import java.util.Map; 023 024import org.jivesoftware.smack.packet.IQ; 025import org.jivesoftware.smack.provider.IQProvider; 026import org.jivesoftware.smack.util.PacketParserUtils; 027import org.jivesoftware.smack.util.ParserUtils; 028 029import org.jivesoftware.smackx.workgroup.MetaData; 030import org.jivesoftware.smackx.workgroup.agent.InvitationRequest; 031import org.jivesoftware.smackx.workgroup.agent.OfferContent; 032import org.jivesoftware.smackx.workgroup.agent.TransferRequest; 033import org.jivesoftware.smackx.workgroup.agent.UserRequest; 034import org.jivesoftware.smackx.workgroup.util.MetaDataUtils; 035 036import org.jxmpp.jid.Jid; 037import org.xmlpull.v1.XmlPullParser; 038 039/** 040 * An IQProvider for agent offer requests. 041 * 042 * @author loki der quaeler 043 */ 044public class OfferRequestProvider extends IQProvider<IQ> { 045 // FIXME It seems because OfferRequestPacket is also defined here, we can 046 // not add it as generic to the provider, the provider and the packet should 047 // be split, but since this is legacy code, I don't think that this will 048 // happen anytime soon. 049 050 @Override 051 public OfferRequestPacket parse(XmlPullParser parser, int initialDepth) throws Exception { 052 int eventType = parser.getEventType(); 053 String sessionID = null; 054 int timeout = -1; 055 OfferContent content = null; 056 boolean done = false; 057 Map<String, List<String>> metaData = new HashMap<>(); 058 059 if (eventType != XmlPullParser.START_TAG) { 060 // throw exception 061 } 062 063 Jid userJID = ParserUtils.getJidAttribute(parser); 064 // Default userID to the JID. 065 Jid userID = userJID; 066 067 while (!done) { 068 eventType = parser.next(); 069 070 if (eventType == XmlPullParser.START_TAG) { 071 String elemName = parser.getName(); 072 073 if ("timeout".equals(elemName)) { 074 timeout = Integer.parseInt(parser.nextText()); 075 } 076 else if (MetaData.ELEMENT_NAME.equals(elemName)) { 077 metaData = MetaDataUtils.parseMetaData(parser); 078 } 079 else if (SessionID.ELEMENT_NAME.equals(elemName)) { 080 sessionID = parser.getAttributeValue("", "id"); 081 } 082 else if (UserID.ELEMENT_NAME.equals(elemName)) { 083 userID = ParserUtils.getJidAttribute(parser, "id"); 084 } 085 else if ("user-request".equals(elemName)) { 086 content = UserRequest.getInstance(); 087 } 088 else if (RoomInvitation.ELEMENT_NAME.equals(elemName)) { 089 RoomInvitation invitation = (RoomInvitation) PacketParserUtils 090 .parseExtensionElement(RoomInvitation.ELEMENT_NAME, RoomInvitation.NAMESPACE, parser); 091 content = new InvitationRequest(invitation.getInviter(), invitation.getRoom(), 092 invitation.getReason()); 093 } 094 else if (RoomTransfer.ELEMENT_NAME.equals(elemName)) { 095 RoomTransfer transfer = (RoomTransfer) PacketParserUtils 096 .parseExtensionElement(RoomTransfer.ELEMENT_NAME, RoomTransfer.NAMESPACE, parser); 097 content = new TransferRequest(transfer.getInviter(), transfer.getRoom(), transfer.getReason()); 098 } 099 } 100 else if (eventType == XmlPullParser.END_TAG) { 101 if ("offer".equals(parser.getName())) { 102 done = true; 103 } 104 } 105 } 106 107 OfferRequestPacket offerRequest = 108 new OfferRequestPacket(userJID, userID, timeout, metaData, sessionID, content); 109 offerRequest.setType(IQ.Type.set); 110 111 return offerRequest; 112 } 113 114 public static class OfferRequestPacket extends IQ { 115 116 public static final String ELEMENT = "offer"; 117 public static final String NAMESPACE = "http://jabber.org/protocol/workgroup"; 118 119 private final int timeout; 120 private final Jid userID; 121 private final Jid userJID; 122 private final Map<String, List<String>> metaData; 123 private final String sessionID; 124 private final OfferContent content; 125 126 public OfferRequestPacket(Jid userJID, Jid userID, int timeout, Map<String, List<String>> metaData, 127 String sessionID, OfferContent content) { 128 super(ELEMENT, NAMESPACE); 129 this.userJID = userJID; 130 this.userID = userID; 131 this.timeout = timeout; 132 this.metaData = metaData; 133 this.sessionID = sessionID; 134 this.content = content; 135 } 136 137 /** 138 * Returns the userID, which is either the same as the userJID or a special 139 * value that the user provided as part of their "join queue" request. 140 * 141 * @return the user ID. 142 */ 143 public Jid getUserID() { 144 return userID; 145 } 146 147 /** 148 * The JID of the user that made the "join queue" request. 149 * 150 * @return the user JID. 151 */ 152 public Jid getUserJID() { 153 return userJID; 154 } 155 156 /** 157 * Returns the session ID associated with the request and ensuing chat. If the offer 158 * does not contain a session ID, <tt>null</tt> will be returned. 159 * 160 * @return the session id associated with the request. 161 */ 162 public String getSessionID() { 163 return sessionID; 164 } 165 166 /** 167 * Returns the number of seconds the agent has to accept the offer before 168 * it times out. 169 * 170 * @return the offer timeout (in seconds). 171 */ 172 public int getTimeout() { 173 return this.timeout; 174 } 175 176 public OfferContent getContent() { 177 return content; 178 } 179 180 /** 181 * Returns any meta-data associated with the offer. 182 * 183 * @return meta-data associated with the offer. 184 */ 185 public Map<String, List<String>> getMetaData() { 186 return this.metaData; 187 } 188 189 @Override 190 protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) { 191 buf.append(" jid=\"").append(userJID).append("\">"); 192 buf.append("<timeout>").append(Integer.toString(timeout)).append("</timeout>"); 193 194 if (sessionID != null) { 195 buf.append('<').append(SessionID.ELEMENT_NAME); 196 buf.append(" session=\""); 197 buf.append(getSessionID()).append("\" xmlns=\""); 198 buf.append(SessionID.NAMESPACE).append("\"/>"); 199 } 200 201 if (metaData != null) { 202 buf.append(MetaDataUtils.serializeMetaData(metaData)); 203 } 204 205 if (userID != null) { 206 buf.append('<').append(UserID.ELEMENT_NAME); 207 buf.append(" id=\""); 208 buf.append(userID).append("\" xmlns=\""); 209 buf.append(UserID.NAMESPACE).append("\"/>"); 210 } 211 212 return buf; 213 } 214 } 215}