1 16 17 package org.apache.taglibs.mailer; 18 19 import java.io.File ; 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 import javax.activation.DataHandler ; 23 import javax.activation.DataSource ; 24 import javax.activation.FileDataSource ; 25 import javax.mail.MessagingException ; 26 import javax.mail.internet.MimeBodyPart ; 27 import javax.servlet.jsp.JspException ; 28 import javax.servlet.jsp.tagext.BodyTagSupport ; 29 30 31 46 47 public class AttachTag extends BodyTagSupport { 48 49 52 private String type = null; 53 54 58 private String url = null; 59 60 64 private String file = null; 65 66 69 private MimeBodyPart mbp = null; 70 71 74 private MailTag myparent = null; 75 76 79 private String body; 80 81 92 public int doStartTag() throws JspException { 93 94 myparent = (MailTag)findAncestorWithClass(this, MailTag.class); 95 if (myparent == null) { 96 throw new JspException ("Attach tag not nested within mail tag."); 97 } 98 mbp = new MimeBodyPart (); body = null; 100 if (type != null || (file != null && file.length() == 0) || 101 (url != null && url.length() == 0) ) { 102 return EVAL_BODY_TAG; 103 } 104 return SKIP_BODY; 105 } 106 107 118 public int doAfterBody() throws JspException { 119 120 body = bodyContent.getString(); 121 bodyContent.clearBody(); 122 if (body == null) { 123 body = ""; 124 } 125 return SKIP_BODY; 126 } 127 128 139 public int doEndTag() throws JspException { 140 141 if (type != null) { 142 try { 145 mbp.setDataHandler(new DataHandler (body, type)); 146 } catch (MessagingException me) { 147 throw new JspException ( 148 "The attachment named with the mimetype " + type + 149 " could not be attached."); 150 } 151 } else if (file != null) { 152 if (file.length() == 0) { 153 body.trim(); 154 if (body.length() > 0 ) { 156 setFileBodyPart(body); 158 } else { 159 throw new JspException ( 161 "The file name must be given in the body of this tag."); 162 } 163 } else { 164 setFileBodyPart(file); 167 } 168 } else if (url != null) { 169 if (url.length() == 0) { 170 body.trim(); 171 if (body.length() > 0) { 173 setUrlBodyPart(body); 175 } else { 176 throw new JspException ( 178 "The url must be givenin the body of this tag."); 179 } 180 } else 181 setUrlBodyPart(url); 183 } 184 185 myparent.setBodyParts(mbp); 187 return EVAL_PAGE; 188 } 189 190 196 public void setType(String value) { 197 type = value; 198 } 199 200 207 public void setUrl(String value) { 208 url = value; 209 } 210 211 218 public void setFile(String value) { 219 file = value; 220 } 221 222 229 protected void setUrlBodyPart(String value) throws JspException { 230 231 233 try { 234 URL url = new URL (value); 235 mbp.setDataHandler(new DataHandler (url)); 236 if(url.getFile() != null) 237 mbp.setFileName(url.getFile()); 238 else 239 mbp.setFileName(value); 240 241 } catch(MalformedURLException e) { 242 throw new JspException ("The URL entered as an attachment was " + 243 "incorrectly formatted please check it and try again."); 244 } catch(MessagingException e) { 245 throw new JspException ("The Resource named by " + url + " could not" 246 + " be attached."); 247 } 248 } 250 251 258 protected void setFileBodyPart(String value) throws JspException { 259 260 String rpath = pageContext.getServletContext().getRealPath(value); 263 264 try { 266 File file = new File (rpath); 267 if (file.exists()) { 268 DataSource attachment = new FileDataSource (file); 269 mbp.setDataHandler(new DataHandler (attachment)); 270 mbp.setFileName(file.getName()); 271 } else { 272 throw new JspException ("File " + rpath + " does not exist or " + 276 "the path to the file is incorrect."); 277 } 278 } catch(MessagingException e) { 279 throw new JspException ("The file named by " + file + " could not be" 280 + " attached."); 281 } 282 283 } 285 } 286 | Popular Tags |