001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.notification;
019:
020: import javax.mail.MessagingException;
021: import javax.mail.internet.AddressException;
022:
023: import org.apache.avalon.framework.configuration.Configurable;
024: import org.apache.avalon.framework.configuration.Configuration;
025: import org.apache.avalon.framework.configuration.ConfigurationException;
026: import org.apache.avalon.framework.service.ServiceException;
027: import org.apache.cocoon.mail.MailSender;
028: import org.apache.lenya.ac.Identifiable;
029: import org.apache.lenya.ac.User;
030: import org.apache.lenya.inbox.InboxNotifier;
031:
032: /**
033: * Default notifier implementation.
034: */
035: public class EmailNotifier extends InboxNotifier implements
036: Configurable {
037:
038: protected void notify(User recipient, Message translatedMessage)
039: throws NotificationException {
040:
041: super .notify(recipient, translatedMessage);
042:
043: if (!this .manager.hasService(MailSender.ROLE)) {
044: getLogger().error(
045: "Can't send mails - no MailSender service found.");
046: return;
047: }
048:
049: Identifiable sender = translatedMessage.getSender();
050: MailSender mailer = null;
051: try {
052: mailer = (MailSender) this .manager.lookup(MailSender.ROLE);
053: if (this .username == null) {
054: mailer.setSmtpHost(this .smtpHost);
055: } else {
056: mailer.setSmtpHost(this .smtpHost, this .username,
057: this .password);
058: }
059:
060: mailer.setTo(recipient.getEmail());
061: if (sender instanceof User) {
062: mailer.setFrom(((User) sender).getEmail());
063: }
064:
065: mailer.setSubject(translatedMessage.getSubject());
066: mailer.setBody(translatedMessage.getBody(),
067: "text/plain; charset=UTF-8");
068: mailer.send();
069:
070: } catch (AddressException e) {
071: getLogger().error("Sending mail failed (address error): ",
072: e);
073: throw new NotificationException(e);
074: } catch (MessagingException e) {
075: getLogger().error("Sending mail failed (mail error): ", e);
076: throw new NotificationException(e);
077: } catch (ServiceException e) {
078: throw new NotificationException(e);
079: } finally {
080: if (mailer != null) {
081: this .manager.release(mailer);
082: }
083: }
084: }
085:
086: private String smtpHost;
087: private String username;
088: private String password;
089:
090: public void configure(Configuration config)
091: throws ConfigurationException {
092: Configuration smtp = config.getChild("smtp");
093: this .smtpHost = smtp.getAttribute("host");
094: this .username = smtp.getAttribute("username", null);
095: if (this .username != null) {
096: this .password = smtp.getAttribute("password");
097: }
098: }
099:
100: }
|