1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.mail.script

File AbstractMailScriptService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

2
16
4
1
151
70
8
0.5
4
4
2

Classes

Class Line # Actions
AbstractMailScriptService 43 16 0% 8 5
0.7727272577.3%
 

Contributing tests

This file is covered by 5 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.mail.script;
21   
22    import java.util.Collections;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Provider;
27    import javax.mail.MessagingException;
28    import javax.mail.internet.MimeMessage;
29   
30    import org.xwiki.component.manager.ComponentLookupException;
31    import org.xwiki.component.manager.ComponentManager;
32    import org.xwiki.context.Execution;
33    import org.xwiki.mail.MailListener;
34    import org.xwiki.mail.MailSender;
35    import org.xwiki.mail.MailSenderConfiguration;
36    import org.xwiki.mail.SessionFactory;
37    import org.xwiki.script.service.ScriptService;
38   
39    /**
40    * @version $Id $
41    * @since 6.4M3
42    */
 
43    public abstract class AbstractMailScriptService implements ScriptService
44    {
45    @Inject
46    protected MailSender mailSender;
47   
48    @Inject
49    @Named("context")
50    protected Provider<ComponentManager> componentManagerProvider;
51   
52    /**
53    * Provides access to the current context.
54    */
55    @Inject
56    protected Execution execution;
57   
58    @Inject
59    protected SessionFactory sessionFactory;
60   
61    @Inject
62    protected MailSenderConfiguration senderConfiguration;
63   
64    /**
65    * Send the mail asynchronously.
66    *
67    * @param messages the list of messages that was tried to be sent
68    * @param listener the {@link org.xwiki.mail.MailListener} component
69    * @param checkPermissions if true then we check authorization to send mail.
70    * @return the result and status of the send batch
71    */
 
72  6 toggle protected ScriptMailResult sendAsynchronously(Iterable<? extends MimeMessage> messages, MailListener listener,
73    boolean checkPermissions)
74    {
75  6 if (checkPermissions)
76    {
77  6 try {
78  6 checkPermissions();
79    } catch (MessagingException e) {
80    // Save the exception for reporting through the script services's getLastError() API
81  0 setError(e);
82    // Don't send the mail!
83  0 return null;
84    }
85    }
86   
87    // NOTE: we don't throw any error since the message is sent asynchronously. All errors can be found using
88    // the passed listener.
89  6 return new ScriptMailResult(this.mailSender.sendAsynchronously(messages, this.sessionFactory.create(
90    Collections.<String, String>emptyMap()), listener), listener.getMailStatusResult());
91    }
92   
93    /**
94    * Check authorization to send mail.
95    *
96    * @throws MessagingException when not authorized to send mail
97    */
 
98  6 toggle private void checkPermissions() throws MessagingException
99    {
100    // Load the configured permission checker
101  6 ScriptServicePermissionChecker checker;
102  6 String hint = this.senderConfiguration.getScriptServicePermissionCheckerHint();
103  6 try {
104  6 checker = this.componentManagerProvider.get().getInstance(ScriptServicePermissionChecker.class, hint);
105    } catch (ComponentLookupException e) {
106    // Failed to load the user-configured hint, in order not to have a security hole, consider that we're not
107    // authorized to send emails!
108  0 throw new MessagingException(String.format("Failed to locate Permission Checker [%s]. "
109    + "The mail has not been sent.", hint), e);
110    }
111   
112  6 try {
113  6 checker.check();
114    } catch (MessagingException e) {
115  0 throw new MessagingException(String.format("Not authorized by the Permission Checker [%s] to send mail! "
116    + "No mail has been sent.", hint), e);
117    }
118    }
119   
120    /**
121    * Get the error generated while performing the previously called action. An error can happen for example when:
122    * <ul>
123    * <li>creating the message to send</li>
124    * <li>if there isn't enough permissions to send mails (for example if the page containing the sending script
125    * doesn't have Programming Rights)</li>
126    * <li>if the MailListener corresponding to the passed hint doesn't exist</li>
127    * </ul>
128    *
129    * @return the exception or {@code null} if no exception was thrown
130    */
 
131  9 toggle public Exception getLastError()
132    {
133  9 return (Exception) this.execution.getContext().getProperty(getErrorKey());
134    }
135   
136    /**
137    * Store a caught exception in the context, so that it can be later retrieved using {@link #getLastError()}.
138    *
139    * @param e the exception to store, can be {@code null} to clear the previously stored exception
140    * @see #getLastError()
141    */
 
142  3 toggle protected void setError(Exception e)
143    {
144  3 this.execution.getContext().setProperty(getErrorKey(), e);
145    }
146   
147    /**
148    * @return The key under which the last encountered error is stored in the current execution context.
149    */
150    protected abstract String getErrorKey();
151    }