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 case "timeout":
56 return TYPE.TIMEOUT;
57 default:
58 return TYPE.INVALID;
59 }
60 }
61
62 public IqPacket generateResponse(final TYPE type) {
63 final IqPacket packet = new IqPacket(type);
64 packet.setTo(this.getFrom());
65 packet.setId(this.getId());
66 return packet;
67 }
68
69 @Override
70 public boolean valid() {
71 String id = getId();
72 return id != null && super.valid();
73 }
74
75}