KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > packet > Bind


1 /**
2  * $RCSfile$
3  * $Revision: $
4  * $Date: $
5  *
6  * Copyright 2003-2004 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20
21 package org.jivesoftware.smack.packet;
22
23 import org.jivesoftware.smack.provider.IQProvider;
24 import org.xmlpull.v1.XmlPullParser;
25
26 /**
27  * IQ packet used by Smack to bind a resource and to obtain the jid assigned by the server.
28  * There are two ways to bind a resource. One is simply sending an empty Bind packet where the
29  * server will assign a new resource for this connection. The other option is to set a desired
30  * resource but the server may return a modified version of the sent resource.<p>
31  *
32  * For more information refer to the following
33  * <a HREF=http://www.xmpp.org/specs/rfc3920.html#bind>link</a>.
34  *
35  * @author Gaston Dombiak
36  */

37 public class Bind extends IQ {
38
39     private String JavaDoc resource = null;
40     private String JavaDoc jid = null;
41
42     public Bind() {
43         setType(IQ.Type.SET);
44     }
45
46     public String JavaDoc getResource() {
47         return resource;
48     }
49
50     public void setResource(String JavaDoc resource) {
51         this.resource = resource;
52     }
53
54     public String JavaDoc getJid() {
55         return jid;
56     }
57
58     public void setJid(String JavaDoc jid) {
59         this.jid = jid;
60     }
61
62     public String JavaDoc getChildElementXML() {
63         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
64         buf.append("<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">");
65         if (resource != null) {
66             buf.append("<resource>").append(resource).append("</resource>");
67         }
68         if (jid != null) {
69             buf.append("<jid>").append(jid).append("</jid>");
70         }
71         buf.append("</bind>");
72         return buf.toString();
73     }
74
75     public static class Provider implements IQProvider {
76
77         public IQ parseIQ(XmlPullParser parser) throws Exception JavaDoc {
78             Bind bind = new Bind();
79             boolean done = false;
80             while (!done) {
81                 int eventType = parser.next();
82                 if (eventType == XmlPullParser.START_TAG) {
83                     if (parser.getName().equals("resource")) {
84                         bind.setResource(parser.nextText());
85                     }
86                     else if (parser.getName().equals("jid")) {
87                         bind.setJid(parser.nextText());
88                     }
89                 } else if (eventType == XmlPullParser.END_TAG) {
90                     if (parser.getName().equals("bind")) {
91                         done = true;
92                     }
93                 }
94             }
95
96             return bind;
97         }
98     }
99 }
100
Popular Tags