01: /*--------------------------------------------------------------------------*
02: | Copyright (C) 2006 Christopher Kohlhaas |
03: | |
04: | This program is free software; you can redistribute it and/or modify |
05: | it under the terms of the GNU General Public License as published by the |
06: | Free Software Foundation. A copy of the license has been included with |
07: | these distribution in the COPYING file, if not go to www.fsf.org |
08: | |
09: | As a special exception, you are granted the permissions to link this |
10: | program with every library, which license fulfills the Open Source |
11: | Definition as published by the Open Source Initiative (OSI). |
12: *--------------------------------------------------------------------------*/
13: package org.rapla.plugin.mail.internal;
14:
15: import java.io.IOException;
16: import java.io.OutputStream;
17: import java.text.ParseException;
18: import java.util.Map;
19:
20: import org.rapla.components.mail.MailException;
21: import org.rapla.components.mail.MailInterface;
22: import org.rapla.entities.User;
23: import org.rapla.entities.configuration.Preferences;
24: import org.rapla.facade.ClientFacade;
25: import org.rapla.facade.RaplaComponent;
26: import org.rapla.framework.RaplaContext;
27: import org.rapla.framework.RaplaException;
28: import org.rapla.plugin.mail.MailPlugin;
29: import org.rapla.plugin.mail.MailToUserInterface;
30: import org.rapla.server.RemoteService;
31: import org.rapla.server.RemoteSession;
32:
33: public class RaplaMailToUserOnLocalhost extends RaplaComponent
34: implements MailToUserInterface, RemoteService {
35: RemoteSession session;
36:
37: public RaplaMailToUserOnLocalhost(RaplaContext context)
38: throws RaplaException {
39: super (context);
40: }
41:
42: public void setSession(RemoteSession session) {
43: this .session = session;
44: }
45:
46: public void sendMail(String userName, String subject, String body)
47: throws RaplaException {
48: User recipientUser = getQuery().getUser(userName);
49: // O.K. We need to generate the mail
50: String recipientEmail = recipientUser.getEmail();
51: if (recipientEmail == null
52: || recipientEmail.trim().length() == 0) {
53: getLogger().warn(
54: "No email adress specified for user "
55: + recipientUser.getUsername()
56: + " Can't send mail.");
57: return;
58: }
59:
60: final MailInterface mail = (MailInterface) getContext().lookup(
61: MailInterface.ROLE);
62: ClientFacade facade = (ClientFacade) getContext().lookup(
63: ClientFacade.ROLE);
64: Preferences prefs = facade.getPreferences(null);
65: final String defaultSender = prefs.getEntryAsString(
66: MailPlugin.DEFAULT_SENDER_ENTRY, "rapla");
67: try {
68: mail.sendMail(defaultSender, recipientEmail, subject, body);
69: } catch (MailException ex) {
70: throw new RaplaException(ex);
71: }
72: if (session != null) {
73: session.getLogger().info("Email send to user " + userName);
74: }
75:
76: }
77:
78: public void remoteMethodCall(String methodName, Map args,
79: OutputStream out) throws RaplaException, IOException,
80: ParseException {
81: if (SEND_MAIL.is(methodName)) {
82: String username = SEND_MAIL.value(args, 0);
83: String subject = SEND_MAIL.value(args, 1);
84: String body = SEND_MAIL.value(args, 2);
85: sendMail(username, subject, body);
86: }
87:
88: }
89: }
|