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