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