KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > util > TemplateMimeMessage


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
14 package org.ejbca.util;
15
16 import java.util.HashMap JavaDoc;
17
18 import javax.mail.MessagingException JavaDoc;
19 import javax.mail.Session JavaDoc;
20 import javax.mail.internet.MimeMessage JavaDoc;
21
22 /**
23  * This is an extremely simple template message to be used to interpolate some values that exists
24  * in the content written as ${identifier}.
25  * <p />
26  * It is nowhere as powerful as Jakarta Velocity with its VelocityEmail but it's not intended to be.
27  * <p />
28  * Only subject and content data is interpolated.
29  * <code>
30  * HashMap params = new HashMap();
31  * params.put("username", "John Doe");
32  *
33  * Session session = (Session)ctx.lookup("java:comp/env/mail/MyMail");
34  * TemplateMimeMessage msg = new TemplateMimeMessage(params, session);
35  * msg.setSubject("${username}, here is a message for your");
36  * msg.setContent("Hello ${username}", "text/plain");
37  * ...
38  *
39  * </code>
40  *
41  * @version $Id: TemplateMimeMessage.java,v 1.4 2006/11/02 08:03:23 anatom Exp $
42  */

43 public class TemplateMimeMessage extends MimeMessage JavaDoc {
44
45     /** the map of Pattern/String objects to interpolate in the content */
46     private HashMap JavaDoc patterns;
47
48     /**
49      * Construct a new TemplateMimeMessage which content is to be interpolated
50      * For instance specifying a map entry as ('welcome', 'Hello World') and having a content
51      * with '${welcome}' will have it to be interpolated as 'Hello World'
52      *
53      * @param patterns the map of String/String objects
54      * @param session the mail session to use.
55      */

56     public TemplateMimeMessage(HashMap JavaDoc patterns, Session JavaDoc session) {
57         super(session);
58         this.patterns = patterns;
59     }
60
61     public void setSubject(String JavaDoc s) throws MessagingException JavaDoc {
62         setSubject(s, null);
63     }
64
65     public void setSubject(String JavaDoc s, String JavaDoc s1) throws MessagingException JavaDoc {
66         String JavaDoc interpolatedContent = interpolate(s);
67         super.setSubject(interpolatedContent, s1);
68     }
69
70     public void setContent(Object JavaDoc content, String JavaDoc s) throws MessagingException JavaDoc {
71         // template message supports only String message
72
if(!(content instanceof String JavaDoc)) {
73             throw new MessagingException JavaDoc("Requires a String content, was given object of type " + content.getClass().toString());
74         }
75         String JavaDoc interpolatedContent = interpolate((String JavaDoc)content);
76         super.setContent(interpolatedContent, s);
77     }
78
79
80     /**
81      * Interpolate the patterns that exists on the input on the form '${pattern}'.
82      * @param input the input content to be interpolated
83      * @return the interpolated content
84      */

85     protected String JavaDoc interpolate(String JavaDoc input) {
86         return NotificationParamGen.interpolate(patterns, input);
87     }
88
89 }
90
Popular Tags