SubscribeForm.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.pubsub;

  18. import java.text.ParseException;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.Date;
  22. import java.util.List;
  23. import java.util.UnknownFormatConversionException;

  24. import org.jxmpp.util.XmppDateTime;
  25. import org.jivesoftware.smackx.xdata.Form;
  26. import org.jivesoftware.smackx.xdata.FormField;
  27. import org.jivesoftware.smackx.xdata.packet.DataForm;

  28. /**
  29.  * A decorator for a {@link Form} to easily enable reading and updating
  30.  * of subscription options.  All operations read or update the underlying {@link DataForm}.
  31.  *
  32.  * <p>Unlike the {@link Form}.setAnswer(XXX)} methods, which throw an exception if the field does not
  33.  * exist, all <b>SubscribeForm.setXXX</b> methods will create the field in the wrapped form
  34.  * if it does not already exist.
  35.  *
  36.  * @author Robin Collier
  37.  */
  38. public class SubscribeForm extends Form
  39. {  
  40.     public SubscribeForm(DataForm configDataForm)
  41.     {
  42.         super(configDataForm);
  43.     }
  44.    
  45.     public SubscribeForm(Form subscribeOptionsForm)
  46.     {
  47.         super(subscribeOptionsForm.getDataFormToSend());
  48.     }
  49.    
  50.     public SubscribeForm(DataForm.Type formType)
  51.     {
  52.         super(formType);
  53.     }
  54.    
  55.     /**
  56.      * Determines if an entity wants to receive notifications.
  57.      *
  58.      * @return true if want to receive, false otherwise
  59.      */
  60.     public boolean isDeliverOn()
  61.     {
  62.         return parseBoolean(getFieldValue(SubscribeOptionFields.deliver));
  63.     }
  64.    
  65.     /**
  66.      * Sets whether an entity wants to receive notifications.
  67.      *
  68.      * @param deliverNotifications
  69.      */
  70.     public void setDeliverOn(boolean deliverNotifications)
  71.     {
  72.         addField(SubscribeOptionFields.deliver, FormField.Type.bool);
  73.         setAnswer(SubscribeOptionFields.deliver.getFieldName(), deliverNotifications);
  74.     }

  75.     /**
  76.      * Determines if notifications should be delivered as aggregations or not.
  77.      *
  78.      * @return true to aggregate, false otherwise
  79.      */
  80.     public boolean isDigestOn()
  81.     {
  82.         return parseBoolean(getFieldValue(SubscribeOptionFields.digest));
  83.     }
  84.    
  85.     /**
  86.      * Sets whether notifications should be delivered as aggregations or not.
  87.      *
  88.      * @param digestOn true to aggregate, false otherwise
  89.      */
  90.     public void setDigestOn(boolean digestOn)
  91.     {
  92.         addField(SubscribeOptionFields.deliver, FormField.Type.bool);
  93.         setAnswer(SubscribeOptionFields.deliver.getFieldName(), digestOn);
  94.     }

  95.     /**
  96.      * Gets the minimum number of milliseconds between sending notification digests
  97.      *
  98.      * @return The frequency in milliseconds
  99.      */
  100.     public int getDigestFrequency()
  101.     {
  102.         return Integer.parseInt(getFieldValue(SubscribeOptionFields.digest_frequency));
  103.     }

  104.     /**
  105.      * Sets the minimum number of milliseconds between sending notification digests
  106.      *
  107.      * @param frequency The frequency in milliseconds
  108.      */
  109.     public void setDigestFrequency(int frequency)
  110.     {
  111.         addField(SubscribeOptionFields.digest_frequency, FormField.Type.text_single);
  112.         setAnswer(SubscribeOptionFields.digest_frequency.getFieldName(), frequency);
  113.     }

  114.     /**
  115.      * Get the time at which the leased subscription will expire, or has expired.
  116.      *
  117.      * @return The expiry date
  118.      */
  119.     public Date getExpiry()
  120.     {
  121.         String dateTime = getFieldValue(SubscribeOptionFields.expire);
  122.         try
  123.         {
  124.             return XmppDateTime.parseDate(dateTime);
  125.         }
  126.         catch (ParseException e)
  127.         {
  128.             UnknownFormatConversionException exc = new UnknownFormatConversionException(dateTime);
  129.             exc.initCause(e);
  130.             throw exc;
  131.         }
  132.     }
  133.    
  134.     /**
  135.      * Sets the time at which the leased subscription will expire, or has expired.
  136.      *
  137.      * @param expire The expiry date
  138.      */
  139.     public void setExpiry(Date expire)
  140.     {
  141.         addField(SubscribeOptionFields.expire, FormField.Type.text_single);
  142.         setAnswer(SubscribeOptionFields.expire.getFieldName(), XmppDateTime.formatXEP0082Date(expire));
  143.     }
  144.    
  145.     /**
  146.      * Determines whether the entity wants to receive an XMPP message body in
  147.      * addition to the payload format.
  148.      *
  149.      * @return true to receive the message body, false otherwise
  150.      */
  151.     public boolean isIncludeBody()
  152.     {
  153.         return parseBoolean(getFieldValue(SubscribeOptionFields.include_body));
  154.     }
  155.    
  156.     /**
  157.      * Sets whether the entity wants to receive an XMPP message body in
  158.      * addition to the payload format.
  159.      *
  160.      * @param include true to receive the message body, false otherwise
  161.      */
  162.     public void setIncludeBody(boolean include)
  163.     {
  164.         addField(SubscribeOptionFields.include_body, FormField.Type.bool);
  165.         setAnswer(SubscribeOptionFields.include_body.getFieldName(), include);
  166.     }

  167.     /**
  168.      * Gets the {@link PresenceState} for which an entity wants to receive
  169.      * notifications.
  170.      *
  171.      * @return the list of states
  172.      */
  173.     public List<PresenceState> getShowValues()
  174.     {
  175.         ArrayList<PresenceState> result = new ArrayList<PresenceState>(5);
  176.        
  177.         for (String state : getFieldValues(SubscribeOptionFields.show_values))
  178.         {
  179.             result.add(PresenceState.valueOf(state));
  180.         }
  181.         return result;
  182.     }
  183.    
  184.     /**
  185.      * Sets the list of {@link PresenceState} for which an entity wants
  186.      * to receive notifications.
  187.      *
  188.      * @param stateValues The list of states
  189.      */
  190.     public void setShowValues(Collection<PresenceState> stateValues)
  191.     {
  192.         ArrayList<String> values = new ArrayList<String>(stateValues.size());
  193.        
  194.         for (PresenceState state : stateValues)
  195.         {
  196.             values.add(state.toString());
  197.         }
  198.         addField(SubscribeOptionFields.show_values, FormField.Type.list_multi);
  199.         setAnswer(SubscribeOptionFields.show_values.getFieldName(), values);
  200.     }
  201.    
  202.    
  203.     static private boolean parseBoolean(String fieldValue)
  204.     {
  205.         return ("1".equals(fieldValue) || "true".equals(fieldValue));
  206.     }

  207.     private String getFieldValue(SubscribeOptionFields field)
  208.     {
  209.         FormField formField = getField(field.getFieldName());
  210.        
  211.         return formField.getValues().get(0);
  212.     }

  213.     private List<String> getFieldValues(SubscribeOptionFields field)
  214.     {
  215.         FormField formField = getField(field.getFieldName());
  216.        
  217.         return formField.getValues();
  218.     }

  219.     private void addField(SubscribeOptionFields nodeField, FormField.Type type)
  220.     {
  221.         String fieldName = nodeField.getFieldName();
  222.        
  223.         if (getField(fieldName) == null)
  224.         {
  225.             FormField field = new FormField(fieldName);
  226.             field.setType(type);
  227.             addField(field);
  228.         }
  229.     }
  230. }