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 javax.mail.*;
  36. import javax.mail.internet.*;
  37. import javax.servlet.*;
  38. import javax.servlet.http.*;
  39.  
  40. /**
  41.  * This servlet gets the input stream for a given msg part and
  42.  * pushes it out to the browser with the correct content type.
  43.  * Used to display attachments and relies on the browser's
  44.  * content handling capabilities.
  45.  */
  46. public class AttachmentServlet extends HttpServlet {
  47.  
  48.     /**
  49.      * This method handles the GET requests from the client.
  50.      */
  51.     public void doGet(HttpServletRequest request, HttpServletResponse  response)
  52.         throws IOException, ServletException {
  53.      
  54.         HttpSession session = request.getSession();
  55.         ServletOutputStream out = response.getOutputStream();
  56.         int msgNum = Integer.parseInt(request.getParameter("message"));
  57.         int partNum = Integer.parseInt(request.getParameter("part"));
  58.         MailUserBean mailuser = (MailUserBean)session.getAttribute("mailuser");
  59.  
  60.         // check to be sure we're still logged in
  61.         if (mailuser.isLoggedIn()) {
  62.             try {
  63.                 Message msg = mailuser.getFolder().getMessage(msgNum);
  64.  
  65.                 Multipart multipart = (Multipart)msg.getContent();
  66.                 Part part = multipart.getBodyPart(partNum);
  67.                
  68.                 String sct = part.getContentType();
  69.                 if (sct == null) {
  70.                     out.println("invalid part");
  71.                     return;
  72.                 }
  73.                 ContentType ct = new ContentType(sct);
  74.  
  75.                 response.setContentType(ct.getBaseType());
  76.                 InputStream is = part.getInputStream();
  77.                 int i;
  78.                 while ((i = is.read()) != -1)
  79.                     out.write(i);
  80.                 out.flush();
  81.                 out.close();
  82.  
  83.             } catch (MessagingException ex) {
  84.                 throw new ServletException(ex.getMessage());
  85.             }
  86.         } else {
  87.             getServletConfig().getServletContext().
  88.                 getRequestDispatcher("/index.html").
  89.                 forward(request, response);
  90.         }
  91.     }  
  92. }
  93.  
  94.