001/** 002 * 003 * Copyright the original author or authors 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.pubsub; 018 019import java.text.ParseException; 020import java.util.ArrayList; 021import java.util.Collection; 022import java.util.Date; 023import java.util.List; 024import java.util.UnknownFormatConversionException; 025 026import org.jivesoftware.smackx.xdata.Form; 027import org.jivesoftware.smackx.xdata.FormField; 028import org.jivesoftware.smackx.xdata.packet.DataForm; 029 030import org.jxmpp.util.XmppDateTime; 031 032/** 033 * A decorator for a {@link Form} to easily enable reading and updating 034 * of subscription options. All operations read or update the underlying {@link DataForm}. 035 * 036 * <p>Unlike the {@link Form}.setAnswer(XXX)} methods, which throw an exception if the field does not 037 * exist, all <b>SubscribeForm.setXXX</b> methods will create the field in the wrapped form 038 * if it does not already exist. 039 * 040 * @author Robin Collier 041 */ 042public class SubscribeForm extends Form 043{ 044 public SubscribeForm(DataForm configDataForm) 045 { 046 super(configDataForm); 047 } 048 049 public SubscribeForm(Form subscribeOptionsForm) 050 { 051 super(subscribeOptionsForm.getDataFormToSend()); 052 } 053 054 public SubscribeForm(DataForm.Type formType) 055 { 056 super(formType); 057 } 058 059 /** 060 * Determines if an entity wants to receive notifications. 061 * 062 * @return true if want to receive, false otherwise 063 */ 064 public boolean isDeliverOn() 065 { 066 return parseBoolean(getFieldValue(SubscribeOptionFields.deliver)); 067 } 068 069 /** 070 * Sets whether an entity wants to receive notifications. 071 * 072 * @param deliverNotifications 073 */ 074 public void setDeliverOn(boolean deliverNotifications) 075 { 076 addField(SubscribeOptionFields.deliver, FormField.Type.bool); 077 setAnswer(SubscribeOptionFields.deliver.getFieldName(), deliverNotifications); 078 } 079 080 /** 081 * Determines if notifications should be delivered as aggregations or not. 082 * 083 * @return true to aggregate, false otherwise 084 */ 085 public boolean isDigestOn() 086 { 087 return parseBoolean(getFieldValue(SubscribeOptionFields.digest)); 088 } 089 090 /** 091 * Sets whether notifications should be delivered as aggregations or not. 092 * 093 * @param digestOn true to aggregate, false otherwise 094 */ 095 public void setDigestOn(boolean digestOn) 096 { 097 addField(SubscribeOptionFields.deliver, FormField.Type.bool); 098 setAnswer(SubscribeOptionFields.deliver.getFieldName(), digestOn); 099 } 100 101 /** 102 * Gets the minimum number of milliseconds between sending notification digests. 103 * 104 * @return The frequency in milliseconds 105 */ 106 public int getDigestFrequency() 107 { 108 return Integer.parseInt(getFieldValue(SubscribeOptionFields.digest_frequency)); 109 } 110 111 /** 112 * Sets the minimum number of milliseconds between sending notification digests. 113 * 114 * @param frequency The frequency in milliseconds 115 */ 116 public void setDigestFrequency(int frequency) 117 { 118 addField(SubscribeOptionFields.digest_frequency, FormField.Type.text_single); 119 setAnswer(SubscribeOptionFields.digest_frequency.getFieldName(), frequency); 120 } 121 122 /** 123 * Get the time at which the leased subscription will expire, or has expired. 124 * 125 * @return The expiry date 126 */ 127 public Date getExpiry() 128 { 129 String dateTime = getFieldValue(SubscribeOptionFields.expire); 130 try 131 { 132 return XmppDateTime.parseDate(dateTime); 133 } 134 catch (ParseException e) 135 { 136 UnknownFormatConversionException exc = new UnknownFormatConversionException(dateTime); 137 exc.initCause(e); 138 throw exc; 139 } 140 } 141 142 /** 143 * Sets the time at which the leased subscription will expire, or has expired. 144 * 145 * @param expire The expiry date 146 */ 147 public void setExpiry(Date expire) 148 { 149 addField(SubscribeOptionFields.expire, FormField.Type.text_single); 150 setAnswer(SubscribeOptionFields.expire.getFieldName(), XmppDateTime.formatXEP0082Date(expire)); 151 } 152 153 /** 154 * Determines whether the entity wants to receive an XMPP message body in 155 * addition to the payload format. 156 * 157 * @return true to receive the message body, false otherwise 158 */ 159 public boolean isIncludeBody() 160 { 161 return parseBoolean(getFieldValue(SubscribeOptionFields.include_body)); 162 } 163 164 /** 165 * Sets whether the entity wants to receive an XMPP message body in 166 * addition to the payload format. 167 * 168 * @param include true to receive the message body, false otherwise 169 */ 170 public void setIncludeBody(boolean include) 171 { 172 addField(SubscribeOptionFields.include_body, FormField.Type.bool); 173 setAnswer(SubscribeOptionFields.include_body.getFieldName(), include); 174 } 175 176 /** 177 * Gets the {@link PresenceState} for which an entity wants to receive 178 * notifications. 179 * 180 * @return the list of states 181 */ 182 public List<PresenceState> getShowValues() 183 { 184 ArrayList<PresenceState> result = new ArrayList<>(5); 185 186 for (String state : getFieldValues(SubscribeOptionFields.show_values)) 187 { 188 result.add(PresenceState.valueOf(state)); 189 } 190 return result; 191 } 192 193 /** 194 * Sets the list of {@link PresenceState} for which an entity wants 195 * to receive notifications. 196 * 197 * @param stateValues The list of states 198 */ 199 public void setShowValues(Collection<PresenceState> stateValues) 200 { 201 ArrayList<String> values = new ArrayList<>(stateValues.size()); 202 203 for (PresenceState state : stateValues) 204 { 205 values.add(state.toString()); 206 } 207 addField(SubscribeOptionFields.show_values, FormField.Type.list_multi); 208 setAnswer(SubscribeOptionFields.show_values.getFieldName(), values); 209 } 210 211 212 static private boolean parseBoolean(String fieldValue) 213 { 214 return ("1".equals(fieldValue) || "true".equals(fieldValue)); 215 } 216 217 private String getFieldValue(SubscribeOptionFields field) 218 { 219 FormField formField = getField(field.getFieldName()); 220 221 return formField.getValues().get(0); 222 } 223 224 private List<String> getFieldValues(SubscribeOptionFields field) 225 { 226 FormField formField = getField(field.getFieldName()); 227 228 return formField.getValues(); 229 } 230 231 private void addField(SubscribeOptionFields nodeField, FormField.Type type) 232 { 233 String fieldName = nodeField.getFieldName(); 234 235 if (getField(fieldName) == null) 236 { 237 FormField field = new FormField(fieldName); 238 field.setType(type); 239 addField(field); 240 } 241 } 242}