1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
package com.ozacc.mail.fetch.impl.sk_jp; |
7 |
|
|
8 |
|
import java.io.IOException; |
9 |
|
import java.io.InputStream; |
10 |
|
import java.io.OutputStream; |
11 |
|
import javax.activation.DataSource; |
12 |
|
import javax.mail.MessageAware; |
13 |
|
import javax.mail.MessageContext; |
14 |
|
import javax.mail.MessagingException; |
15 |
|
import javax.mail.Part; |
16 |
|
import javax.mail.internet.ContentType; |
17 |
|
import javax.mail.internet.ParseException; |
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
class CorrectedContentTypeDataSource implements DataSource, MessageAware { |
32 |
|
|
33 |
|
protected DataSource source; |
34 |
|
|
35 |
|
protected String defaultCharset; |
36 |
|
|
37 |
|
protected String forceCharset; |
38 |
|
|
39 |
0 |
public CorrectedContentTypeDataSource() {} |
40 |
|
|
41 |
0 |
public CorrectedContentTypeDataSource(DataSource dataSource, String defaultCharset) { |
42 |
0 |
setDataSource(dataSource); |
43 |
0 |
setDefaultCharset(defaultCharset); |
44 |
0 |
} |
45 |
|
|
46 |
0 |
public CorrectedContentTypeDataSource(Part part, String defaultCharset) |
47 |
|
throws MessagingException { |
48 |
0 |
setPart(part); |
49 |
0 |
setDefaultCharset(defaultCharset); |
50 |
0 |
} |
51 |
|
|
52 |
|
public void setPart(Part part) throws MessagingException { |
53 |
|
|
54 |
0 |
setDataSource(part.getDataHandler().getDataSource()); |
55 |
0 |
} |
56 |
|
|
57 |
|
public void setDataSource(DataSource newSource) { |
58 |
0 |
source = newSource; |
59 |
0 |
} |
60 |
|
|
61 |
|
public void setDefaultCharset(String defaultCharset) { |
62 |
0 |
this.defaultCharset = defaultCharset; |
63 |
0 |
} |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
public void setForceCharset(String forceCharset) { |
72 |
0 |
this.forceCharset = forceCharset; |
73 |
0 |
} |
74 |
|
|
75 |
|
public String getContentType() { |
76 |
0 |
ContentType contentType = null; |
77 |
|
try { |
78 |
0 |
contentType = new ContentType(source.getContentType()); |
79 |
0 |
} catch (ParseException e) { |
80 |
0 |
return "text/plain; charset=" + defaultCharset; |
81 |
|
} |
82 |
0 |
String specifiedCharset = contentType.getParameter("charset"); |
83 |
0 |
if (specclass="keyword">ifiedCharset == null) { |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
0 |
contentType.setParameter("charset", defaultCharset); |
88 |
0 |
} else if (forceCharset != null) { |
89 |
0 |
contentType.setParameter("charset", forceCharset); |
90 |
|
} |
91 |
0 |
return contentType.toString(); |
92 |
|
} |
93 |
|
|
94 |
|
public String getName() { |
95 |
0 |
return source.getName(); |
96 |
|
} |
97 |
|
|
98 |
|
public InputStream getInputStream() throws IOException { |
99 |
0 |
return source.getInputStream(); |
100 |
|
} |
101 |
|
|
102 |
|
public OutputStream getOutputStream() throws IOException { |
103 |
0 |
return source.getOutputStream(); |
104 |
|
} |
105 |
|
|
106 |
|
public synchronized MessageContext getMessageContext() { |
107 |
0 |
if (source instanceof MessageAware) { |
108 |
0 |
return ((MessageAware)source).getMessageContext(); |
109 |
|
} |
110 |
0 |
throw new RuntimeException(source + " isn't MessageAware."); |
111 |
|
} |
112 |
|
} |