aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/xmpp/stanzas/IqPacket.java
blob: 415d5de3bb332ae8f06311336f7a4a2b70940f56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package de.thedevstack.conversationsplus.xmpp.stanzas;

import de.thedevstack.conversationsplus.xml.Element;

public class IqPacket extends AbstractAcknowledgeableStanza {

	public enum TYPE {
		ERROR,
		SET,
		RESULT,
		GET,
		INVALID,
		TIMEOUT
	}

	public IqPacket(final TYPE type) {
		super("iq");
		if (type != TYPE.INVALID) {
			this.setAttribute("type", type.toString().toLowerCase());
		}
	}

	public IqPacket() {
		super("iq");
	}

	public Element query() {
		Element query = findChild("query");
		if (query == null) {
			query = addChild("query");
		}
		return query;
	}

	public Element query(final String xmlns) {
		final Element query = query();
		query.setAttribute("xmlns", xmlns);
		return query();
	}

	public TYPE getType() {
		final String type = getAttribute("type");
		if (type == null) {
			return TYPE.INVALID;
		}
		switch (type) {
			case "error":
				return TYPE.ERROR;
			case "result":
				return TYPE.RESULT;
			case "set":
				return TYPE.SET;
			case "get":
				return TYPE.GET;
			case "timeout":
				return TYPE.TIMEOUT;
			default:
				return TYPE.INVALID;
		}
	}

	public Element getChildElement() {
        return this.children.get(0);
    }

	public IqPacket generateResponse(final TYPE type) {
		final IqPacket packet = new IqPacket(type);
		packet.setTo(this.getFrom());
		packet.setId(this.getId());
		return packet;
	}

}