summaryrefslogtreecommitdiffstats
blob: d06d3a639a5b8b36024b129774a232602b59f865 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * Copyright (c) 2010-2012, Isode Limited, London, England.
 * All rights reserved.
 */
/*
 * Copyright (c) 2010, Remko Tronçon.
 * All rights reserved.
 */
package com.isode.stroke.elements;

import com.isode.stroke.jid.JID;

public class IQ extends Stanza {
    public enum Type {Get, Set, Result, Error}

    private Type type_;

    public IQ() {
        this(Type.Get);
    }

    public IQ(Type type) {
        type_ = type;
    }

    public Type getType() {
        return type_;
    }

    public void setType(Type type) {
        type_ = type;
    }

    public static IQ createRequest(Type type, JID to, String id, Payload payload) {
        IQ iq = new IQ(type);
        iq.setTo(to);
        iq.setID(id);
        iq.addPayload(payload);
        return iq;
    }

    public static IQ createResult(JID to, String id, Payload payload) {
        IQ iq = new IQ(Type.Result);
        iq.setTo(to);
        iq.setID(id);
        iq.addPayload(payload);
        return iq;
    }
    
    public static IQ createResult(JID to, JID from, String id, Payload payload) {
        IQ iq = new IQ(Type.Result);
        iq.setTo(to);
        iq.setFrom(from);
        iq.setID(id);
        iq.addPayload(payload);
        return iq;
    }

    public static IQ createError(JID to, String id, ErrorPayload.Condition condition, ErrorPayload.Type type) {
        IQ iq = new IQ(Type.Error);
        iq.setTo(to);
        iq.setID(id);
        iq.addPayload(new ErrorPayload(condition, type));
        return iq;
    }
    
    public static IQ createError(JID to, String id, ErrorPayload.Condition condition, ErrorPayload.Type type, Payload payload) {
        IQ iq = new IQ(Type.Error);
        iq.setTo(to);
        iq.setID(id);
        ErrorPayload errorPayload = new ErrorPayload(condition, type);
        errorPayload.setPayload(payload);
        iq.addPayload(errorPayload);
        return iq;
    }

    public static IQ createError(JID to, JID from, String id, ErrorPayload.Condition condition, ErrorPayload.Type type, Payload payload) {
        IQ iq = new IQ(Type.Error);
        iq.setTo(to);
        iq.setFrom(from);
        iq.setID(id);
        ErrorPayload errorPayload = new ErrorPayload(condition, type);
        errorPayload.setPayload(payload);
        iq.addPayload(errorPayload);
        return iq;
    }
    
    @Override
    public String toString() {
        return super.toString() + " Type=" + type_; 
    }
}