1 4 package com.openedit.modules.email; 5 6 import java.io.IOException ; 7 import java.io.StringWriter ; 8 import java.io.Writer ; 9 10 import javax.mail.MessagingException ; 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 27 public class TemplateWebEmail extends WebEmail 28 { 29 protected PostMail postMail; 30 protected Page fieldMailTemplatePage; 31 protected String 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 getContentType() 56 { 57 return getMailTemplatePage().getMimeType(); 58 } 59 60 public String getMailTemplatePath() 61 { 62 return fieldMailTemplatePath; 63 } 64 65 public void setMailTemplatePath(String 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 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 ); setMailTemplatePage(templatePage); 86 } 87 else 92 { 93 String body = inContext.getRequestParameter("body"); 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 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 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 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 147 { 148 149 StringWriter out = new StringWriter (); 150 render(out); 151 postMail.postMail(new String [] { inRecp.toString() },getSubject(),out.toString(),getAlternativeMessage(),getFrom()); 152 } 153 public void send() throws OpenEditException, MessagingException 154 { 155 156 StringWriter out = new StringWriter (); 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 |