001/* 002 * Copyright 2017 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.pagesubscription.observer; 017 018import java.util.Collection; 019import java.util.HashSet; 020import java.util.Map; 021import java.util.Set; 022 023import javax.jcr.Node; 024import javax.jcr.RepositoryException; 025import javax.jcr.Value; 026import javax.mail.MessagingException; 027 028import org.apache.avalon.framework.service.ServiceException; 029import org.apache.avalon.framework.service.ServiceManager; 030import org.apache.avalon.framework.service.Serviceable; 031 032import org.ametys.core.observation.Event; 033import org.ametys.core.observation.Observer; 034import org.ametys.core.user.UserManager; 035import org.ametys.core.util.I18nUtils; 036import org.ametys.core.util.mail.SendMailHelper; 037import org.ametys.plugins.repository.AmetysObjectResolver; 038import org.ametys.plugins.repository.RepositoryConstants; 039import org.ametys.plugins.repository.jcr.JCRAmetysObject; 040import org.ametys.runtime.config.Config; 041import org.ametys.runtime.plugin.component.AbstractLogEnabled; 042import org.ametys.web.repository.page.Page; 043import org.ametys.web.repository.site.Site; 044import org.ametys.web.site.SiteConfigurationExtensionPoint; 045 046/** 047 * Abstract implementation of the observers of Page Subscription. 048 */ 049public abstract class AbstractPageSubscriptionObserver extends AbstractLogEnabled implements Observer, Serviceable 050{ 051 /** Constant for the attachment node name. */ 052 public static final String SUBSCRIBERS_PROPERTY_NAME = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":subscriberMails"; 053 054 /** The user manager */ 055 protected UserManager _userManager; 056 /** The site configuration */ 057 protected SiteConfigurationExtensionPoint _siteConfigurationEP; 058 /** The Ametys object resolver */ 059 protected AmetysObjectResolver _resolver; 060 /** The i18n utils */ 061 protected I18nUtils _i18nUtils; 062 063 @Override 064 public void service(ServiceManager smanager) throws ServiceException 065 { 066 _userManager = (UserManager) smanager.lookup(UserManager.ROLE); 067 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 068 _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE); 069 _siteConfigurationEP = (SiteConfigurationExtensionPoint) smanager.lookup(SiteConfigurationExtensionPoint.ROLE); 070 } 071 072 public int getPriority(Event event) 073 { 074 return Observer.MIN_PRIORITY; 075 } 076 077 @Override 078 public void observe(Event event, Map<String, Object> transientVars) 079 { 080 Collection<Page> pages = _getPages(event); 081 for (Page page : pages) 082 { 083 if (isNotificationEnabled(page)) 084 { 085 Set<String> subscribers = _getSubscribers((JCRAmetysObject) page); 086 if (!subscribers.isEmpty()) 087 { 088 String textBody = _getMailBody(event, page); 089 String subject = _getMailSubject(event, page); 090 091 String sender = Config.getInstance().getValueAsString("smtp.mail.from"); 092 093 for (String subscriberMail : subscribers) 094 { 095 if (!_userManager.getUser(event.getIssuer()).getEmail().equals(subscriberMail)) 096 { 097 try 098 { 099 SendMailHelper.sendMail(subject, null, textBody, subscriberMail, sender); 100 } 101 catch (MessagingException e) 102 { 103 getLogger().error("Unable to send a mail to '" + subscriberMail); 104 } 105 } 106 } 107 } 108 } 109 } 110 } 111 112 /** 113 * Determines if the notification is currently enabled 114 * @param page The page 115 * @return true if the notification is enabled 116 */ 117 protected boolean isNotificationEnabled(Page page) 118 { 119 Site site = page.getSite(); 120 return _siteConfigurationEP.getValueAsBoolean(site.getName(), getSiteParameterId()); 121 } 122 123 /** 124 * Id of the site parameter to enable/disable the notification 125 * @return The id of site 126 */ 127 protected abstract String getSiteParameterId(); 128 129 /** 130 * Get the page's subscribers 131 * @param page the page 132 * @return the registered emails 133 */ 134 protected Set<String> _getSubscribers(JCRAmetysObject page) 135 { 136 Set<String> subscriberMails = new HashSet<>(); 137 138 try 139 { 140 Node node = page.getNode(); 141 if (node.hasProperty(SUBSCRIBERS_PROPERTY_NAME)) 142 { 143 Value[] values = node.getProperty(SUBSCRIBERS_PROPERTY_NAME).getValues(); 144 for (Value value : values) 145 { 146 subscriberMails.add(value.getString()); 147 } 148 } 149 } 150 catch (RepositoryException e) 151 { 152 getLogger().error("Unable to retrieve subscribers for page of id'" + page.getId() + "'", e); 153 } 154 155 return subscriberMails; 156 } 157 158 /** 159 * Get the absolute URL of a page 160 * @param page The page to retrieve the url 161 * @return The absolute url of the page 162 */ 163 protected String _getAbsolutePageUrl(Page page) 164 { 165 return new StringBuilder().append(page.getSite().getUrl()) 166 .append("/").append(page.getSitemapName()) 167 .append("/").append(page.getPathInSitemap()) 168 .append(".html").toString(); 169 } 170 171 /** 172 * Create the mail subject depend on the event 173 * @param event the event 174 * @param page the page 175 * @return the mail subject 176 */ 177 protected abstract String _getMailSubject(Event event, Page page); 178 179 /** 180 * Create the mail body depend on the event 181 * @param event the event 182 * @param page the page 183 * @return the mail body 184 */ 185 protected abstract String _getMailBody(Event event, Page page); 186 187 /** 188 * Get the pages linked to the event 189 * @param event the event 190 * @return the pages linked to the event 191 */ 192 protected abstract Collection<Page> _getPages(Event event); 193}