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.mail.search.*; 47 import javax.servlet.jsp.*; 48 import javax.servlet.jsp.tagext.*; 49 50 54 public class ListMessagesTag extends BodyTagSupport { 55 private String folder; 56 private String session; 57 private int msgNum = 0; 58 private int messageCount = 0; 59 private Message message; 60 private Message[] messages; 61 private MessageInfo messageinfo; 62 63 66 public String getFolder() { 67 return folder; 68 } 69 70 73 public String getSession() { 74 return session; 75 } 76 77 80 public void setFolder(String folder) { 81 this.folder = folder; 82 } 83 84 87 public void setSession(String session) { 88 this.session = session; 89 } 90 91 94 public int doStartTag() throws JspException { 95 messageinfo = new MessageInfo(); 96 97 try { 98 Folder folder = (Folder)pageContext.getAttribute( 99 getFolder(), PageContext.SESSION_SCOPE); 100 FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.DELETED), false); 101 messages = folder.search(ft); 102 messageCount = messages.length; 103 } catch (Exception ex) { 104 throw new JspException(ex.getMessage()); 105 } 106 107 if (messageCount > 0) { 108 getMessage(); 109 return BodyTag.EVAL_BODY_TAG; 110 } else 111 return BodyTag.SKIP_BODY; 112 } 113 114 117 public int doAfterBody() throws JspException { 118 119 BodyContent body = getBodyContent(); 120 try { 121 body.writeOut(getPreviousOut()); 122 } catch (IOException e) { 123 throw new JspTagException("IterationTag: " + e.getMessage()); 124 } 125 126 body.clearBody(); 128 129 if (msgNum < messageCount) { 130 getMessage(); 131 return BodyTag.EVAL_BODY_TAG; 132 } else { 133 return BodyTag.SKIP_BODY; 134 } 135 } 136 137 140 private void getMessage() throws JspException { 141 message = messages[msgNum++]; 142 messageinfo.setMessage(message); 143 pageContext.setAttribute(getId(), messageinfo); 144 } 145 } 146 147 | Popular Tags |