KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > email > TemplateWebEmail


1 /*
2  * Created on Oct 3, 2003
3  */

4 package com.openedit.modules.email;
5
6 import java.io.IOException JavaDoc;
7 import java.io.StringWriter JavaDoc;
8 import java.io.Writer JavaDoc;
9
10 import javax.mail.MessagingException JavaDoc;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15 import com.openedit.OpenEditException;
16 import com.openedit.WebPageRequest;
17 import com.openedit.generators.Output;
18 import com.openedit.page.Page;
19 import com.openedit.page.PageStreamer;
20 import com.openedit.page.manage.PageManager;
21 import com.openedit.util.PathUtilities;
22
23 /**
24  * @author cburkey
25  *
26  */

27 public class TemplateWebEmail extends WebEmail
28 {
29     protected PostMail postMail;
30     protected Page fieldMailTemplatePage;
31     protected String JavaDoc fieldMailTemplatePath;
32     private static final Log log = LogFactory.getLog(TemplateWebEmail.class);
33     
34     protected TemplateWebEmail()
35     {
36         super();
37     }
38     
39     protected TemplateWebEmail( WebPageRequest inContext, Page inTemplate )
40     {
41         fieldWebPageContext = inContext;
42         setMailTemplatePage( inTemplate );
43     }
44     
45     public Page getMailTemplatePage()
46     {
47         return fieldMailTemplatePage;
48     }
49     
50     public void setMailTemplatePage(Page page)
51     {
52         fieldMailTemplatePage = page;
53     }
54
55     public String JavaDoc getContentType()
56     {
57         return getMailTemplatePage().getMimeType();
58     }
59     
60     public String JavaDoc getMailTemplatePath()
61     {
62         return fieldMailTemplatePath;
63     }
64
65     public void setMailTemplatePath(String JavaDoc inMailTemplatePath)
66     {
67         fieldMailTemplatePath = inMailTemplatePath;
68     }
69     
70     public void loadSettings( WebPageRequest inContext, PageManager inManager) throws OpenEditException
71     {
72         super.loadSettings(inContext);
73         
74         Page page = inContext.getPage();
75         
76         String JavaDoc templatePath = page.get( EMAIL_TEMPLATE_REQUEST_PARAMETER );
77         if (templatePath == null || templatePath.length() <= 0)
78         {
79             templatePath = inContext.getRequestParameter( EMAIL_TEMPLATE_REQUEST_PARAMETER );
80         }
81         if( templatePath != null)
82         {
83             templatePath = PathUtilities.buildRelative(templatePath,inContext.getPath());
84             Page templatePage = inManager.getPage( templatePath ); //home is only needed when dealing with full URL's
85
setMailTemplatePage(templatePage);
86         }
87 // else
88
// {
89
// throw new OpenEditException("Email must use an HTML template page. To avoid spammers.");
90
// }
91
else
92         {
93             String JavaDoc body = inContext.getRequestParameter("body"); //Is this SPAM prof?
94
if( body!= null && body.indexOf("Message-Id:") > 0)
95             {
96                 throw new OpenEditException("Email message looks like spam");
97             }
98             setMessage(body);
99         }
100         
101     }
102     
103     public void render(Writer JavaDoc outputStream) throws OpenEditException
104     {
105         if( getWebPageContext() != null && getMailTemplatePage() != null)
106         {
107             PageStreamer streamer = getWebPageContext().getPageStreamer().copy();
108             
109             Output out = new Output();
110             out.setWriter(outputStream);
111             streamer.setOutput(out);
112     
113             WebPageRequest context = getWebPageContext().copy(getMailTemplatePage());
114             context.putPageStreamer(streamer);
115             streamer.stream(getMailTemplatePage(), context);
116         }
117         else if ( getMailTemplatePage() != null)
118         {
119             log.info("No context set. Using raw html");
120             try
121             {
122                 outputStream.write(getMailTemplatePage().getContent());
123             }
124             catch (IOException JavaDoc ex)
125             {
126                 throw new OpenEditException( ex );
127             }
128         }
129         else if ( getMessage() != null)
130         {
131             try
132             {
133                 outputStream.write(getMessage());
134             }
135             catch (IOException JavaDoc ex)
136             {
137                 throw new OpenEditException( ex );
138             }
139         }
140         else
141         {
142             throw new OpenEditException("No message set");
143         }
144     }
145     
146     public void send(Recipient inRecp) throws OpenEditException, MessagingException JavaDoc
147     {
148         
149         StringWriter JavaDoc out = new StringWriter JavaDoc();
150         render(out);
151         postMail.postMail(new String JavaDoc[] { inRecp.toString() },getSubject(),out.toString(),getAlternativeMessage(),getFrom());
152     }
153     public void send() throws OpenEditException, MessagingException JavaDoc
154     {
155         
156         StringWriter JavaDoc out = new StringWriter JavaDoc();
157         render(out);
158         postMail.postMail(getTo(),getSubject(),out.toString(),null,getFrom());
159     }
160
161     public PostMail getPostMail() {
162         return postMail;
163     }
164
165     public void setPostMail(PostMail postMail) {
166         this.postMail = postMail;
167     }
168 }
169
Popular Tags