Exemplo n.º 1
0
 /**
  * Returns true if events in general can be sent. This method checks basic conditions common to
  * all type of event notifications (e.g. item was published, node configuration has changed, new
  * child node was added to collection node, etc.).
  *
  * @return true if events in general can be sent.
  */
 private boolean canSendEvents() {
   // Check if the subscription is active
   if (!isActive()) {
     return false;
   }
   // Check if delivery of notifications is disabled
   if (!shouldDeliverNotifications()) {
     return false;
   }
   // Check if delivery is subject to presence-based policy
   if (!getPresenceStates().isEmpty()) {
     Collection<String> shows = service.getShowPresences(jid);
     if (shows.isEmpty() || Collections.disjoint(getPresenceStates(), shows)) {
       return false;
     }
   }
   // Check if node is only sending events when user is online
   if (node.isPresenceBasedDelivery()) {
     // Check that user is online
     if (service.getShowPresences(jid).isEmpty()) {
       return false;
     }
   }
   return true;
 }
Exemplo n.º 2
0
  void configure(DataForm options) {
    List<String> values;
    String booleanValue;

    boolean wasUsingPresence = !presenceStates.isEmpty();

    // Remove this field from the form
    options.removeField("FORM_TYPE");
    // Process and remove specific collection node fields
    FormField collectionField = options.getField("pubsub#subscription_type");
    if (collectionField != null) {
      values = collectionField.getValues();
      if (values.size() > 0) {
        type = Type.valueOf(values.get(0));
      }
      options.removeField("pubsub#subscription_type");
    }
    collectionField = options.getField("pubsub#subscription_depth");
    if (collectionField != null) {
      values = collectionField.getValues();
      depth = "all".equals(values.get(0)) ? 0 : 1;
      options.removeField("pubsub#subscription_depth");
    }
    // If there are more fields in the form then process them and set that
    // the subscription has been configured
    for (FormField field : options.getFields()) {
      boolean fieldExists = true;
      if ("pubsub#deliver".equals(field.getVariable())) {
        values = field.getValues();
        booleanValue = (values.size() > 0 ? values.get(0) : "1");
        deliverNotifications = "1".equals(booleanValue);
      } else if ("pubsub#digest".equals(field.getVariable())) {
        values = field.getValues();
        booleanValue = (values.size() > 0 ? values.get(0) : "1");
        usingDigest = "1".equals(booleanValue);
      } else if ("pubsub#digest_frequency".equals(field.getVariable())) {
        values = field.getValues();
        digestFrequency = values.size() > 0 ? Integer.parseInt(values.get(0)) : 86400000;
      } else if ("pubsub#expire".equals(field.getVariable())) {
        values = field.getValues();
        synchronized (dateFormat) {
          try {
            expire = dateFormat.parse(values.get(0));
          } catch (ParseException e) {
            Log.error("Error parsing date", e);
          }
        }
      } else if ("pubsub#include_body".equals(field.getVariable())) {
        values = field.getValues();
        booleanValue = (values.size() > 0 ? values.get(0) : "1");
        includingBody = "1".equals(booleanValue);
      } else if ("pubsub#show-values".equals(field.getVariable())) {
        // Get the new list of presence states for which an entity wants to
        // receive notifications
        presenceStates = new ArrayList<String>();
        for (String value : field.getValues()) {
          try {
            presenceStates.add(value);
          } catch (Exception e) {
            // Do nothing
          }
        }
      } else if ("x-pubsub#keywords".equals(field.getVariable())) {
        values = field.getValues();
        keyword = values.isEmpty() ? null : values.get(0);
      } else {
        fieldExists = false;
      }
      if (fieldExists) {
        // Subscription has been configured so set the next state
        if (node.getAccessModel().isAuthorizationRequired() && !node.isAdmin(owner)) {
          state = State.pending;
        } else {
          state = State.subscribed;
        }
      }
    }
    if (savedToDB) {
      // Update the subscription in the backend store
      PubSubPersistenceManager.saveSubscription(service, node, this, false);
    }
    // Check if the service needs to subscribe or unsubscribe from the owner presence
    if (!node.isPresenceBasedDelivery() && wasUsingPresence != !presenceStates.isEmpty()) {
      if (presenceStates.isEmpty()) {
        service.presenceSubscriptionNotRequired(node, owner);
      } else {
        service.presenceSubscriptionRequired(node, owner);
      }
    }
  }