/
AttachmentInfo.java
117 lines (101 loc) · 2.68 KB
/
AttachmentInfo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Copyright (c) 2001, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package demo;
import java.io.*;
import java.util.*;
import jakarta.mail.*;
import jakarta.mail.internet.*;
/**
* Used to store attachment information.
*/
public class AttachmentInfo {
private Part part;
private int num;
/**
* Returns the attachment's content type.
*/
public String getAttachmentType() throws MessagingException {
String contentType;
if ((contentType = part.getContentType()) == null)
return "invalid part";
else
return contentType;
}
/**
* Returns the attachment's content (if it is plain text).
*/
public String getContent() throws IOException, MessagingException {
if (hasMimeType("text/plain"))
return (String)part.getContent();
else
return "";
}
/**
* Returns the attachment's description.
*/
public String getDescription() throws MessagingException {
String description;
if ((description = part.getDescription()) != null)
return description;
else
return "";
}
/**
* Returns the attachment's filename.
*/
public String getFilename() throws MessagingException {
String filename;
if ((filename = part.getFileName()) != null)
return filename;
else
return "";
}
/**
* Returns the attachment number.
*/
public String getNum() {
return (Integer.toString(num));
}
/**
* Method for checking if the attachment has a description.
*/
public boolean hasDescription() throws MessagingException {
return (part.getDescription() != null);
}
/**
* Method for checking if the attachment has a filename.
*/
public boolean hasFilename() throws MessagingException {
return (part.getFileName() != null);
}
/**
* Method for checking if the attachment has the desired mime type.
*/
public boolean hasMimeType(String mimeType) throws MessagingException {
return part.isMimeType(mimeType);
}
/**
* Method for checking the content disposition.
*/
public boolean isInline() throws MessagingException {
if (part.getDisposition() != null)
return part.getDisposition().equals(Part.INLINE);
else
return true;
}
/**
* Method for mapping a message part to this AttachmentInfo class.
*/
public void setPart(int num, Part part)
throws MessagingException, ParseException {
this.part = part;
this.num = num;
}
}