OfferRequestProvider.java revision d7955ce24d294fb2014c59d11fca184471056f44
1/**
2 * $Revision$
3 * $Date$
4 *
5 * Copyright 2003-2007 Jive Software.
6 *
7 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *     http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20package org.jivesoftware.smackx.workgroup.packet;
21
22import org.jivesoftware.smackx.workgroup.MetaData;
23import org.jivesoftware.smackx.workgroup.agent.InvitationRequest;
24import org.jivesoftware.smackx.workgroup.agent.OfferContent;
25import org.jivesoftware.smackx.workgroup.agent.TransferRequest;
26import org.jivesoftware.smackx.workgroup.agent.UserRequest;
27import org.jivesoftware.smackx.workgroup.util.MetaDataUtils;
28import org.jivesoftware.smack.packet.IQ;
29import org.jivesoftware.smack.provider.IQProvider;
30import org.jivesoftware.smack.util.PacketParserUtils;
31import org.xmlpull.v1.XmlPullParser;
32
33import java.util.HashMap;
34import java.util.List;
35import java.util.Map;
36
37/**
38 * An IQProvider for agent offer requests.
39 *
40 * @author loki der quaeler
41 */
42public class OfferRequestProvider implements IQProvider {
43
44    public OfferRequestProvider() {
45    }
46
47    public IQ parseIQ(XmlPullParser parser) throws Exception {
48        int eventType = parser.getEventType();
49        String sessionID = null;
50        int timeout = -1;
51        OfferContent content = null;
52        boolean done = false;
53        Map<String, List<String>> metaData = new HashMap<String, List<String>>();
54
55        if (eventType != XmlPullParser.START_TAG) {
56            // throw exception
57        }
58
59        String userJID = parser.getAttributeValue("", "jid");
60        // Default userID to the JID.
61        String userID = userJID;
62
63        while (!done) {
64            eventType = parser.next();
65
66            if (eventType == XmlPullParser.START_TAG) {
67                String elemName = parser.getName();
68
69                if ("timeout".equals(elemName)) {
70                    timeout = Integer.parseInt(parser.nextText());
71                }
72                else if (MetaData.ELEMENT_NAME.equals(elemName)) {
73                    metaData = MetaDataUtils.parseMetaData(parser);
74                }
75                else if (SessionID.ELEMENT_NAME.equals(elemName)) {
76                   sessionID = parser.getAttributeValue("", "id");
77                }
78                else if (UserID.ELEMENT_NAME.equals(elemName)) {
79                    userID = parser.getAttributeValue("", "id");
80                }
81                else if ("user-request".equals(elemName)) {
82                    content = UserRequest.getInstance();
83                }
84                else if (RoomInvitation.ELEMENT_NAME.equals(elemName)) {
85                    RoomInvitation invitation = (RoomInvitation) PacketParserUtils
86                            .parsePacketExtension(RoomInvitation.ELEMENT_NAME, RoomInvitation.NAMESPACE, parser);
87                    content = new InvitationRequest(invitation.getInviter(), invitation.getRoom(),
88                            invitation.getReason());
89                }
90                else if (RoomTransfer.ELEMENT_NAME.equals(elemName)) {
91                    RoomTransfer transfer = (RoomTransfer) PacketParserUtils
92                            .parsePacketExtension(RoomTransfer.ELEMENT_NAME, RoomTransfer.NAMESPACE, parser);
93                    content = new TransferRequest(transfer.getInviter(), transfer.getRoom(), transfer.getReason());
94                }
95            }
96            else if (eventType == XmlPullParser.END_TAG) {
97                if ("offer".equals(parser.getName())) {
98                    done = true;
99                }
100            }
101        }
102
103        OfferRequestPacket offerRequest =
104                new OfferRequestPacket(userJID, userID, timeout, metaData, sessionID, content);
105        offerRequest.setType(IQ.Type.SET);
106
107        return offerRequest;
108    }
109
110    public static class OfferRequestPacket extends IQ {
111
112        private int timeout;
113        private String userID;
114        private String userJID;
115        private Map<String, List<String>> metaData;
116        private String sessionID;
117        private OfferContent content;
118
119        public OfferRequestPacket(String userJID, String userID, int timeout, Map<String, List<String>> metaData,
120                String sessionID, OfferContent content)
121        {
122            this.userJID = userJID;
123            this.userID = userID;
124            this.timeout = timeout;
125            this.metaData = metaData;
126            this.sessionID = sessionID;
127            this.content = content;
128        }
129
130        /**
131         * Returns the userID, which is either the same as the userJID or a special
132         * value that the user provided as part of their "join queue" request.
133         *
134         * @return the user ID.
135         */
136        public String getUserID() {
137            return userID;
138        }
139
140        /**
141         * The JID of the user that made the "join queue" request.
142         *
143         * @return the user JID.
144         */
145        public String getUserJID() {
146            return userJID;
147        }
148
149        /**
150         * Returns the session ID associated with the request and ensuing chat. If the offer
151         * does not contain a session ID, <tt>null</tt> will be returned.
152         *
153         * @return the session id associated with the request.
154         */
155        public String getSessionID() {
156            return sessionID;
157        }
158
159        /**
160         * Returns the number of seconds the agent has to accept the offer before
161         * it times out.
162         *
163         * @return the offer timeout (in seconds).
164         */
165        public int getTimeout() {
166            return this.timeout;
167        }
168
169        public OfferContent getContent() {
170            return content;
171        }
172
173        /**
174         * Returns any meta-data associated with the offer.
175         *
176         * @return meta-data associated with the offer.
177         */
178        public Map<String, List<String>> getMetaData() {
179            return this.metaData;
180        }
181
182        public String getChildElementXML () {
183            StringBuilder buf = new StringBuilder();
184
185            buf.append("<offer xmlns=\"http://jabber.org/protocol/workgroup\" jid=\"").append(userJID).append("\">");
186            buf.append("<timeout>").append(timeout).append("</timeout>");
187
188            if (sessionID != null) {
189                buf.append('<').append(SessionID.ELEMENT_NAME);
190                buf.append(" session=\"");
191                buf.append(getSessionID()).append("\" xmlns=\"");
192                buf.append(SessionID.NAMESPACE).append("\"/>");
193            }
194
195            if (metaData != null) {
196                buf.append(MetaDataUtils.serializeMetaData(metaData));
197            }
198
199            if (userID != null) {
200                buf.append('<').append(UserID.ELEMENT_NAME);
201                buf.append(" id=\"");
202                buf.append(userID).append("\" xmlns=\"");
203                buf.append(UserID.NAMESPACE).append("\"/>");
204            }
205
206            buf.append("</offer>");
207
208            return buf.toString();
209        }
210    }
211}
212