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