Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  *   - Redistributions of source code must retain the above copyright
  9.  *     notice, this list of conditions and the following disclaimer.
  10.  *
  11.  *   - Redistributions in binary form must reproduce the above copyright
  12.  *     notice, this list of conditions and the following disclaimer in the
  13.  *     documentation and/or other materials provided with the distribution.
  14.  *
  15.  *   - Neither the name of Sun Microsystems nor the names of its
  16.  *     contributors may be used to endorse or promote products derived
  17.  *     from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. package demo;
  33.  
  34. import java.io.*;
  35. import java.util.*;
  36. import javax.mail.*;
  37. import javax.mail.internet.*;
  38. import javax.mail.search.*;
  39. import javax.servlet.jsp.*;
  40. import javax.servlet.jsp.tagext.*;
  41.  
  42. /**
  43.  * Custom tag for listing messages. The scripting variable is only
  44.  * within the body of the tag.
  45.  */
  46. public class ListMessagesTag extends BodyTagSupport {
  47.     private String folder;
  48.     private String session;
  49.     private int msgNum = 0;
  50.     private int messageCount = 0;
  51.     private Message message;
  52.     private Message[] messages;
  53.     private MessageInfo messageinfo;
  54.    
  55.     /**
  56.      * folder attribute getter method.
  57.      */
  58.     public String getFolder() {
  59.         return folder;
  60.     }
  61.    
  62.     /**
  63.      * session attribute getter method.
  64.      */
  65.     public String getSession() {
  66.         return session;
  67.     }
  68.    
  69.     /**
  70.      * folder setter method.
  71.      */
  72.     public void setFolder(String folder) {
  73.         this.folder = folder;
  74.     }
  75.  
  76.     /**
  77.      * session attribute setter method.
  78.      */
  79.     public void setSession(String session) {
  80.         this.session = session;
  81.     }
  82.  
  83.     /**
  84.      * Method for processing the start of the tag.
  85.      */
  86.     public int doStartTag() throws JspException {
  87.         messageinfo = new MessageInfo();
  88.        
  89.         try {
  90.             Folder folder = (Folder)pageContext.getAttribute(
  91.                 getFolder(), PageContext.SESSION_SCOPE);
  92.             FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.DELETED), false);
  93.             messages = folder.search(ft);
  94.             messageCount = messages.length;
  95.             msgNum = 0;
  96.         } catch (Exception ex) {
  97.             throw new JspException(ex.getMessage());
  98.         }
  99.  
  100.         if (messageCount > 0) {
  101.             getMessage();
  102.             return BodyTag.EVAL_BODY_TAG;
  103.         } else
  104.             return BodyTag.SKIP_BODY;
  105.     }
  106.    
  107.     /**
  108.      * Method for processing the body content of the tag.
  109.      */
  110.     public int doAfterBody() throws JspException {
  111.        
  112.         BodyContent body = getBodyContent();
  113.         try {
  114.             body.writeOut(getPreviousOut());
  115.         } catch (IOException e) {
  116.             throw new JspTagException("IterationTag: " + e.getMessage());
  117.         }
  118.        
  119.         // clear up so the next time the body content is empty
  120.         body.clearBody();
  121.        
  122.         if (msgNum < messageCount) {
  123.             getMessage();
  124.             return BodyTag.EVAL_BODY_TAG;
  125.         } else {
  126.             return BodyTag.SKIP_BODY;
  127.         }
  128.     }
  129.    
  130.     /**
  131.      * Helper method for retrieving messages.
  132.      */
  133.     private void getMessage() throws JspException {
  134.         message = messages[msgNum++];
  135.         messageinfo.setMessage(message);
  136.         pageContext.setAttribute(getId(), messageinfo);
  137.     }
  138. }
  139.  
  140.