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.  
  37. import javax.mail.*;
  38. import javax.mail.internet.*;
  39. import javax.servlet.*;
  40. import javax.servlet.http.*;
  41.  
  42. /**
  43.  * Used to store attachment information.
  44.  */
  45. public class AttachmentInfo {
  46.     private Part part;
  47.     private int num;
  48.    
  49.  
  50.     /**
  51.      * Returns the attachment's content type.
  52.      */
  53.     public String getAttachmentType() throws MessagingException {
  54.         String contentType;
  55.         if ((contentType = part.getContentType()) == null)
  56.             return "invalid part";
  57.         else
  58.             return contentType;
  59.     }
  60.  
  61.     /**
  62.      * Returns the attachment's content (if it is plain text).
  63.      */
  64.     public String getContent() throws java.io.IOException, MessagingException {
  65.         if (hasMimeType("text/plain"))
  66.             return (String)part.getContent();
  67.         else
  68.             return "";
  69.     }
  70.    
  71.     /**
  72.      * Returns the attachment's description.
  73.      */
  74.     public String getDescription() throws MessagingException {
  75.         String description;
  76.         if ((description = part.getDescription()) != null)
  77.             return description;
  78.         else
  79.             return "";
  80.     }
  81.    
  82.     /**
  83.      * Returns the attachment's filename.
  84.      */
  85.     public String getFilename() throws MessagingException {
  86.         String filename;
  87.         if ((filename = part.getFileName()) != null)
  88.             return filename;
  89.         else
  90.             return "";
  91.     }
  92.  
  93.     /**
  94.      * Returns the attachment number.
  95.      */
  96.     public String getNum() {
  97.         return (Integer.toString(num));
  98.     }
  99.    
  100.     /**
  101.      * Method for checking if the attachment has a description.
  102.      */
  103.     public boolean hasDescription() throws MessagingException {
  104.         return (part.getDescription() != null);
  105.     }
  106.    
  107.     /**
  108.      * Method for checking if the attachment has a filename.
  109.      */
  110.     public boolean hasFilename() throws MessagingException {
  111.         return (part.getFileName() != null);
  112.     }
  113.    
  114.     /**
  115.      * Method for checking if the attachment has the desired mime type.
  116.      */
  117.     public boolean hasMimeType(String mimeType) throws MessagingException {
  118.         return part.isMimeType(mimeType);
  119.     }
  120.    
  121.     /**
  122.      * Method for checking the content disposition.
  123.      */
  124.     public boolean isInline() throws MessagingException {
  125.         if (part.getDisposition() != null)
  126.             return part.getDisposition().equals(Part.INLINE);
  127.         else
  128.             return true;
  129.     }
  130.    
  131.     /**
  132.      * Method for mapping a message part to this AttachmentInfo class.
  133.      */
  134.     public void setPart(int num, Part part)
  135.         throws MessagingException, ParseException {
  136.            
  137.         this.part = part;
  138.         this.num = num;
  139.     }
  140. }
  141.  
  142.