1package eu.siacs.conversations.xmpp.stanzas;
2
3import eu.siacs.conversations.xml.Element;
4
5public class IqPacket extends AbstractAcknowledgeableStanza {
6
7 public enum TYPE {
8 ERROR,
9 SET,
10 RESULT,
11 GET,
12 INVALID,
13 TIMEOUT
14 }
15
16 public IqPacket(final TYPE type) {
17 super("iq");
18 if (type != TYPE.INVALID) {
19 this.setAttribute("type", type.toString().toLowerCase());
20 }
21 }
22
23 public IqPacket() {
24 super("iq");
25 }
26
27 public Element query() {
28 Element query = findChild("query");
29 if (query == null) {
30 query = addChild("query");
31 }
32 return query;
33 }
34
35 public Element query(final String xmlns) {
36 final Element query = query();
37 query.setAttribute("xmlns", xmlns);
38 return query();
39 }
40
41 public TYPE getType() {
42 final String type = getAttribute("type");
43 if (type == null) {
44 return TYPE.INVALID;
45 }
46 switch (type) {
47 case "error":
48 return TYPE.ERROR;
49 case "result":
50 return TYPE.RESULT;
51 case "set":
52 return TYPE.SET;
53 case "get":
54 return TYPE.GET;
55 default:
56 return TYPE.INVALID;
57 }
58 }
59
60 public IqPacket generateResponse(final TYPE type) {
61 final IqPacket packet = new IqPacket(type);
62 packet.setTo(this.getFrom());
63 packet.setId(this.getId());
64 return packet;
65 }
66
67}