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