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