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