/** * 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.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}. * *
Unlike the {@link Form}.setAnswer(XXX)} methods, which throw an exception if the field does not
* exist, all SubscribeForm.setXXX methods will create the field in the wrapped form
* if it does not already exist.
*
* @author Robin Collier
*/
public class SubscribeForm extends Form
{
private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
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 format.parse(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(), format.format(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