FileDocCategorySizeDatePackage
AttachmentServlet.javaAPI DocGlassfish v2 API3848Mon Oct 17 14:54:24 BST 2005demo

AttachmentServlet

public class AttachmentServlet extends HttpServlet
This servlet gets the input stream for a given msg part and pushes it out to the browser with the correct content type. Used to display attachments and relies on the browser's content handling capabilities.

Fields Summary
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
This method handles the GET requests from the client.

      
        HttpSession session = request.getSession();
        ServletOutputStream out = response.getOutputStream();
        int msgNum = Integer.parseInt(request.getParameter("message"));
        int partNum = Integer.parseInt(request.getParameter("part"));
        MailUserBean mailuser = (MailUserBean)session.getAttribute("mailuser");

        // check to be sure we're still logged in
        if (mailuser.isLoggedIn()) {
            try {
                Message msg = mailuser.getFolder().getMessage(msgNum);

                Multipart multipart = (Multipart)msg.getContent();
	        Part part = multipart.getBodyPart(partNum);
                
                String sct = part.getContentType();
	        if (sct == null) {
		    out.println("invalid part");
		    return;
	        }
	        ContentType ct = new ContentType(sct);

	        response.setContentType(ct.getBaseType());
	        InputStream is = part.getInputStream();
	        int i;
	        while ((i = is.read()) != -1)
		    out.write(i);
	        out.flush();
	        out.close();

            } catch (MessagingException ex) {
                throw new ServletException(ex.getMessage());
            }
        } else {
            getServletConfig().getServletContext().
                getRequestDispatcher("/index.html").
                forward(request, response);
        }