001/*
002 *  Copyright 2019 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.workspaces.project.notification;
017
018import java.io.ByteArrayOutputStream;
019import java.io.IOException;
020import java.io.InputStream;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.avalon.framework.context.Context;
025import org.apache.avalon.framework.context.ContextException;
026import org.apache.avalon.framework.context.Contextualizable;
027import org.apache.avalon.framework.service.ServiceException;
028import org.apache.avalon.framework.service.ServiceManager;
029import org.apache.avalon.framework.service.Serviceable;
030import org.apache.cocoon.components.ContextHelper;
031import org.apache.cocoon.environment.Request;
032import org.apache.commons.lang.StringUtils;
033import org.apache.excalibur.source.Source;
034import org.apache.excalibur.source.SourceResolver;
035import org.apache.excalibur.source.SourceUtil;
036
037import org.ametys.core.group.GroupManager;
038import org.ametys.core.observation.AsyncObserver;
039import org.ametys.core.observation.Event;
040import org.ametys.core.user.UserManager;
041import org.ametys.core.util.I18nUtils;
042import org.ametys.core.util.mail.SendMailHelper;
043import org.ametys.plugins.repository.AmetysObjectResolver;
044import org.ametys.plugins.workspaces.ObservationConstants;
045import org.ametys.plugins.workspaces.members.ProjectMemberManager;
046import org.ametys.plugins.workspaces.project.ProjectManager;
047import org.ametys.plugins.workspaces.project.objects.Project;
048import org.ametys.runtime.i18n.I18nizableText;
049import org.ametys.runtime.plugin.component.AbstractLogEnabled;
050import org.ametys.runtime.plugin.component.PluginAware;
051import org.ametys.web.WebConstants;
052import org.ametys.web.population.PopulationContextHelper;
053import org.ametys.web.renderingcontext.RenderingContext;
054import org.ametys.web.renderingcontext.RenderingContextHandler;
055import org.ametys.web.repository.site.Site;
056import org.ametys.web.repository.site.SiteManager;
057
058import jakarta.mail.MessagingException;
059
060/**
061 * Abstract observer for sending mail to members
062 */
063public abstract class AbstractMemberMailNotifierObserver  extends AbstractLogEnabled implements AsyncObserver, PluginAware, Serviceable, Contextualizable
064{
065    /** The avalon context */
066    protected Context _context;
067    /** The name of current plugin */
068    protected String _pluginName;
069    /** The Ametys Object resolver */
070    protected AmetysObjectResolver _resolver;
071    /** The i18n utils */
072    protected I18nUtils _i18nUtils;
073    /** The project member manager */
074    protected ProjectMemberManager _projectMemberManager;
075    /** Project manager */
076    protected ProjectManager _projectManager;
077    /** Site manager */
078    protected SiteManager _siteManager;
079    /** Population context helper */
080    protected PopulationContextHelper _populationContextHelper;
081    /** Source Resolver */
082    protected SourceResolver _srcResolver;
083    /** User manager */
084    protected UserManager _userManager;
085    /** Group manager */
086    protected GroupManager _groupManager;
087    /** The rendering context */
088    protected RenderingContextHandler _renderingContextHandler;
089    
090    
091    @Override
092    public void setPluginInfo(String pluginName, String featureName, String id)
093    {
094        _pluginName = pluginName;
095    }
096    
097    @Override
098    public void contextualize(Context context) throws ContextException
099    {
100        _context = context;
101    }
102    
103    public void service(ServiceManager manager) throws ServiceException
104    {
105        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
106        _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE);
107        _projectMemberManager = (ProjectMemberManager) manager.lookup(ProjectMemberManager.ROLE);
108        _projectManager = (ProjectManager) manager.lookup(ProjectManager.ROLE);
109        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
110        _populationContextHelper = (PopulationContextHelper) manager.lookup(org.ametys.core.user.population.PopulationContextHelper.ROLE);
111        _srcResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
112        _userManager = (UserManager) manager.lookup(UserManager.ROLE);
113        _groupManager = (GroupManager) manager.lookup(GroupManager.ROLE);
114        _renderingContextHandler = (RenderingContextHandler) manager.lookup(RenderingContextHandler.ROLE);
115    }
116    
117    @Override
118    public int getPriority()
119    {
120        return MIN_PRIORITY;
121    }
122    
123    @Override
124    public void observe(Event event, Map<String, Object> transientVars) throws Exception
125    {
126        Map<String, Object> args = event.getArguments();
127        
128        String projectId = (String) args.get(ObservationConstants.ARGS_PROJECT_ID);
129        Project project = _resolver.resolveById(projectId);
130        Site site = project.getSite();
131        String lang = site.getSitemaps().iterator().next().getName();
132        // Compute subject and body
133        I18nizableText i18nSubject = getI18nSubject(event, project);
134        
135        // Send mail to removed members
136        Map<String, List<String>> recipientsByLanguage = getUserToNotifyByLanguage(event, project);
137        
138        for (String language : recipientsByLanguage.keySet())
139        {
140            String userLanguage = StringUtils.defaultIfBlank(language, lang);
141            
142            String subject = _i18nUtils.translate(i18nSubject, userLanguage);
143            
144            String mailBody = null;
145            Source source = null;
146            RenderingContext currentContext = _renderingContextHandler.getRenderingContext();
147            try
148            {
149                // Force rendering context.FRONT to resolve URI
150                _renderingContextHandler.setRenderingContext(RenderingContext.FRONT);
151                Request request = ContextHelper.getRequest(_context);
152                request.setAttribute("forceAbsoluteUrl", true);
153                request.setAttribute("lang", userLanguage);
154                request.setAttribute(WebConstants.REQUEST_ATTR_SITE, site);
155                request.setAttribute(WebConstants.REQUEST_ATTR_SITE_NAME, site.getName());
156                request.setAttribute(WebConstants.REQUEST_ATTR_SKIN_ID, site.getSkinId());
157                source = _srcResolver.resolveURI(getMailBodyURI(event, project), null, Map.of("event", event, "project", project));
158                try (InputStream is = source.getInputStream())
159                {
160                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
161                    SourceUtil.copy(is, bos);
162                    
163                    mailBody = bos.toString("UTF-8");
164                }
165            }
166            catch (IOException e)
167            {
168                throw new RuntimeException("Failed to create mail body", e);
169            }
170            finally
171            {
172                _renderingContextHandler.setRenderingContext(currentContext);
173                
174                if (source != null)
175                {
176                    _srcResolver.release(source);
177                }
178            }
179            
180            try
181            {
182                SendMailHelper.newMail()
183                    .withSubject(subject)
184                    .withHTMLBody(mailBody)
185                    .withRecipients(recipientsByLanguage.get(language))
186                    .withAsync(true)
187                    .withInlineCSS(false)
188                    .sendMail();
189            }
190            catch (MessagingException | IOException e)
191            {
192                getLogger().warn("Could not send a notification e-mail to " + recipientsByLanguage + " following his removal from the project " + project.getTitle(), e);
193            }
194        }
195    }
196    
197    /**
198     * Get recipients' emails sorted by language
199     * @param event the event
200     * @param project the project
201     * @return the recipients' emails sorted by language
202     */
203    protected abstract Map<String, List<String>> getUserToNotifyByLanguage(Event event, Project project);
204    
205    /**
206     * Returns the URI for HTML mail body
207     * @param event the event
208     * @param project the project
209     * @return The URI for HTML mail body
210     */
211    protected String getMailBodyURI(Event event, Project project)
212    {
213        return "cocoon://_plugins/workspaces/notification-mail-member";
214    }
215    
216    /**
217     * Get the {@link I18nizableText} for mail subject
218     * @param event the event
219     * @param project the project
220     * @return the {@link I18nizableText} for subject
221     */
222    protected abstract I18nizableText getI18nSubject(Event event, Project project);
223}