1package eu.siacs.conversations.xmpp.stanzas;
2
3import eu.siacs.conversations.xml.Element;
4
5public class IqPacket extends AbstractStanza {
6
7 public static final int TYPE_ERROR = -1;
8 public static final int TYPE_SET = 0;
9 public static final int TYPE_RESULT = 1;
10 public static final int TYPE_GET = 2;
11
12 private IqPacket(final String name) {
13 super(name);
14 }
15
16 public IqPacket(final int type) {
17 super("iq");
18 switch (type) {
19 case TYPE_SET:
20 this.setAttribute("type", "set");
21 break;
22 case TYPE_GET:
23 this.setAttribute("type", "get");
24 break;
25 case TYPE_RESULT:
26 this.setAttribute("type", "result");
27 break;
28 case TYPE_ERROR:
29 this.setAttribute("type", "error");
30 break;
31 default:
32 break;
33 }
34 }
35
36 public IqPacket() {
37 super("iq");
38 }
39
40 public Element query() {
41 Element query = findChild("query");
42 if (query == null) {
43 query = addChild("query");
44 }
45 return query;
46 }
47
48 public Element query(final String xmlns) {
49 final Element query = query();
50 query.setAttribute("xmlns", xmlns);
51 return query();
52 }
53
54 public int getType() {
55 final String type = getAttribute("type");
56 switch (type) {
57 case "error":
58 return TYPE_ERROR;
59 case "result":
60 return TYPE_RESULT;
61 case "set":
62 return TYPE_SET;
63 case "get":
64 return TYPE_GET;
65 default:
66 return 1000;
67 }
68 }
69
70 public IqPacket generateResponse(final int type) {
71 final IqPacket packet = new IqPacket(type);
72 packet.setTo(this.getFrom());
73 packet.setId(this.getId());
74 return packet;
75 }
76
77}