Skip to content

Package: AttachmentServlet

AttachmentServlet

nameinstructionbranchcomplexitylinemethod
AttachmentServlet()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
doGet(HttpServletRequest, HttpServletResponse)
M: 89 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 27 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2001, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package demo;
12:
13: import java.io.*;
14: import jakarta.mail.*;
15: import jakarta.mail.internet.*;
16: import jakarta.servlet.*;
17: import jakarta.servlet.http.HttpServlet;
18: import jakarta.servlet.http.HttpServletRequest;
19: import jakarta.servlet.http.HttpServletResponse;
20: import jakarta.servlet.http.HttpSession;
21:
22: /**
23: * This servlet gets the input stream for a given msg part and
24: * pushes it out to the browser with the correct content type.
25: * Used to display attachments and relies on the browser's
26: * content handling capabilities.
27: */
28: public class AttachmentServlet extends HttpServlet {
29:
30: /**
31: * This method handles the GET requests from the client.
32: */
33: public void doGet(HttpServletRequest request, HttpServletResponse response)
34:         throws IOException, ServletException {
35:
36:         HttpSession session = request.getSession();
37:         ServletOutputStream out = response.getOutputStream();
38:         int msgNum = Integer.parseInt(request.getParameter("message"));
39:         int partNum = Integer.parseInt(request.getParameter("part"));
40:         MailUserBean mailuser = (MailUserBean)session.getAttribute("mailuser");
41:
42:         // check to be sure we're still logged in
43:•        if (mailuser.isLoggedIn()) {
44:          try {
45:                 Message msg = mailuser.getFolder().getMessage(msgNum);
46:
47:                 Multipart multipart = (Multipart)msg.getContent();
48:                 Part part = multipart.getBodyPart(partNum);
49:                 
50:                 String sct = part.getContentType();
51:•                if (sct == null) {
52:                  out.println("invalid part");
53:                  return;
54:                 }
55:                 ContentType ct = new ContentType(sct);
56:
57:                 response.setContentType(ct.getBaseType());
58:                 InputStream is = part.getInputStream();
59:                 int i;
60:•                while ((i = is.read()) != -1)
61:                  out.write(i);
62:                 out.flush();
63:                 out.close();
64:
65:          } catch (MessagingException ex) {
66:                 throw new ServletException(ex.getMessage());
67:          }
68:         } else {
69:          getServletConfig().getServletContext().
70:                 getRequestDispatcher("/index.html").
71:                 forward(request, response);
72:         }
73: }
74: }