2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright 2003-2007 Jive Software.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smackx.workgroup.packet;
|
|
|
|
|
2017-06-14 17:12:43 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
|
|
|
import org.jivesoftware.smack.provider.IQProvider;
|
|
|
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
|
|
|
import org.jivesoftware.smack.util.ParserUtils;
|
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smackx.workgroup.MetaData;
|
|
|
|
import org.jivesoftware.smackx.workgroup.agent.InvitationRequest;
|
|
|
|
import org.jivesoftware.smackx.workgroup.agent.OfferContent;
|
|
|
|
import org.jivesoftware.smackx.workgroup.agent.TransferRequest;
|
|
|
|
import org.jivesoftware.smackx.workgroup.agent.UserRequest;
|
|
|
|
import org.jivesoftware.smackx.workgroup.util.MetaDataUtils;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2015-02-14 17:15:02 +01:00
|
|
|
import org.jxmpp.jid.Jid;
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An IQProvider for agent offer requests.
|
|
|
|
*
|
|
|
|
* @author loki der quaeler
|
|
|
|
*/
|
2014-10-07 21:15:20 +02:00
|
|
|
public class OfferRequestProvider extends IQProvider<IQ> {
|
|
|
|
// FIXME It seems because OfferRequestPacket is also defined here, we can
|
|
|
|
// not add it as generic to the provider, the provider and the packet should
|
|
|
|
// be split, but since this is legacy code, I don't think that this will
|
|
|
|
// happen anytime soon.
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2014-10-07 21:15:20 +02:00
|
|
|
@Override
|
2015-03-13 09:27:18 +01:00
|
|
|
public OfferRequestPacket parse(XmlPullParser parser, int initialDepth) throws Exception {
|
2014-02-17 23:58:40 +01:00
|
|
|
int eventType = parser.getEventType();
|
|
|
|
String sessionID = null;
|
|
|
|
int timeout = -1;
|
|
|
|
OfferContent content = null;
|
|
|
|
boolean done = false;
|
2017-12-13 23:10:11 +01:00
|
|
|
Map<String, List<String>> metaData = new HashMap<>();
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
if (eventType != XmlPullParser.START_TAG) {
|
|
|
|
// throw exception
|
|
|
|
}
|
|
|
|
|
2015-02-14 17:15:02 +01:00
|
|
|
Jid userJID = ParserUtils.getJidAttribute(parser);
|
2014-02-17 23:58:40 +01:00
|
|
|
// Default userID to the JID.
|
2015-02-14 17:15:02 +01:00
|
|
|
Jid userID = userJID;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
while (!done) {
|
|
|
|
eventType = parser.next();
|
|
|
|
|
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
|
|
|
String elemName = parser.getName();
|
|
|
|
|
|
|
|
if ("timeout".equals(elemName)) {
|
|
|
|
timeout = Integer.parseInt(parser.nextText());
|
|
|
|
}
|
|
|
|
else if (MetaData.ELEMENT_NAME.equals(elemName)) {
|
|
|
|
metaData = MetaDataUtils.parseMetaData(parser);
|
|
|
|
}
|
|
|
|
else if (SessionID.ELEMENT_NAME.equals(elemName)) {
|
|
|
|
sessionID = parser.getAttributeValue("", "id");
|
|
|
|
}
|
|
|
|
else if (UserID.ELEMENT_NAME.equals(elemName)) {
|
2015-02-14 17:15:02 +01:00
|
|
|
userID = ParserUtils.getJidAttribute(parser, "id");
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
else if ("user-request".equals(elemName)) {
|
|
|
|
content = UserRequest.getInstance();
|
|
|
|
}
|
|
|
|
else if (RoomInvitation.ELEMENT_NAME.equals(elemName)) {
|
|
|
|
RoomInvitation invitation = (RoomInvitation) PacketParserUtils
|
2015-03-21 18:56:27 +01:00
|
|
|
.parseExtensionElement(RoomInvitation.ELEMENT_NAME, RoomInvitation.NAMESPACE, parser);
|
2014-02-17 23:58:40 +01:00
|
|
|
content = new InvitationRequest(invitation.getInviter(), invitation.getRoom(),
|
|
|
|
invitation.getReason());
|
|
|
|
}
|
|
|
|
else if (RoomTransfer.ELEMENT_NAME.equals(elemName)) {
|
|
|
|
RoomTransfer transfer = (RoomTransfer) PacketParserUtils
|
2015-03-21 18:56:27 +01:00
|
|
|
.parseExtensionElement(RoomTransfer.ELEMENT_NAME, RoomTransfer.NAMESPACE, parser);
|
2014-02-17 23:58:40 +01:00
|
|
|
content = new TransferRequest(transfer.getInviter(), transfer.getRoom(), transfer.getReason());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
|
|
|
if ("offer".equals(parser.getName())) {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OfferRequestPacket offerRequest =
|
|
|
|
new OfferRequestPacket(userJID, userID, timeout, metaData, sessionID, content);
|
2014-06-06 02:20:45 +02:00
|
|
|
offerRequest.setType(IQ.Type.set);
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
return offerRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class OfferRequestPacket extends IQ {
|
|
|
|
|
2015-05-18 12:55:29 +02:00
|
|
|
public static final String ELEMENT = "offer";
|
|
|
|
public static final String NAMESPACE = "http://jabber.org/protocol/workgroup";
|
|
|
|
|
2017-12-13 23:10:11 +01:00
|
|
|
private final int timeout;
|
|
|
|
private final Jid userID;
|
|
|
|
private final Jid userJID;
|
|
|
|
private final Map<String, List<String>> metaData;
|
|
|
|
private final String sessionID;
|
|
|
|
private final OfferContent content;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2015-02-14 17:15:02 +01:00
|
|
|
public OfferRequestPacket(Jid userJID, Jid userID, int timeout, Map<String, List<String>> metaData,
|
2018-04-06 10:21:46 +02:00
|
|
|
String sessionID, OfferContent content) {
|
2015-05-18 12:55:29 +02:00
|
|
|
super(ELEMENT, NAMESPACE);
|
2014-02-17 23:58:40 +01:00
|
|
|
this.userJID = userJID;
|
|
|
|
this.userID = userID;
|
|
|
|
this.timeout = timeout;
|
|
|
|
this.metaData = metaData;
|
|
|
|
this.sessionID = sessionID;
|
|
|
|
this.content = content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the userID, which is either the same as the userJID or a special
|
|
|
|
* value that the user provided as part of their "join queue" request.
|
|
|
|
*
|
|
|
|
* @return the user ID.
|
|
|
|
*/
|
2015-02-14 17:15:02 +01:00
|
|
|
public Jid getUserID() {
|
2014-02-17 23:58:40 +01:00
|
|
|
return userID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The JID of the user that made the "join queue" request.
|
|
|
|
*
|
|
|
|
* @return the user JID.
|
|
|
|
*/
|
2015-02-14 17:15:02 +01:00
|
|
|
public Jid getUserJID() {
|
2014-02-17 23:58:40 +01:00
|
|
|
return userJID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the session ID associated with the request and ensuing chat. If the offer
|
|
|
|
* does not contain a session ID, <tt>null</tt> will be returned.
|
|
|
|
*
|
|
|
|
* @return the session id associated with the request.
|
|
|
|
*/
|
|
|
|
public String getSessionID() {
|
|
|
|
return sessionID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of seconds the agent has to accept the offer before
|
|
|
|
* it times out.
|
|
|
|
*
|
|
|
|
* @return the offer timeout (in seconds).
|
|
|
|
*/
|
|
|
|
public int getTimeout() {
|
|
|
|
return this.timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
public OfferContent getContent() {
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns any meta-data associated with the offer.
|
|
|
|
*
|
|
|
|
* @return meta-data associated with the offer.
|
|
|
|
*/
|
|
|
|
public Map<String, List<String>> getMetaData() {
|
|
|
|
return this.metaData;
|
|
|
|
}
|
|
|
|
|
2014-11-07 21:12:01 +01:00
|
|
|
@Override
|
|
|
|
protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
|
|
|
|
buf.append(" jid=\"").append(userJID).append("\">");
|
|
|
|
buf.append("<timeout>").append(Integer.toString(timeout)).append("</timeout>");
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
if (sessionID != null) {
|
|
|
|
buf.append('<').append(SessionID.ELEMENT_NAME);
|
|
|
|
buf.append(" session=\"");
|
|
|
|
buf.append(getSessionID()).append("\" xmlns=\"");
|
|
|
|
buf.append(SessionID.NAMESPACE).append("\"/>");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (metaData != null) {
|
|
|
|
buf.append(MetaDataUtils.serializeMetaData(metaData));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userID != null) {
|
|
|
|
buf.append('<').append(UserID.ELEMENT_NAME);
|
|
|
|
buf.append(" id=\"");
|
|
|
|
buf.append(userID).append("\" xmlns=\"");
|
|
|
|
buf.append(UserID.NAMESPACE).append("\"/>");
|
|
|
|
}
|
|
|
|
|
2014-11-07 21:12:01 +01:00
|
|
|
return buf;
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|