summaryrefslogtreecommitdiffstats
blob: 84645e4d901d44ff12523de2fadc55d7062d5079 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * 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);
        if(to.isValid()) {
            iq.setTo(to);            
        }
        iq.setID(id);
        if(payload != null) {
            iq.addPayload(payload);
        }
        return iq;
    }

    public static IQ createResult(JID to, String id) {
        return createResult(to, id, null);
    }

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

    public static IQ createResult(JID to, JID from, String id) {
        return createResult(to, from, id, null);
    }

    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);
        if(payload != null) {
            iq.addPayload(payload);
        }
        return iq;
    }

    public static IQ createError(JID to, String id) {
        return createError(to, id, ErrorPayload.Condition.BadRequest, ErrorPayload.Type.Cancel, null);
    }

    public static IQ createError(JID to, String id, ErrorPayload.Condition condition) {
        return createError(to, id, condition, ErrorPayload.Type.Cancel, null);
    }

    public static IQ createError(JID to, String id, ErrorPayload.Condition condition, ErrorPayload.Type type) {
        return createError(to, id, condition, type, null);
    }
    
    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) {
        return createError(to, from, id, ErrorPayload.Condition.BadRequest, ErrorPayload.Type.Cancel, null);
    }

    public static IQ createError(JID to, JID from, String id, ErrorPayload.Condition condition) {
        return createError(to, from, id, condition, ErrorPayload.Type.Cancel, null);
    }

    public static IQ createError(JID to, JID from, String id, ErrorPayload.Condition condition, ErrorPayload.Type type) {
        return createError(to, from, id, condition, type, null);
    }

    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_; 
    }
}