001: /*
002: * Created on Oct 3, 2003
003: */
004: package com.openedit.modules.email;
005:
006: import java.io.IOException;
007: import java.io.StringWriter;
008: import java.io.Writer;
009:
010: import javax.mail.MessagingException;
011:
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014:
015: import com.openedit.OpenEditException;
016: import com.openedit.OpenEditRuntimeException;
017: import com.openedit.WebPageRequest;
018: import com.openedit.generators.Output;
019: import com.openedit.page.Page;
020: import com.openedit.page.PageStreamer;
021: import com.openedit.page.manage.PageManager;
022: import com.openedit.util.PathUtilities;
023:
024: /**
025: * @author cburkey
026: *
027: */
028: public class TemplateWebEmail extends WebEmail {
029: protected PostMail postMail;
030: protected Page fieldMailTemplatePage;
031: protected String fieldMailTemplatePath;
032: protected PageManager fieldPageManager;
033:
034: private static final Log log = LogFactory
035: .getLog(TemplateWebEmail.class);
036:
037: public TemplateWebEmail() {
038: super ();
039: }
040:
041: protected TemplateWebEmail(WebPageRequest inContext, Page inTemplate) {
042: fieldWebPageContext = inContext;
043: setMailTemplatePage(inTemplate);
044: }
045:
046: public Page getMailTemplatePage() {
047: if (fieldMailTemplatePage == null) {
048: try {
049: Page templatePage = getPageManager().getPage(
050: getMailTemplatePath()); //home is only needed when dealing with full URL's
051: fieldMailTemplatePage = templatePage;
052: } catch (OpenEditException ex) {
053: throw new OpenEditRuntimeException(ex);
054: }
055: }
056: return fieldMailTemplatePage;
057: }
058:
059: public void setMailTemplatePage(Page page) {
060: fieldMailTemplatePage = page;
061: if (page != null) {
062: setMailTemplatePath(page.getPath());
063: }
064: }
065:
066: public String getContentType() {
067: return getMailTemplatePage().getMimeType();
068: }
069:
070: public String getMailTemplatePath() {
071: return fieldMailTemplatePath;
072: }
073:
074: public void setMailTemplatePath(String inMailTemplatePath) {
075: fieldMailTemplatePath = inMailTemplatePath;
076: }
077:
078: public void loadSettings(WebPageRequest inContext)
079: throws OpenEditException {
080: super .loadSettings(inContext);
081:
082: Page page = inContext.getPage();
083:
084: String templatePath = page.get("emailbody");
085: if (templatePath == null || templatePath.length() <= 0) {
086: templatePath = inContext
087: .findValue(EMAIL_TEMPLATE_REQUEST_PARAMETER);
088: if (templatePath == null) {
089: templatePath = inContext
090: .findValue(OLDEMAIL_TEMPLATE_REQUEST_PARAMETER);
091: }
092: }
093: if (templatePath != null) {
094: templatePath = PathUtilities.buildRelative(templatePath,
095: inContext.getPath());
096: setMailTemplatePath(templatePath);
097: } else {
098: String body = inContext.getRequestParameter("body"); //Is this SPAM prof? TODO: remove
099: if (body != null && body.indexOf("Message-Id:") > 0) {
100: throw new OpenEditException(
101: "Email message looks like spam");
102: }
103: setMessage(body);
104: }
105: }
106:
107: public void render(Writer outputStream) throws OpenEditException {
108: if (getWebPageContext() != null
109: && getMailTemplatePath() != null) {
110: PageStreamer streamer = getWebPageContext()
111: .getPageStreamer().copy();
112:
113: Output out = new Output();
114: out.setWriter(outputStream);
115: streamer.setOutput(out);
116:
117: WebPageRequest context = getWebPageContext().copy(
118: getMailTemplatePage());
119: context.putPageStreamer(streamer);
120: streamer.include(getMailTemplatePage(), context);
121: } else if (getMailTemplatePath() != null) {
122: log.info("No context set. Using raw html");
123: try {
124: outputStream.write(getMailTemplatePage().getContent());
125: } catch (IOException ex) {
126: throw new OpenEditException(ex);
127: }
128: } else if (getMessage() != null) {
129: try {
130: outputStream.write(getMessage());
131: } catch (IOException ex) {
132: throw new OpenEditException(ex);
133: }
134: } else {
135: throw new OpenEditException("No template found "
136: + getMailTemplatePath());
137: }
138: }
139:
140: public void send(Recipient inRecp) throws OpenEditException,
141: MessagingException {
142:
143: StringWriter out = new StringWriter();
144: render(out);
145: postMail.postMail(new String[] { inRecp.toString() },
146: getSubject(), out.toString(), getAlternativeMessage(),
147: getFrom());
148: }
149:
150: public void send() throws OpenEditException, MessagingException {
151: if (getFrom() == null) {
152: throw new MessagingException("Missing 'From' field.");
153: }
154: StringWriter out = new StringWriter();
155: render(out);
156: postMail.postMail(getTo(), getSubject(), out.toString(), null,
157: getFrom());
158: }
159:
160: public PostMail getPostMail() {
161: return postMail;
162: }
163:
164: public void setPostMail(PostMail postMail) {
165: this .postMail = postMail;
166: }
167:
168: public PageManager getPageManager() {
169: return fieldPageManager;
170: }
171:
172: public void setPageManager(PageManager inPageManager) {
173: fieldPageManager = inPageManager;
174: }
175:
176: public void loadBodyFromForm(WebPageRequest inReq)
177: throws OpenEditException {
178: //We are going to load up the body of this email from a form
179: String path = inReq.findValue(EMAIL_TEMPLATE_REQUEST_PARAMETER);
180: if (path == null) {
181: path = inReq.findValue(OLDEMAIL_TEMPLATE_REQUEST_PARAMETER);
182: }
183: if (path == null) {
184: path = inReq.getPath();
185: }
186: setMailTemplatePath(path);
187:
188: PageStreamer streamer = getWebPageContext().getPageStreamer()
189: .copy();
190: Output out = new Output();
191: out.setWriter(new StringWriter());
192: streamer.setOutput(out);
193: WebPageRequest context = getWebPageContext().copy(
194: getMailTemplatePage());
195: context.putPageStreamer(streamer);
196: streamer.include(getMailTemplatePage(), context);
197:
198: String content = out.getWriter().toString();
199: setMailTemplatePath(null);
200: String done = replaceText(content, inReq, "input");
201: done = replaceText(done, inReq, "textarea");
202: //inReq.getPage().generate(inReq, inOut)
203:
204: setMessage(done);
205: }
206:
207: private String replaceText(String inContent, WebPageRequest inReq,
208: String inType) {
209: StringBuffer done = new StringBuffer(inContent.length() + 10);
210: //Look for any <input
211: String[] inputs = inContent.split("<" + inType);
212: for (int i = 0; i < inputs.length; i++) {
213: String chunk = inputs[i];
214: int start = chunk.indexOf("name");
215: if (start > -1) {
216: if (start != -1) {
217: start = chunk.indexOf("\"", start);
218: if (start != -1) {
219: start++;
220: int end = chunk.indexOf("\"", start); //TODO: Use RegEx
221: if (end != -1) {
222: String name = chunk.substring(start, end);
223: String value = inReq
224: .getRequestParameter(name);
225: if (value != null) {
226: done.append(value);
227: }
228: }
229: }
230: }
231: }
232: done.append("<hidden");
233: done.append(chunk);
234: }
235: String end = done.toString().replaceAll(inType, "hidden"); //handle the close tags if any
236: return end;
237: }
238: }
|