Skip to content

Method: hasMimeType(String)

1: /*
2: * Copyright (c) 2001, 2023 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 jakarta.mail.MessagingException;
14: import jakarta.mail.Part;
15: import jakarta.mail.internet.ParseException;
16:
17: import java.io.IOException;
18:
19: /**
20: * Used to store attachment information.
21: */
22: public class AttachmentInfo {
23: private Part part;
24: private int num;
25:
26:
27: /**
28: * Returns the attachment's content type.
29: */
30: public String getAttachmentType() throws MessagingException {
31: String contentType;
32: if ((contentType = part.getContentType()) == null)
33: return "invalid part";
34: else
35: return contentType;
36: }
37:
38: /**
39: * Returns the attachment's content (if it is plain text).
40: */
41: public String getContent() throws IOException, MessagingException {
42: if (hasMimeType("text/plain"))
43: return (String) part.getContent();
44: else
45: return "";
46: }
47:
48: /**
49: * Returns the attachment's description.
50: */
51: public String getDescription() throws MessagingException {
52: String description;
53: if ((description = part.getDescription()) != null)
54: return description;
55: else
56: return "";
57: }
58:
59: /**
60: * Returns the attachment's filename.
61: */
62: public String getFilename() throws MessagingException {
63: String filename;
64: if ((filename = part.getFileName()) != null)
65: return filename;
66: else
67: return "";
68: }
69:
70: /**
71: * Returns the attachment number.
72: */
73: public String getNum() {
74: return (Integer.toString(num));
75: }
76:
77: /**
78: * Method for checking if the attachment has a description.
79: */
80: public boolean hasDescription() throws MessagingException {
81: return (part.getDescription() != null);
82: }
83:
84: /**
85: * Method for checking if the attachment has a filename.
86: */
87: public boolean hasFilename() throws MessagingException {
88: return (part.getFileName() != null);
89: }
90:
91: /**
92: * Method for checking if the attachment has the desired mime type.
93: */
94: public boolean hasMimeType(String mimeType) throws MessagingException {
95: return part.isMimeType(mimeType);
96: }
97:
98: /**
99: * Method for checking the content disposition.
100: */
101: public boolean isInline() throws MessagingException {
102: if (part.getDisposition() != null)
103: return part.getDisposition().equals(Part.INLINE);
104: else
105: return true;
106: }
107:
108: /**
109: * Method for mapping a message part to this AttachmentInfo class.
110: */
111: public void setPart(int num, Part part)
112: throws MessagingException, ParseException {
113:
114: this.part = part;
115: this.num = num;
116: }
117: }