KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $RCSfile$
3  * $Revision: 2408 $
4  * $Date: 2004-11-02 20:53:30 -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.smack.packet;
22
23 import org.jivesoftware.smack.util.StringUtils;
24
25 /**
26  * The base IQ (Info/Query) packet. IQ packets are used to get and set information
27  * on the server, including authentication, roster operations, and creating
28  * accounts. Each IQ packet has a specific type that indicates what type of action
29  * is being taken: "get", "set", "result", or "error".<p>
30  *
31  * IQ packets can contain a single child element that exists in a specific XML
32  * namespace. The combination of the element name and namespace determines what
33  * type of IQ packet it is. Some example IQ subpacket snippets:<ul>
34  *
35  * <li>&lt;query xmlns="jabber:iq:auth"&gt; -- an authentication IQ.
36  * <li>&lt;query xmlns="jabber:iq:private"&gt; -- a private storage IQ.
37  * <li>&lt;pubsub xmlns="http://jabber.org/protocol/pubsub"&gt; -- a pubsub IQ.
38  * </ul>
39  *
40  * @author Matt Tucker
41  */

42 public abstract class IQ extends Packet {
43
44     private Type type = Type.GET;
45
46     /**
47      * Returns the type of the IQ packet.
48      *
49      * @return the type of the IQ packet.
50      */

51     public Type getType() {
52         return type;
53     }
54
55     /**
56      * Sets the type of the IQ packet.
57      *
58      * @param type the type of the IQ packet.
59      */

60     public void setType(Type type) {
61         if (type == null) {
62             this.type = Type.GET;
63         }
64         else {
65             this.type = type;
66         }
67     }
68
69     public String JavaDoc toXML() {
70         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
71         buf.append("<iq ");
72         if (getPacketID() != null) {
73             buf.append("id=\"" + getPacketID() + "\" ");
74         }
75         if (getTo() != null) {
76             buf.append("to=\"").append(StringUtils.escapeForXML(getTo())).append("\" ");
77         }
78         if (getFrom() != null) {
79             buf.append("from=\"").append(StringUtils.escapeForXML(getFrom())).append("\" ");
80         }
81         if (type == null) {
82             buf.append("type=\"get\">");
83         }
84         else {
85             buf.append("type=\"").append(getType()).append("\">");
86         }
87         // Add the query section if there is one.
88
String JavaDoc queryXML = getChildElementXML();
89         if (queryXML != null) {
90             buf.append(queryXML);
91         }
92         // Add the error sub-packet, if there is one.
93
XMPPError error = getError();
94         if (error != null) {
95             buf.append(error.toXML());
96         }
97         buf.append("</iq>");
98         return buf.toString();
99     }
100
101     /**
102      * Returns the sub-element XML section of the IQ packet, or <tt>null</tt> if there
103      * isn't one. Packet extensions <b>must</b> be included, if any are defined.<p>
104      *
105      * Extensions of this class must override this method.
106      *
107      * @return the child element section of the IQ XML.
108      */

109     public abstract String JavaDoc getChildElementXML();
110
111     /**
112      * A class to represent the type of the IQ packet. The types are:
113      *
114      * <ul>
115      * <li>IQ.Type.GET
116      * <li>IQ.Type.SET
117      * <li>IQ.Type.RESULT
118      * <li>IQ.Type.ERROR
119      * </ul>
120      */

121     public static class Type {
122
123         public static final Type GET = new Type("get");
124         public static final Type SET = new Type("set");
125         public static final Type RESULT = new Type("result");
126         public static final Type ERROR = new Type("error");
127
128         /**
129          * Converts a String into the corresponding types. Valid String values
130          * that can be converted to types are: "get", "set", "result", and "error".
131          *
132          * @param type the String value to covert.
133          * @return the corresponding Type.
134          */

135         public static Type fromString(String JavaDoc type) {
136             if (type == null) {
137                 return null;
138             }
139             type = type.toLowerCase();
140             if (GET.toString().equals(type)) {
141                 return GET;
142             }
143             else if (SET.toString().equals(type)) {
144                 return SET;
145             }
146             else if (ERROR.toString().equals(type)) {
147                 return ERROR;
148             }
149             else if (RESULT.toString().equals(type)) {
150                 return RESULT;
151             }
152             else {
153                 return null;
154             }
155         }
156
157         private String JavaDoc value;
158
159         private Type(String JavaDoc value) {
160             this.value = value;
161         }
162
163         public String JavaDoc toString() {
164             return value;
165         }
166     }
167 }
168
Popular Tags