1 39 40 package demo; 41 42 import java.io.*; 43 import java.util.*; 44 import javax.mail.*; 45 import javax.mail.internet.*; 46 import javax.servlet.jsp.*; 47 import javax.servlet.jsp.tagext.*; 48 49 53 public class ListAttachmentsTag extends BodyTagSupport { 54 private String messageinfo; 55 private int partNum = 1; 56 private int numParts = 0; 57 private AttachmentInfo attachmentinfo; 58 private MessageInfo messageInfo; 59 private Multipart multipart; 60 61 64 public String getMessageinfo() { 65 return messageinfo; 66 } 67 68 71 public void setMessageinfo(String messageinfo) { 72 this.messageinfo = messageinfo; 73 } 74 75 78 public int doStartTag() throws JspException { 79 messageInfo = (MessageInfo)pageContext.getAttribute(getMessageinfo()); 80 attachmentinfo = new AttachmentInfo(); 81 82 try { 83 multipart = (Multipart)messageInfo.getMessage().getContent(); 84 numParts = multipart.getCount(); 85 } catch (Exception ex) { 86 throw new JspException(ex.getMessage()); 87 } 88 89 getPart(); 90 91 return BodyTag.EVAL_BODY_TAG; 92 } 93 94 97 public int doAfterBody() throws JspException { 98 99 BodyContent body = getBodyContent(); 100 try { 101 body.writeOut(getPreviousOut()); 102 } catch (IOException e) { 103 throw new JspTagException("IterationTag: " + e.getMessage()); 104 } 105 106 body.clearBody(); 108 109 partNum++; 110 if (partNum < numParts) { 111 getPart(); 112 return BodyTag.EVAL_BODY_TAG; 113 } else { 114 return BodyTag.SKIP_BODY; 115 } 116 } 117 118 121 private void getPart() throws JspException { 122 try { 123 attachmentinfo.setPart(partNum, multipart.getBodyPart(partNum)); 124 pageContext.setAttribute(getId(), attachmentinfo); 125 } catch (Exception ex) { 126 throw new JspException(ex.getMessage()); 127 } 128 } 129 } 130 131 | Popular Tags |