1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
package com.ozacc.mail.fetch.impl.sk_jp; |
7 |
|
|
8 |
|
import java.io.IOException; |
9 |
|
import java.io.UnsupportedEncodingException; |
10 |
|
|
11 |
|
import javax.activation.DataHandler; |
12 |
|
import javax.mail.MessagingException; |
13 |
|
import javax.mail.Multipart; |
14 |
|
import javax.mail.Part; |
15 |
|
import javax.mail.internet.ContentType; |
16 |
|
import javax.mail.internet.MimeBodyPart; |
17 |
|
import javax.mail.internet.MimeMultipart; |
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
0 |
public class MultipartUtility { |
28 |
|
|
29 |
|
private static final String JIS_CHARSET = "ISO-2022-JP"; |
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
public static Object getContent(Part part) throws MessagingException, IOException { |
42 |
0 |
return getContent(part, JIS_CHARSET); |
43 |
|
} |
44 |
|
|
45 |
0 |
private static CorrectedContentTypeDataSource correctedDataSource = new CorrectedContentTypeDataSourceUTF7Support(); |
46 |
|
|
47 |
0 |
private static DataHandler correctedDataHandler = new DataHandler(correctedDataSource); |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
public static Object getContent(Part part, String charset) throws MessagingException, |
59 |
|
IOException { |
60 |
0 |
synchronized (correctedDataSource) { |
61 |
|
|
62 |
0 |
correctedDataSource.setPart(part); |
63 |
|
try { |
64 |
0 |
correctedDataSource.setDefaultCharset(charset); |
65 |
0 |
return correctedDataHandler.getContent(); |
66 |
0 |
} catch (UnsupportedEncodingException e) { |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
0 |
correctedDataSource.setForceCharset(JIS_CHARSET); |
74 |
0 |
return correctedDataHandler.getContent(); |
75 |
|
} |
76 |
|
|
77 |
|
} |
78 |
|
} |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
public static String getFirstPlainText(Part part) throws MessagingException { |
85 |
0 |
FirstPlainPartExtractor h = new FirstPlainPartExtractor(); |
86 |
0 |
process(part, h); |
87 |
0 |
return h.getText(); |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
public static String getPlainText(Part part) throws MessagingException { |
95 |
0 |
PlainPartExtractor h = new PlainPartExtractor(); |
96 |
0 |
process(part, h); |
97 |
0 |
return h.getText(); |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
public static void process(Part part, PartHandler handler) throws MessagingException { |
107 |
0 |
process(part, handler, null); |
108 |
0 |
} |
109 |
|
|
110 |
|
private static boolean process(Part part, PartHandler handler, ContentType context) |
111 |
|
throws MessagingException { |
112 |
|
try { |
113 |
0 |
if (part.isMimeType("multipart/*")) { |
114 |
0 |
Multipart mp = (Multipart)part.getContent(); |
115 |
0 |
ContentType cType = new ContentType(part.getContentType()); |
116 |
0 |
for (int i = 0; i < mp.getCount(); i++) { |
117 |
0 |
if (!process(mp.getBodyPart(i), handler, cType)) { |
118 |
0 |
return false; |
119 |
|
} |
120 |
|
} |
121 |
0 |
return true; |
122 |
|
} |
123 |
0 |
return handler.processPart(part, context); |
124 |
0 |
} catch (IOException e) { |
125 |
0 |
throw new MessagingException("Got exception \nin " + part + "\n", e); |
126 |
|
} |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
public static void addBodyPart(Part part, MimeBodyPart bodyPart) throws MessagingException, |
136 |
|
IOException { |
137 |
0 |
if (part.isMimeType("multipart/*")) { |
138 |
0 |
((MimeMultipart)part.getContent()).addBodyPart(bodyPart); |
139 |
0 |
return; |
140 |
|
} |
141 |
|
|
142 |
0 |
MimeMultipart mp = new MimeMultipart("mixed"); |
143 |
0 |
MimeBodyPart original = new MimeBodyPart(); |
144 |
0 |
original.setContent(part.getContent(), part.getContentType()); |
145 |
0 |
mp.addBodyPart(original); |
146 |
0 |
mp.addBodyPart(bodyPart); |
147 |
0 |
part.setContent(mp); |
148 |
0 |
} |
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
public static void dump(Part part) { |
154 |
0 |
dump(part, 0); |
155 |
0 |
} |
156 |
|
|
157 |
|
private static void dump(Part part, int layer) { |
158 |
0 |
for (int i = 0; i < layer; i++) { |
159 |
0 |
System.out.print(" "); |
160 |
|
} |
161 |
|
try { |
162 |
0 |
System.out.println(part.getClass() + ":" + part.getContentType()); |
163 |
0 |
if (part.isMimeType("multipart/*")) { |
164 |
0 |
MimeMultipart mp = (MimeMultipart)part.getContent(); |
165 |
0 |
for (int i = 0; i < mp.getCount(); i++) { |
166 |
0 |
dump(mp.getBodyPart(i), layer + 1); |
167 |
|
} |
168 |
|
} |
169 |
0 |
} catch (Exception e) { |
170 |
0 |
e.printStackTrace(); |
171 |
|
} |
172 |
0 |
} |
173 |
|
} |