aboutsummaryrefslogtreecommitdiff
path: root/src/org/jivesoftware/smackx/pubsub/SubscribeForm.java
blob: 53f26068f551346f119627254e8404e908325526 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.smackx.pubsub;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.UnknownFormatConversionException;

import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.Form;
import org.jivesoftware.smackx.FormField;
import org.jivesoftware.smackx.packet.DataForm;

/**
 * A decorator for a {@link Form} to easily enable reading and updating
 * of subscription options.  All operations read or update the underlying {@link DataForm}.
 * 
 * <p>Unlike the {@link Form}.setAnswer(XXX)} methods, which throw an exception if the field does not
 * exist, all <b>SubscribeForm.setXXX</b> methods will create the field in the wrapped form
 * if it does not already exist.
 * 
 * @author Robin Collier
 */
public class SubscribeForm extends Form
{	
	public SubscribeForm(DataForm configDataForm)
	{
		super(configDataForm);
	}
	
	public SubscribeForm(Form subscribeOptionsForm)
	{
		super(subscribeOptionsForm.getDataFormToSend());
	}
	
	public SubscribeForm(FormType formType)
	{
		super(formType.toString());
	}
	
	/**
	 * Determines if an entity wants to receive notifications.
	 * 
	 * @return true if want to receive, false otherwise
	 */
	public boolean isDeliverOn()
	{
		return parseBoolean(getFieldValue(SubscribeOptionFields.deliver));
	}
	
	/**
	 * Sets whether an entity wants to receive notifications.
	 *
	 * @param deliverNotifications
	 */
	public void setDeliverOn(boolean deliverNotifications)
	{
		addField(SubscribeOptionFields.deliver, FormField.TYPE_BOOLEAN);
		setAnswer(SubscribeOptionFields.deliver.getFieldName(), deliverNotifications);
	}

	/**
	 * Determines if notifications should be delivered as aggregations or not.
	 * 
	 * @return true to aggregate, false otherwise
	 */
	public boolean isDigestOn()
	{
		return parseBoolean(getFieldValue(SubscribeOptionFields.digest));
	}
	
	/**
	 * Sets whether notifications should be delivered as aggregations or not.
	 * 
	 * @param digestOn true to aggregate, false otherwise 
	 */
	public void setDigestOn(boolean digestOn)
	{
		addField(SubscribeOptionFields.deliver, FormField.TYPE_BOOLEAN);
		setAnswer(SubscribeOptionFields.deliver.getFieldName(), digestOn);
	}

	/**
	 * Gets the minimum number of milliseconds between sending notification digests
	 * 
	 * @return The frequency in milliseconds
	 */
	public int getDigestFrequency()
	{
		return Integer.parseInt(getFieldValue(SubscribeOptionFields.digest_frequency));
	}

	/**
	 * Sets the minimum number of milliseconds between sending notification digests
	 * 
	 * @param frequency The frequency in milliseconds
	 */
	public void setDigestFrequency(int frequency)
	{
		addField(SubscribeOptionFields.digest_frequency, FormField.TYPE_TEXT_SINGLE);
		setAnswer(SubscribeOptionFields.digest_frequency.getFieldName(), frequency);
	}

	/**
	 * Get the time at which the leased subscription will expire, or has expired.
	 * 
	 * @return The expiry date
	 */
	public Date getExpiry()
	{
		String dateTime = getFieldValue(SubscribeOptionFields.expire);
		try
		{
			return StringUtils.parseDate(dateTime);
		}
		catch (ParseException e)
		{
			UnknownFormatConversionException exc = new UnknownFormatConversionException(dateTime);
			exc.initCause(e);
			throw exc;
		}
	}
	
	/**
	 * Sets the time at which the leased subscription will expire, or has expired.
	 * 
	 * @param expire The expiry date
	 */
	public void setExpiry(Date expire)
	{
		addField(SubscribeOptionFields.expire, FormField.TYPE_TEXT_SINGLE);
		setAnswer(SubscribeOptionFields.expire.getFieldName(), StringUtils.formatXEP0082Date(expire));
	}
	
	/**
	 * Determines whether the entity wants to receive an XMPP message body in 
	 * addition to the payload format.
	 * 
	 * @return true to receive the message body, false otherwise
	 */
	public boolean isIncludeBody()
	{
		return parseBoolean(getFieldValue(SubscribeOptionFields.include_body));
	}
	
	/**
	 * Sets whether the entity wants to receive an XMPP message body in 
	 * addition to the payload format.
	 * 
	 * @param include true to receive the message body, false otherwise
	 */
	public void setIncludeBody(boolean include)
	{
		addField(SubscribeOptionFields.include_body, FormField.TYPE_BOOLEAN);
		setAnswer(SubscribeOptionFields.include_body.getFieldName(), include);
	}

	/**
	 * Gets the {@link PresenceState} for which an entity wants to receive 
	 * notifications.
	 * 
	 * @return iterator over the list of states
	 */
	public Iterator<PresenceState> getShowValues()
	{
		ArrayList<PresenceState> result = new ArrayList<PresenceState>(5);
		Iterator<String > it = getFieldValues(SubscribeOptionFields.show_values);
		
		while (it.hasNext())
		{
			String state = it.next();
			result.add(PresenceState.valueOf(state));
		}
		return result.iterator();
	}
	
	/**
	 * Sets the list of {@link PresenceState} for which an entity wants
	 * to receive notifications.
	 * 
	 * @param stateValues The list of states
	 */
	public void setShowValues(Collection<PresenceState> stateValues)
	{
		ArrayList<String> values = new ArrayList<String>(stateValues.size());
		
		for (PresenceState state : stateValues)
		{
			values.add(state.toString());
		}
		addField(SubscribeOptionFields.show_values, FormField.TYPE_LIST_MULTI);
		setAnswer(SubscribeOptionFields.show_values.getFieldName(), values);
	}
	
	
	static private boolean parseBoolean(String fieldValue)
	{
		return ("1".equals(fieldValue) || "true".equals(fieldValue));
	}

	private String getFieldValue(SubscribeOptionFields field)
	{
		FormField formField = getField(field.getFieldName());
		
		return formField.getValues().next();
	}

	private Iterator<String> getFieldValues(SubscribeOptionFields field)
	{
		FormField formField = getField(field.getFieldName());
		
		return formField.getValues();
	}

	private void addField(SubscribeOptionFields nodeField, String type)
	{
		String fieldName = nodeField.getFieldName();
		
		if (getField(fieldName) == null)
		{
			FormField field = new FormField(fieldName);
			field.setType(type);
			addField(field);
		}
	}
}