KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smackx > packet > DiscoverInfo


1 /**
2  * $RCSfile$
3  * $Revision: 2407 $
4  * $Date: 2004-11-02 20:37:00 -0300 (Tue, 02 Nov 2004) $
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.smackx.packet;
22
23 import java.util.*;
24
25 import org.jivesoftware.smack.packet.IQ;
26
27 /**
28  * A DiscoverInfo IQ packet, which is used by XMPP clients to request and receive information
29  * to/from other XMPP entities.<p>
30  *
31  * The received information may contain one or more identities of the requested XMPP entity, and
32  * a list of supported features by the requested XMPP entity.
33  *
34  * @author Gaston Dombiak
35  */

36 public class DiscoverInfo extends IQ {
37
38     private List features = new ArrayList();
39     private List identities = new ArrayList();
40     private String JavaDoc node;
41
42     /**
43      * Adds a new feature to the discovered information.
44      *
45      * @param feature the discovered feature
46      */

47     public void addFeature(String JavaDoc feature) {
48         addFeature(new DiscoverInfo.Feature(feature));
49     }
50
51     private void addFeature(Feature feature) {
52         synchronized (features) {
53             features.add(feature);
54         }
55     }
56
57     /**
58      * Returns the discovered features of an XMPP entity.
59      *
60      * @return an Iterator on the discovered features of an XMPP entity
61      */

62     Iterator getFeatures() {
63         synchronized (features) {
64             return Collections.unmodifiableList(new ArrayList(features)).iterator();
65         }
66     }
67
68     /**
69      * Adds a new identity of the requested entity to the discovered information.
70      *
71      * @param identity the discovered entity's identity
72      */

73     public void addIdentity(Identity identity) {
74         synchronized (identities) {
75             identities.add(identity);
76         }
77     }
78
79     /**
80      * Returns the discovered identities of an XMPP entity.
81      *
82      * @return an Iterator on the discoveted identities
83      */

84     public Iterator getIdentities() {
85         synchronized (identities) {
86             return Collections.unmodifiableList(new ArrayList(identities)).iterator();
87         }
88     }
89
90     /**
91      * Returns the node attribute that supplements the 'jid' attribute. A node is merely
92      * something that is associated with a JID and for which the JID can provide information.<p>
93      *
94      * Node attributes SHOULD be used only when trying to provide or query information which
95      * is not directly addressable.
96      *
97      * @return the node attribute that supplements the 'jid' attribute
98      */

99     public String JavaDoc getNode() {
100         return node;
101     }
102
103     /**
104      * Sets the node attribute that supplements the 'jid' attribute. A node is merely
105      * something that is associated with a JID and for which the JID can provide information.<p>
106      *
107      * Node attributes SHOULD be used only when trying to provide or query information which
108      * is not directly addressable.
109      *
110      * @param node the node attribute that supplements the 'jid' attribute
111      */

112     public void setNode(String JavaDoc node) {
113         this.node = node;
114     }
115
116     /**
117      * Returns true if the specified feature is part of the discovered information.
118      *
119      * @param feature the feature to check
120      * @return true if the requestes feature has been discovered
121      */

122     public boolean containsFeature(String JavaDoc feature) {
123         for (Iterator it = getFeatures(); it.hasNext();) {
124             if (feature.equals(((DiscoverInfo.Feature) it.next()).getVar()))
125                 return true;
126         }
127         return false;
128     }
129
130     public String JavaDoc getChildElementXML() {
131         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
132         buf.append("<query xmlns=\"http://jabber.org/protocol/disco#info\"");
133         if (getNode() != null) {
134             buf.append(" node=\"");
135             buf.append(getNode());
136             buf.append("\"");
137         }
138         buf.append(">");
139         synchronized (identities) {
140             for (int i = 0; i < identities.size(); i++) {
141                 Identity identity = (Identity) identities.get(i);
142                 buf.append(identity.toXML());
143             }
144         }
145         synchronized (features) {
146             for (int i = 0; i < features.size(); i++) {
147                 Feature feature = (Feature) features.get(i);
148                 buf.append(feature.toXML());
149             }
150         }
151         // Add packet extensions, if any are defined.
152
buf.append(getExtensionsXML());
153         buf.append("</query>");
154         return buf.toString();
155     }
156
157     /**
158      * Represents the identity of a given XMPP entity. An entity may have many identities but all
159      * the identities SHOULD have the same name.<p>
160      *
161      * Refer to <a HREF="http://www.jabber.org/registrar/disco-categories.html">Jabber::Registrar</a>
162      * in order to get the official registry of values for the <i>category</i> and <i>type</i>
163      * attributes.
164      *
165      */

166     public static class Identity {
167
168         private String JavaDoc category;
169         private String JavaDoc name;
170         private String JavaDoc type;
171
172         /**
173          * Creates a new identity for an XMPP entity.
174          *
175          * @param category the entity's category.
176          * @param name the entity's name.
177          */

178         public Identity(String JavaDoc category, String JavaDoc name) {
179             this.category = category;
180             this.name = name;
181         }
182
183         /**
184          * Returns the entity's category. To get the official registry of values for the
185          * 'category' attribute refer to <a HREF="http://www.jabber.org/registrar/disco-categories.html">Jabber::Registrar</a>
186          *
187          * @return the entity's category.
188          */

189         public String JavaDoc getCategory() {
190             return category;
191         }
192
193         /**
194          * Returns the identity's name.
195          *
196          * @return the identity's name.
197          */

198         public String JavaDoc getName() {
199             return name;
200         }
201
202         /**
203          * Returns the entity's type. To get the official registry of values for the
204          * 'type' attribute refer to <a HREF="http://www.jabber.org/registrar/disco-categories.html">Jabber::Registrar</a>
205          *
206          * @return the entity's type.
207          */

208         public String JavaDoc getType() {
209             return type;
210         }
211
212         /**
213          * Sets the entity's type. To get the official registry of values for the
214          * 'type' attribute refer to <a HREF="http://www.jabber.org/registrar/disco-categories.html">Jabber::Registrar</a>
215          *
216          * @param type the identity's type.
217          */

218         public void setType(String JavaDoc type) {
219             this.type = type;
220         }
221
222         public String JavaDoc toXML() {
223             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
224             buf.append("<identity category=\"").append(category).append("\"");
225             buf.append(" name=\"").append(name).append("\"");
226             if (type != null) {
227                 buf.append(" type=\"").append(type).append("\"");
228             }
229             buf.append("/>");
230             return buf.toString();
231         }
232     }
233
234     /**
235      * Represents the features offered by the item. This information helps requestors determine
236      * what actions are possible with regard to this item (registration, search, join, etc.)
237      * as well as specific feature types of interest, if any (e.g., for the purpose of feature
238      * negotiation).
239      */

240     public static class Feature {
241
242         private String JavaDoc variable;
243
244         /**
245          * Creates a new feature offered by an XMPP entity or item.
246          *
247          * @param variable the feature's variable.
248          */

249         public Feature(String JavaDoc variable) {
250             this.variable = variable;
251         }
252
253         /**
254          * Returns the feature's variable.
255          *
256          * @return the feature's variable.
257          */

258         public String JavaDoc getVar() {
259             return variable;
260         }
261
262         public String JavaDoc toXML() {
263             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
264             buf.append("<feature var=\"").append(variable).append("\"/>");
265             return buf.toString();
266         }
267     }
268 }
Popular Tags