public void notifySubscribers(String accessCode) { Element invitationElem = getInvitationElement(accessCode); if (invitationElem == null) { Logger.getLogger(getClass()) .warn("invitation for subscription notification not found: " + accessCode); return; } Element subscriberListElem = XmlUtil.getChildByTagName(invitationElem, "subscriberList"); if (subscriberListElem == null) { return; } NodeList subscriberList = subscriberListElem.getElementsByTagName("subscriber"); if ((subscriberList == null) || (subscriberList.getLength() == 0)) { return; } Element notifyElem = XmlUtil.getChildByTagName(invitationElem, "notifySubscribers"); if (notifyElem == null) { notifyElem = doc.createElement("notifySubscribers"); invitationElem.appendChild(notifyElem); } XmlUtil.setChildText(notifyElem, "changed", Long.toString(System.currentTimeMillis())); changed = true; }
public boolean addSubscriber(String virtualUser, String subscriberEmail) { Element watchedInvitation = getInvitationElementByVirtualUser(virtualUser); if (watchedInvitation == null) { return false; } Element subscriberListElem = XmlUtil.getChildByTagName(watchedInvitation, "subscriberList"); if (subscriberListElem == null) { subscriberListElem = doc.createElement("subscriberList"); watchedInvitation.appendChild(subscriberListElem); } NodeList subscriberList = subscriberListElem.getElementsByTagName("subscriber"); if (subscriberList != null) { int listLength = subscriberList.getLength(); for (int i = 0; i < listLength; i++) { Element subscriberElement = (Element) subscriberList.item(i); if (XmlUtil.getChildText(subscriberElement, "email").equals(subscriberEmail)) { return false; } } } Element subscriberElem = doc.createElement("subscriber"); XmlUtil.setChildText(subscriberElem, "email", subscriberEmail); XmlUtil.setChildText(subscriberElem, "code", generateAccessCode()); subscriberListElem.appendChild(subscriberElem); changed = true; return true; }
public ArrayList<String> listSubscribers(String owner, String path) { ArrayList<String> subscribers = new ArrayList<String>(); Element watchedInvitation = getInvitationElemenByOwnerAndPath(owner, path); if (watchedInvitation == null) { return subscribers; } Element subscriberListElem = XmlUtil.getChildByTagName(watchedInvitation, "subscriberList"); if (subscriberListElem == null) { return subscribers; } NodeList subscriberList = subscriberListElem.getElementsByTagName("subscriber"); if (subscriberList != null) { int listLength = subscriberList.getLength(); for (int i = 0; i < listLength; i++) { Element subscriberElement = (Element) subscriberList.item(i); subscribers.add(XmlUtil.getChildText(subscriberElement, "email")); } } if (subscribers.size() > 1) { Collections.sort(subscribers); } return subscribers; }
public boolean unsubscribe(String virtualUser, String subscriberEmail, String code) { Element watchedInvitation = getInvitationElementByVirtualUser(virtualUser); if (watchedInvitation == null) { return false; } Element subscriberListElem = XmlUtil.getChildByTagName(watchedInvitation, "subscriberList"); if (subscriberListElem == null) { return false; } NodeList subscriberList = subscriberListElem.getElementsByTagName("subscriber"); if (subscriberList != null) { int listLength = subscriberList.getLength(); for (int i = 0; i < listLength; i++) { Element subscriberElement = (Element) subscriberList.item(i); String email = XmlUtil.getChildText(subscriberElement, "email"); if ((email != null) && email.equals(subscriberEmail)) { String subscriptionCode = XmlUtil.getChildText(subscriberElement, "code"); if ((subscriptionCode != null) && subscriptionCode.equals(code)) { subscriberListElem.removeChild(subscriberElement); if (listLength == 1) { watchedInvitation.removeChild(subscriberListElem); } changed = true; return true; } else { Logger.getLogger(getClass()) .warn( "unsubscribe attempt with invalid code, virtualUser="******" email=" + subscriberEmail + " code=" + code); } } } } return false; }
private void checkSubscriberNotifications() { if (Logger.getLogger(getClass()).isDebugEnabled()) { Logger.getLogger(getClass()).debug("checking subscriber notifications"); } NodeList invitationList = invitationRoot.getElementsByTagName("invitation"); if (invitationList == null) { return; } int listLength = invitationList.getLength(); for (int i = 0; i < listLength; i++) { Element invitationElem = (Element) invitationList.item(i); Element notifyElem = XmlUtil.getChildByTagName(invitationElem, "notifySubscribers"); if (notifyElem != null) { Element changedElem = XmlUtil.getChildByTagName(notifyElem, "changed"); if (changedElem != null) { long lastChangeTime = 0; try { lastChangeTime = Long.parseLong(XmlUtil.getElementText(changedElem)); } catch (NumberFormatException numEx) { Logger.getLogger(getClass()).error("invalid change time", numEx); } if (System.currentTimeMillis() - lastChangeTime > NOTIFIY_DELAY_AFTER_CHANGE) { // wait some time after blog change until subscribers are notified, in case more entries // will be created soon long lastNotified = 0; String lastNotificationTime = XmlUtil.getChildText(notifyElem, "lastNotified"); if (!CommonUtils.isEmpty(lastNotificationTime)) { try { lastNotified = Long.parseLong(lastNotificationTime); } catch (NumberFormatException numEx) { Logger.getLogger(getClass()) .error("invalid lastNotified time: " + lastNotificationTime); } } if (System.currentTimeMillis() - lastNotified > MIN_SUBSCRIBER_NOTIFICATION_INTERVAL) { Element subscriberListElem = XmlUtil.getChildByTagName(invitationElem, "subscriberList"); if (subscriberListElem != null) { NodeList subscriberList = subscriberListElem.getElementsByTagName("subscriber"); if (subscriberList != null) { String blogPath = XmlUtil.getChildText(invitationElem, "path"); String virtualUser = XmlUtil.getChildText(invitationElem, "virtualUser"); String blogTitle = MetaInfManager.getInstance().getDescription(blogPath, "."); if (CommonUtils.isEmpty(blogTitle)) { blogTitle = virtualUser; } String blogAccessCode = invitationElem.getAttribute("accessCode"); int subscriberListLength = subscriberList.getLength(); for (int k = 0; k < subscriberListLength; k++) { Element subscriberElement = (Element) subscriberList.item(k); String email = XmlUtil.getChildText(subscriberElement, "email"); String code = XmlUtil.getChildText(subscriberElement, "code"); sendSubscriberNotification(email, code, blogTitle, virtualUser, blogAccessCode); } } } notifyElem.removeChild(changedElem); XmlUtil.setChildText( notifyElem, "lastNotified", Long.toString(System.currentTimeMillis())); changed = true; } } } } } }