KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > services > actions > MailAction


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13 package org.ejbca.core.model.services.actions;
14
15 import java.util.Date JavaDoc;
16
17 import javax.ejb.EJBException JavaDoc;
18 import javax.mail.Message JavaDoc;
19 import javax.mail.Session JavaDoc;
20 import javax.mail.Transport JavaDoc;
21 import javax.mail.internet.InternetAddress JavaDoc;
22 import javax.mail.internet.MimeMessage JavaDoc;
23
24 import org.apache.log4j.Logger;
25 import org.ejbca.core.model.InternalResources;
26 import org.ejbca.core.model.log.Admin;
27 import org.ejbca.core.model.log.LogEntry;
28 import org.ejbca.core.model.services.ActionException;
29 import org.ejbca.core.model.services.ActionInfo;
30 import org.ejbca.core.model.services.BaseAction;
31
32 /**
33  * Class managing the sending of emails from a service.
34  *
35  *
36  * @author Philip Vendil
37  * @version $Id: MailAction.java,v 1.5 2006/12/13 10:35:10 anatom Exp $
38  */

39 public class MailAction extends BaseAction {
40     
41     private static final Logger log = Logger.getLogger(MailAction.class);
42     /** Internal localization of logs and errors */
43     private static final InternalResources intres = InternalResources.getInstance();
44     
45     private static final Admin admin = new Admin(Admin.TYPE_INTERNALUSER);
46     
47     public static final String JavaDoc PROP_SENDERADDRESS = "action.mail.senderAddress";
48     public static final String JavaDoc PROP_RECIEVERADDRESS = "action.mail.recieverAddress";
49
50     /**
51      * Sends the mail
52      *
53      * Only supports the MailActionInfo othervise is ActionException thrown.
54      *
55      * @see org.ejbca.core.model.services.IAction#performAction(org.ejbca.core.model.services.ActionInfo)
56      */

57     public void performAction(ActionInfo actionInfo) throws ActionException {
58         checkConfig(actionInfo);
59         
60         MailActionInfo mailActionInfo = (MailActionInfo) actionInfo;
61         String JavaDoc senderAddress = properties.getProperty(PROP_SENDERADDRESS);
62         
63         String JavaDoc reciverAddress = mailActionInfo.getReciever();
64         if(reciverAddress== null){
65             reciverAddress = properties.getProperty(PROP_RECIEVERADDRESS);
66         }
67                 
68         if(reciverAddress == null || reciverAddress.trim().equals("")){
69             String JavaDoc msg = intres.getLocalizedMessage("services.mailaction.errorreceiveraddress");
70             throw new ActionException(msg);
71         }
72                 
73         try {
74               String JavaDoc mailJndi = getLocator().getString("java:comp/env/MailJNDIName");
75               Session JavaDoc mailSession = getLocator().getMailSession(mailJndi);
76
77               Message JavaDoc msg = new MimeMessage JavaDoc(mailSession);
78               msg.setFrom(new InternetAddress JavaDoc(senderAddress));
79               msg.addRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(reciverAddress, false));
80               msg.setSubject(mailActionInfo.getSubject());
81               msg.setContent(mailActionInfo.getMessage(), "text/plain");
82               msg.setHeader("X-Mailer", "JavaMailer");
83               msg.setSentDate(new Date JavaDoc());
84               Transport.send(msg);
85
86               String JavaDoc logmsg = intres.getLocalizedMessage("services.mailaction.sent", reciverAddress);
87               getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_APPROVAL, new java.util.Date JavaDoc(), null, null, LogEntry.EVENT_INFO_NOTIFICATION, logmsg);
88         } catch (Exception JavaDoc e) {
89             String JavaDoc msg = intres.getLocalizedMessage("services.mailaction.errorsend", reciverAddress);
90             log.error(msg, e);
91             try{
92                 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_APPROVAL, new java.util.Date JavaDoc(),null, null, LogEntry.EVENT_ERROR_NOTIFICATION, msg);
93             }catch(Exception JavaDoc f){
94                 throw new EJBException JavaDoc(f);
95             }
96         }
97         
98
99     }
100     
101     /**
102      * Method that checks the configuration sets the variables and throws an exception
103      * if it's invalid
104      *
105      * @param actionInfo
106      * @throws ActionException
107      */

108     private void checkConfig(ActionInfo actionInfo) throws ActionException {
109         if(!(actionInfo instanceof MailActionInfo)){
110             String JavaDoc msg = intres.getLocalizedMessage("services.mailaction.erroractioninfo");
111             throw new ActionException(msg);
112         }
113         
114         String JavaDoc senderAddress = properties.getProperty(PROP_SENDERADDRESS);
115         if(senderAddress == null || senderAddress.trim().equals("")){
116             String JavaDoc msg = intres.getLocalizedMessage("services.mailaction.errorsenderaddress");
117             throw new ActionException(msg);
118         }
119     }
120     
121
122
123 }
124
Popular Tags