2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright the original author or authors
|
|
|
|
*
|
|
|
|
* 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.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Date;
|
2014-04-09 08:26:28 +02:00
|
|
|
import java.util.List;
|
2014-02-17 23:58:40 +01:00
|
|
|
import java.util.UnknownFormatConversionException;
|
|
|
|
|
2019-07-24 09:18:39 +02:00
|
|
|
import org.jivesoftware.smack.util.ParserUtils;
|
2019-10-29 21:12:22 +01:00
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
import org.jivesoftware.smackx.xdata.Form;
|
|
|
|
import org.jivesoftware.smackx.xdata.FormField;
|
|
|
|
import org.jivesoftware.smackx.xdata.packet.DataForm;
|
|
|
|
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.jxmpp.util.XmppDateTime;
|
|
|
|
|
2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
* A decorator for a {@link Form} to easily enable reading and updating
|
|
|
|
* of subscription options. All operations read or update the underlying {@link DataForm}.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* <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.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @author Robin Collier
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public class SubscribeForm extends Form {
|
|
|
|
public SubscribeForm(DataForm configDataForm) {
|
2017-02-07 22:02:40 +01:00
|
|
|
super(configDataForm);
|
|
|
|
}
|
|
|
|
|
2018-04-06 10:21:46 +02:00
|
|
|
public SubscribeForm(Form subscribeOptionsForm) {
|
2017-02-07 22:02:40 +01:00
|
|
|
super(subscribeOptionsForm.getDataFormToSend());
|
|
|
|
}
|
|
|
|
|
2018-04-06 10:21:46 +02:00
|
|
|
public SubscribeForm(DataForm.Type formType) {
|
2017-02-07 22:02:40 +01:00
|
|
|
super(formType);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if an entity wants to receive notifications.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2017-02-07 22:02:40 +01:00
|
|
|
* @return true if want to receive, false otherwise
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public boolean isDeliverOn() {
|
2019-07-24 09:18:39 +02:00
|
|
|
return ParserUtils.parseXmlBoolean(getFieldValue(SubscribeOptionFields.deliver));
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets whether an entity wants to receive notifications.
|
|
|
|
*
|
2019-08-30 12:08:30 +02:00
|
|
|
* @param deliverNotifications TODO javadoc me please
|
2017-02-07 22:02:40 +01:00
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public void setDeliverOn(boolean deliverNotifications) {
|
2017-02-07 22:02:40 +01:00
|
|
|
addField(SubscribeOptionFields.deliver, FormField.Type.bool);
|
|
|
|
setAnswer(SubscribeOptionFields.deliver.getFieldName(), deliverNotifications);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if notifications should be delivered as aggregations or not.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2017-02-07 22:02:40 +01:00
|
|
|
* @return true to aggregate, false otherwise
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public boolean isDigestOn() {
|
2019-07-24 09:18:39 +02:00
|
|
|
return ParserUtils.parseXmlBoolean(getFieldValue(SubscribeOptionFields.digest));
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets whether notifications should be delivered as aggregations or not.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
|
|
|
* @param digestOn true to aggregate, false otherwise
|
2017-02-07 22:02:40 +01:00
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public void setDigestOn(boolean digestOn) {
|
2017-02-07 22:02:40 +01:00
|
|
|
addField(SubscribeOptionFields.deliver, FormField.Type.bool);
|
|
|
|
setAnswer(SubscribeOptionFields.deliver.getFieldName(), digestOn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the minimum number of milliseconds between sending notification digests.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2017-02-07 22:02:40 +01:00
|
|
|
* @return The frequency in milliseconds
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public int getDigestFrequency() {
|
2017-02-07 22:02:40 +01:00
|
|
|
return Integer.parseInt(getFieldValue(SubscribeOptionFields.digest_frequency));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the minimum number of milliseconds between sending notification digests.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2017-02-07 22:02:40 +01:00
|
|
|
* @param frequency The frequency in milliseconds
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public void setDigestFrequency(int frequency) {
|
2017-02-07 22:02:40 +01:00
|
|
|
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.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2017-02-07 22:02:40 +01:00
|
|
|
* @return The expiry date
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public Date getExpiry() {
|
2017-02-07 22:02:40 +01:00
|
|
|
String dateTime = getFieldValue(SubscribeOptionFields.expire);
|
2018-04-06 10:21:46 +02:00
|
|
|
try {
|
2017-02-07 22:02:40 +01:00
|
|
|
return XmppDateTime.parseDate(dateTime);
|
|
|
|
}
|
2018-04-06 10:21:46 +02:00
|
|
|
catch (ParseException e) {
|
2017-02-07 22:02:40 +01:00
|
|
|
UnknownFormatConversionException exc = new UnknownFormatConversionException(dateTime);
|
|
|
|
exc.initCause(e);
|
|
|
|
throw exc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the time at which the leased subscription will expire, or has expired.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2017-02-07 22:02:40 +01:00
|
|
|
* @param expire The expiry date
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public void setExpiry(Date expire) {
|
2017-02-07 22:02:40 +01:00
|
|
|
addField(SubscribeOptionFields.expire, FormField.Type.text_single);
|
|
|
|
setAnswer(SubscribeOptionFields.expire.getFieldName(), XmppDateTime.formatXEP0082Date(expire));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* Determines whether the entity wants to receive an XMPP message body in
|
2017-02-07 22:02:40 +01:00
|
|
|
* addition to the payload format.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2017-02-07 22:02:40 +01:00
|
|
|
* @return true to receive the message body, false otherwise
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public boolean isIncludeBody() {
|
2019-07-24 09:18:39 +02:00
|
|
|
return ParserUtils.parseXmlBoolean(getFieldValue(SubscribeOptionFields.include_body));
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* Sets whether the entity wants to receive an XMPP message body in
|
2017-02-07 22:02:40 +01:00
|
|
|
* addition to the payload format.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2017-02-07 22:02:40 +01:00
|
|
|
* @param include true to receive the message body, false otherwise
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public void setIncludeBody(boolean include) {
|
2017-02-07 22:02:40 +01:00
|
|
|
addField(SubscribeOptionFields.include_body, FormField.Type.bool);
|
|
|
|
setAnswer(SubscribeOptionFields.include_body.getFieldName(), include);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* Gets the {@link PresenceState} for which an entity wants to receive
|
2017-02-07 22:02:40 +01:00
|
|
|
* notifications.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2017-02-07 22:02:40 +01:00
|
|
|
* @return the list of states
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public List<PresenceState> getShowValues() {
|
2017-12-13 23:10:11 +01:00
|
|
|
ArrayList<PresenceState> result = new ArrayList<>(5);
|
2017-02-07 22:02:40 +01:00
|
|
|
|
2018-04-06 10:21:46 +02:00
|
|
|
for (String state : getFieldValues(SubscribeOptionFields.show_values)) {
|
2017-02-07 22:02:40 +01:00
|
|
|
result.add(PresenceState.valueOf(state));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the list of {@link PresenceState} for which an entity wants
|
|
|
|
* to receive notifications.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2017-02-07 22:02:40 +01:00
|
|
|
* @param stateValues The list of states
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public void setShowValues(Collection<PresenceState> stateValues) {
|
2017-12-13 23:10:11 +01:00
|
|
|
ArrayList<String> values = new ArrayList<>(stateValues.size());
|
2017-02-07 22:02:40 +01:00
|
|
|
|
2018-04-06 10:21:46 +02:00
|
|
|
for (PresenceState state : stateValues) {
|
2017-02-07 22:02:40 +01:00
|
|
|
values.add(state.toString());
|
|
|
|
}
|
|
|
|
addField(SubscribeOptionFields.show_values, FormField.Type.list_multi);
|
|
|
|
setAnswer(SubscribeOptionFields.show_values.getFieldName(), values);
|
|
|
|
}
|
|
|
|
|
2018-04-06 10:21:46 +02:00
|
|
|
private String getFieldValue(SubscribeOptionFields field) {
|
2017-02-07 22:02:40 +01:00
|
|
|
FormField formField = getField(field.getFieldName());
|
|
|
|
|
2018-04-06 13:24:54 +02:00
|
|
|
return formField.getFirstValue();
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
|
|
|
|
2018-04-06 10:21:46 +02:00
|
|
|
private List<String> getFieldValues(SubscribeOptionFields field) {
|
2017-02-07 22:02:40 +01:00
|
|
|
FormField formField = getField(field.getFieldName());
|
|
|
|
|
2018-04-06 13:24:54 +02:00
|
|
|
return formField.getValuesAsString();
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
|
|
|
|
2018-04-06 10:21:46 +02:00
|
|
|
private void addField(SubscribeOptionFields nodeField, FormField.Type type) {
|
2017-02-07 22:02:40 +01:00
|
|
|
String fieldName = nodeField.getFieldName();
|
|
|
|
|
2018-04-06 10:21:46 +02:00
|
|
|
if (getField(fieldName) == null) {
|
2019-06-10 16:58:38 +02:00
|
|
|
FormField.Builder field = FormField.builder(fieldName);
|
2017-02-07 22:02:40 +01:00
|
|
|
field.setType(type);
|
2019-06-10 16:58:38 +02:00
|
|
|
addField(field.build());
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|