1
2
3
4
5
6
7
8
9
10 package org.astrogrid.portal.cocoon.common;
11 import java.util.HashMap;
12 import java.util.Map;
13
14 import org.apache.avalon.framework.logger.ConsoleLogger;
15 import org.apache.avalon.framework.logger.Logger;
16 import org.apache.avalon.framework.parameters.ParameterException;
17 import org.apache.avalon.framework.parameters.Parameters;
18 import org.apache.cocoon.acting.AbstractAction;
19 import org.apache.cocoon.environment.ObjectModelHelper;
20 import org.apache.cocoon.environment.Redirector;
21 import org.apache.cocoon.environment.Request;
22 import org.apache.cocoon.environment.SourceResolver;
23 import org.astrogrid.config.Config;
24 import org.astrogrid.config.SimpleConfig;
25 import org.astrogrid.portal.cocoon.messaging.EmailMessengerFactory;
26 import org.astrogrid.portal.cocoon.messaging.Messenger;
27 import org.astrogrid.portal.cocoon.messaging.MessengerException;
28 /***
29 * Currently sends an email - could do this with one of Cocoon's built in
30 * actions instead, but thought I'd do it this way so that we could change to
31 * send the message to a log, or to jabber or whatever. Send a message to the
32 * administrator
33 *
34 * @author jdt
35 */
36 public final class MessageToAdminAction extends AbstractAction {
37 /***
38 * What to look for in the request header This could be used in the xsp
39 * pages, but it makes the code so disgusting it's clearer just to hardwire
40 * it.
41 */
42 public static final String NAME = "name";
43 /***
44 * What to look for in the request header
45 */
46 public static final String EMAIL = "email";
47 /***
48 * What to look for in the request header
49 */
50 public static final String MESSAGE = "message";
51 /***
52 * What to look for in the request header
53 */
54 public static final String SUBJECT = "subject";
55 /***
56 * JNDI property keys for email - see DataCenter QuerierPlugin
57 */
58 public static final String EMAIL_SERVER = "emailserver.address";
59 /***
60 * JNDI property keys for email
61 */
62 public static final String EMAIL_USER = "emailserver.user";
63 /***
64 * JNDI property keys for email
65 */
66 public static final String EMAIL_PWD = "emailserver.password";
67 /***
68 * JNDI property keys for email
69 */
70 public static final String EMAIL_FROM = "emailserver.from";
71 /***
72 * JNDI property keys for email
73 */
74 public static final String EMAIL_TO = "astrogrid.portal.admin.email";
75 /***
76 * Emailer
77 */
78 private Messenger cachedMessenger;
79 /***
80 * Determines whether or not to use a mock emailer
81 * or a real one.
82 */
83 private boolean useMockEmailer;
84 /***
85 * Set up mailer Constructor
86 *
87 * @param useMockEmailer true if you want to use a dummy emailer for
88 * testing
89 */
90 public MessageToAdminAction(final boolean useMockEmailer) {
91 super();
92 this.useMockEmailer=useMockEmailer;
93
94 }
95 /***
96 * Set up mailer Constructor
97 */
98 public MessageToAdminAction() {
99 this(false);
100 }
101 /***
102 * Lazily return the Messenger.
103 * Has default access since used by unit tests
104 * @TODO what to do about defaults? If we remove the defaults then this
105 * method will fail with a PropertyNotFoundException if the props aren't set
106 * up. THis is probably what we want, but leave defaults in for now for testing.
107 * @return the cachedMessenger
108 */
109 Messenger getMessenger() {
110 if (cachedMessenger!=null) {
111 return cachedMessenger;
112 }
113 Config config = SimpleConfig.getSingleton();
114
115
116 final String smtpServer = config.getString(EMAIL_SERVER,"127.0.0.1");
117 final String smtpUser = config.getString(EMAIL_USER,"astrogrid");
118 final String smtpPass = config.getString(EMAIL_PWD,"");
119 final String returnAddress = config.getString(EMAIL_FROM,"astrogrid@star.le.ac.uk");
120
121 final String recipient = config.getString(EMAIL_TO, "jdt@roe.ac.uk");
122
123 final EmailMessengerFactory factory =
124 new EmailMessengerFactory(
125 smtpServer,
126 smtpUser,
127 smtpPass,
128 returnAddress);
129 cachedMessenger = factory.getEmailMessenger(recipient, useMockEmailer);
130 assert cachedMessenger != null;
131 return cachedMessenger;
132 }
133
134
135
136 /***
137 * The Action's main method. Sends an email to the administrator containing
138 * the "name", "email", stored in the request and "subject" and "message" stored in the params
139 * object.
140 *
141 * @param redirector see org.apache.cocoon.acting.Action#act
142 * @param resolver see org.apache.cocoon.acting.Action#act
143 * @param objectModel see org.apache.cocoon.acting.Action#act
144 * @param source see org.apache.cocoon.acting.Action#act
145 * @param params see org.apache.cocoon.acting.Action#act
146 * @return null on failure, an empty HashMap on success
147 * @see org.apache.cocoon.acting.Action#act(org.apache.cocoon.environment.Redirector,
148 * org.apache.cocoon.environment.SourceResolver, java.util.Map,
149 * java.lang.String,
150 * org.apache.avalon.framework.parameters.Parameters)
151 */
152 public Map act(
153 final Redirector redirector,
154 final SourceResolver resolver,
155 final Map objectModel,
156 final String source,
157 final Parameters params) {
158
159 checkLogger();
160 final Logger log = getLogger();
161 final Request request = ObjectModelHelper.getRequest(objectModel);
162 assert log != null;
163 assert request != null;
164 final String username = request.getParameter(NAME);
165 final String userEmail = request.getParameter(EMAIL);
166 String subject = null;
167 String message = null;
168 try {
169 subject = params.getParameter(SUBJECT);
170 message = params.getParameter(MESSAGE);
171 } catch (ParameterException pe) {
172 log.error(
173 "Couldn't get subject and message from params...",
174 pe);
175 }
176
177 log.debug(
178 "act(): username="
179 + username
180 + ", userEmail="
181 + userEmail
182 + ", subject="
183 + subject
184 + ",message="
185 + message);
186
187 if (username == null) {
188 log.error("User name was null");
189 return null;
190 }
191 if (userEmail == null) {
192 log.error("User email was null");
193 return null;
194 }
195 if (subject == null) {
196 log.error("subject was null");
197 return null;
198 }
199 if (message == null) {
200 log.error("message was null");
201 return null;
202 }
203
204 final Messenger messenger = getMessenger();
205
206 final String fullMessage =
207 message + "\n" + username + "\n" + userEmail + "\n";
208 try {
209 messenger.sendMessage(subject, fullMessage);
210 } catch (MessengerException me) {
211 log.error("Unable to send message ", me);
212 return null;
213 }
214 return new HashMap();
215 }
216 /***
217 * During unit tests the logger isn't setup properly, hence this method to
218 * use a console logger instead.
219 *
220 */
221 private void checkLogger() {
222 final Logger log = super.getLogger();
223 if (log == null) {
224 enableLogging(new ConsoleLogger());
225 }
226 }
227 }
228
229
230
231
232
233
234
235
236
237
238
239
240