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        {
129            super(ELEMENT, NAMESPACE);
130            this.userJID = userJID;
131            this.userID = userID;
132            this.timeout = timeout;
133            this.metaData = metaData;
134            this.sessionID = sessionID;
135            this.content = content;
136        }
137
138        /**
139         * Returns the userID, which is either the same as the userJID or a special
140         * value that the user provided as part of their "join queue" request.
141         *
142         * @return the user ID.
143         */
144        public Jid getUserID() {
145            return userID;
146        }
147
148        /**
149         * The JID of the user that made the "join queue" request.
150         *
151         * @return the user JID.
152         */
153        public Jid getUserJID() {
154            return userJID;
155        }
156
157        /**
158         * Returns the session ID associated with the request and ensuing chat. If the offer
159         * does not contain a session ID, <tt>null</tt> will be returned.
160         *
161         * @return the session id associated with the request.
162         */
163        public String getSessionID() {
164            return sessionID;
165        }
166
167        /**
168         * Returns the number of seconds the agent has to accept the offer before
169         * it times out.
170         *
171         * @return the offer timeout (in seconds).
172         */
173        public int getTimeout() {
174            return this.timeout;
175        }
176
177        public OfferContent getContent() {
178            return content;
179        }
180
181        /**
182         * Returns any meta-data associated with the offer.
183         *
184         * @return meta-data associated with the offer.
185         */
186        public Map<String, List<String>> getMetaData() {
187            return this.metaData;
188        }
189
190        @Override
191        protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder buf) {
192            buf.append(" jid=\"").append(userJID).append("\">");
193            buf.append("<timeout>").append(Integer.toString(timeout)).append("</timeout>");
194
195            if (sessionID != null) {
196                buf.append('<').append(SessionID.ELEMENT_NAME);
197                buf.append(" session=\"");
198                buf.append(getSessionID()).append("\" xmlns=\"");
199                buf.append(SessionID.NAMESPACE).append("\"/>");
200            }
201
202            if (metaData != null) {
203                buf.append(MetaDataUtils.serializeMetaData(metaData));
204            }
205
206            if (userID != null) {
207                buf.append('<').append(UserID.ELEMENT_NAME);
208                buf.append(" id=\"");
209                buf.append(userID).append("\" xmlns=\"");
210                buf.append(UserID.NAMESPACE).append("\"/>");
211            }
212
213            return buf;
214        }
215    }
216}